+ @Test
+ public void testParsingAMandatorySone() throws FcpException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone, is(sone));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
+ }
+
+ @Test
+ public void testParsingAMandatoryLocalSone() throws FcpException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").local().withName("Test").withProfileName("First", "M.", "Last").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone, is(sone));
+ assertThat(parsedSone.isLocal(), is(true));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
+ }
+
+ @Test
+ public void testParsingAnExistingOptionalSone() throws FcpException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone.isPresent(), is(true));
+ assertThat(parsedSone.get(), is(sone));
+ }
+
+ @Test
+ public void testParsingANonExistingOptionalSone() throws FcpException {
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone.isPresent(), is(false));
+ }
+
+ @Test
+ public void testParsingAnOptionalSoneFromANonExistingField() throws FcpException {
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Optional<Sone> sone = abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
+ assertThat(sone, notNullValue());
+ assertThat(sone.isPresent(), is(false));
+ }
+
+ @Test
+ public void testParsingAPost() throws FcpException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
+ Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
+ assertThat(parsedPost, notNullValue());
+ assertThat(parsedPost, is(post));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
+ SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", "InvalidPostId").get();
+ abstractSoneCommand.getPost(postFieldSet, "Post");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
+ SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
+ abstractSoneCommand.getPost(postFieldSet, "Post");
+ }
+
+ @Test
+ public void testParsingAReply() throws FcpException {
+ Sone sone = mocks.mockSone(randomUUID().toString()).create();
+ PostReply reply = mocks.mockPostReply(sone, randomUUID().toString()).create();
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
+ PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ assertThat(parsedReply, notNullValue());
+ assertThat(parsedReply, is(reply));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingANonExistingReply() throws FcpException {
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", "InvalidReplyId").get();
+ abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAReplyFromANonExistingField() throws FcpException {
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
+ abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPost(postFieldSet, "Post.", post);
+ }
+
+ private void verifyPost(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
+ assertThat(postFieldSet.get(prefix + "ID"), is(post.getId()));
+ assertThat(postFieldSet.get(prefix + "Sone"), is(post.getSone().getId()));
+ assertThat(postFieldSet.get(prefix + "Recipient"), is(post.getRecipientId().orNull()));
+ assertThat(postFieldSet.getLong(prefix + "Time"), is(post.getTime()));
+ assertThat(postFieldSet.get(prefix + "Text"), is(post.getText()));
+ }
+
+ @Test
+ public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPost(postFieldSet, "Post.", post);
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostWithReplies(postFieldSet, "Post.", post);
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientWithFutureReplies() throws FSParseException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply.").create();
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostWithReplies(postFieldSet, "Post.", post);
+ }
+
+ @Test
+ public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
+ Sone sone = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test").withProfileName("First", "M.", "Last").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post = mocks.mockPost(sone, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ PostReply postReply = mocks.mockPostReply(sone, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply.").create();
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostWithReplies(postFieldSet, "Post.", post);
+ }
+
+ @Test
+ public void testEncodingPostsWithoutRecipientAndReplies() throws FSParseException {
+ Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
+ }
+
+ @Test
+ public void testEncodingPostsWithRecipientWithoutReplies() throws FSParseException {
+ Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPosts(postFieldSet, "Posts.", asList(post1, post2));
+ }
+
+ @Test
+ public void testEncodingPostsWithoutRecipientWithReplies() throws FSParseException {
+ Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient(null).withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
+ PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
+ PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
+ when(post1.getReplies()).thenReturn(asList(postReply1));
+ when(post2.getReplies()).thenReturn(asList(postReply2));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
+ }
+
+ @Test
+ public void testEncodingPostsWithRecipientAndReplies() throws FSParseException {
+ Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
+ PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
+ PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 1 to 2").create();
+ when(post1.getReplies()).thenReturn(asList(postReply1));
+ when(post2.getReplies()).thenReturn(asList(postReply2));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
+ }
+
+ @Test
+ public void testEncodingPostsWithRecipientAndFutureReplies() throws FSParseException {
+ Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Sone sone2 = mocks.mockSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withName("Test2").withProfileName("Beta", "B.", "Second").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();
+ Post post1 = mocks.mockPost(sone1, randomUUID().toString()).withRecipient("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some Text.").create();
+ Post post2 = mocks.mockPost(sone2, randomUUID().toString()).withRecipient("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withTime((long) (Math.random() * Long.MAX_VALUE)).withText("Some other Text.").create();
+ PostReply postReply1 = mocks.mockPostReply(sone2, randomUUID().toString()).withTime(currentTimeMillis()).withText("Reply from 2 to 1").create();
+ PostReply postReply2 = mocks.mockPostReply(sone1, randomUUID().toString()).withTime(currentTimeMillis() + DAYS.toMillis(1)).withText("Reply from 1 to 2").create();
+ when(post1.getReplies()).thenReturn(asList(postReply1));
+ when(post2.getReplies()).thenReturn(asList(postReply2));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts.");
+ assertThat(postFieldSet, notNullValue());
+ verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2));
+ }
+