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