X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommandTest.java;h=84e3ec58b8e3398b2770498ea690eaa937bd2ab6;hb=500504890e6fc9d125c46ddd7192c92215d4a119;hp=f0fd58cdfe0655f5b906bab60853ae5d8d078a48;hpb=f8afc0d2fca50c71b9a8dfc97f11014f07270ad6;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java index f0fd58c..84e3ec5 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java @@ -17,8 +17,11 @@ package net.pterodactylus.sone.fcp; +import static com.google.common.base.Optional.of; +import static java.util.Arrays.asList; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString; +import static net.pterodactylus.sone.template.SoneAccessor.getNiceName; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; @@ -26,6 +29,8 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.List; + import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; @@ -34,6 +39,7 @@ import freenet.support.SimpleFieldSet; import com.google.common.base.Optional; import org.junit.Test; +import org.mockito.Matchers; /** * Unit test for {@link AbstractSoneCommand}. @@ -73,16 +79,100 @@ public class AbstractSoneCommandTest { assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1")); } + @Test + public void testEncodingAFollowedSone() throws FSParseException { + Sone sone = prepareSoneToBeEncoded(); + Sone localSone = prepareLocalSoneThatFollowsEverybody(); + SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone)); + assertThat(soneFieldSet, notNullValue()); + assertThat(soneFieldSet.get("Prefix.Name"), is("test")); + assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last")); + assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime())); + assertThat(soneFieldSet.get("Prefix.Followed"), is("true")); + assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1)); + assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1")); + assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1")); + } + + @Test + public void testEncodingANotFollowedSone() throws FSParseException { + Sone sone = prepareSoneToBeEncoded(); + Sone localSone = prepareLocalSoneThatFollowsNobody(); + SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone)); + assertThat(soneFieldSet, notNullValue()); + assertThat(soneFieldSet.get("Prefix.Name"), is("test")); + assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last")); + assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime())); + assertThat(soneFieldSet.get("Prefix.Followed"), is("false")); + assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1)); + assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1")); + assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1")); + } + + private Sone prepareLocalSoneThatFollowsEverybody() { + Sone sone = mock(Sone.class); + when(sone.hasFriend(Matchers.any())).thenReturn(true); + return sone; + } + + private Sone prepareLocalSoneThatFollowsNobody() { + Sone sone = mock(Sone.class); + when(sone.hasFriend(Matchers.any())).thenReturn(false); + return sone; + } + private Sone prepareSoneToBeEncoded() { long time = (long) (Math.random() * Long.MAX_VALUE); Sone sone = mock(Sone.class); - Profile profile = new Profile(sone); - profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").update(); - profile.setField(profile.addField("Test1"), "Value1"); when(sone.getName()).thenReturn("test"); when(sone.getTime()).thenReturn(time); - when(sone.getProfile()).thenReturn(profile); + when(sone.getProfile()).thenReturn(prepareProfile(sone, "First", "M.", "Last")); return sone; } + @Test + public void testEncodingMultipleSones() throws FSParseException { + List sones = prepareMultipleSones(); + SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix."); + assertThat(sonesFieldSet, notNullValue()); + assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size())); + assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId())); + assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName())); + assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0)))); + assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime())); + assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId())); + assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName())); + assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1)))); + assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime())); + assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId())); + assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName())); + assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2)))); + assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime())); + } + + private List prepareMultipleSones() { + Sone sone1 = mock(Sone.class); + when(sone1.getId()).thenReturn("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"); + when(sone1.getName()).thenReturn("Test1"); + when(sone1.getProfile()).thenReturn(prepareProfile(sone1, "Alpha", "A.", "First")); + when(sone1.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE)); + Sone sone2 = mock(Sone.class); + when(sone2.getId()).thenReturn("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"); + when(sone2.getName()).thenReturn("Test2"); + when(sone2.getProfile()).thenReturn(prepareProfile(sone2, "Beta", "B.", "Second")); + when(sone2.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE)); + Sone sone3 = mock(Sone.class); + when(sone3.getId()).thenReturn("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U"); + when(sone3.getName()).thenReturn("Test3"); + when(sone3.getProfile()).thenReturn(prepareProfile(sone3, "Gamma", "C.", "Third")); + when(sone3.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE)); + return asList(sone1, sone2, sone3); + } + + private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) { + Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update(); + profile.setField(profile.addField("Test1"), "Value1"); + return profile; + } + }