X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParserTest.java;h=19e5ae2fd4899584ffe161fcce9f4c677b4c7663;hb=22d68072fb1770cdc4262ede974bd0a6202d7062;hp=53f11ae033ddb0e7e1e645f9164ed16e9e639fed;hpb=a43c73c72e81bb2a0ffa0af0fba6ec8673dcc1b1;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java index 53f11ae..19e5ae2 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -1,31 +1,45 @@ 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; /** @@ -42,7 +56,6 @@ public class SoneParserTest { 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.absent()); - private final SoneXmlBuilder soneXmlBuilder = new SoneXmlBuilder(); private final SoneParser soneParser = new SoneParser(); public SoneParserTest() { @@ -50,151 +63,263 @@ public class SoneParserTest { when(core.getImage(anyString())).thenReturn(image); } - @Test - public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException { - Optional sone = soneParser.parseSone(database, originalSone, getInputStream("This is not valid XML.")); - 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 = 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 = 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 = 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 = 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 time = of(1000L); - private Optional protocolVersion = of("0"); - private Optional clientInformation = of("Test-Client1.0"); - private Optional profile = of(Joiner.on("").join( - "First", - "M.", - "Last", - "2000", - "9", - "13", - "avatar-id", - "", - "Custom FieldCustom Value", - "" + 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 posts = of("post-idrecipientHello!"); - private Optional replies = of("reply-idpost-idReply!"); - - 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 + "" + 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(""); - 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 postReplyMatcher(final String id, final String postId, final long time, final String text) { + return new TypeSafeMatcher() { + @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 postMatcher(final String id, final String recipient, final long time, final String text) { + return new TypeSafeMatcher() { + @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(""); - try { - String xmlString = content.toString(); - return new ByteArrayInputStream(xmlString.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - /* ignore. */ + }; + } + + private Matcher fieldMatcher(final String name, final String value) { + return new TypeSafeMatcher() { + @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", 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)); } }