Add unit test for GetSoneCommand.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 Nov 2013 20:52:02 +0000 (21:52 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:52 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/fcp/GetSoneCommandTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/fcp/Verifiers.java

diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetSoneCommandTest.java
new file mode 100644 (file)
index 0000000..4b51388
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Sone - GetSoneCommandTest.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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.fcp;
+
+import static java.util.Arrays.asList;
+import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer;
+import static net.pterodactylus.sone.fcp.Verifiers.verifyFollowedSone;
+import static net.pterodactylus.sone.fcp.Verifiers.verifyNotFollowedSone;
+import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
+
+import java.util.Arrays;
+
+import net.pterodactylus.sone.data.Mocks;
+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.Test;
+
+/**
+ * Unit test for {@link GetSoneCommand}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class GetSoneCommandTest {
+
+       private final Mocks mocks = new Mocks();
+       private final GetSoneCommand getSoneCommand = new GetSoneCommand(mocks.core);
+
+       @Test
+       public void gettingAFollowedSone() throws FcpException, FSParseException {
+               Sone followedSone = mocks.mockSone("FSone").withName("Followed Sone").withProfileName("F.", "Ollowed", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
+               mocks.mockSone("LSone").withFriends(asList(followedSone.getId())).local().create();
+               SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
+                               .put("Message", "GetSone")
+                               .put("Sone", "FSone")
+                               .put("LocalSone", "LSone")
+                               .get();
+               Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
+               verifyAnswer(response, "Sone");
+               verifyFollowedSone(response.getReplyParameters(), "", followedSone);
+       }
+
+       @Test
+       public void gettingANotFollowedSone() throws FcpException, FSParseException {
+               Sone unfollowedSone = mocks.mockSone("FSone").withName("Followed Sone").withProfileName("F.", "Ollowed", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
+               mocks.mockSone("LSone").withFriends(Arrays.<String>asList()).local().create();
+               SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
+                               .put("Message", "GetSone")
+                               .put("Sone", "FSone")
+                               .put("LocalSone", "LSone")
+                               .get();
+               Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
+               verifyAnswer(response, "Sone");
+               verifyNotFollowedSone(response.getReplyParameters(), "", unfollowedSone);
+       }
+
+       @Test
+       public void gettingASoneWithoutALocalSone() throws FcpException, FSParseException {
+               Sone sone = mocks.mockSone("Sone").withName("Some Sone").withProfileName("S.", "Ome", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
+               SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
+                               .put("Message", "GetSone")
+                               .put("Sone", "Sone")
+                               .get();
+               Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
+               verifyAnswer(response, "Sone");
+               verifyNotFollowedSone(response.getReplyParameters(), "", sone);
+       }
+
+       @Test(expected = FcpException.class)
+       public void gettingANonExistingSoneCausesAnError() throws FcpException {
+               SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
+                               .put("Message", "GetSone")
+                               .put("Sone", "Sone")
+                               .get();
+               getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
+       }
+
+}
index d3d9f69..1578161 100644 (file)
@@ -20,6 +20,7 @@ package net.pterodactylus.sone.fcp;
 import static com.google.common.collect.FluentIterable.from;
 import static java.lang.String.format;
 import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
+import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
@@ -28,6 +29,8 @@ import java.util.List;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.data.Profile.Field;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 
 import freenet.node.FSParseException;
@@ -96,4 +99,22 @@ public class Verifiers {
                verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
        }
 
+       static void verifyFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
+               verifyNotFollowedSone(simpleFieldSet, prefix, sone);
+               assertThat(simpleFieldSet.getBoolean(prefix + "Followed"), is(true));
+       }
+
+       static void verifyNotFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
+               assertThat(simpleFieldSet.get(prefix + "Name"), is(sone.getName()));
+               assertThat(simpleFieldSet.get(prefix + "NiceName"), is(getNiceName(sone)));
+               assertThat(simpleFieldSet.getLong(prefix + "LastUpdated"), is(sone.getTime()));
+               assertThat(simpleFieldSet.getInt(prefix + "Field.Count"), is(sone.getProfile().getFields().size()));
+               int fieldIndex = 0;
+               for (Field field : sone.getProfile().getFields()) {
+                       assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Name"), is(field.getName()));
+                       assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Value"), is(field.getValue()));
+                       fieldIndex++;
+               }
+       }
+
 }