1 package net.pterodactylus.sone.core;
3 import static com.google.common.base.Optional.absent;
4 import static com.google.common.base.Optional.fromNullable;
5 import static com.google.common.base.Optional.of;
6 import static java.lang.String.format;
7 import static org.hamcrest.CoreMatchers.is;
8 import static org.hamcrest.CoreMatchers.notNullValue;
9 import static org.hamcrest.CoreMatchers.nullValue;
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import java.io.UnsupportedEncodingException;
19 import net.pterodactylus.sone.data.Client;
20 import net.pterodactylus.sone.data.Image;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.sone.database.Database;
23 import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
24 import net.pterodactylus.sone.database.memory.MemoryDatabase;
26 import com.google.common.base.Joiner;
27 import com.google.common.base.Optional;
28 import org.junit.Test;
31 * Unit test for {@link SoneParser}.
33 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
35 public class SoneParserTest {
37 private final Core core = mock(Core.class);
38 private final Database database = new MemoryDatabase(null);
39 private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
40 private final SoneXmlBuilder soneXmlBuilder = new SoneXmlBuilder();
41 private final SoneParser soneParser = new SoneParser();
43 public SoneParserTest() {
44 Optional<Image> image = mock(Optional.class);
45 when(core.getImage(anyString())).thenReturn(image);
49 public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException, SoneException {
50 assertThat(soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>")), nullValue());
54 public void verifyThatANegativeProtocolVersionCausesAnError() throws SoneException {
55 assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue());
59 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() throws SoneException {
60 Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
61 assertThat(sone, notNullValue());
62 assertThat(sone.getClient(), notNullValue());
63 assertThat(sone.getClient(), is(originalSone.getClient()));
67 public void verifyThatTheCreatedSoneMeetsAllExpectations() throws SoneException {
68 Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get());
69 assertThat(sone, notNullValue());
70 assertThat(sone.getTime(), is(1000L));
71 assertThat(sone.getClient(), notNullValue());
72 assertThat(sone.getClient().getName(), is("Test-Client"));
73 assertThat(sone.getClient().getVersion(), is("1.0"));
74 assertThat(sone.getProfile(), notNullValue());
75 assertThat(sone.getProfile().getFirstName(), is("First"));
76 assertThat(sone.getProfile().getMiddleName(), is("M."));
77 assertThat(sone.getProfile().getLastName(), is("Last"));
78 assertThat(sone.getProfile().getBirthYear(), is(2000));
79 assertThat(sone.getProfile().getBirthMonth(), is(9));
80 assertThat(sone.getProfile().getBirthDay(), is(13));
81 assertThat(sone.getProfile().getAvatar(), is("avatar-id"));
84 public InputStream getInputStream(String content) throws UnsupportedEncodingException {
85 return new ByteArrayInputStream(content.getBytes("UTF-8"));
88 private static class SoneXmlBuilder {
90 private Optional<Long> time = of(1000L);
91 private Optional<String> protocolVersion = of("0");
92 private Optional<String> clientInformation = of("<name>Test-Client</name><version>1.0</version>");
93 private Optional<String> profile = of(Joiner.on("").join(
94 "<first-name>First</first-name>",
95 "<middle-name>M.</middle-name>",
96 "<last-name>Last</last-name>",
97 "<birth-year>2000</birth-year>",
98 "<birth-month>9</birth-month>",
99 "<birth-day>13</birth-day>",
100 "<avatar>avatar-id</avatar>",
102 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
105 private Optional<String> posts = of("<post><id>post-id</id><time>1</time><recipient>recipient</recipient><text>Hello!</text></post>");
106 private Optional<String> replies = of("<reply><id>reply-id</id><post-id>post-id</post-id><time>2</time><text>Reply!</text></reply>");
108 public SoneXmlBuilder removeTime() {
113 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
114 this.protocolVersion = fromNullable(protocolVersion);
118 public SoneXmlBuilder removeProtocolVersion() {
119 this.protocolVersion = absent();
123 public SoneXmlBuilder setClientInformation(String name, String version) {
124 clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
128 public SoneXmlBuilder removeClientInformation() {
129 clientInformation = absent();
133 public SoneXmlBuilder removeProfile() {
138 public SoneXmlBuilder removePost() {
143 public SoneXmlBuilder removeReply() {
148 public InputStream get() {
149 StringBuilder content = new StringBuilder();
150 content.append("<sone>");
151 if (time.isPresent()) {
152 content.append(createXmlElement("time", String.valueOf(time.get())));
154 if (protocolVersion.isPresent()) {
155 content.append(createXmlElement("protocol-version", protocolVersion.get()));
157 if (clientInformation.isPresent()) {
158 content.append(createXmlElement("client", clientInformation.get()));
160 if (profile.isPresent()) {
161 content.append(createXmlElement("profile", profile.get()));
163 if (posts.isPresent()) {
164 content.append(createXmlElement("posts", posts.get()));
166 content.append("</sone>");
168 String xmlString = content.toString();
169 System.out.println(xmlString);
170 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
171 } catch (UnsupportedEncodingException e) {
177 private String createXmlElement(String xmlElement, String content) {
178 return format("<%s>%s</%1$s>", xmlElement, content);