Add method that verifies multiple posts.
[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.fromNullable;
21 import static com.google.common.base.Optional.of;
22 import static java.util.Arrays.asList;
23 import static java.util.UUID.randomUUID;
24 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
25 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
26 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.notNullValue;
29 import static org.hamcrest.CoreMatchers.nullValue;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
38
39 import net.pterodactylus.sone.core.Core;
40 import net.pterodactylus.sone.data.Post;
41 import net.pterodactylus.sone.data.PostReply;
42 import net.pterodactylus.sone.data.Profile;
43 import net.pterodactylus.sone.data.Sone;
44 import net.pterodactylus.sone.database.Database;
45 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
46 import net.pterodactylus.sone.freenet.fcp.FcpException;
47
48 import freenet.node.FSParseException;
49 import freenet.support.SimpleFieldSet;
50 import freenet.support.api.Bucket;
51
52 import com.google.common.base.Optional;
53 import org.junit.Test;
54 import org.mockito.Matchers;
55
56 /**
57  * Unit test for {@link AbstractSoneCommand}.
58  *
59  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60  */
61 public class AbstractSoneCommandTest {
62
63         private final Core core = mock(Core.class);
64         private final Database database = mock(Database.class);
65         private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(core) {
66                 @Override
67                 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
68                         return null;
69                 }
70         };
71
72         public AbstractSoneCommandTest() {
73                 when(core.getDatabase()).thenReturn(database);
74         }
75
76         @Test
77         public void testStringEncoding() {
78                 String testString = prepareStringToBeEncoded();
79
80                 String encodedString = encodeString(testString);
81                 assertThat(encodedString, notNullValue());
82                 assertThat(encodedString.length(), is(testString.length() + 3));
83         }
84
85         private String prepareStringToBeEncoded() {
86                 StringBuilder testString = new StringBuilder();
87                 for (int i = 0; i < 4000; ++i) {
88                         testString.append((char) i);
89                 }
90                 return testString.toString();
91         }
92
93         @Test
94         public void testEncodingASone() throws FSParseException {
95                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
96                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
97                 assertThat(soneFieldSet, notNullValue());
98                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
99                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
100                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
101                 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
102                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
103                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
104                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
105         }
106
107         @Test
108         public void testEncodingAFollowedSone() throws FSParseException {
109                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
110                 Sone localSone = prepareLocalSoneThatFollowsEverybody();
111                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
112                 assertThat(soneFieldSet, notNullValue());
113                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
114                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
115                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
116                 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
117                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
118                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
119                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
120         }
121
122         @Test
123         public void testEncodingANotFollowedSone() throws FSParseException {
124                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
125                 Sone localSone = prepareLocalSoneThatFollowsNobody();
126                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
127                 assertThat(soneFieldSet, notNullValue());
128                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
129                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
130                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
131                 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
132                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
133                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
134                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
135         }
136
137         private Sone prepareLocalSoneThatFollowsEverybody() {
138                 Sone sone = mock(Sone.class);
139                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
140                 return sone;
141         }
142
143         private Sone prepareLocalSoneThatFollowsNobody() {
144                 Sone sone = mock(Sone.class);
145                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
146                 return sone;
147         }
148
149         @Test
150         public void testEncodingMultipleSones() throws FSParseException {
151                 List<Sone> sones = prepareMultipleSones();
152                 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
153                 assertThat(sonesFieldSet, notNullValue());
154                 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
155                 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
156                 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
157                 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
158                 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
159                 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
160                 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
161                 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
162                 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
163                 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
164                 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
165                 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
166                 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
167         }
168
169         private List<Sone> prepareMultipleSones() {
170                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
171                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
172                 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
173                 return asList(sone1, sone2, sone3);
174         }
175
176         private Sone createSone(String id, String name, String firstName, String middleName, String lastName, long time) {
177                 Sone sone = mock(Sone.class);
178                 when(sone.getId()).thenReturn(id);
179                 when(sone.getName()).thenReturn(name);
180                 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
181                 when(sone.getTime()).thenReturn(time);
182                 return sone;
183         }
184
185         private Sone createLocalSone(String id, String name, String firstName, String middleName, String lastName, long time) {
186                 Sone sone = mock(Sone.class);
187                 when(sone.getId()).thenReturn(id);
188                 when(sone.getName()).thenReturn(name);
189                 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
190                 when(sone.getTime()).thenReturn(time);
191                 when(sone.isLocal()).thenReturn(true);
192                 return sone;
193         }
194
195         private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
196                 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
197                 profile.setField(profile.addField("Test1"), "Value1");
198                 return profile;
199         }
200
201         @Test
202         public void testEncodingReplies() throws FSParseException {
203                 List<PostReply> postReplies = preparePostReplies();
204                 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
205                 assertThat(postRepliesFieldSet, notNullValue());
206                 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
207                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
208                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
209                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
210                 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
211                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
212                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
213                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
214                 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
215                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
216                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
217                 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
218                 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
219         }
220
221         private List<PostReply> preparePostReplies() {
222                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
223                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
224                 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
225                 PostReply postReply1 = createPostReply(sone1, "Text 1");
226                 PostReply postReply2 = createPostReply(sone2, "Text 2");
227                 PostReply postReply3 = createPostReply(sone3, "Text 3");
228                 return asList(postReply1, postReply2, postReply3);
229         }
230
231         private PostReply createPostReply(Sone sone, String text) {
232                 PostReply postReply = mock(PostReply.class);
233                 when(postReply.getId()).thenReturn(randomUUID().toString());
234                 when(postReply.getSone()).thenReturn(sone);
235                 when(postReply.getTime()).thenReturn(System.currentTimeMillis());
236                 when(postReply.getText()).thenReturn(text);
237                 return postReply;
238         }
239
240         private PostReply createFuturePostReply(Sone sone, String text) {
241                 PostReply postReply = createPostReply(sone, text);
242                 when(postReply.getTime()).thenReturn(System.currentTimeMillis() + 86400000);
243                 return postReply;
244         }
245
246         @Test
247         public void testEncodingLikes() throws FSParseException {
248                 List<Sone> likes = prepareMultipleSones();
249                 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
250                 assertThat(likesFieldSet, notNullValue());
251                 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
252                 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
253                 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
254                 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
255         }
256
257         @Test
258         public void testParsingAMandatorySone() throws FcpException {
259                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
260                 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
261                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
262                 Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
263                 assertThat(parsedSone, notNullValue());
264                 assertThat(parsedSone, is(sone));
265         }
266
267         @Test(expected = FcpException.class)
268         public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
269                 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
270                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
271                 abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
272         }
273
274         @Test(expected = FcpException.class)
275         public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
276                 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
277                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
278                 abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
279         }
280
281         @Test
282         public void testParsingAMandatoryLocalSone() throws FcpException {
283                 Sone sone = createLocalSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
284                 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
285                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
286                 Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
287                 assertThat(parsedSone, notNullValue());
288                 assertThat(parsedSone, is(sone));
289                 assertThat(parsedSone.isLocal(), is(true));
290         }
291
292         @Test(expected = FcpException.class)
293         public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
294                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
295                 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
296                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
297                 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
298         }
299
300         @Test(expected = FcpException.class)
301         public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
302                 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
303                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
304                 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
305         }
306
307         @Test
308         public void testParsingAnExistingOptionalSone() throws FcpException {
309                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
310                 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
311                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
312                 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
313                 assertThat(parsedSone, notNullValue());
314                 assertThat(parsedSone.isPresent(), is(true));
315                 assertThat(parsedSone.get(), is(sone));
316         }
317
318         @Test
319         public void testParsingANonExistingOptionalSone() throws FcpException {
320                 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
321                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
322                 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
323                 assertThat(parsedSone, notNullValue());
324                 assertThat(parsedSone.isPresent(), is(false));
325         }
326
327         @Test(expected = FcpException.class)
328         public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException {
329                 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
330                 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
331                 abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
332         }
333
334         @Test
335         public void testParsingAPost() throws FcpException {
336                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
337                 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
338                 when(database.getPost(eq(post.getId()))).thenReturn(of(post));
339                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
340                 Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
341                 assertThat(parsedPost, notNullValue());
342                 assertThat(parsedPost, is(post));
343         }
344
345         private Post createPost(Sone sone, String recipient, long time, String text) {
346                 Post post = mock(Post.class);
347                 when(post.getId()).thenReturn(randomUUID().toString());
348                 when(post.getSone()).thenReturn(sone);
349                 when(post.getRecipientId()).thenReturn(fromNullable(recipient));
350                 when(post.getTime()).thenReturn(time);
351                 when(post.getText()).thenReturn(text);
352                 return post;
353         }
354
355         @Test(expected = FcpException.class)
356         public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
357                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
358                 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
359                 when(database.getPost(Matchers.<String>any())).thenReturn(Optional.<Post>absent());
360                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
361                 abstractSoneCommand.getPost(postFieldSet, "Post");
362         }
363
364         @Test(expected = FcpException.class)
365         public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
366                 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
367                 abstractSoneCommand.getPost(postFieldSet, "Post");
368         }
369
370         @Test
371         public void testParsingAReply() throws FcpException {
372                 PostReply reply = createPostReply();
373                 when(database.getPostReply(eq(reply.getId()))).thenReturn(of(reply));
374                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
375                 PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
376                 assertThat(parsedReply, notNullValue());
377                 assertThat(parsedReply, is(reply));
378         }
379
380         private PostReply createPostReply() {
381                 PostReply postReply = mock(PostReply.class);
382                 when(postReply.getId()).thenReturn(randomUUID().toString());
383                 return postReply;
384         }
385
386         @Test(expected = FcpException.class)
387         public void testParsingANonExistingReply() throws FcpException {
388                 PostReply reply = createPostReply();
389                 when(database.getPostReply(Matchers.<String>any())).thenReturn(Optional.<PostReply>absent());
390                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
391                 abstractSoneCommand.getReply(replyFieldSet, "Reply");
392         }
393
394         @Test(expected = FcpException.class)
395         public void testParsingAReplyFromANonExistingField() throws FcpException {
396                 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
397                 abstractSoneCommand.getReply(replyFieldSet, "Reply");
398         }
399
400         @Test
401         public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
402                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
403                 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
404                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
405                 assertThat(postFieldSet, notNullValue());
406                 verifyPost(postFieldSet, "Post.", post);
407         }
408
409         private void verifyPost(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
410                 assertThat(postFieldSet.get(prefix + "ID"), is(post.getId()));
411                 assertThat(postFieldSet.get(prefix + "Sone"), is(post.getSone().getId()));
412                 assertThat(postFieldSet.get(prefix + "Recipient"), is(post.getRecipientId().orNull()));
413                 assertThat(postFieldSet.getLong(prefix + "Time"), is(post.getTime()));
414                 assertThat(postFieldSet.get(prefix + "Text"), is(post.getText()));
415         }
416
417         @Test
418         public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
419                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
420                 Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
421                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
422                 assertThat(postFieldSet, notNullValue());
423                 verifyPost(postFieldSet, "Post.", post);
424         }
425
426         @Test
427         public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
428                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
429                 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
430                 PostReply postReply = createPostReply(sone, "Reply.");
431                 when(post.getReplies()).thenReturn(asList(postReply));
432                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
433                 assertThat(postFieldSet, notNullValue());
434                 verifyPost(postFieldSet, "Post.", post);
435                 verifyPostReplies(postFieldSet, "Post.", asList(postReply));
436         }
437
438         private void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection<PostReply> postReplies) throws FSParseException {
439                 assertThat(postFieldSet.getInt(prefix + "Replies.Count"), is(postReplies.size()));
440                 int postReplyIndex = 0;
441                 for (PostReply postReply : postReplies) {
442                         assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".ID"), is(postReply.getId()));
443                         assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Sone"), is(postReply.getSone().getId()));
444                         assertThat(postFieldSet.getLong(prefix + "Replies." + postReplyIndex + ".Time"), is(postReply.getTime()));
445                         assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Text"), is(postReply.getText()));
446                         postReplyIndex++;
447                 }
448         }
449
450         @Test
451         public void testEncodingAPostWithoutRecipientWithFutureReplies() throws FSParseException {
452                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
453                 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
454                 PostReply postReply = createFuturePostReply(sone, "Reply.");
455                 when(post.getReplies()).thenReturn(asList(postReply));
456                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
457                 assertThat(postFieldSet, notNullValue());
458                 verifyPost(postFieldSet, "Post.", post);
459                 verifyPostReplies(postFieldSet, "Post.", Collections.<PostReply>emptyList());
460         }
461
462         @Test
463         public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
464                 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
465                 Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
466                 PostReply postReply = createPostReply(sone, "Reply.");
467                 when(post.getReplies()).thenReturn(asList(postReply));
468                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
469                 assertThat(postFieldSet, notNullValue());
470                 verifyPost(postFieldSet, "Post.", post);
471                 verifyPostReplies(postFieldSet, "Post.", asList(postReply));
472         }
473
474         @Test
475         public void testEncodingPostsWithoutRecipientAndReplies() throws FSParseException {
476                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
477                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
478                 Post post1 = createPost(sone1, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
479                 Post post2 = createPost(sone2, null, (long) (Math.random() * Long.MAX_VALUE), "Some other Text.");
480                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
481                 assertThat(postFieldSet, notNullValue());
482                 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
483         }
484
485         private void verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
486                 assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size()));
487                 int postIndex = 0;
488                 for (Post post : posts) {
489                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
490                         postIndex++;
491                 }
492         }
493
494         @Test
495         public void testEncodingPostsWithRecipientWithoutReplies() throws FSParseException {
496                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
497                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
498                 Post post1 = createPost(sone1, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
499                 Post post2 = createPost(sone2, "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", (long) (Math.random() * Long.MAX_VALUE), "Some other Text.");
500                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
501                 assertThat(postFieldSet, notNullValue());
502                 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
503         }
504
505         @Test
506         public void testEncodingPostsWithoutRecipientWithReplies() throws FSParseException {
507                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
508                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
509                 Post post1 = createPost(sone1, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
510                 Post post2 = createPost(sone2, null, (long) (Math.random() * Long.MAX_VALUE), "Some other Text.");
511                 PostReply postReply1 = createPostReply(sone2, "Reply from 2 to 1");
512                 PostReply postReply2 = createPostReply(sone1, "Reply from 1 to 2");
513                 when(post1.getReplies()).thenReturn(asList(postReply1));
514                 when(post2.getReplies()).thenReturn(asList(postReply2));
515                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
516                 assertThat(postFieldSet, notNullValue());
517                 assertThat(postFieldSet.getInt("Posts.Count"), is(2));
518                 assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId()));
519                 assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId()));
520                 assertThat(postFieldSet.get("Posts.0.Recipient"), nullValue());
521                 assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime()));
522                 assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText()));
523                 assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1));
524                 assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId()));
525                 assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId()));
526                 assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime()));
527                 assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText()));
528                 assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId()));
529                 assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId()));
530                 assertThat(postFieldSet.get("Posts.1.Recipient"), nullValue());
531                 assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime()));
532                 assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText()));
533                 assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(1));
534                 assertThat(postFieldSet.get("Posts.1.Replies.0.ID"), is(postReply2.getId()));
535                 assertThat(postFieldSet.get("Posts.1.Replies.0.Sone"), is(postReply2.getSone().getId()));
536                 assertThat(postFieldSet.getLong("Posts.1.Replies.0.Time"), is(postReply2.getTime()));
537                 assertThat(postFieldSet.get("Posts.1.Replies.0.Text"), is(postReply2.getText()));
538         }
539
540         @Test
541         public void testEncodingPostsWithRecipientAndReplies() throws FSParseException {
542                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
543                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
544                 Post post1 = createPost(sone1, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
545                 Post post2 = createPost(sone2, "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", (long) (Math.random() * Long.MAX_VALUE), "Some other Text.");
546                 PostReply postReply1 = createPostReply(sone2, "Reply from 2 to 1");
547                 PostReply postReply2 = createPostReply(sone1, "Reply from 1 to 2");
548                 when(post1.getReplies()).thenReturn(asList(postReply1));
549                 when(post2.getReplies()).thenReturn(asList(postReply2));
550                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
551                 assertThat(postFieldSet, notNullValue());
552                 assertThat(postFieldSet.getInt("Posts.Count"), is(2));
553                 assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId()));
554                 assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId()));
555                 assertThat(postFieldSet.get("Posts.0.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
556                 assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime()));
557                 assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText()));
558                 assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1));
559                 assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId()));
560                 assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId()));
561                 assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime()));
562                 assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText()));
563                 assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId()));
564                 assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId()));
565                 assertThat(postFieldSet.get("Posts.1.Recipient"), is("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"));
566                 assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime()));
567                 assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText()));
568                 assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(1));
569                 assertThat(postFieldSet.get("Posts.1.Replies.0.ID"), is(postReply2.getId()));
570                 assertThat(postFieldSet.get("Posts.1.Replies.0.Sone"), is(postReply2.getSone().getId()));
571                 assertThat(postFieldSet.getLong("Posts.1.Replies.0.Time"), is(postReply2.getTime()));
572                 assertThat(postFieldSet.get("Posts.1.Replies.0.Text"), is(postReply2.getText()));
573         }
574
575         @Test
576         public void testEncodingPostsWithRecipientAndFutureReplies() throws FSParseException {
577                 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
578                 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
579                 Post post1 = createPost(sone1, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
580                 Post post2 = createPost(sone2, "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", (long) (Math.random() * Long.MAX_VALUE), "Some other Text.");
581                 PostReply postReply1 = createPostReply(sone2, "Reply from 2 to 1");
582                 PostReply postReply2 = createFuturePostReply(sone1, "Reply from 1 to 2");
583                 when(post1.getReplies()).thenReturn(asList(postReply1));
584                 when(post2.getReplies()).thenReturn(asList(postReply2));
585                 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
586                 assertThat(postFieldSet, notNullValue());
587                 assertThat(postFieldSet.getInt("Posts.Count"), is(2));
588                 assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId()));
589                 assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId()));
590                 assertThat(postFieldSet.get("Posts.0.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
591                 assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime()));
592                 assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText()));
593                 assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1));
594                 assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId()));
595                 assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId()));
596                 assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime()));
597                 assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText()));
598                 assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId()));
599                 assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId()));
600                 assertThat(postFieldSet.get("Posts.1.Recipient"), is("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"));
601                 assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime()));
602                 assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText()));
603                 assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(0));
604         }
605
606 }