From a2a37ed250210c719bb21ab8dd886f089721b6e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 4 Nov 2013 21:52:02 +0100 Subject: [PATCH] Add unit test for GetSoneCommand. --- .../pterodactylus/sone/fcp/GetSoneCommandTest.java | 98 ++++++++++++++++++++++ .../java/net/pterodactylus/sone/fcp/Verifiers.java | 21 +++++ 2 files changed, 119 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/fcp/GetSoneCommandTest.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 index 0000000..4b51388 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/GetSoneCommandTest.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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.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); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java b/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java index d3d9f69..1578161 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java +++ b/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java @@ -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++; + } + } + } -- 2.7.4