Add test for adding a field.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneParserTest.java
index dbc0d10..ee6c82f 100644 (file)
@@ -4,9 +4,9 @@ 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 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.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
@@ -15,6 +15,7 @@ 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.data.Client;
 import net.pterodactylus.sone.data.Image;
@@ -34,6 +35,10 @@ import org.junit.Test;
  */
 public class SoneParserTest {
 
+       static {
+               Logger.getLogger("").setLevel(OFF);
+       }
+
        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());
@@ -46,45 +51,69 @@ public class SoneParserTest {
        }
 
        @Test
-       public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException, SoneException {
-               assertThat(soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>")), nullValue());
+       public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
+               assertThat(sone, notNullValue());
+               assertThat(sone.isPresent(), is(false));
+       }
+
+       @Test
+       public void verifyThatANegativeProtocolVersionCausesAnError() {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get());
+               assertThat(sone, notNullValue());
+               assertThat(sone.isPresent(), is(false));
+       }
+
+       @Test
+       public void verifyThatATooLargeProtocolVersionCausesAnError() {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get());
+               assertThat(sone, notNullValue());
+               assertThat(sone.isPresent(), is(false));
        }
 
        @Test
-       public void verifyThatANegativeProtocolVersionCausesAnError() throws SoneException {
-               assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue());
+       public void verifyThatAMissingTimeCausesAnError() {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeTime().get());
+               assertThat(sone, notNullValue());
+               assertThat(sone.isPresent(), is(false));
        }
 
        @Test
-       public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() throws SoneException {
-               Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
+       public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
                assertThat(sone, notNullValue());
-               assertThat(sone.getClient(), notNullValue());
-               assertThat(sone.getClient(), is(originalSone.getClient()));
+               assertThat(sone.isPresent(), is(true));
+               assertThat(sone.get().getClient(), notNullValue());
+               assertThat(sone.get().getClient(), is(originalSone.getClient()));
        }
 
        @Test
-       public void verifyThatTheCreatedSoneMeetsAllExpectations() throws SoneException {
-               Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get());
+       public void verifyThatTheCreatedSoneMeetsAllExpectations() {
+               Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("complete"));
                assertThat(sone, notNullValue());
-               assertThat(sone.getTime(), is(1000L));
-               assertThat(sone.getClient(), notNullValue());
-               assertThat(sone.getClient().getName(), is("Test-Client"));
-               assertThat(sone.getClient().getVersion(), is("1.0"));
-               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(2000));
-               assertThat(sone.getProfile().getBirthMonth(), is(9));
-               assertThat(sone.getProfile().getBirthDay(), is(13));
-               assertThat(sone.getProfile().getAvatar(), is("avatar-id"));
+               assertThat(sone.isPresent(), is(true));
+               assertThat(sone.get().getTime(), is(1382419919000L));
+               assertThat(sone.get().getClient(), notNullValue());
+               assertThat(sone.get().getClient().getName(), is("Sone"));
+               assertThat(sone.get().getClient().getVersion(), is("0.8.7"));
+               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(2013));
+               assertThat(sone.get().getProfile().getBirthMonth(), is(10));
+               assertThat(sone.get().getProfile().getBirthDay(), is(22));
+               assertThat(sone.get().getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
        }
 
        public InputStream getInputStream(String content) throws UnsupportedEncodingException {
                return new ByteArrayInputStream(content.getBytes("UTF-8"));
        }
 
+       private InputStream getXml(String name) {
+               return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));
+       }
+
        private static class SoneXmlBuilder {
 
                private Optional<Long> time = of(1000L);
@@ -166,7 +195,6 @@ public class SoneParserTest {
                        content.append("</sone>");
                        try {
                                String xmlString = content.toString();
-                               System.out.println(xmlString);
                                return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                                /* ignore. */