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.CoreMatchers.is;
7 import static org.hamcrest.CoreMatchers.notNullValue;
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.Matchers.containsInAnyOrder;
10 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
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.InputStream;
16 import java.util.logging.Logger;
18 import net.pterodactylus.sone.core.SoneParser.InvalidProtocolVersion;
19 import net.pterodactylus.sone.core.SoneParser.InvalidXml;
20 import net.pterodactylus.sone.core.SoneParser.MalformedXml;
21 import net.pterodactylus.sone.data.Client;
22 import net.pterodactylus.sone.data.Image;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.data.PostReply;
25 import net.pterodactylus.sone.data.Profile.Field;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.database.Database;
28 import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
29 import net.pterodactylus.sone.database.memory.MemoryDatabase;
31 import com.google.common.base.Optional;
32 import org.hamcrest.Description;
33 import org.hamcrest.Matcher;
34 import org.hamcrest.TypeSafeMatcher;
35 import org.junit.Test;
38 * Unit test for {@link SoneParser}.
40 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
42 public class SoneParserTest {
45 Logger.getLogger("").setLevel(OFF);
48 private final Core core = mock(Core.class);
49 private final Database database = new MemoryDatabase(null);
50 private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
51 private final SoneParser soneParser = new SoneParser();
53 public SoneParserTest() {
54 Optional<Image> image = mock(Optional.class);
55 when(core.getImage(anyString())).thenReturn(image);
58 @Test(expected = InvalidXml.class)
59 public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
60 soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
63 @Test(expected = InvalidProtocolVersion.class)
64 public void verifyThatANegativeProtocolVersionCausesAnError() {
65 soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
68 @Test(expected = InvalidProtocolVersion.class)
69 public void verifyThatATooLargeProtocolVersionCausesAnError() {
70 soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
73 @Test(expected = MalformedXml.class)
74 public void verifyThatAMissingTimeCausesAnError() {
75 soneParser.parseSone(database, originalSone, getXml("missing-time"));
79 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
80 Sone sone = soneParser.parseSone(database, originalSone, getXml("missing-client"));
81 assertThat(sone, notNullValue());
82 assertThat(sone.getClient(), notNullValue());
83 assertThat(sone.getClient(), is(originalSone.getClient()));
87 public void verifyThatAnInvalidClientCausesTheOriginalClientToBeUsed() {
88 Sone sone = soneParser.parseSone(database, originalSone, getXml("invalid-client"));
89 assertThat(sone, notNullValue());
90 assertThat(sone.getClient(), notNullValue());
91 assertThat(sone.getClient(), is(originalSone.getClient()));
95 public void verifyThatTheCreatedSoneMeetsAllExpectations() {
96 Sone sone = soneParser.parseSone(database, originalSone, getXml("complete"));
97 assertThat(sone, notNullValue());
98 assertThat(sone.getTime(), is(1382419919000L));
99 assertThat(sone.getClient(), notNullValue());
100 assertThat(sone.getClient().getName(), is("Sone"));
101 assertThat(sone.getClient().getVersion(), is("0.8.7"));
102 assertThat(sone.getProfile(), notNullValue());
103 assertThat(sone.getProfile().getFirstName(), is("First"));
104 assertThat(sone.getProfile().getMiddleName(), is("M."));
105 assertThat(sone.getProfile().getLastName(), is("Last"));
106 assertThat(sone.getProfile().getBirthYear(), is(2013));
107 assertThat(sone.getProfile().getBirthMonth(), is(10));
108 assertThat(sone.getProfile().getBirthDay(), is(22));
109 assertThat(sone.getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
110 assertThat(sone.getProfile().getFields(), contains(
111 fieldMatcher("Field1", "Value1"),
112 fieldMatcher("Field2", "Value2")
114 assertThat(sone.getPosts(), contains(
115 postMatcher("d8c9586e-3adb-11e3-bb31-171fc040e645", "0rpD4gL8mszav2trndhIdKIxvKUCNAe2kjA3dLV8CVU", 1382420181000L, "Hello, User!"),
116 postMatcher("bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", null, 1382420140000L, "Hello, World!")
118 assertThat(sone.getReplies(), containsInAnyOrder(
119 postReplyMatcher("f09fa448-3adb-11e3-a783-ab54a11aacc4", "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3", 1382420224000L, "Talking to myself."),
120 postReplyMatcher("0a376440-3adc-11e3-8f45-c7cc157436a5", "11ebe86e-3adc-11e3-b7b9-7f2c88018a33", 1382420271000L, "Talking to somebody I can't see.")
122 assertThat(sone.getLikedPostIds(), containsInAnyOrder(
123 "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3",
124 "305d85e6-3adc-11e3-be45-8b53dd91f0af"
126 assertThat(sone.getLikedReplyIds(), containsInAnyOrder(
127 "f09fa448-3adb-11e3-a783-ab54a11aacc4",
128 "3ba28960-3adc-11e3-93c7-6713d170f44c"
132 private Matcher<PostReply> postReplyMatcher(final String id, final String postId, final long time, final String text) {
133 return new TypeSafeMatcher<PostReply>() {
135 protected boolean matchesSafely(PostReply postReply) {
136 return postReply.getId().equals(id) && postReply.getPostId().equals(postId) && (postReply.getTime() == time) && postReply.getText().equals(text);
140 public void describeTo(Description description) {
141 description.appendText("PostReply(")
142 .appendValue(id).appendText(", ")
143 .appendValue(postId).appendText(", ")
144 .appendValue(time).appendText(", ")
145 .appendValue(text).appendText(")");
149 protected void describeMismatchSafely(PostReply postReply, Description mismatchDescription) {
150 mismatchDescription.appendText("PostReply(")
151 .appendValue(postReply.getId()).appendText(", ")
152 .appendValue(postReply.getPostId()).appendText(", ")
153 .appendValue(postReply.getTime()).appendText(", ")
154 .appendValue(postReply.getText()).appendText(")");
159 private Matcher<Post> postMatcher(final String id, final String recipient, final long time, final String text) {
160 return new TypeSafeMatcher<Post>() {
162 protected boolean matchesSafely(Post post) {
163 return post.getId().equals(id) && equal(post.getRecipientId().orNull(), recipient) && (post.getTime() == time) && post.getText().equals(text);
167 public void describeTo(Description description) {
168 description.appendText("Post(")
169 .appendValue(id).appendText(", ")
170 .appendValue(recipient).appendText(", ")
171 .appendValue(time).appendText(", ")
172 .appendValue(text).appendText(")");
176 protected void describeMismatchSafely(Post post, Description mismatchDescription) {
177 mismatchDescription.appendText("Post(")
178 .appendValue(post.getId()).appendText(", ")
179 .appendValue(post.getRecipientId().orNull()).appendText(", ")
180 .appendValue(post.getTime()).appendText(", ")
181 .appendValue(post.getText()).appendText(")");
186 private Matcher<Field> fieldMatcher(final String name, final String value) {
187 return new TypeSafeMatcher<Field>() {
189 protected boolean matchesSafely(Field field) {
190 return field.getName().equals(name) && field.getValue().equals(value);
194 public void describeTo(Description description) {
195 description.appendText("field named ").appendValue(name).appendText(" with value ").appendValue(value);
199 protected void describeMismatchSafely(Field field, Description mismatchDescription) {
200 mismatchDescription.appendText("field named ").appendValue(field.getName()).appendText(" with value ").appendValue(field.getValue());
205 private InputStream getXml(String name) {
206 return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));