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 verifyThatATooLargeProtocolVersionCausesAnError() throws SoneException {
60 assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get()), nullValue());
64 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() throws SoneException {
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()));
72 public void verifyThatTheCreatedSoneMeetsAllExpectations() throws SoneException {
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"));
89 public InputStream getInputStream(String content) throws UnsupportedEncodingException {
90 return new ByteArrayInputStream(content.getBytes("UTF-8"));
93 private static class SoneXmlBuilder {
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>",
107 "<field><field-name>Custom Field</field-name><field-value>Custom Value</field-value></field>",
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>");
113 public SoneXmlBuilder removeTime() {
118 public SoneXmlBuilder setProtocolVersion(String protocolVersion) {
119 this.protocolVersion = fromNullable(protocolVersion);
123 public SoneXmlBuilder removeProtocolVersion() {
124 this.protocolVersion = absent();
128 public SoneXmlBuilder setClientInformation(String name, String version) {
129 clientInformation = of("<name>" + name + "</name><version>" + version + "</version>");
133 public SoneXmlBuilder removeClientInformation() {
134 clientInformation = absent();
138 public SoneXmlBuilder removeProfile() {
143 public SoneXmlBuilder removePost() {
148 public SoneXmlBuilder removeReply() {
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())));
159 if (protocolVersion.isPresent()) {
160 content.append(createXmlElement("protocol-version", protocolVersion.get()));
162 if (clientInformation.isPresent()) {
163 content.append(createXmlElement("client", clientInformation.get()));
165 if (profile.isPresent()) {
166 content.append(createXmlElement("profile", profile.get()));
168 if (posts.isPresent()) {
169 content.append(createXmlElement("posts", posts.get()));
171 content.append("</sone>");
173 String xmlString = content.toString();
174 System.out.println(xmlString);
175 return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
176 } catch (UnsupportedEncodingException e) {
182 private String createXmlElement(String xmlElement, String content) {
183 return format("<%s>%s</%1$s>", xmlElement, content);