package net.pterodactylus.sone.fcp;
import static com.google.common.base.Optional.of;
+import static java.util.Arrays.asList;
+import static java.util.UUID.randomUUID;
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;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.util.List;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
+import net.pterodactylus.sone.freenet.fcp.FcpException;
import freenet.node.FSParseException;
import freenet.support.SimpleFieldSet;
+import freenet.support.api.Bucket;
import com.google.common.base.Optional;
import org.junit.Test;
*/
public class AbstractSoneCommandTest {
+ private final Core core = mock(Core.class);
+ private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(core) {
+ @Override
+ public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
+ return null;
+ }
+ };
+
@Test
public void testStringEncoding() {
String testString = prepareStringToBeEncoded();
@Test
public void testEncodingASone() throws FSParseException {
- Sone sone = prepareSoneToBeEncoded();
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
assertThat(soneFieldSet, notNullValue());
assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
@Test
public void testEncodingAFollowedSone() throws FSParseException {
- Sone sone = prepareSoneToBeEncoded();
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
Sone localSone = prepareLocalSoneThatFollowsEverybody();
SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
assertThat(soneFieldSet, notNullValue());
@Test
public void testEncodingANotFollowedSone() throws FSParseException {
- Sone sone = prepareSoneToBeEncoded();
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
Sone localSone = prepareLocalSoneThatFollowsNobody();
SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
assertThat(soneFieldSet, notNullValue());
return sone;
}
- private Sone prepareSoneToBeEncoded() {
- long time = (long) (Math.random() * Long.MAX_VALUE);
+ @Test
+ public void testEncodingMultipleSones() throws FSParseException {
+ List<Sone> 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<Sone> prepareMultipleSones() {
+ Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
+ Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
+ Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
+ return asList(sone1, sone2, sone3);
+ }
+
+ private Sone createSone(String id, String name, String firstName, String middleName, String lastName, long time) {
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.getId()).thenReturn(id);
+ when(sone.getName()).thenReturn(name);
+ when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
when(sone.getTime()).thenReturn(time);
- when(sone.getProfile()).thenReturn(profile);
return sone;
}
+ 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;
+ }
+
+ @Test
+ public void testEncodingReplies() throws FSParseException {
+ List<PostReply> postReplies = preparePostReplies();
+ SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
+ assertThat(postRepliesFieldSet, notNullValue());
+ assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
+ assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
+ assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
+ assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
+ assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
+ }
+
+ private List<PostReply> preparePostReplies() {
+ Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
+ Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
+ Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
+ PostReply postReply1 = createPostReply(sone1, "Text 1");
+ PostReply postReply2 = createPostReply(sone2, "Text 2");
+ PostReply postReply3 = createPostReply(sone3, "Text 3");
+ return asList(postReply1, postReply2, postReply3);
+ }
+
+ private PostReply createPostReply(Sone sone, String text) {
+ PostReply postReply = mock(PostReply.class);
+ when(postReply.getId()).thenReturn(randomUUID().toString());
+ when(postReply.getSone()).thenReturn(sone);
+ when(postReply.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
+ when(postReply.getText()).thenReturn(text);
+ return postReply;
+ }
+
+ @Test
+ public void testEncodingLikes() throws FSParseException {
+ List<Sone> likes = prepareMultipleSones();
+ SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
+ assertThat(likesFieldSet, notNullValue());
+ assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
+ assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
+ assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
+ assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
+ }
+
+ @Test
+ public void testParsingAMandatorySone() throws FcpException {
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone, is(sone));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
+ when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
+ when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
+ }
+
}