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