Add test for encoding a Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 25 Oct 2013 17:29:51 +0000 (19:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:39 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java

index b2d7556..8683b24 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
+import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Sone;
+
+import freenet.node.FSParseException;
+import freenet.support.SimpleFieldSet;
+
+import com.google.common.base.Optional;
 import org.junit.Test;
 
 /**
@@ -48,4 +58,29 @@ public class AbstractSoneCommandTest {
                return testString.toString();
        }
 
+       @Test
+       public void testEncodingASone() throws FSParseException {
+               Sone sone = prepareSoneToBeEncoded();
+               SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
+               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.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 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);
+               return sone;
+       }
+
 }