From: David ‘Bombe’ Roden Date: Mon, 21 Oct 2013 04:38:21 +0000 (+0200) Subject: Add unit test for the Sone parser. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ff3f8b6cca48116e75137d88ab83e1645db82e3e;p=Sone.git Add unit test for the Sone parser. --- diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java new file mode 100644 index 0000000..dbc0d10 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -0,0 +1,183 @@ +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 java.lang.String.format; +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; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import net.pterodactylus.sone.data.Client; +import net.pterodactylus.sone.data.Image; +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.junit.Test; + +/** + * Unit test for {@link SoneParser}. + * + * @author David Roden + */ +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() { + Optional image = mock(Optional.class); + when(core.getImage(anyString())).thenReturn(image); + } + + @Test + public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException, SoneException { + assertThat(soneParser.parseSone(database, originalSone, getInputStream("This is not valid XML.")), nullValue()); + } + + @Test + public void verifyThatANegativeProtocolVersionCausesAnError() throws SoneException { + assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue()); + } + + @Test + public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() throws SoneException { + Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get()); + assertThat(sone, notNullValue()); + assertThat(sone.getClient(), notNullValue()); + assertThat(sone.getClient(), is(originalSone.getClient())); + } + + @Test + public void verifyThatTheCreatedSoneMeetsAllExpectations() throws SoneException { + Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get()); + 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")); + } + + 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", + "" + )); + 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()))); + } + if (protocolVersion.isPresent()) { + content.append(createXmlElement("protocol-version", protocolVersion.get())); + } + if (clientInformation.isPresent()) { + content.append(createXmlElement("client", clientInformation.get())); + } + if (profile.isPresent()) { + content.append(createXmlElement("profile", profile.get())); + } + if (posts.isPresent()) { + content.append(createXmlElement("posts", posts.get())); + } + content.append(""); + try { + String xmlString = content.toString(); + System.out.println(xmlString); + return new ByteArrayInputStream(xmlString.getBytes("UTF-8")); + } catch (UnsupportedEncodingException e) { + /* ignore. */ + } + return null; + } + + private String createXmlElement(String xmlElement, String content) { + return format("<%s>%s", xmlElement, content); + } + + } + +}