Verify that an invalid album causes an error.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneParserTest.java
index 726354b..87f6600 100644 (file)
@@ -3,20 +3,25 @@ package net.pterodactylus.sone.core;
 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.CoreMatchers.nullValue;
 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.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.SoneParser.DuplicateField;
+import net.pterodactylus.sone.core.SoneParser.InvalidProtocolVersion;
+import net.pterodactylus.sone.core.SoneParser.InvalidXml;
+import net.pterodactylus.sone.core.SoneParser.MalformedTime;
+import net.pterodactylus.sone.core.SoneParser.MalformedXml;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
@@ -54,22 +59,22 @@ public class SoneParserTest {
                when(core.getImage(anyString())).thenReturn(image);
        }
 
-       @Test
+       @Test(expected = InvalidXml.class)
        public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
                soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
        }
 
-       @Test
+       @Test(expected = InvalidProtocolVersion.class)
        public void verifyThatANegativeProtocolVersionCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
        }
 
-       @Test
+       @Test(expected = InvalidProtocolVersion.class)
        public void verifyThatATooLargeProtocolVersionCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
        }
 
-       @Test
+       @Test(expected = MalformedXml.class)
        public void verifyThatAMissingTimeCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("missing-time"));
        }
@@ -90,6 +95,84 @@ public class SoneParserTest {
                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
+       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
+       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() {
                Sone sone = soneParser.parseSone(database, originalSone, getXml("complete"));