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 java.util.logging.Level.OFF;
8 import static org.hamcrest.CoreMatchers.is;
9 import static org.hamcrest.CoreMatchers.notNullValue;
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;
18 import java.util.logging.Logger;
20 import net.pterodactylus.sone.data.Client;
21 import net.pterodactylus.sone.data.Image;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.database.Database;
24 import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
25 import net.pterodactylus.sone.database.memory.MemoryDatabase;
27 import com.google.common.base.Joiner;
28 import com.google.common.base.Optional;
29 import org.junit.Test;
32 * Unit test for {@link SoneParser}.
34 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
36 public class SoneParserTest {
39 Logger.getLogger("").setLevel(OFF);
42 private final Core core = mock(Core.class);
43 private final Database database = new MemoryDatabase(null);
44 private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
45 private final SoneXmlBuilder soneXmlBuilder = new SoneXmlBuilder();
46 private final SoneParser soneParser = new SoneParser();
48 public SoneParserTest() {
49 Optional<Image> image = mock(Optional.class);
50 when(core.getImage(anyString())).thenReturn(image);
54 public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException {
55 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>"));
56 assertThat(sone, notNullValue());
57 assertThat(sone.isPresent(), is(false));
61 public void verifyThatANegativeProtocolVersionCausesAnError() {
62 Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get());
63 assertThat(sone, notNullValue());
64 assertThat(sone.isPresent(), is(false));
68 public void verifyThatATooLargeProtocolVersionCausesAnError() {
69 Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get());
70 assertThat(sone, notNullValue());
71 assertThat(sone.isPresent(), is(false));
75 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
76 Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
77 assertThat(sone, notNullValue());
78 assertThat(sone.isPresent(), is(true));
79 assertThat(sone.get().getClient(), notNullValue());
80 assertThat(sone.get().getClient(), is(originalSone.getClient()));
84 public void verifyThatTheCreatedSoneMeetsAllExpectations() {
85 Optional<Sone> sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get());
86 assertThat(sone, notNullValue());
87 assertThat(sone.isPresent(), is(true));
88 assertThat(sone.get().getTime(), is(1000L));
89 assertThat(sone.get().getClient(), notNullValue());
90 assertThat(sone.get().getClient().getName(), is("Test-Client"));
91 assertThat(sone.get().getClient().getVersion(), is("1.0"));
92 assertThat(sone.get().getProfile(), notNullValue());
93 assertThat(sone.get().getProfile().getFirstName(), is("First"));
94 assertThat(sone.get().getProfile().getMiddleName(), is("M."));
95 assertThat(sone.get().getProfile().getLastName(), is("Last"));
96 assertThat(sone.get().getProfile().getBirthYear(), is(2000));
97 assertThat(sone.get().getProfile().getBirthMonth(), is(9));
98 assertThat(sone.get().getProfile().getBirthDay(), is(13));
99 assertThat(sone.get().getProfile().getAvatar(), is("avatar-id"));
102 public InputStream getInputStream(String content) throws UnsupportedEncodingException {
103 return new ByteArrayInputStream(content.getBytes("UTF-8"));
106 private static class SoneXmlBuilder {
108 private Optional<Long> time = of(1000L);
109 private Optional<String> protocolVersion = of("0");
110 private Optional<String> clientInformation = of("<name>Test-Client</name><version>1.0</version>");
111 private Optional<String> profile = of(Joiner.on("").join(
112 "<first-name>First</first-name>",
113 "<middle-name>M.</middle-name>",
114 "<last-name>Last</last-name>",
115 "<birth-year>2000</birth-year>",
116 "<birth-month>9</birth-month>",
117 "<birth-day>13</birth-day>",
118 "<avatar>avatar-id</avatar>",
120 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
123 private Optional<String> posts = of("<post><id>post-id</id><time>1</time><recipient>recipient</recipient><text>Hello!</text></post>");
124 private Optional<String> replies = of("<reply><id>reply-id</id><post-id>post-id</post-id><time>2</time><text>Reply!</text></reply>");
126 public SoneXmlBuilder removeTime() {
131 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
132 this.protocolVersion = fromNullable(protocolVersion);
136 public SoneXmlBuilder removeProtocolVersion() {
137 this.protocolVersion = absent();
141 public SoneXmlBuilder setClientInformation(String name, String version) {
142 clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
146 public SoneXmlBuilder removeClientInformation() {
147 clientInformation = absent();
151 public SoneXmlBuilder removeProfile() {
156 public SoneXmlBuilder removePost() {
161 public SoneXmlBuilder removeReply() {
166 public InputStream get() {
167 StringBuilder content = new StringBuilder();
168 content.append("<sone>");
169 if (time.isPresent()) {
170 content.append(createXmlElement("time", String.valueOf(time.get())));
172 if (protocolVersion.isPresent()) {
173 content.append(createXmlElement("protocol-version", protocolVersion.get()));
175 if (clientInformation.isPresent()) {
176 content.append(createXmlElement("client", clientInformation.get()));
178 if (profile.isPresent()) {
179 content.append(createXmlElement("profile", profile.get()));
181 if (posts.isPresent()) {
182 content.append(createXmlElement("posts", posts.get()));
184 content.append("</sone>");
186 String xmlString = content.toString();
187 System.out.println(xmlString);
188 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
189 } catch (UnsupportedEncodingException e) {
195 private String createXmlElement(String xmlElement, String content) {
196 return format("<%s>%s</%1$s>", xmlElement, content);