Don’t echo the HTML to stdout.
[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 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;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import java.io.UnsupportedEncodingException;
18 import java.util.logging.Logger;
19
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;
26
27 import com.google.common.base.Joiner;
28 import com.google.common.base.Optional;
29 import org.junit.Test;
30
31 /**
32  * Unit test for {@link SoneParser}.
33  *
34  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
35  */
36 public class SoneParserTest {
37
38         static {
39                 Logger.getLogger("").setLevel(OFF);
40         }
41
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();
47
48         public SoneParserTest() {
49                 Optional<Image> image = mock(Optional.class);
50                 when(core.getImage(anyString())).thenReturn(image);
51         }
52
53         @Test
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));
58         }
59
60         @Test
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));
65         }
66
67         @Test
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));
72         }
73
74         @Test
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()));
81         }
82
83         @Test
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"));
100         }
101
102         public InputStream getInputStream(String content) throws UnsupportedEncodingException {
103                 return new ByteArrayInputStream(content.getBytes("UTF-8"));
104         }
105
106         private static class SoneXmlBuilder {
107
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>",
119                                 "<fields>",
120                                 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
121                                 "</fields>"
122                 ));
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>");
125
126                 public SoneXmlBuilder removeTime() {
127                         time = absent();
128                         return this;
129                 }
130
131                 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
132                         this.protocolVersion = fromNullable(protocolVersion);
133                         return this;
134                 }
135
136                 public SoneXmlBuilder removeProtocolVersion() {
137                         this.protocolVersion = absent();
138                         return this;
139                 }
140
141                 public SoneXmlBuilder setClientInformation(String name, String version) {
142                         clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
143                         return this;
144                 }
145
146                 public SoneXmlBuilder removeClientInformation() {
147                         clientInformation = absent();
148                         return this;
149                 }
150
151                 public SoneXmlBuilder removeProfile() {
152                         profile = absent();
153                         return this;
154                 }
155
156                 public SoneXmlBuilder removePost() {
157                         posts = absent();
158                         return this;
159                 }
160
161                 public SoneXmlBuilder removeReply() {
162                         replies = absent();
163                         return this;
164                 }
165
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())));
171                         }
172                         if (protocolVersion.isPresent()) {
173                                 content.append(createXmlElement("protocol-version", protocolVersion.get()));
174                         }
175                         if (clientInformation.isPresent()) {
176                                 content.append(createXmlElement("client", clientInformation.get()));
177                         }
178                         if (profile.isPresent()) {
179                                 content.append(createXmlElement("profile", profile.get()));
180                         }
181                         if (posts.isPresent()) {
182                                 content.append(createXmlElement("posts", posts.get()));
183                         }
184                         content.append("</sone>");
185                         try {
186                                 String xmlString = content.toString();
187                                 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
188                         } catch (UnsupportedEncodingException e) {
189                                 /* ignore. */
190                         }
191                         return null;
192                 }
193
194                 private String createXmlElement(String xmlElement, String content) {
195                         return format("<%s>%s</%1$s>", xmlElement, content);
196                 }
197
198         }
199
200 }