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.io.UnsupportedEncodingException;
17 import java.util.logging.Logger;
19 import net.pterodactylus.sone.data.Client;
20 import net.pterodactylus.sone.data.Image;
21 import net.pterodactylus.sone.data.Post;
22 import net.pterodactylus.sone.data.PostReply;
23 import net.pterodactylus.sone.data.Profile.Field;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.database.Database;
26 import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
27 import net.pterodactylus.sone.database.memory.MemoryDatabase;
29 import com.google.common.base.Optional;
30 import org.hamcrest.Description;
31 import org.hamcrest.Matcher;
32 import org.hamcrest.TypeSafeMatcher;
33 import org.junit.Test;
36 * Unit test for {@link SoneParser}.
38 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
40 public class SoneParserTest {
43 Logger.getLogger("").setLevel(OFF);
46 private final Core core = mock(Core.class);
47 private final Database database = new MemoryDatabase(null);
48 private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.<SoneCreated>absent());
49 private final SoneParser soneParser = new SoneParser();
51 public SoneParserTest() {
52 Optional<Image> image = mock(Optional.class);
53 when(core.getImage(anyString())).thenReturn(image);
57 public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException {
58 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
59 assertThat(sone, notNullValue());
60 assertThat(sone.isPresent(), is(false));
64 public void verifyThatANegativeProtocolVersionCausesAnError() {
65 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
66 assertThat(sone, notNullValue());
67 assertThat(sone.isPresent(), is(false));
71 public void verifyThatATooLargeProtocolVersionCausesAnError() {
72 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
73 assertThat(sone, notNullValue());
74 assertThat(sone.isPresent(), is(false));
78 public void verifyThatAMissingTimeCausesAnError() {
79 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("missing-time"));
80 assertThat(sone, notNullValue());
81 assertThat(sone.isPresent(), is(false));
85 public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() {
86 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("missing-client"));
87 assertThat(sone, notNullValue());
88 assertThat(sone.isPresent(), is(true));
89 assertThat(sone.get().getClient(), notNullValue());
90 assertThat(sone.get().getClient(), is(originalSone.getClient()));
94 public void verifyThatTheCreatedSoneMeetsAllExpectations() {
95 Optional<Sone> sone = soneParser.parseSone(database, originalSone, getXml("complete"));
96 assertThat(sone, notNullValue());
97 assertThat(sone.isPresent(), is(true));
98 assertThat(sone.get().getTime(), is(1382419919000L));
99 assertThat(sone.get().getClient(), notNullValue());
100 assertThat(sone.get().getClient().getName(), is("Sone"));
101 assertThat(sone.get().getClient().getVersion(), is("0.8.7"));
102 assertThat(sone.get().getProfile(), notNullValue());
103 assertThat(sone.get().getProfile().getFirstName(), is("First"));
104 assertThat(sone.get().getProfile().getMiddleName(), is("M."));
105 assertThat(sone.get().getProfile().getLastName(), is("Last"));
106 assertThat(sone.get().getProfile().getBirthYear(), is(2013));
107 assertThat(sone.get().getProfile().getBirthMonth(), is(10));
108 assertThat(sone.get().getProfile().getBirthDay(), is(22));
109 assertThat(sone.get().getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d"));
110 assertThat(sone.get().getProfile().getFields(), contains(
111 fieldMatcher("Field1", "Value1"),
112 fieldMatcher("Field2", "Value2")
114 assertThat(sone.get().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.get().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.get().getLikedPostIds(), containsInAnyOrder(
123 "bbb7ebf0-3adb-11e3-8a0b-630cd8f21cf3",
124 "305d85e6-3adc-11e3-be45-8b53dd91f0af"
126 assertThat(sone.get().getLikedReplyIds(), containsInAnyOrder(
127 "f09fa448-3adb-11e3-a783-ab54a11aacc4",
128 "3ba28960-3adc-11e3-93c7-6713d170f44c"
133 private Matcher<PostReply> postReplyMatcher(final String id, final String postId, final long time, final String text) {
134 return new TypeSafeMatcher<PostReply>() {
136 protected boolean matchesSafely(PostReply postReply) {
137 return postReply.getId().equals(id) && postReply.getPostId().equals(postId) && (postReply.getTime() == time) && postReply.getText().equals(text);
141 public void describeTo(Description description) {
142 description.appendText("PostReply(")
143 .appendValue(id).appendText(", ")
144 .appendValue(postId).appendText(", ")
145 .appendValue(time).appendText(", ")
146 .appendValue(text).appendText(")");
150 protected void describeMismatchSafely(PostReply postReply, Description mismatchDescription) {
151 mismatchDescription.appendText("PostReply(")
152 .appendValue(postReply.getId()).appendText(", ")
153 .appendValue(postReply.getPostId()).appendText(", ")
154 .appendValue(postReply.getTime()).appendText(", ")
155 .appendValue(postReply.getText()).appendText(")");
160 private Matcher<Post> postMatcher(final String id, final String recipient, final long time, final String text) {
161 return new TypeSafeMatcher<Post>() {
163 protected boolean matchesSafely(Post post) {
164 return post.getId().equals(id) && equal(post.getRecipientId().orNull(), recipient) && (post.getTime() == time) && post.getText().equals(text);
168 public void describeTo(Description description) {
169 description.appendText("Post(")
170 .appendValue(id).appendText(", ")
171 .appendValue(recipient).appendText(", ")
172 .appendValue(time).appendText(", ")
173 .appendValue(text).appendText(")");
177 protected void describeMismatchSafely(Post post, Description mismatchDescription) {
178 mismatchDescription.appendText("Post(")
179 .appendValue(post.getId()).appendText(", ")
180 .appendValue(post.getRecipientId().orNull()).appendText(", ")
181 .appendValue(post.getTime()).appendText(", ")
182 .appendValue(post.getText()).appendText(")");
187 private Matcher<Field> fieldMatcher(final String name, final String value) {
188 return new TypeSafeMatcher<Field>() {
190 protected boolean matchesSafely(Field field) {
191 return field.getName().equals(name) && field.getValue().equals(value);
195 public void describeTo(Description description) {
196 description.appendText("field named ").appendValue(name).appendText(" with value ").appendValue(value);
200 protected void describeMismatchSafely(Field field, Description mismatchDescription) {
201 mismatchDescription.appendText("field named ").appendValue(field.getName()).appendText(" with value ").appendValue(field.getValue());
206 private InputStream getXml(String name) {
207 return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name));