From 964e03744f8c3d918e5c78fb414772a0d2454584 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 30 Oct 2013 20:50:48 +0100 Subject: [PATCH] Add unit test for GetPostFeedCommand. --- .../sone/fcp/GetPostFeedCommandTest.java | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.java diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.java new file mode 100644 index 0000000..e54f338 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.java @@ -0,0 +1,171 @@ +/* + * Sone - GetPostFeedCommandTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.fcp; + +import static java.lang.System.currentTimeMillis; +import static java.util.Arrays.asList; +import static java.util.concurrent.TimeUnit.DAYS; +import static net.pterodactylus.sone.fcp.Verifiers.verifyPost; +import static net.pterodactylus.sone.fcp.Verifiers.verifyPostReply; +import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; +import net.pterodactylus.sone.freenet.fcp.Command.Response; +import net.pterodactylus.sone.freenet.fcp.FcpException; + +import freenet.node.FSParseException; +import freenet.support.SimpleFieldSet; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link GetPostFeedCommand}. + * + * @author David ‘Bombe’ Roden + */ +public class GetPostFeedCommandTest { + + private final Mocks mocks = new Mocks(); + private final GetPostFeedCommand getPostFeedCommand = new GetPostFeedCommand(mocks.core); + private Sone remoteSone; + private Sone friendSone; + private Sone localSone; + private Post localPost; + private PostReply friendReplyToLocalPost; + private PostReply remoteReplyToLocalPost; + private Post friendPost; + private Post remotePost; + + @Before + public void setup() { + remoteSone = mocks.mockSone("RSone").create(); + friendSone = mocks.mockSone("FSone").create(); + localSone = mocks.mockSone("LSone").local().withFriends(asList(friendSone.getId())).create(); + localPost = mocks.mockPost(localSone, "LPost").withTime(daysBefore(11)).withText("My post.").create(); + friendReplyToLocalPost = mocks.mockPostReply(friendSone, "FReply").inReplyTo(localPost).withTime(daysBefore(9)).withText("No.").create(); + remoteReplyToLocalPost = mocks.mockPostReply(remoteSone, "RReply").inReplyTo(localPost).withTime(daysBefore(7)).withText("Yes.").create(); + mocks.mockPostReply(remoteSone, "FutureReply1").inReplyTo(localPost).withTime(daysBefore(-1)).withText("Future!").create(); + friendPost = mocks.mockPost(friendSone, "FPost").withTime(daysBefore(12)).withText("The friend's post.").create(); + remotePost = mocks.mockPost(remoteSone, "RPost").withTime(daysBefore(13)).withText("Hello, LSone!").withRecipient(localSone.getId()).create(); + } + + @Test + public void getFeedForLocalSone() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .put("Sone", "LSone") + .get(); + Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostFeed")); + assertThat(response.getReplyParameters().getInt("Posts.Count"), is(3)); + verifyPost(response.getReplyParameters(), "Posts.0.", localPost); + assertThat(response.getReplyParameters().getInt("Posts.0.Replies.Count"), is(2)); + verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.0.", friendReplyToLocalPost); + verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.1.", remoteReplyToLocalPost); + verifyPost(response.getReplyParameters(), "Posts.1.", friendPost); + verifyPost(response.getReplyParameters(), "Posts.2.", remotePost); + } + + @Test + public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .put("Sone", "LSone") + .put("StartPost", "1") + .get(); + Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostFeed")); + assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2)); + verifyPost(response.getReplyParameters(), "Posts.0.", friendPost); + verifyPost(response.getReplyParameters(), "Posts.1.", remotePost); + } + + @Test + public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .put("Sone", "LSone") + .put("MaxPosts", "2") + .get(); + Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostFeed")); + assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2)); + verifyPost(response.getReplyParameters(), "Posts.0.", localPost); + assertThat(response.getReplyParameters().getInt("Posts.0.Replies.Count"), is(2)); + verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.0.", friendReplyToLocalPost); + verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.1.", remoteReplyToLocalPost); + verifyPost(response.getReplyParameters(), "Posts.1.", friendPost); + } + + @Test + public void getFeedForLocalSoneWithOnePostSkippingTheFirst() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .put("Sone", "LSone") + .put("MaxPosts", "1") + .put("StartPost", "1") + .get(); + Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostFeed")); + assertThat(response.getReplyParameters().getInt("Posts.Count"), is(1)); + verifyPost(response.getReplyParameters(), "Posts.0.", friendPost); + } + + @Test + public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .put("Sone", "LSone") + .put("StartPost", "10") + .get(); + Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostFeed")); + assertThat(response.getReplyParameters().getInt("Posts.Count"), is(0)); + } + + @Test(expected = FcpException.class) + public void getFeedWithoutLocalSone() throws FcpException, FSParseException { + SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetPostFeed") + .get(); + getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT); + } + + private long daysBefore(int days) { + return currentTimeMillis() - DAYS.toMillis(days); + } + +} -- 2.7.4