package net.pterodactylus.sone.core;
-import static com.google.common.base.Optional.absent;
-import static com.google.common.base.Optional.fromNullable;
-import static com.google.common.base.Optional.of;
+import static com.google.common.base.Objects.equal;
import static java.lang.String.format;
import static java.util.logging.Level.OFF;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.io.ByteArrayInputStream;
import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;
+import net.pterodactylus.sone.core.SoneParser.DuplicateField;
+import net.pterodactylus.sone.core.SoneParser.InvalidAvatarId;
+import net.pterodactylus.sone.core.SoneParser.InvalidParentAlbum;
+import net.pterodactylus.sone.core.SoneParser.InvalidProtocolVersion;
+import net.pterodactylus.sone.core.SoneParser.InvalidXml;
+import net.pterodactylus.sone.core.SoneParser.MalformedDimension;
+import net.pterodactylus.sone.core.SoneParser.MalformedTime;
+import net.pterodactylus.sone.core.SoneParser.MalformedXml;
+import net.pterodactylus.sone.core.SoneParser.SoneTooNew;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
+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.database.Database;
import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
import net.pterodactylus.sone.database.memory.MemoryDatabase;
-import com.google.common.base.Joiner;
import com.google.common.base.Optional;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
/**
private final Core core = mock(Core.class);
private final Database database = new MemoryDatabase(null);
private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
- private final SoneXmlBuilder soneXmlBuilder = new SoneXmlBuilder();
private final SoneParser soneParser = new SoneParser();
public SoneParserTest() {
when(core.getImage(anyString())).thenReturn(image);
}
- @Test
- public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException {
- Optional<Sone> sone = soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>"));
- assertThat(sone, notNullValue());
- assertThat(sone.isPresent(), is(false));
+ @Test(expected = InvalidXml.class)
+ public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
}
@Test
+ public void verifyThatAMissingProtocolVersionDoesNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-protocol-version"));
+ }
+
+ @Test(expected = InvalidProtocolVersion.class)
public void verifyThatANegativeProtocolVersionCausesAnError() {
- Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get());
- assertThat(sone, notNullValue());
- assertThat(sone.isPresent(), is(false));
+ soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
}
- @Test
+ @Test(expected = SoneTooNew.class)
public void verifyThatATooLargeProtocolVersionCausesAnError() {
- Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get());
- assertThat(sone, notNullValue());
- assertThat(sone.isPresent(), is(false));
+ soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatAMissingTimeCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-time"));
+ }
+
+ @Test(expected = MalformedTime.class)
+ public void verifyThatAnInvalidTimeCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-time"));
}
@Test
public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
- Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
+ Sone sone = soneParser.parseSone(database, originalSone, getXml("missing-client"));
+ assertThat(sone, notNullValue());
+ assertThat(sone.getClient(), notNullValue());
+ assertThat(sone.getClient(), is(originalSone.getClient()));
+ }
+
+ @Test
+ public void verifyThatAnInvalidClientCausesTheOriginalClientToBeUsed() {
+ Sone sone = soneParser.parseSone(database, originalSone, getXml("invalid-client"));
assertThat(sone, notNullValue());
- assertThat(sone.isPresent(), is(true));
- assertThat(sone.get().getClient(), notNullValue());
- assertThat(sone.get().getClient(), is(originalSone.getClient()));
+ assertThat(sone.getClient(), notNullValue());
+ assertThat(sone.getClient(), is(originalSone.getClient()));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatAMissingProfileCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-profile"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatInvalidFieldsCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-field"));
+ }
+
+ @Test(expected = DuplicateField.class)
+ public void verifyThatDuplicateFieldsCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("duplicate-field"));
+ }
+
+ @Test(expected = InvalidAvatarId.class)
+ public void verifyThatAnInvalidAvatarIdCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-avatar"));
+ }
+
+ @Test
+ public void verifyThatMissingPostsDoNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-posts"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatInvalidPostsCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-posts"));
+ }
+
+ @Test(expected = MalformedTime.class)
+ public void verifyThatAMalformedTimeCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-post-time"));
+ }
+
+ @Test
+ public void verifyThatMissingRepliesDoNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-replies"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatInvalidRepliesCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-replies"));
+ }
+
+ @Test(expected = MalformedTime.class)
+ public void verifyThatAMalformedReplyTimeCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-reply-time"));
+ }
+
+ @Test
+ public void verifyThatAMissingPostLikesSectionDoesNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-post-likes"));
+ }
+
+ @Test
+ public void verifyThatAMissingReplyLikesSectionDoesNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-reply-likes"));
+ }
+
+ @Test
+ public void verifyThatMissingAlbumsSectionDoNotCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("missing-albums"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatAnInvalidAlbumCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-album"));
+ }
+
+ @Test(expected = InvalidParentAlbum.class)
+ public void verifyThatAnInvalidParentAlbumCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-parent-album"));
+ }
+
+ @Test(expected = MalformedXml.class)
+ public void verifyThatAnInvalidImageCausesAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-image"));
+ }
+
+ @Test(expected = MalformedDimension.class)
+ public void verifyThatInvalidImageDimensionsCauseAnError() {
+ soneParser.parseSone(database, originalSone, getXml("invalid-image-dimensions"));
+ }
+
+ @Test
+ public void verifyThatAnEmptyProfileIsParsedWithoutError() {
+ Sone sone = soneParser.parseSone(database, originalSone, getXml("empty-profile"));
+ assertThat(sone.getProfile().getFirstName(), nullValue());
+ assertThat(sone.getProfile().getMiddleName(), nullValue());
+ assertThat(sone.getProfile().getLastName(), nullValue());
+ assertThat(sone.getProfile().getBirthYear(), nullValue());
+ assertThat(sone.getProfile().getBirthMonth(), nullValue());
+ assertThat(sone.getProfile().getBirthDay(), nullValue());
+ assertThat(sone.getProfile().getAvatar(), nullValue());
+ assertThat(sone.getProfile().getFields(), empty());
}
@Test
public void verifyThatTheCreatedSoneMeetsAllExpectations() {
- Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get());
+ Sone sone = soneParser.parseSone(database, originalSone, getXml("complete"));
assertThat(sone, notNullValue());
- assertThat(sone.isPresent(), is(true));
- assertThat(sone.get().getTime(), is(1000L));
- assertThat(sone.get().getClient(), notNullValue());
- assertThat(sone.get().getClient().getName(), is("Test-Client"));
- assertThat(sone.get().getClient().getVersion(), is("1.0"));
- assertThat(sone.get().getProfile(), notNullValue());
- assertThat(sone.get().getProfile().getFirstName(), is("First"));
- assertThat(sone.get().getProfile().getMiddleName(), is("M."));
- assertThat(sone.get().getProfile().getLastName(), is("Last"));
- assertThat(sone.get().getProfile().getBirthYear(), is(2000));
- assertThat(sone.get().getProfile().getBirthMonth(), is(9));
- assertThat(sone.get().getProfile().getBirthDay(), is(13));
- assertThat(sone.get().getProfile().getAvatar(), is("avatar-id"));
- }
-
- public InputStream getInputStream(String content) throws UnsupportedEncodingException {
- return new ByteArrayInputStream(content.getBytes("UTF-8"));
- }
-
- private static class SoneXmlBuilder {
-
- private Optional<Long> time = of(1000L);
- private Optional<String> protocolVersion = of("0");
- private Optional<String> clientInformation = of("<name>Test-Client</name><version>1.0</version>");
- private Optional<String> profile = of(Joiner.on("").join(
- "<first-name>First</first-name>",
- "<middle-name>M.</middle-name>",
- "<last-name>Last</last-name>",
- "<birth-year>2000</birth-year>",
- "<birth-month>9</birth-month>",
- "<birth-day>13</birth-day>",
- "<avatar>avatar-id</avatar>",
- "<fields>",
- "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
- "</fields>"
+ assertThat(sone.getTime(), is(1382419919000L));
+ assertThat(sone.getClient(), notNullValue());
+ assertThat(sone.getClient().getName(), is("Sone"));
+ assertThat(sone.getClient().getVersion(), is("0.8.7"));
+ assertThat(sone.getProfile(), notNullValue());
+ assertThat(sone.getProfile().getFirstName(), is("First"));
+ assertThat(sone.getProfile().getMiddleName(), is("M."));
+ assertThat(sone.getProfile().getLastName(), is("Last"));
+ assertThat(sone.getProfile().getBirthYear(), is(2013));
+ assertThat(sone.getProfile().getBirthMonth(), is(10));
+ assertThat(sone.getProfile().getBirthDay(), is(22));
+ assertThat(sone.getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
+ assertThat(sone.getProfile().getFields(), contains(
+ fieldMatcher("Field1", "Value1"),
+ fieldMatcher("Field2", "Value2")
));
- private Optional<String> posts = of("<post><id>post-id</id><time>1</time><recipient>recipient</recipient><text>Hello!</text></post>");
- private Optional<String> replies = of("<reply><id>reply-id</id><post-id>post-id</post-id><time>2</time><text>Reply!</text></reply>");
-
- public SoneXmlBuilder removeTime() {
- time = absent();
- return this;
- }
-
- public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
- this.protocolVersion = fromNullable(protocolVersion);
- return this;
- }
-
- public SoneXmlBuilder removeProtocolVersion() {
- this.protocolVersion = absent();
- return this;
- }
-
- public SoneXmlBuilder setClientInformation(String name, String version) {
- clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
- return this;
- }
-
- public SoneXmlBuilder removeClientInformation() {
- clientInformation = absent();
- return this;
- }
-
- public SoneXmlBuilder removeProfile() {
- profile = absent();
- return this;
- }
-
- public SoneXmlBuilder removePost() {
- posts = absent();
- return this;
- }
-
- public SoneXmlBuilder removeReply() {
- replies = absent();
- return this;
- }
-
- public InputStream get() {
- StringBuilder content = new StringBuilder();
- content.append("<sone>");
- if (time.isPresent()) {
- content.append(createXmlElement("time", String.valueOf(time.get())));
+ assertThat(sone.getPosts(), contains(
+ postMatcher("d8c9586e-3adb-11e3-bb31-171fc040e645", "0rpD4gL8mszav2trndhIdKIxvKUCNAe2kjA3dLV8CVU", 1382420181000L, "Hello, User!"),
+ postMatcher("bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", null, 1382420140000L, "Hello, World!")
+ ));
+ assertThat(sone.getReplies(), containsInAnyOrder(
+ postReplyMatcher("f09fa448-3adb-11e3-a783-ab54a11aacc4", "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", 1382420224000L, "Talking to myself."),
+ postReplyMatcher("0a376440-3adc-11e3-8f45-c7cc157436a5", "11ebe86e-3adc-11e3-b7b9-7f2c88018a33", 1382420271000L, "Talking to somebody I can't see.")
+ ));
+ assertThat(sone.getLikedPostIds(), containsInAnyOrder(
+ "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3",
+ "305d85e6-3adc-11e3-be45-8b53dd91f0af"
+ ));
+ assertThat(sone.getLikedReplyIds(), containsInAnyOrder(
+ "f09fa448-3adb-11e3-a783-ab54a11aacc4",
+ "3ba28960-3adc-11e3-93c7-6713d170f44c"
+ ));
+ }
+
+ private Matcher<PostReply> postReplyMatcher(final String id, final String postId, final long time, final String text) {
+ return new TypeSafeMatcher<PostReply>() {
+ @Override
+ protected boolean matchesSafely(PostReply postReply) {
+ return postReply.getId().equals(id) && postReply.getPostId().equals(postId) && (postReply.getTime() == time) && postReply.getText().equals(text);
}
- if (protocolVersion.isPresent()) {
- content.append(createXmlElement("protocol-version", protocolVersion.get()));
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("PostReply(")
+ .appendValue(id).appendText(", ")
+ .appendValue(postId).appendText(", ")
+ .appendValue(time).appendText(", ")
+ .appendValue(text).appendText(")");
}
- if (clientInformation.isPresent()) {
- content.append(createXmlElement("client", clientInformation.get()));
+
+ @Override
+ protected void describeMismatchSafely(PostReply postReply, Description mismatchDescription) {
+ mismatchDescription.appendText("PostReply(")
+ .appendValue(postReply.getId()).appendText(", ")
+ .appendValue(postReply.getPostId()).appendText(", ")
+ .appendValue(postReply.getTime()).appendText(", ")
+ .appendValue(postReply.getText()).appendText(")");
}
- if (profile.isPresent()) {
- content.append(createXmlElement("profile", profile.get()));
+ };
+ }
+
+ private Matcher<Post> postMatcher(final String id, final String recipient, final long time, final String text) {
+ return new TypeSafeMatcher<Post>() {
+ @Override
+ protected boolean matchesSafely(Post post) {
+ return post.getId().equals(id) && equal(post.getRecipientId().orNull(), recipient) && (post.getTime() == time) && post.getText().equals(text);
}
- if (posts.isPresent()) {
- content.append(createXmlElement("posts", posts.get()));
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Post(")
+ .appendValue(id).appendText(", ")
+ .appendValue(recipient).appendText(", ")
+ .appendValue(time).appendText(", ")
+ .appendValue(text).appendText(")");
+ }
+
+ @Override
+ protected void describeMismatchSafely(Post post, Description mismatchDescription) {
+ mismatchDescription.appendText("Post(")
+ .appendValue(post.getId()).appendText(", ")
+ .appendValue(post.getRecipientId().orNull()).appendText(", ")
+ .appendValue(post.getTime()).appendText(", ")
+ .appendValue(post.getText()).appendText(")");
}
- content.append("</sone>");
- try {
- String xmlString = content.toString();
- System.out.println(xmlString);
- return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- /* ignore. */
+ };
+ }
+
+ private Matcher<Field> fieldMatcher(final String name, final String value) {
+ return new TypeSafeMatcher<Field>() {
+ @Override
+ protected boolean matchesSafely(Field field) {
+ return field.getName().equals(name) && field.getValue().equals(value);
}
- return null;
- }
- private String createXmlElement(String xmlElement, String content) {
- return format("<%s>%s</%1$s>", xmlElement, content);
- }
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("field named ").appendValue(name).appendText(" with value ").appendValue(value);
+ }
+
+ @Override
+ protected void describeMismatchSafely(Field field, Description mismatchDescription) {
+ mismatchDescription.appendText("field named ").appendValue(field.getName()).appendText(" with value ").appendValue(field.getValue());
+ }
+ };
+ }
+ private InputStream getXml(String name) {
+ return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));
}
}