d510ab6ed934bf2f1b93275c72484ae1fc5f9b09
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / AbstractSoneCommandTest.java
1 /*
2  * Sone - AbstractSoneCommandTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
20 import static com.google.common.base.Optional.of;
21 import static java.lang.System.currentTimeMillis;
22 import static java.util.Arrays.asList;
23 import static java.util.UUID.randomUUID;
24 import static java.util.concurrent.TimeUnit.DAYS;
25 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
26 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
27 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostWithReplies;
28 import static net.pterodactylus.sone.fcp.Verifiers.verifyPosts;
29 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostsWithReplies;
30 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
31 import static org.hamcrest.CoreMatchers.is;
32 import static org.hamcrest.CoreMatchers.notNullValue;
33 import static org.hamcrest.CoreMatchers.nullValue;
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37
38 import java.util.List;
39
40 import net.pterodactylus.sone.data.Mocks;
41 import net.pterodactylus.sone.data.Post;
42 import net.pterodactylus.sone.data.PostReply;
43 import net.pterodactylus.sone.data.Sone;
44 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
45 import net.pterodactylus.sone.freenet.fcp.FcpException;
46
47 import freenet.node.FSParseException;
48 import freenet.support.SimpleFieldSet;
49 import freenet.support.api.Bucket;
50
51 import com.google.common.base.Optional;
52 import org.junit.Test;
53 import org.mockito.Matchers;
54
55 /**
56  * Unit test for {@link AbstractSoneCommand}.
57  *
58  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59  */
60 public class AbstractSoneCommandTest {
61
62         private final Mocks mocks = new Mocks();
63         private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(mocks.core) {
64                 @Override
65                 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
66                         return null;
67                 }
68         };
69
70         @Test
71         public void testStringEncoding() {
72                 String testString = prepareStringToBeEncoded();
73
74                 String encodedString = encodeString(testString);
75                 assertThat(encodedString, notNullValue());
76                 assertThat(encodedString.length(), is(testString.length() + 3));
77         }
78
79         private String prepareStringToBeEncoded() {
80                 StringBuilder testString = new StringBuilder();
81                 for (int i = 0; i < 4000; ++i) {
82                         testString.append((char) i);
83                 }
84                 return testString.toString();
85         }
86
87         @Test
88         public void testEncodingASone() throws FSParseException {
89                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
90                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
91                 assertThat(soneFieldSet, notNullValue());
92                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
93                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
94                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
95                 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
96                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
97                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
98                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
99         }
100
101         @Test
102         public void testEncodingAFollowedSone() throws FSParseException {
103                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
104                 Sone localSone = prepareLocalSoneThatFollowsEverybody();
105                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
106                 assertThat(soneFieldSet, notNullValue());
107                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
108                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
109                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
110                 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
111                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
112                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
113                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
114         }
115
116         @Test
117         public void testEncodingANotFollowedSone() throws FSParseException {
118                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
119                 Sone localSone = prepareLocalSoneThatFollowsNobody();
120                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
121                 assertThat(soneFieldSet, notNullValue());
122                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
123                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
124                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
125                 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
126                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
127                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
128                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
129         }
130
131         private Sone prepareLocalSoneThatFollowsEverybody() {
132                 Sone sone = mock(Sone.class);
133                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
134                 return sone;
135         }
136
137         private Sone prepareLocalSoneThatFollowsNobody() {
138                 Sone sone = mock(Sone.class);
139                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
140                 return sone;
141         }
142
143         @Test
144         public void testEncodingMultipleSones() throws FSParseException {
145                 List<Sone> sones = prepareMultipleSones();
146                 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
147                 assertThat(sonesFieldSet, notNullValue());
148                 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
149                 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
150                 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
151                 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
152                 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
153                 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
154                 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
155                 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
156                 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
157                 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
158                 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
159                 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
160                 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
161         }
162
163         private List<Sone> prepareMultipleSones() {
164                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
165                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
166                 Sone sone3 = mocks.mockSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U").withName("Test3").withProfileName("Gamma", "C.", "Third").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
167                 return asList(sone1, sone2, sone3);
168         }
169
170         @Test
171         public void testEncodingReplies() throws FSParseException {
172                 List<PostReply> postReplies = preparePostReplies();
173                 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
174                 assertThat(postRepliesFieldSet, notNullValue());
175                 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
176                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
177                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
178                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
179                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
180                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
181                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
182                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
183                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
184                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
185                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
186                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
187                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
188         }
189
190         private List<PostReply> preparePostReplies() {
191                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
192                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
193                 Sone sone3 = mocks.mockSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U").withName("Test3").withProfileName("Gamma", "C.", "Third").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
194                 PostReply postReply1 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 1").create();
195                 PostReply postReply2 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 2").create();
196                 PostReply postReply3 = mocks.mockPostReply(sone3, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 3").create();
197                 return asList(postReply1, postReply2, postReply3);
198         }
199
200         @Test
201         public void testEncodingLikes() throws FSParseException {
202                 List<Sone> likes = prepareMultipleSones();
203                 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
204                 assertThat(likesFieldSet, notNullValue());
205                 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
206                 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
207                 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
208                 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
209         }
210
211         @Test
212         public void testParsingAMandatorySone() throws FcpException {
213                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
214                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
215                 Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
216                 assertThat(parsedSone, notNullValue());
217                 assertThat(parsedSone, is(sone));
218         }
219
220         @Test(expected = FcpException.class)
221         public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
222                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
223                 abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
224         }
225
226         @Test(expected = FcpException.class)
227         public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
228                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
229                 abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
230         }
231
232         @Test
233         public void testParsingAMandatoryLocalSone() throws FcpException {
234                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").local().withName("Test").withProfileName("First", "M.", "Last").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
235                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
236                 Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
237                 assertThat(parsedSone, notNullValue());
238                 assertThat(parsedSone, is(sone));
239                 assertThat(parsedSone.isLocal(), is(true));
240         }
241
242         @Test(expected = FcpException.class)
243         public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
244                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
245                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
246                 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
247         }
248
249         @Test(expected = FcpException.class)
250         public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
251                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
252                 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
253         }
254
255         @Test
256         public void testParsingAnExistingOptionalSone() throws FcpException {
257                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
258                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
259                 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
260                 assertThat(parsedSone, notNullValue());
261                 assertThat(parsedSone.isPresent(), is(true));
262                 assertThat(parsedSone.get(), is(sone));
263         }
264
265         @Test
266         public void testParsingANonExistingOptionalSone() throws FcpException {
267                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
268                 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
269                 assertThat(parsedSone, notNullValue());
270                 assertThat(parsedSone.isPresent(), is(false));
271         }
272
273         @Test
274         public void testParsingAnOptionalSoneFromANonExistingField() throws FcpException {
275                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
276                 Optional<Sone> sone = abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
277                 assertThat(sone, notNullValue());
278                 assertThat(sone.isPresent(), is(false));
279         }
280
281         @Test
282         public void testParsingAPost() throws FcpException {
283                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
284                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
285                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
286                 Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
287                 assertThat(parsedPost, notNullValue());
288                 assertThat(parsedPost, is(post));
289         }
290
291         @Test(expected = FcpException.class)
292         public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
293                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", "InvalidPostId").get();
294                 abstractSoneCommand.getPost(postFieldSet, "Post");
295         }
296
297         @Test(expected = FcpException.class)
298         public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
299                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
300                 abstractSoneCommand.getPost(postFieldSet, "Post");
301         }
302
303         @Test
304         public void testParsingAReply() throws FcpException {
305                 Sone sone = mocks.mockSone(randomUUID().toString()).create();
306                 PostReply reply = mocks.mockPostReply(sone, randomUUID().toString()).create();
307                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
308                 PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
309                 assertThat(parsedReply, notNullValue());
310                 assertThat(parsedReply, is(reply));
311         }
312
313         @Test(expected = FcpException.class)
314         public void testParsingANonExistingReply() throws FcpException {
315                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", "InvalidReplyId").get();
316                 abstractSoneCommand.getReply(replyFieldSet, "Reply");
317         }
318
319         @Test(expected = FcpException.class)
320         public void testParsingAReplyFromANonExistingField() throws FcpException {
321                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
322                 abstractSoneCommand.getReply(replyFieldSet, "Reply");
323         }
324
325         @Test
326         public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
327                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
328                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
329                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
330                 assertThat(postFieldSet, notNullValue());
331                 verifyPost(postFieldSet, "Post.", post);
332         }
333
334         private void verifyPost(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
335                 assertThat(postFieldSet.get(prefix + "ID"), is(post.getId()));
336                 assertThat(postFieldSet.get(prefix + "Sone"), is(post.getSone().getId()));
337                 assertThat(postFieldSet.get(prefix + "Recipient"), is(post.getRecipientId().orNull()));
338                 assertThat(postFieldSet.getLong(prefix + "Time"), is(post.getTime()));
339                 assertThat(postFieldSet.get(prefix + "Text"), is(post.getText()));
340         }
341
342         @Test
343         public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
344                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
345                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
346                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
347                 assertThat(postFieldSet, notNullValue());
348                 verifyPost(postFieldSet, "Post.", post);
349         }
350
351         @Test
352         public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
353                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
354                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
355                 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
356                 when(post.getReplies()).thenReturn(asList(postReply));
357                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
358                 assertThat(postFieldSet, notNullValue());
359                 verifyPostWithReplies(postFieldSet, "Post.", post);
360         }
361
362         @Test
363         public void testEncodingAPostWithoutRecipientWithFutureReplies() throws FSParseException {
364                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
365                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
366                 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply.").create();
367                 when(post.getReplies()).thenReturn(asList(postReply));
368                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
369                 assertThat(postFieldSet, notNullValue());
370                 verifyPostWithReplies(postFieldSet, "Post.", post);
371         }
372
373         @Test
374         public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
375                 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
376                 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
377                 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
378                 when(post.getReplies()).thenReturn(asList(postReply));
379                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
380                 assertThat(postFieldSet, notNullValue());
381                 verifyPostWithReplies(postFieldSet, "Post.", post);
382         }
383
384         @Test
385         public void testEncodingPostsWithoutRecipientAndReplies() throws FSParseException {
386                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
387                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
388                 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
389                 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
390                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
391                 assertThat(postFieldSet, notNullValue());
392                 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
393         }
394
395         @Test
396         public void testEncodingPostsWithRecipientWithoutReplies() throws FSParseException {
397                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
398                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
399                 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
400                 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
401                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
402                 assertThat(postFieldSet, notNullValue());
403                 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
404         }
405
406         @Test
407         public void testEncodingPostsWithoutRecipientWithReplies() throws FSParseException {
408                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
409                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
410                 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
411                 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
412                 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
413                 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
414                 when(post1.getReplies()).thenReturn(asList(postReply1));
415                 when(post2.getReplies()).thenReturn(asList(postReply2));
416                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
417                 assertThat(postFieldSet, notNullValue());
418                 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
419         }
420
421         @Test
422         public void testEncodingPostsWithRecipientAndReplies() throws FSParseException {
423                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
424                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
425                 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
426                 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
427                 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
428                 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
429                 when(post1.getReplies()).thenReturn(asList(postReply1));
430                 when(post2.getReplies()).thenReturn(asList(postReply2));
431                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
432                 assertThat(postFieldSet, notNullValue());
433                 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
434         }
435
436         @Test
437         public void testEncodingPostsWithRecipientAndFutureReplies() throws FSParseException {
438                 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
439                 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
440                 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
441                 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
442                 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
443                 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply from 1 to 2").create();
444                 when(post1.getReplies()).thenReturn(asList(postReply1));
445                 when(post2.getReplies()).thenReturn(asList(postReply2));
446                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
447                 assertThat(postFieldSet, notNullValue());
448                 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
449         }
450
451 }