baa3fdc2befe3a07cafb7cabd1339af36caa9f44
[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 {
50                 assertThat(soneParser.parseSone(database, originalSone, getInputStream("<xml>This is not valid XML.</invalid>")), nullValue());
51         }
52
53         @Test
54         public void verifyThatANegativeProtocolVersionCausesAnError() {
55                 assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue());
56         }
57
58         @Test
59         public void verifyThatATooLargeProtocolVersionCausesAnError() {
60                 assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get()), nullValue());
61         }
62
63         @Test
64         public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
65                 Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get());
66                 assertThat(sone, notNullValue());
67                 assertThat(sone.getClient(), notNullValue());
68                 assertThat(sone.getClient(), is(originalSone.getClient()));
69         }
70
71         @Test
72         public void verifyThatTheCreatedSoneMeetsAllExpectations() {
73                 Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get());
74                 assertThat(sone, notNullValue());
75                 assertThat(sone.getTime(), is(1000L));
76                 assertThat(sone.getClient(), notNullValue());
77                 assertThat(sone.getClient().getName(), is("Test-Client"));
78                 assertThat(sone.getClient().getVersion(), is("1.0"));
79                 assertThat(sone.getProfile(), notNullValue());
80                 assertThat(sone.getProfile().getFirstName(), is("First"));
81                 assertThat(sone.getProfile().getMiddleName(), is("M."));
82                 assertThat(sone.getProfile().getLastName(), is("Last"));
83                 assertThat(sone.getProfile().getBirthYear(), is(2000));
84                 assertThat(sone.getProfile().getBirthMonth(), is(9));
85                 assertThat(sone.getProfile().getBirthDay(), is(13));
86                 assertThat(sone.getProfile().getAvatar(), is("avatar-id"));
87         }
88
89         public InputStream getInputStream(String content) throws UnsupportedEncodingException {
90                 return new ByteArrayInputStream(content.getBytes("UTF-8"));
91         }
92
93         private static class SoneXmlBuilder {
94
95                 private Optional<Long> time = of(1000L);
96                 private Optional<String> protocolVersion = of("0");
97                 private Optional<String> clientInformation = of("<name>Test-Client</name><version>1.0</version>");
98                 private Optional<String> profile = of(Joiner.on("").join(
99                                 "<first-name>First</first-name>",
100                                 "<middle-name>M.</middle-name>",
101                                 "<last-name>Last</last-name>",
102                                 "<birth-year>2000</birth-year>",
103                                 "<birth-month>9</birth-month>",
104                                 "<birth-day>13</birth-day>",
105                                 "<avatar>avatar-id</avatar>",
106                                 "<fields>",
107                                 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
108                                 "</fields>"
109                 ));
110                 private Optional<String> posts = of("<post><id>post-id</id><time>1</time><recipient>recipient</recipient><text>Hello!</text></post>");
111                 private Optional<String> replies = of("<reply><id>reply-id</id><post-id>post-id</post-id><time>2</time><text>Reply!</text></reply>");
112
113                 public SoneXmlBuilder removeTime() {
114                         time = absent();
115                         return this;
116                 }
117
118                 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
119                         this.protocolVersion = fromNullable(protocolVersion);
120                         return this;
121                 }
122
123                 public SoneXmlBuilder removeProtocolVersion() {
124                         this.protocolVersion = absent();
125                         return this;
126                 }
127
128                 public SoneXmlBuilder setClientInformation(String name, String version) {
129                         clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
130                         return this;
131                 }
132
133                 public SoneXmlBuilder removeClientInformation() {
134                         clientInformation = absent();
135                         return this;
136                 }
137
138                 public SoneXmlBuilder removeProfile() {
139                         profile = absent();
140                         return this;
141                 }
142
143                 public SoneXmlBuilder removePost() {
144                         posts = absent();
145                         return this;
146                 }
147
148                 public SoneXmlBuilder removeReply() {
149                         replies = absent();
150                         return this;
151                 }
152
153                 public InputStream get() {
154                         StringBuilder content = new StringBuilder();
155                         content.append("<sone>");
156                         if (time.isPresent()) {
157                                 content.append(createXmlElement("time", String.valueOf(time.get())));
158                         }
159                         if (protocolVersion.isPresent()) {
160                                 content.append(createXmlElement("protocol-version", protocolVersion.get()));
161                         }
162                         if (clientInformation.isPresent()) {
163                                 content.append(createXmlElement("client", clientInformation.get()));
164                         }
165                         if (profile.isPresent()) {
166                                 content.append(createXmlElement("profile", profile.get()));
167                         }
168                         if (posts.isPresent()) {
169                                 content.append(createXmlElement("posts", posts.get()));
170                         }
171                         content.append("</sone>");
172                         try {
173                                 String xmlString = content.toString();
174                                 System.out.println(xmlString);
175                                 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
176                         } catch (UnsupportedEncodingException e) {
177                                 /* ignore. */
178                         }
179                         return null;
180                 }
181
182                 private String createXmlElement(String xmlElement, String content) {
183                         return format("<%s>%s</%1$s>", xmlElement, content);
184                 }
185
186         }
187
188 }