Add unit test for the Sone parser.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneParserTest.java
1 package net.pterodactylus.sone.core;
2
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;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import java.io.UnsupportedEncodingException;
18
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;
25
26 import com.google.common.base.Joiner;
27 import com.google.common.base.Optional;
28 import org.junit.Test;
29
30 /**
31  * Unit test for {@link SoneParser}.
32  *
33  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
34  */
35 public class SoneParserTest {
36
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();
42
43         public SoneParserTest() {
44                 Optional<Image> image = mock(Optional.class);
45                 when(core.getImage(anyString())).thenReturn(image);
46         }
47
48         @Test
49         public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException, SoneException {
50                 assertThat(soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>")), nullValue());
51         }
52
53         @Test
54         public void verifyThatANegativeProtocolVersionCausesAnError() throws SoneException {
55                 assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue());
56         }
57
58         @Test
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()));
64         }
65
66         @Test
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"));
82         }
83
84         public InputStream getInputStream(String content) throws UnsupportedEncodingException {
85                 return new ByteArrayInputStream(content.getBytes("UTF-8"));
86         }
87
88         private static class SoneXmlBuilder {
89
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>",
101                                 "<fields>",
102                                 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
103                                 "</fields>"
104                 ));
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>");
107
108                 public SoneXmlBuilder removeTime() {
109                         time = absent();
110                         return this;
111                 }
112
113                 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
114                         this.protocolVersion = fromNullable(protocolVersion);
115                         return this;
116                 }
117
118                 public SoneXmlBuilder removeProtocolVersion() {
119                         this.protocolVersion = absent();
120                         return this;
121                 }
122
123                 public SoneXmlBuilder setClientInformation(String name, String version) {
124                         clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
125                         return this;
126                 }
127
128                 public SoneXmlBuilder removeClientInformation() {
129                         clientInformation = absent();
130                         return this;
131                 }
132
133                 public SoneXmlBuilder removeProfile() {
134                         profile = absent();
135                         return this;
136                 }
137
138                 public SoneXmlBuilder removePost() {
139                         posts = absent();
140                         return this;
141                 }
142
143                 public SoneXmlBuilder removeReply() {
144                         replies = absent();
145                         return this;
146                 }
147
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())));
153                         }
154                         if (protocolVersion.isPresent()) {
155                                 content.append(createXmlElement("protocol-version", protocolVersion.get()));
156                         }
157                         if (clientInformation.isPresent()) {
158                                 content.append(createXmlElement("client", clientInformation.get()));
159                         }
160                         if (profile.isPresent()) {
161                                 content.append(createXmlElement("profile", profile.get()));
162                         }
163                         if (posts.isPresent()) {
164                                 content.append(createXmlElement("posts", posts.get()));
165                         }
166                         content.append("</sone>");
167                         try {
168                                 String xmlString = content.toString();
169                                 System.out.println(xmlString);
170                                 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
171                         } catch (UnsupportedEncodingException e) {
172                                 /* ignore. */
173                         }
174                         return null;
175                 }
176
177                 private String createXmlElement(String xmlElement, String content) {
178                         return format("<%s>%s</%1$s>", xmlElement, content);
179                 }
180
181         }
182
183 }