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.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;
35 import java.util.List;
37 import net.pterodactylus.sone.core.Core;
38 import net.pterodactylus.sone.data.Post;
39 import net.pterodactylus.sone.data.PostReply;
40 import net.pterodactylus.sone.data.Profile;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.database.Database;
43 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
44 import net.pterodactylus.sone.freenet.fcp.FcpException;
46 import freenet.node.FSParseException;
47 import freenet.support.SimpleFieldSet;
48 import freenet.support.api.Bucket;
50 import com.google.common.base.Optional;
51 import org.junit.Test;
52 import org.mockito.Matchers;
55 * Unit test for {@link AbstractSoneCommand}.
57 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59 public class AbstractSoneCommandTest {
61 private final Core core = mock(Core.class);
62 private final Database database = mock(Database.class);
63 private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(core) {
65 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
70 public AbstractSoneCommandTest() {
71 when(core.getDatabase()).thenReturn(database);
75 public void testStringEncoding() {
76 String testString = prepareStringToBeEncoded();
78 String encodedString = encodeString(testString);
79 assertThat(encodedString, notNullValue());
80 assertThat(encodedString.length(), is(testString.length() + 3));
83 private String prepareStringToBeEncoded() {
84 StringBuilder testString = new StringBuilder();
85 for (int i = 0; i < 4000; ++i) {
86 testString.append((char) i);
88 return testString.toString();
92 public void testEncodingASone() throws FSParseException {
93 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
94 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
95 assertThat(soneFieldSet, notNullValue());
96 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
97 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
98 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
99 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
100 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
101 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
102 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
106 public void testEncodingAFollowedSone() throws FSParseException {
107 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
108 Sone localSone = prepareLocalSoneThatFollowsEverybody();
109 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
110 assertThat(soneFieldSet, notNullValue());
111 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
112 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
113 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
114 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
115 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
116 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
117 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
121 public void testEncodingANotFollowedSone() throws FSParseException {
122 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
123 Sone localSone = prepareLocalSoneThatFollowsNobody();
124 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
125 assertThat(soneFieldSet, notNullValue());
126 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
127 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
128 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
129 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
130 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
131 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
132 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
135 private Sone prepareLocalSoneThatFollowsEverybody() {
136 Sone sone = mock(Sone.class);
137 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
141 private Sone prepareLocalSoneThatFollowsNobody() {
142 Sone sone = mock(Sone.class);
143 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
148 public void testEncodingMultipleSones() throws FSParseException {
149 List<Sone> sones = prepareMultipleSones();
150 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
151 assertThat(sonesFieldSet, notNullValue());
152 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
153 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
154 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
155 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
156 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
157 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
158 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
159 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
160 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
161 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
162 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
163 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
164 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
167 private List<Sone> prepareMultipleSones() {
168 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
169 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
170 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
171 return asList(sone1, sone2, sone3);
174 private Sone createSone(String id, String name, String firstName, String middleName, String lastName, long time) {
175 Sone sone = mock(Sone.class);
176 when(sone.getId()).thenReturn(id);
177 when(sone.getName()).thenReturn(name);
178 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
179 when(sone.getTime()).thenReturn(time);
183 private Sone createLocalSone(String id, String name, String firstName, String middleName, String lastName, long time) {
184 Sone sone = mock(Sone.class);
185 when(sone.getId()).thenReturn(id);
186 when(sone.getName()).thenReturn(name);
187 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
188 when(sone.getTime()).thenReturn(time);
189 when(sone.isLocal()).thenReturn(true);
193 private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
194 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
195 profile.setField(profile.addField("Test1"), "Value1");
200 public void testEncodingReplies() throws FSParseException {
201 List<PostReply> postReplies = preparePostReplies();
202 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
203 assertThat(postRepliesFieldSet, notNullValue());
204 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
205 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
206 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
207 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
208 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
209 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
210 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
211 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
212 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
213 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
214 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
215 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
216 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
219 private List<PostReply> preparePostReplies() {
220 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
221 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
222 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
223 PostReply postReply1 = createPostReply(sone1, "Text 1");
224 PostReply postReply2 = createPostReply(sone2, "Text 2");
225 PostReply postReply3 = createPostReply(sone3, "Text 3");
226 return asList(postReply1, postReply2, postReply3);
229 private PostReply createPostReply(Sone sone, String text) {
230 PostReply postReply = mock(PostReply.class);
231 when(postReply.getId()).thenReturn(randomUUID().toString());
232 when(postReply.getSone()).thenReturn(sone);
233 when(postReply.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
234 when(postReply.getText()).thenReturn(text);
239 public void testEncodingLikes() throws FSParseException {
240 List<Sone> likes = prepareMultipleSones();
241 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
242 assertThat(likesFieldSet, notNullValue());
243 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
244 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
245 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
246 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
250 public void testParsingAMandatorySone() throws FcpException {
251 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
252 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
253 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
254 Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
255 assertThat(parsedSone, notNullValue());
256 assertThat(parsedSone, is(sone));
259 @Test(expected = FcpException.class)
260 public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
261 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
262 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
263 abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
266 @Test(expected = FcpException.class)
267 public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
268 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
269 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
270 abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
274 public void testParsingAMandatoryLocalSone() throws FcpException {
275 Sone sone = createLocalSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
276 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
277 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
278 Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
279 assertThat(parsedSone, notNullValue());
280 assertThat(parsedSone, is(sone));
281 assertThat(parsedSone.isLocal(), is(true));
284 @Test(expected = FcpException.class)
285 public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
286 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
287 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
288 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
289 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
292 @Test(expected = FcpException.class)
293 public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
294 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
295 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
296 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
300 public void testParsingAnExistingOptionalSone() throws FcpException {
301 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
302 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
303 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
304 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
305 assertThat(parsedSone, notNullValue());
306 assertThat(parsedSone.isPresent(), is(true));
307 assertThat(parsedSone.get(), is(sone));
311 public void testParsingANonExistingOptionalSone() throws FcpException {
312 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
313 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
314 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
315 assertThat(parsedSone, notNullValue());
316 assertThat(parsedSone.isPresent(), is(false));
319 @Test(expected = FcpException.class)
320 public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException {
321 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
322 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
323 abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
327 public void testParsingAPost() throws FcpException {
328 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
329 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
330 when(database.getPost(eq(post.getId()))).thenReturn(of(post));
331 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
332 Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
333 assertThat(parsedPost, notNullValue());
334 assertThat(parsedPost, is(post));
337 private Post createPost(Sone sone, String recipient, long time, String text) {
338 Post post = mock(Post.class);
339 when(post.getId()).thenReturn(randomUUID().toString());
340 when(post.getSone()).thenReturn(sone);
341 when(post.getRecipientId()).thenReturn(fromNullable(recipient));
342 when(post.getTime()).thenReturn(time);
343 when(post.getText()).thenReturn(text);
347 @Test(expected = FcpException.class)
348 public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
349 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
350 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
351 when(database.getPost(Matchers.<String>any())).thenReturn(Optional.<Post>absent());
352 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
353 abstractSoneCommand.getPost(postFieldSet, "Post");
356 @Test(expected = FcpException.class)
357 public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
358 SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
359 abstractSoneCommand.getPost(postFieldSet, "Post");
363 public void testParsingAReply() throws FcpException {
364 PostReply reply = createPostReply();
365 when(database.getPostReply(eq(reply.getId()))).thenReturn(of(reply));
366 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
367 PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
368 assertThat(parsedReply, notNullValue());
369 assertThat(parsedReply, is(reply));
372 private PostReply createPostReply() {
373 PostReply postReply = mock(PostReply.class);
374 when(postReply.getId()).thenReturn(randomUUID().toString());
378 @Test(expected = FcpException.class)
379 public void testParsingANonExistingReply() throws FcpException {
380 PostReply reply = createPostReply();
381 when(database.getPostReply(Matchers.<String>any())).thenReturn(Optional.<PostReply>absent());
382 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
383 abstractSoneCommand.getReply(replyFieldSet, "Reply");
386 @Test(expected = FcpException.class)
387 public void testParsingAReplyFromANonExistingField() throws FcpException {
388 SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
389 abstractSoneCommand.getReply(replyFieldSet, "Reply");
393 public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
394 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
395 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
396 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
397 assertThat(postFieldSet, notNullValue());
398 assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
399 assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
400 assertThat(postFieldSet.get("Post.Recipient"), nullValue());
401 assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
402 assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
406 public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
407 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
408 Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
409 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
410 assertThat(postFieldSet, notNullValue());
411 assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
412 assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
413 assertThat(postFieldSet.get("Post.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
414 assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
415 assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
419 public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
420 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
421 Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
422 PostReply postReply = createPostReply(sone, "Reply.");
423 when(post.getReplies()).thenReturn(asList(postReply));
424 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
425 assertThat(postFieldSet, notNullValue());
426 assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
427 assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
428 assertThat(postFieldSet.get("Post.Recipient"), nullValue());
429 assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
430 assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
431 assertThat(postFieldSet.getInt("Post.Replies.Count"), is(1));
432 assertThat(postFieldSet.get("Post.Replies.0.ID"), is(postReply.getId()));
433 assertThat(postFieldSet.get("Post.Replies.0.Sone"), is(postReply.getSone().getId()));
434 assertThat(postFieldSet.getLong("Post.Replies.0.Time"), is(postReply.getTime()));
435 assertThat(postFieldSet.get("Post.Replies.0.Text"), is(postReply.getText()));
439 public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
440 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
441 Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
442 PostReply postReply = createPostReply(sone, "Reply.");
443 when(post.getReplies()).thenReturn(asList(postReply));
444 SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
445 assertThat(postFieldSet, notNullValue());
446 assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
447 assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
448 assertThat(postFieldSet.get("Post.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
449 assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
450 assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
451 assertThat(postFieldSet.getInt("Post.Replies.Count"), is(1));
452 assertThat(postFieldSet.get("Post.Replies.0.ID"), is(postReply.getId()));
453 assertThat(postFieldSet.get("Post.Replies.0.Sone"), is(postReply.getSone().getId()));
454 assertThat(postFieldSet.getLong("Post.Replies.0.Time"), is(postReply.getTime()));
455 assertThat(postFieldSet.get("Post.Replies.0.Text"), is(postReply.getText()));