1 package net.pterodactylus.sone.core;
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;
17 import java.io.InputStream;
18 import java.util.logging.Logger;
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;
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;
44 * Unit test for {@link SoneParser}.
46 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
48 public class SoneParserTest {
51 Logger.getLogger("").setLevel(OFF);
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();
59 public SoneParserTest() {
60 Optional<Image> image = mock(Optional.class);
61 when(core.getImage(anyString())).thenReturn(image);
64 @Test(expected = InvalidXml.class)
65 public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
66 soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
70 public void verifyThatAMissingProtocolVersionDoesNotCauseAnError() {
71 soneParser.parseSone(database, originalSone, getXml("missing-protocol-version"));
74 @Test(expected = InvalidProtocolVersion.class)
75 public void verifyThatANegativeProtocolVersionCausesAnError() {
76 soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
79 @Test(expected = InvalidProtocolVersion.class)
80 public void verifyThatATooLargeProtocolVersionCausesAnError() {
81 soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
84 @Test(expected = MalformedXml.class)
85 public void verifyThatAMissingTimeCausesAnError() {
86 soneParser.parseSone(database, originalSone, getXml("missing-time"));
90 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
91 Sone sone = soneParser.parseSone(database, originalSone, getXml("missing-client"));
92 assertThat(sone, notNullValue());
93 assertThat(sone.getClient(), notNullValue());
94 assertThat(sone.getClient(), is(originalSone.getClient()));
98 public void verifyThatAnInvalidClientCausesTheOriginalClientToBeUsed() {
99 Sone sone = soneParser.parseSone(database, originalSone, getXml("invalid-client"));
100 assertThat(sone, notNullValue());
101 assertThat(sone.getClient(), notNullValue());
102 assertThat(sone.getClient(), is(originalSone.getClient()));
105 @Test(expected = MalformedXml.class)
106 public void verifyThatAMissingProfileCausesAnError() {
107 soneParser.parseSone(database, originalSone, getXml("missing-profile"));
110 @Test(expected = MalformedXml.class)
111 public void verifyThatInvalidFieldsCauseAnError() {
112 soneParser.parseSone(database, originalSone, getXml("invalid-field"));
115 @Test(expected = DuplicateField.class)
116 public void verifyThatDuplicateFieldsCauseAnError() {
117 soneParser.parseSone(database, originalSone, getXml("duplicate-field"));
121 public void verifyThatMissingPostsDoNotCauseAnError() {
122 soneParser.parseSone(database, originalSone, getXml("missing-posts"));
125 @Test(expected = MalformedXml.class)
126 public void verifyThatInvalidPostsCauseAnError() {
127 soneParser.parseSone(database, originalSone, getXml("invalid-posts"));
130 @Test(expected = MalformedTime.class)
131 public void verifyThatAMalformedTimeCausesAnError() {
132 soneParser.parseSone(database, originalSone, getXml("invalid-post-time"));
136 public void verifyThatMissingRepliesDoNotCauseAnError() {
137 soneParser.parseSone(database, originalSone, getXml("missing-replies"));
140 @Test(expected = MalformedXml.class)
141 public void verifyThatInvalidRepliesCauseAnError() {
142 soneParser.parseSone(database, originalSone, getXml("invalid-replies"));
145 @Test(expected = MalformedTime.class)
146 public void verifyThatAMalformedReplyTimeCausesAnError() {
147 soneParser.parseSone(database, originalSone, getXml("invalid-reply-time"));
151 public void verifyThatAMissingPostLikesSectionDoesNotCauseAnError() {
152 soneParser.parseSone(database, originalSone, getXml("missing-post-likes"));
156 public void verifyThatAMissingReplyLikesSectionDoesNotCauseAnError() {
157 soneParser.parseSone(database, originalSone, getXml("missing-reply-likes"));
161 public void verifyThatMissingAlbumsSectionDoNotCauseAnError() {
162 soneParser.parseSone(database, originalSone, getXml("missing-albums"));
165 @Test(expected = MalformedXml.class)
166 public void verifyThatAnInvalidAlbumCausesAnError() {
167 soneParser.parseSone(database, originalSone, getXml("invalid-album"));
170 @Test(expected = InvalidParentAlbum.class)
171 public void verifyThatAnInvalidParentAlbumCausesAnError() {
172 soneParser.parseSone(database, originalSone, getXml("invalid-parent-album"));
175 @Test(expected = MalformedXml.class)
176 public void verifyThatAnInvalidImageCausesAnError() {
177 soneParser.parseSone(database, originalSone, getXml("invalid-image"));
180 @Test(expected = MalformedDimension.class)
181 public void verifyThatInvalidImageDimensionsCauseAnError() {
182 soneParser.parseSone(database, originalSone, getXml("invalid-image-dimensions"));
186 public void verifyThatAnEmptyProfileIsParsedWithoutError() {
187 Sone sone = soneParser.parseSone(database, originalSone, getXml("empty-profile"));
188 assertThat(sone.getProfile().getFirstName(), nullValue());
189 assertThat(sone.getProfile().getMiddleName(), nullValue());
190 assertThat(sone.getProfile().getLastName(), nullValue());
191 assertThat(sone.getProfile().getBirthYear(), nullValue());
192 assertThat(sone.getProfile().getBirthMonth(), nullValue());
193 assertThat(sone.getProfile().getBirthDay(), nullValue());
194 assertThat(sone.getProfile().getAvatar(), nullValue());
195 assertThat(sone.getProfile().getFields(), empty());
199 public void verifyThatTheCreatedSoneMeetsAllExpectations() {
200 Sone sone = soneParser.parseSone(database, originalSone, getXml("complete"));
201 assertThat(sone, notNullValue());
202 assertThat(sone.getTime(), is(1382419919000L));
203 assertThat(sone.getClient(), notNullValue());
204 assertThat(sone.getClient().getName(), is("Sone"));
205 assertThat(sone.getClient().getVersion(), is("0.8.7"));
206 assertThat(sone.getProfile(), notNullValue());
207 assertThat(sone.getProfile().getFirstName(), is("First"));
208 assertThat(sone.getProfile().getMiddleName(), is("M."));
209 assertThat(sone.getProfile().getLastName(), is("Last"));
210 assertThat(sone.getProfile().getBirthYear(), is(2013));
211 assertThat(sone.getProfile().getBirthMonth(), is(10));
212 assertThat(sone.getProfile().getBirthDay(), is(22));
213 assertThat(sone.getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
214 assertThat(sone.getProfile().getFields(), contains(
215 fieldMatcher("Field1", "Value1"),
216 fieldMatcher("Field2", "Value2")
218 assertThat(sone.getPosts(), contains(
219 postMatcher("d8c9586e-3adb-11e3-bb31-171fc040e645", "0rpD4gL8mszav2trndhIdKIxvKUCNAe2kjA3dLV8CVU", 1382420181000L, "Hello, User!"),
220 postMatcher("bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", null, 1382420140000L, "Hello, World!")
222 assertThat(sone.getReplies(), containsInAnyOrder(
223 postReplyMatcher("f09fa448-3adb-11e3-a783-ab54a11aacc4", "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", 1382420224000L, "Talking to myself."),
224 postReplyMatcher("0a376440-3adc-11e3-8f45-c7cc157436a5", "11ebe86e-3adc-11e3-b7b9-7f2c88018a33", 1382420271000L, "Talking to somebody I can't see.")
226 assertThat(sone.getLikedPostIds(), containsInAnyOrder(
227 "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3",
228 "305d85e6-3adc-11e3-be45-8b53dd91f0af"
230 assertThat(sone.getLikedReplyIds(), containsInAnyOrder(
231 "f09fa448-3adb-11e3-a783-ab54a11aacc4",
232 "3ba28960-3adc-11e3-93c7-6713d170f44c"
236 private Matcher<PostReply> postReplyMatcher(final String id, final String postId, final long time, final String text) {
237 return new TypeSafeMatcher<PostReply>() {
239 protected boolean matchesSafely(PostReply postReply) {
240 return postReply.getId().equals(id) && postReply.getPostId().equals(postId) && (postReply.getTime() == time) && postReply.getText().equals(text);
244 public void describeTo(Description description) {
245 description.appendText("PostReply(")
246 .appendValue(id).appendText(", ")
247 .appendValue(postId).appendText(", ")
248 .appendValue(time).appendText(", ")
249 .appendValue(text).appendText(")");
253 protected void describeMismatchSafely(PostReply postReply, Description mismatchDescription) {
254 mismatchDescription.appendText("PostReply(")
255 .appendValue(postReply.getId()).appendText(", ")
256 .appendValue(postReply.getPostId()).appendText(", ")
257 .appendValue(postReply.getTime()).appendText(", ")
258 .appendValue(postReply.getText()).appendText(")");
263 private Matcher<Post> postMatcher(final String id, final String recipient, final long time, final String text) {
264 return new TypeSafeMatcher<Post>() {
266 protected boolean matchesSafely(Post post) {
267 return post.getId().equals(id) && equal(post.getRecipientId().orNull(), recipient) && (post.getTime() == time) && post.getText().equals(text);
271 public void describeTo(Description description) {
272 description.appendText("Post(")
273 .appendValue(id).appendText(", ")
274 .appendValue(recipient).appendText(", ")
275 .appendValue(time).appendText(", ")
276 .appendValue(text).appendText(")");
280 protected void describeMismatchSafely(Post post, Description mismatchDescription) {
281 mismatchDescription.appendText("Post(")
282 .appendValue(post.getId()).appendText(", ")
283 .appendValue(post.getRecipientId().orNull()).appendText(", ")
284 .appendValue(post.getTime()).appendText(", ")
285 .appendValue(post.getText()).appendText(")");
290 private Matcher<Field> fieldMatcher(final String name, final String value) {
291 return new TypeSafeMatcher<Field>() {
293 protected boolean matchesSafely(Field field) {
294 return field.getName().equals(name) && field.getValue().equals(value);
298 public void describeTo(Description description) {
299 description.appendText("field named ").appendValue(name).appendText(" with value ").appendValue(value);
303 protected void describeMismatchSafely(Field field, Description mismatchDescription) {
304 mismatchDescription.appendText("field named ").appendValue(field.getName()).appendText(" with value ").appendValue(field.getValue());
309 private InputStream getXml(String name) {
310 return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));