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