2 * Sone - AbstractSoneCommandTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import static com.google.common.base.Optional.of;
21 import static com.google.common.collect.FluentIterable.from;
22 import static java.lang.System.currentTimeMillis;
23 import static java.util.Arrays.asList;
24 import static java.util.UUID.randomUUID;
25 import static java.util.concurrent.TimeUnit.DAYS;
26 import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
27 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
28 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
29 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
30 import static org.hamcrest.CoreMatchers.is;
31 import static org.hamcrest.CoreMatchers.notNullValue;
32 import static org.hamcrest.CoreMatchers.nullValue;
33 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
41 import net.pterodactylus.sone.data.Mocks;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.PostReply;
44 import net.pterodactylus.sone.data.Sone;
45 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
46 import net.pterodactylus.sone.freenet.fcp.FcpException;
48 import freenet.node.FSParseException;
49 import freenet.support.SimpleFieldSet;
50 import freenet.support.api.Bucket;
52 import com.google.common.base.Optional;
53 import org.junit.Test;
54 import org.mockito.Matchers;
57 * Unit test for {@link AbstractSoneCommand}.
59 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61 public class AbstractSoneCommandTest {
63 private final Mocks mocks = new Mocks();
64 private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(mocks.core) {
66 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
72 public void testStringEncoding() {
73 String testString = prepareStringToBeEncoded();
75 String encodedString = encodeString(testString);
76 assertThat(encodedString, notNullValue());
77 assertThat(encodedString.length(), is(testString.length() + 3));
80 private String prepareStringToBeEncoded() {
81 StringBuilder testString = new StringBuilder();
82 for (int i = 0; i < 4000; ++i) {
83 testString.append((char) i);
85 return testString.toString();
89 public void testEncodingASone() throws FSParseException {
90 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
91 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
92 assertThat(soneFieldSet, notNullValue());
93 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
94 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
95 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
96 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
97 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
98 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
99 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
103 public void testEncodingAFollowedSone() throws FSParseException {
104 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
105 Sone localSone = prepareLocalSoneThatFollowsEverybody();
106 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
107 assertThat(soneFieldSet, notNullValue());
108 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
109 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
110 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
111 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
112 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
113 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
114 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
118 public void testEncodingANotFollowedSone() throws FSParseException {
119 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
120 Sone localSone = prepareLocalSoneThatFollowsNobody();
121 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
122 assertThat(soneFieldSet, notNullValue());
123 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
124 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
125 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
126 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
127 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
128 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
129 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
132 private Sone prepareLocalSoneThatFollowsEverybody() {
133 Sone sone = mock(Sone.class);
134 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
138 private Sone prepareLocalSoneThatFollowsNobody() {
139 Sone sone = mock(Sone.class);
140 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
145 public void testEncodingMultipleSones() throws FSParseException {
146 List<Sone> sones = prepareMultipleSones();
147 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
148 assertThat(sonesFieldSet, notNullValue());
149 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
150 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
151 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
152 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
153 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
154 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
155 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
156 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
157 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
158 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
159 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
160 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
161 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
164 private List<Sone> prepareMultipleSones() {
165 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
166 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
167 Sone sone3 = mocks.mockSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U").withName("Test3").withProfileName("Gamma", "C.", "Third").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
168 return asList(sone1, sone2, sone3);
172 public void testEncodingReplies() throws FSParseException {
173 List<PostReply> postReplies = preparePostReplies();
174 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
175 assertThat(postRepliesFieldSet, notNullValue());
176 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
177 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
178 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
179 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
180 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
181 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
182 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
183 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
184 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
185 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
186 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
187 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
188 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
191 private List<PostReply> preparePostReplies() {
192 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
193 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
194 Sone sone3 = mocks.mockSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U").withName("Test3").withProfileName("Gamma", "C.", "Third").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
195 PostReply postReply1 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 1").create();
196 PostReply postReply2 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 2").create();
197 PostReply postReply3 = mocks.mockPostReply(sone3, randomUUID().toString()).withTime(currentTimeMillis()).withText("Text 3").create();
198 return asList(postReply1, postReply2, postReply3);
202 public void testEncodingLikes() throws FSParseException {
203 List<Sone> likes = prepareMultipleSones();
204 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
205 assertThat(likesFieldSet, notNullValue());
206 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
207 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
208 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
209 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
213 public void testParsingAMandatorySone() throws FcpException {
214 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
215 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
216 Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
217 assertThat(parsedSone, notNullValue());
218 assertThat(parsedSone, is(sone));
221 @Test(expected = FcpException.class)
222 public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
223 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
224 abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
227 @Test(expected = FcpException.class)
228 public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
229 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
230 abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
234 public void testParsingAMandatoryLocalSone() throws FcpException {
235 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").local().withName("Test").withProfileName("First", "M.", "Last").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
236 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
237 Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
238 assertThat(parsedSone, notNullValue());
239 assertThat(parsedSone, is(sone));
240 assertThat(parsedSone.isLocal(), is(true));
243 @Test(expected = FcpException.class)
244 public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
245 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
246 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
247 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
250 @Test(expected = FcpException.class)
251 public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
252 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
253 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
257 public void testParsingAnExistingOptionalSone() throws FcpException {
258 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
259 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
260 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
261 assertThat(parsedSone, notNullValue());
262 assertThat(parsedSone.isPresent(), is(true));
263 assertThat(parsedSone.get(), is(sone));
267 public void testParsingANonExistingOptionalSone() throws FcpException {
268 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
269 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
270 assertThat(parsedSone, notNullValue());
271 assertThat(parsedSone.isPresent(), is(false));
274 @Test(expected = FcpException.class)
275 public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException {
276 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
277 abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
281 public void testParsingAPost() throws FcpException {
282 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
283 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
284 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
285 Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
286 assertThat(parsedPost, notNullValue());
287 assertThat(parsedPost, is(post));
290 @Test(expected = FcpException.class)
291 public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
292 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", "InvalidPostId").get();
293 abstractSoneCommand.getPost(postFieldSet, "Post");
296 @Test(expected = FcpException.class)
297 public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
298 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
299 abstractSoneCommand.getPost(postFieldSet, "Post");
303 public void testParsingAReply() throws FcpException {
304 Sone sone = mocks.mockSone(randomUUID().toString()).create();
305 PostReply reply = mocks.mockPostReply(sone, randomUUID().toString()).create();
306 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
307 PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
308 assertThat(parsedReply, notNullValue());
309 assertThat(parsedReply, is(reply));
312 @Test(expected = FcpException.class)
313 public void testParsingANonExistingReply() throws FcpException {
314 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", "InvalidReplyId").get();
315 abstractSoneCommand.getReply(replyFieldSet, "Reply");
318 @Test(expected = FcpException.class)
319 public void testParsingAReplyFromANonExistingField() throws FcpException {
320 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
321 abstractSoneCommand.getReply(replyFieldSet, "Reply");
325 public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
326 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
327 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
328 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
329 assertThat(postFieldSet, notNullValue());
330 verifyPost(postFieldSet, "Post.", post);
333 private void verifyPost(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
334 assertThat(postFieldSet.get(prefix + "ID"), is(post.getId()));
335 assertThat(postFieldSet.get(prefix + "Sone"), is(post.getSone().getId()));
336 assertThat(postFieldSet.get(prefix + "Recipient"), is(post.getRecipientId().orNull()));
337 assertThat(postFieldSet.getLong(prefix + "Time"), is(post.getTime()));
338 assertThat(postFieldSet.get(prefix + "Text"), is(post.getText()));
342 public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
343 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
344 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
345 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
346 assertThat(postFieldSet, notNullValue());
347 verifyPost(postFieldSet, "Post.", post);
351 public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
352 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
353 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
354 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
355 when(post.getReplies()).thenReturn(asList(postReply));
356 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
357 assertThat(postFieldSet, notNullValue());
358 verifyPost(postFieldSet, "Post.", post);
359 verifyPostReplies(postFieldSet, "Post.", asList(postReply));
362 private void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection<PostReply> postReplies) throws FSParseException {
363 assertThat(postFieldSet.getInt(prefix + "Replies.Count"), is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
364 int postReplyIndex = 0;
365 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
366 assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".ID"), is(postReply.getId()));
367 assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Sone"), is(postReply.getSone().getId()));
368 assertThat(postFieldSet.getLong(prefix + "Replies." + postReplyIndex + ".Time"), is(postReply.getTime()));
369 assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Text"), is(postReply.getText()));
375 public void testEncodingAPostWithoutRecipientWithFutureReplies() throws FSParseException {
376 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
377 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
378 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply.").create();
379 when(post.getReplies()).thenReturn(asList(postReply));
380 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
381 assertThat(postFieldSet, notNullValue());
382 verifyPost(postFieldSet, "Post.", post);
383 verifyPostReplies(postFieldSet, "Post.", Collections.<PostReply>emptyList());
387 public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
388 Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
389 Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
390 PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
391 when(post.getReplies()).thenReturn(asList(postReply));
392 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
393 assertThat(postFieldSet, notNullValue());
394 verifyPost(postFieldSet, "Post.", post);
395 verifyPostReplies(postFieldSet, "Post.", asList(postReply));
399 public void testEncodingPostsWithoutRecipientAndReplies() throws FSParseException {
400 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
401 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
402 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
403 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
404 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
405 assertThat(postFieldSet, notNullValue());
406 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
409 private void verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
410 assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size()));
412 for (Post post : posts) {
413 verifyPost(postFieldSet, prefix + postIndex + ".", post);
419 public void testEncodingPostsWithRecipientWithoutReplies() throws FSParseException {
420 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
421 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
422 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
423 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
424 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
425 assertThat(postFieldSet, notNullValue());
426 verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
430 public void testEncodingPostsWithoutRecipientWithReplies() throws FSParseException {
431 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
432 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
433 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
434 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
435 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
436 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
437 when(post1.getReplies()).thenReturn(asList(postReply1));
438 when(post2.getReplies()).thenReturn(asList(postReply2));
439 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
440 assertThat(postFieldSet, notNullValue());
441 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
444 private void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
445 assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size()));
447 for (Post post : posts) {
448 verifyPost(postFieldSet, prefix + postIndex + ".", post);
449 verifyPostReplies(postFieldSet, prefix + postIndex + ".", post.getReplies());
455 public void testEncodingPostsWithRecipientAndReplies() throws FSParseException {
456 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
457 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
458 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
459 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
460 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
461 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
462 when(post1.getReplies()).thenReturn(asList(postReply1));
463 when(post2.getReplies()).thenReturn(asList(postReply2));
464 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
465 assertThat(postFieldSet, notNullValue());
466 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
470 public void testEncodingPostsWithRecipientAndFutureReplies() throws FSParseException {
471 Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
472 Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
473 Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
474 Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
475 PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
476 PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply from 1 to 2").create();
477 when(post1.getReplies()).thenReturn(asList(postReply1));
478 when(post2.getReplies()).thenReturn(asList(postReply2));
479 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
480 assertThat(postFieldSet, notNullValue());
481 verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));