f3f36b3374b1daab24b2e9093d4ee25f5703e7d7
[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.Objects.equal;
4 import static java.lang.String.format;
5 import static java.util.logging.Level.OFF;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.hamcrest.Matchers.containsInAnyOrder;
8 import static org.hamcrest.Matchers.empty;
9 import static org.hamcrest.Matchers.is;
10 import static org.hamcrest.Matchers.notNullValue;
11 import static org.hamcrest.Matchers.nullValue;
12 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.io.InputStream;
18 import java.util.logging.Logger;
19
20 import net.pterodactylus.sone.core.SoneParser.DuplicateField;
21 import net.pterodactylus.sone.core.SoneParser.InvalidProtocolVersion;
22 import net.pterodactylus.sone.core.SoneParser.InvalidXml;
23 import net.pterodactylus.sone.core.SoneParser.MalformedXml;
24 import net.pterodactylus.sone.data.Client;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.sone.data.Post;
27 import net.pterodactylus.sone.data.PostReply;
28 import net.pterodactylus.sone.data.Profile.Field;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.database.Database;
31 import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
32 import net.pterodactylus.sone.database.memory.MemoryDatabase;
33
34 import com.google.common.base.Optional;
35 import org.hamcrest.Description;
36 import org.hamcrest.Matcher;
37 import org.hamcrest.TypeSafeMatcher;
38 import org.junit.Test;
39
40 /**
41  * Unit test for {@link SoneParser}.
42  *
43  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
44  */
45 public class SoneParserTest {
46
47         static {
48                 Logger.getLogger("").setLevel(OFF);
49         }
50
51         private final Core core = mock(Core.class);
52         private final Database database = new MemoryDatabase(null);
53         private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
54         private final SoneParser soneParser = new SoneParser();
55
56         public SoneParserTest() {
57                 Optional<Image> image = mock(Optional.class);
58                 when(core.getImage(anyString())).thenReturn(image);
59         }
60
61         @Test(expected = InvalidXml.class)
62         public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
63                 soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
64         }
65
66         @Test(expected = InvalidProtocolVersion.class)
67         public void verifyThatANegativeProtocolVersionCausesAnError() {
68                 soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
69         }
70
71         @Test(expected = InvalidProtocolVersion.class)
72         public void verifyThatATooLargeProtocolVersionCausesAnError() {
73                 soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
74         }
75
76         @Test(expected = MalformedXml.class)
77         public void verifyThatAMissingTimeCausesAnError() {
78                 soneParser.parseSone(database, originalSone, getXml("missing-time"));
79         }
80
81         @Test
82         public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
83                 Sone sone = soneParser.parseSone(database, originalSone, getXml("missing-client"));
84                 assertThat(sone, notNullValue());
85                 assertThat(sone.getClient(), notNullValue());
86                 assertThat(sone.getClient(), is(originalSone.getClient()));
87         }
88
89         @Test
90         public void verifyThatAnInvalidClientCausesTheOriginalClientToBeUsed() {
91                 Sone sone = soneParser.parseSone(database, originalSone, getXml("invalid-client"));
92                 assertThat(sone, notNullValue());
93                 assertThat(sone.getClient(), notNullValue());
94                 assertThat(sone.getClient(), is(originalSone.getClient()));
95         }
96
97         @Test(expected = MalformedXml.class)
98         public void verifyThatAMissingProfileCausesAnError() {
99                 soneParser.parseSone(database, originalSone, getXml("missing-profile"));
100         }
101
102         @Test(expected = MalformedXml.class)
103         public void verifyThatInvalidFieldsCauseAnError() {
104                 soneParser.parseSone(database, originalSone, getXml("invalid-field"));
105         }
106
107         @Test(expected = DuplicateField.class)
108         public void verifyThatDuplicateFieldsCauseAnError() {
109                 soneParser.parseSone(database, originalSone, getXml("duplicate-field"));
110         }
111
112         @Test
113         public void verifyThatMissingPostsDoNotCauseAnError() {
114                 soneParser.parseSone(database, originalSone, getXml("missing-posts"));
115         }
116
117         @Test(expected = MalformedXml.class)
118         public void verifyThatInvalidPostsCauseAnError() {
119                 soneParser.parseSone(database, originalSone, getXml("invalid-posts"));
120         }
121
122         @Test
123         public void verifyThatAnEmptyProfileIsParsedWithoutError() {
124                 Sone sone = soneParser.parseSone(database, originalSone, getXml("empty-profile"));
125                 assertThat(sone.getProfile().getFirstName(), nullValue());
126                 assertThat(sone.getProfile().getMiddleName(), nullValue());
127                 assertThat(sone.getProfile().getLastName(), nullValue());
128                 assertThat(sone.getProfile().getBirthYear(), nullValue());
129                 assertThat(sone.getProfile().getBirthMonth(), nullValue());
130                 assertThat(sone.getProfile().getBirthDay(), nullValue());
131                 assertThat(sone.getProfile().getAvatar(), nullValue());
132                 assertThat(sone.getProfile().getFields(), empty());
133         }
134
135         @Test
136         public void verifyThatTheCreatedSoneMeetsAllExpectations() {
137                 Sone sone = soneParser.parseSone(database, originalSone, getXml("complete"));
138                 assertThat(sone, notNullValue());
139                 assertThat(sone.getTime(), is(1382419919000L));
140                 assertThat(sone.getClient(), notNullValue());
141                 assertThat(sone.getClient().getName(), is("Sone"));
142                 assertThat(sone.getClient().getVersion(), is("0.8.7"));
143                 assertThat(sone.getProfile(), notNullValue());
144                 assertThat(sone.getProfile().getFirstName(), is("First"));
145                 assertThat(sone.getProfile().getMiddleName(), is("M."));
146                 assertThat(sone.getProfile().getLastName(), is("Last"));
147                 assertThat(sone.getProfile().getBirthYear(), is(2013));
148                 assertThat(sone.getProfile().getBirthMonth(), is(10));
149                 assertThat(sone.getProfile().getBirthDay(), is(22));
150                 assertThat(sone.getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
151                 assertThat(sone.getProfile().getFields(), contains(
152                                 fieldMatcher("Field1", "Value1"),
153                                 fieldMatcher("Field2", "Value2")
154                 ));
155                 assertThat(sone.getPosts(), contains(
156                                 postMatcher("d8c9586e-3adb-11e3-bb31-171fc040e645", "0rpD4gL8mszav2trndhIdKIxvKUCNAe2kjA3dLV8CVU", 1382420181000L, "Hello, User!"),
157                                 postMatcher("bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", null, 1382420140000L, "Hello, World!")
158                 ));
159                 assertThat(sone.getReplies(), containsInAnyOrder(
160                                 postReplyMatcher("f09fa448-3adb-11e3-a783-ab54a11aacc4", "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", 1382420224000L, "Talking to myself."),
161                                 postReplyMatcher("0a376440-3adc-11e3-8f45-c7cc157436a5", "11ebe86e-3adc-11e3-b7b9-7f2c88018a33", 1382420271000L, "Talking to somebody I can't see.")
162                 ));
163                 assertThat(sone.getLikedPostIds(), containsInAnyOrder(
164                                 "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3",
165                                 "305d85e6-3adc-11e3-be45-8b53dd91f0af"
166                 ));
167                 assertThat(sone.getLikedReplyIds(), containsInAnyOrder(
168                                 "f09fa448-3adb-11e3-a783-ab54a11aacc4",
169                                 "3ba28960-3adc-11e3-93c7-6713d170f44c"
170                 ));
171         }
172
173         private Matcher<PostReply> postReplyMatcher(final String id, final String postId, final long time, final String text) {
174                 return new TypeSafeMatcher<PostReply>() {
175                         @Override
176                         protected boolean matchesSafely(PostReply postReply) {
177                                 return postReply.getId().equals(id) && postReply.getPostId().equals(postId) && (postReply.getTime() == time) && postReply.getText().equals(text);
178                         }
179
180                         @Override
181                         public void describeTo(Description description) {
182                                 description.appendText("PostReply(")
183                                                 .appendValue(id).appendText(", ")
184                                                 .appendValue(postId).appendText(", ")
185                                                 .appendValue(time).appendText(", ")
186                                                 .appendValue(text).appendText(")");
187                         }
188
189                         @Override
190                         protected void describeMismatchSafely(PostReply postReply, Description mismatchDescription) {
191                                 mismatchDescription.appendText("PostReply(")
192                                                 .appendValue(postReply.getId()).appendText(", ")
193                                                 .appendValue(postReply.getPostId()).appendText(", ")
194                                                 .appendValue(postReply.getTime()).appendText(", ")
195                                                 .appendValue(postReply.getText()).appendText(")");
196                         }
197                 };
198         }
199
200         private Matcher<Post> postMatcher(final String id, final String recipient, final long time, final String text) {
201                 return new TypeSafeMatcher<Post>() {
202                         @Override
203                         protected boolean matchesSafely(Post post) {
204                                 return post.getId().equals(id) && equal(post.getRecipientId().orNull(), recipient) && (post.getTime() == time) && post.getText().equals(text);
205                         }
206
207                         @Override
208                         public void describeTo(Description description) {
209                                 description.appendText("Post(")
210                                                 .appendValue(id).appendText(", ")
211                                                 .appendValue(recipient).appendText(", ")
212                                                 .appendValue(time).appendText(", ")
213                                                 .appendValue(text).appendText(")");
214                         }
215
216                         @Override
217                         protected void describeMismatchSafely(Post post, Description mismatchDescription) {
218                                 mismatchDescription.appendText("Post(")
219                                                 .appendValue(post.getId()).appendText(", ")
220                                                 .appendValue(post.getRecipientId().orNull()).appendText(", ")
221                                                 .appendValue(post.getTime()).appendText(", ")
222                                                 .appendValue(post.getText()).appendText(")");
223                         }
224                 };
225         }
226
227         private Matcher<Field> fieldMatcher(final String name, final String value) {
228                 return new TypeSafeMatcher<Field>() {
229                         @Override
230                         protected boolean matchesSafely(Field field) {
231                                 return field.getName().equals(name) && field.getValue().equals(value);
232                         }
233
234                         @Override
235                         public void describeTo(Description description) {
236                                 description.appendText("field named ").appendValue(name).appendText(" with value ").appendValue(value);
237                         }
238
239                         @Override
240                         protected void describeMismatchSafely(Field field, Description mismatchDescription) {
241                                 mismatchDescription.appendText("field named ").appendValue(field.getName()).appendText(" with value ").appendValue(field.getValue());
242                         }
243                 };
244         }
245
246         private InputStream getXml(String name) {
247                 return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));
248         }
249
250 }