20edd2c2822f670e7083d1446bd1f839a8aee526
[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.Optional.of;
4 import static freenet.keys.InsertableClientSSK.createRandom;
5 import static java.lang.System.currentTimeMillis;
6 import static java.util.UUID.randomUUID;
7 import static java.util.concurrent.TimeUnit.DAYS;
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.Matchers.hasSize;
10 import static org.hamcrest.Matchers.is;
11 import static org.hamcrest.Matchers.not;
12 import static org.hamcrest.Matchers.notNullValue;
13 import static org.hamcrest.Matchers.nullValue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.anyLong;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.Mockito.doAnswer;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import net.pterodactylus.sone.data.Album;
31 import net.pterodactylus.sone.data.Album.Modifier;
32 import net.pterodactylus.sone.data.Client;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.database.AlbumBuilder;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.database.ImageBuilder;
41 import net.pterodactylus.sone.database.PostBuilder;
42 import net.pterodactylus.sone.database.PostReplyBuilder;
43 import net.pterodactylus.sone.database.SoneBuilder;
44 import net.pterodactylus.sone.database.memory.MemorySoneBuilder;
45 import net.pterodactylus.sone.freenet.wot.Identity;
46 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
47
48 import freenet.crypt.DummyRandomSource;
49 import freenet.keys.FreenetURI;
50 import freenet.keys.InsertableClientSSK;
51
52 import com.google.common.base.Optional;
53 import com.google.common.collect.ArrayListMultimap;
54 import com.google.common.collect.ImmutableSet;
55 import com.google.common.collect.ListMultimap;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.mockito.invocation.InvocationOnMock;
59 import org.mockito.stubbing.Answer;
60
61 /**
62  * Unit test for {@link SoneParser}.
63  *
64  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
65  */
66 public class SoneParserTest {
67
68         private final Database database = mock(Database.class);
69         private final SoneParser soneParser = new SoneParser(database);
70         private final Sone sone = mock(Sone.class);
71         private FreenetURI requestUri = mock(FreenetURI.class);
72         private final PostBuilder postBuilder = mock(PostBuilder.class);
73         private final List<Post> createdPosts = new ArrayList<Post>();
74         private Post post = mock(Post.class);
75         private final PostReplyBuilder postReplyBuilder = mock(PostReplyBuilder.class);
76         private final Set<PostReply> createdPostReplies = new HashSet<PostReply>();
77         private PostReply postReply = mock(PostReply.class);
78         private final AlbumBuilder albumBuilder = mock(AlbumBuilder.class);
79         private final ListMultimap<Album, Album>
80                         nestedAlbums = ArrayListMultimap.create();
81         private final ListMultimap<Album, Image> albumImages = ArrayListMultimap.create();
82         private Album album = mock(Album.class);
83         private final Map<String, Album> albums = new HashMap<String, Album>();
84         private final ImageBuilder imageBuilder = mock(ImageBuilder.class);
85         private Image image = mock(Image.class);
86         private final Map<String, Image> images = new HashMap<String, Image>();
87
88         @Before
89         public void setupSone() {
90                 setupSone(this.sone, Identity.class);
91         }
92
93         private void setupSone(Sone sone, Class<? extends Identity> identityClass) {
94                 Identity identity = mock(identityClass);
95                 InsertableClientSSK clientSSK =
96                                 createRandom(new DummyRandomSource(), "WoT");
97                 when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString());
98                 when(identity.getId()).thenReturn("identity");
99                 when(sone.getId()).thenReturn("identity");
100                 when(sone.getIdentity()).thenReturn(identity);
101                 requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone");
102                 when(sone.getRequestUri()).thenAnswer(new Answer<FreenetURI>() {
103                         @Override
104                         public FreenetURI answer(InvocationOnMock invocation)
105                         throws Throwable {
106                                 return requestUri;
107                         }
108                 });
109                 when(sone.getTime())
110                                 .thenReturn(currentTimeMillis() - DAYS.toMillis(1));
111         }
112
113         @Before
114         public void setupSoneBuilder() {
115                 when(database.newSoneBuilder()).thenAnswer(new Answer<SoneBuilder>() {
116                         @Override
117                         public SoneBuilder answer(InvocationOnMock invocation) {
118                                 return new MemorySoneBuilder(null);
119                         }
120                 });
121         }
122
123         @Before
124         public void setupPost() {
125                 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
126         }
127
128         @Before
129         public void setupPostBuilder() {
130                 when(postBuilder.withId(anyString())).thenAnswer(new Answer<PostBuilder>() {
131                         @Override
132                         public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
133                                 when(post.getId()).thenReturn((String) invocation.getArguments()[0]);
134                                 return postBuilder;
135                         }
136                 });
137                 when(postBuilder.from(anyString())).thenAnswer(new Answer<PostBuilder>() {
138                         @Override
139                         public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
140                                 final Sone sone = mock(Sone.class);
141                                 when(sone.getId()).thenReturn((String) invocation.getArguments()[0]);
142                                 when(post.getSone()).thenReturn(sone);
143                                 return postBuilder;
144                         }
145                 });
146                 when(postBuilder.withTime(anyLong())).thenAnswer(new Answer<PostBuilder>() {
147                         @Override
148                         public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
149                                 when(post.getTime()).thenReturn((Long) invocation.getArguments()[0]);
150                                 return postBuilder;
151                         }
152                 });
153                 when(postBuilder.withText(anyString())).thenAnswer(new Answer<PostBuilder>() {
154                         @Override
155                         public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
156                                 when(post.getText()).thenReturn((String) invocation.getArguments()[0]);
157                                 return postBuilder;
158                         }
159                 });
160                 when(postBuilder.to(anyString())).thenAnswer(new Answer<PostBuilder>() {
161                         @Override
162                         public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
163                                 when(post.getRecipientId()).thenReturn(of((String) invocation.getArguments()[0]));
164                                 return postBuilder;
165                         }
166                 });
167                 when(postBuilder.build()).thenAnswer(new Answer<Post>() {
168                         @Override
169                         public Post answer(InvocationOnMock invocation) throws Throwable {
170                                 Post post = SoneParserTest.this.post;
171                                 SoneParserTest.this.post = mock(Post.class);
172                                 setupPost();
173                                 createdPosts.add(post);
174                                 return post;
175                         }
176                 });
177                 when(database.newPostBuilder()).thenReturn(postBuilder);
178         }
179
180         @Before
181         public void setupPostReplyBuilder() {
182                 when(postReplyBuilder.withId(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
183                         @Override
184                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
185                                 when(postReply.getId()).thenReturn((String) invocation.getArguments()[0]);
186                                 return postReplyBuilder;
187                         }
188                 });
189                 when(postReplyBuilder.from(anyString())).thenAnswer(
190                                 new Answer<PostReplyBuilder>() {
191                                         @Override
192                                         public PostReplyBuilder answer(
193                                                         InvocationOnMock invocation) throws Throwable {
194                                                 Sone sone = when(mock(Sone.class).getId()).thenReturn(
195                                                                 (String) invocation.getArguments()[0])
196                                                                 .getMock();
197                                                 when(postReply.getSone()).thenReturn(sone);
198                                                 return postReplyBuilder;
199                                         }
200                                 });
201                 when(postReplyBuilder.to(anyString())).thenAnswer(
202                                 new Answer<PostReplyBuilder>() {
203                                         @Override
204                                         public PostReplyBuilder answer(
205                                                         InvocationOnMock invocation) throws Throwable {
206                                                 when(postReply.getPostId()).thenReturn(
207                                                                 (String) invocation.getArguments()[0]);
208                                                 Post post = when(mock(Post.class).getId()).thenReturn(
209                                                                 (String) invocation.getArguments()[0])
210                                                                 .getMock();
211                                                 when(postReply.getPost()).thenReturn(of(post));
212                                                 return postReplyBuilder;
213                                         }
214                                 });
215                 when(postReplyBuilder.withTime(anyLong())).thenAnswer(
216                                 new Answer<PostReplyBuilder>() {
217                                         @Override
218                                         public PostReplyBuilder answer(
219                                                         InvocationOnMock invocation) throws Throwable {
220                                                 when(postReply.getTime()).thenReturn(
221                                                                 (Long) invocation.getArguments()[0]);
222                                                 return postReplyBuilder;
223                                         }
224                                 });
225                 when(postReplyBuilder.withText(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
226                         @Override
227                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
228                                 when(postReply.getText()).thenReturn((String) invocation.getArguments()[0]);
229                                 return postReplyBuilder;
230                         }
231                 });
232                 when(postReplyBuilder.build()).thenAnswer(new Answer<PostReply>() {
233                         @Override
234                         public PostReply answer(InvocationOnMock invocation) throws Throwable {
235                                 PostReply postReply = SoneParserTest.this.postReply;
236                                 createdPostReplies.add(postReply);
237                                 SoneParserTest.this.postReply = mock(PostReply.class);
238                                 return postReply;
239                         }
240                 });
241                 when(database.newPostReplyBuilder()).thenReturn(postReplyBuilder);
242         }
243
244         @Before
245         public void setupAlbum() {
246                 final Album album = SoneParserTest.this.album;
247                 doAnswer(new Answer<Void>() {
248                         @Override
249                         public Void answer(InvocationOnMock invocation) {
250                                 nestedAlbums.put(album, (Album) invocation.getArguments()[0]);
251                                 return null;
252                         }
253                 }).when(album).addAlbum(any(Album.class));
254                 doAnswer(new Answer<Void>() {
255                         @Override
256                         public Void answer(InvocationOnMock invocation) {
257                                 albumImages.put(album, (Image) invocation.getArguments()[0]);
258                                 return null;
259                         }
260                 }).when(album).addImage(any(Image.class));
261                 when(album.getAlbums()).thenAnswer(new Answer<List<Album>>() {
262                         @Override
263                         public List<Album> answer(InvocationOnMock invocation) {
264                                 return nestedAlbums.get(album);
265                         }
266                 });
267                 when(album.getImages()).thenAnswer(new Answer<List<Image>>() {
268                         @Override
269                         public List<Image> answer(InvocationOnMock invocation) {
270                                 return albumImages.get(album);
271                         }
272                 });
273                 final Modifier albumModifier = new Modifier() {
274                         private String title = album.getTitle();
275                         private String description = album.getDescription();
276
277                         @Override
278                         public Modifier setTitle(String title) {
279                                 this.title = title;
280                                 return this;
281                         }
282
283                         @Override
284                         public Modifier setDescription(String description) {
285                                 this.description = description;
286                                 return this;
287                         }
288
289                         @Override
290                         public Album update() throws IllegalStateException {
291                                 when(album.getTitle()).thenReturn(title);
292                                 when(album.getDescription()).thenReturn(description);
293                                 return album;
294                         }
295                 };
296                 when(album.modify()).thenReturn(albumModifier);
297         }
298
299         @Before
300         public void setupAlbumBuilder() {
301                 when(albumBuilder.withId(anyString())).thenAnswer(new Answer<AlbumBuilder>() {
302                         @Override
303                         public AlbumBuilder answer(InvocationOnMock invocation) {
304                                 when(album.getId()).thenReturn((String) invocation.getArguments()[0]);
305                                 return albumBuilder;
306                         }
307                 });
308                 when(albumBuilder.randomId()).thenAnswer(new Answer<AlbumBuilder>() {
309                         @Override
310                         public AlbumBuilder answer(InvocationOnMock invocation) {
311                                 when(album.getId()).thenReturn(randomUUID().toString());
312                                 return albumBuilder;
313                         }
314                 });
315                 when(albumBuilder.by(any(Sone.class))).thenAnswer(new Answer<AlbumBuilder>() {
316                         @Override
317                         public AlbumBuilder answer(InvocationOnMock invocation) {
318                                 when(album.getSone()).thenReturn((Sone) invocation.getArguments()[0]);
319                                 return albumBuilder;
320                         }
321                 });
322                 when(albumBuilder.build()).thenAnswer(new Answer<Album>() {
323                         @Override
324                         public Album answer(InvocationOnMock invocation) {
325                                 Album album = SoneParserTest.this.album;
326                                 albums.put(album.getId(), album);
327                                 SoneParserTest.this.album = mock(Album.class);
328                                 setupAlbum();
329                                 return album;
330                         }
331                 });
332                 when(database.newAlbumBuilder()).thenReturn(albumBuilder);
333         }
334
335         @Before
336         public void setupAlbums() {
337                 when(database.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
338                         @Override
339                         public Album answer(InvocationOnMock invocation)
340                         throws Throwable {
341                                 return albums.get(invocation.getArguments()[0]);
342                         }
343                 });
344         }
345
346         @Before
347         public void setupImage() {
348                 final Image image = SoneParserTest.this.image;
349                 Image.Modifier modifier = new Image.Modifier() {
350                         private Sone sone = image.getSone();
351                         private long creationTime = image.getCreationTime();
352                         private String key = image.getKey();
353                         private String title = image.getTitle();
354                         private String description = image.getDescription();
355                         private int width = image.getWidth();
356                         private int height = image.getHeight();
357
358                         @Override
359                         public Image.Modifier setSone(Sone sone) {
360                                 this.sone = sone;
361                                 return this;
362                         }
363
364                         @Override
365                         public Image.Modifier setCreationTime(long creationTime) {
366                                 this.creationTime = creationTime;
367                                 return this;
368                         }
369
370                         @Override
371                         public Image.Modifier setKey(String key) {
372                                 this.key = key;
373                                 return this;
374                         }
375
376                         @Override
377                         public Image.Modifier setTitle(String title) {
378                                 this.title = title;
379                                 return this;
380                         }
381
382                         @Override
383                         public Image.Modifier setDescription(String description) {
384                                 this.description = description;
385                                 return this;
386                         }
387
388                         @Override
389                         public Image.Modifier setWidth(int width) {
390                                 this.width = width;
391                                 return this;
392                         }
393
394                         @Override
395                         public Image.Modifier setHeight(int height) {
396                                 this.height = height;
397                                 return this;
398                         }
399
400                         @Override
401                         public Image update() throws IllegalStateException {
402                                 when(image.getSone()).thenReturn(sone);
403                                 when(image.getCreationTime()).thenReturn(creationTime);
404                                 when(image.getKey()).thenReturn(key);
405                                 when(image.getTitle()).thenReturn(title);
406                                 when(image.getDescription()).thenReturn(description);
407                                 when(image.getWidth()).thenReturn(width);
408                                 when(image.getHeight()).thenReturn(height);
409                                 return image;
410                         }
411                 };
412                 when(image.getSone()).thenReturn(sone);
413                 when(image.modify()).thenReturn(modifier);
414         }
415
416         @Before
417         public void setupImageBuilder() {
418                 when(imageBuilder.randomId()).thenAnswer(new Answer<ImageBuilder>() {
419                         @Override
420                         public ImageBuilder answer(InvocationOnMock invocation) {
421                                 when(image.getId()).thenReturn(randomUUID().toString());
422                                 return imageBuilder;
423                         }
424                 });
425                 when(imageBuilder.withId(anyString())).thenAnswer(new Answer<ImageBuilder>() {
426                         @Override
427                         public ImageBuilder answer(InvocationOnMock invocation) {
428                                 when(image.getId()).thenReturn(
429                                                 (String) invocation.getArguments()[0]);
430                                 return imageBuilder;
431                         }
432                 });
433                 when(imageBuilder.build()).thenAnswer(new Answer<Image>() {
434                         @Override
435                         public Image answer(InvocationOnMock invocation) {
436                                 Image image = SoneParserTest.this.image;
437                                 images.put(image.getId(), image);
438                                 SoneParserTest.this.image = mock(Image.class);
439                                 setupImage();
440                                 return image;
441                         }
442                 });
443                 when(database.newImageBuilder()).thenReturn(imageBuilder);
444         }
445
446         @Before
447         public void setupImages() {
448                 when(database.getImage(anyString())).thenAnswer(new Answer<Image>() {
449                         @Override
450                         public Image answer(InvocationOnMock invocation)
451                         throws Throwable {
452                                 return images.get(invocation.getArguments()[0]);
453                         }
454                 });
455         }
456         @Test
457         public void parsingASoneFailsWhenDocumentIsNotXml() throws SoneException {
458                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-not-xml.xml");
459                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
460         }
461
462         @Test
463         public void parsingASoneFailsWhenDocumentHasNegativeProtocolVersion() throws SoneException {
464                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-negative-protocol-version.xml");
465                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
466         }
467
468         @Test
469         public void parsingASoneFailsWhenProtocolVersionIsTooLarge() throws SoneException {
470                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-too-large-protocol-version.xml");
471                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
472         }
473
474         @Test
475         public void parsingASoneFailsWhenThereIsNoTime() throws SoneException {
476                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-time.xml");
477                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
478         }
479
480         @Test
481         public void parsingASoneFailsWhenTimeIsNotNumeric() throws SoneException {
482                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-time-not-numeric.xml");
483                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
484         }
485
486         @Test
487         public void parsingASoneFailsWhenProfileIsMissing() throws SoneException {
488                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-profile.xml");
489                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
490         }
491
492         @Test
493         public void parsingASoneFailsWhenProfileFieldIsMissingAFieldName() throws SoneException {
494                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-missing-field-name.xml");
495                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
496         }
497
498         @Test
499         public void parsingASoneFailsWhenProfileFieldNameIsEmpty() throws SoneException {
500                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-empty-field-name.xml");
501                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
502         }
503
504         @Test
505         public void parsingASoneFailsWhenProfileFieldNameIsNotUnique() throws SoneException {
506                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-duplicate-field-name.xml");
507                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
508         }
509
510         @Test
511         public void parsingASoneSucceedsWithoutPayload() throws SoneException {
512                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
513                 assertThat(soneParser.parseSone(sone, inputStream).getTime(), is(
514                                 1407197508000L));
515         }
516
517         @Test
518         public void parsingALocalSoneSucceedsWithoutPayload() throws SoneException {
519                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
520                 Sone localSone = mock(Sone.class);
521                 setupSone(localSone, OwnIdentity.class);
522                 when(localSone.isLocal()).thenReturn(true);
523                 Sone parsedSone = soneParser.parseSone(localSone, inputStream);
524                 assertThat(parsedSone.getTime(), is(1407197508000L));
525                 assertThat(parsedSone.isLocal(), is(true));
526         }
527
528         @Test
529         public void parsingASoneSucceedsWithoutProtocolVersion() throws SoneException {
530                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-protocol-version.xml");
531                 assertThat(soneParser.parseSone(sone, inputStream), not(
532                                 nullValue()));
533         }
534
535         @Test
536         public void parsingASoneFailsWithMissingClientName() throws SoneException {
537                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-name.xml");
538                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
539         }
540
541         @Test
542         public void parsingASoneFailsWithMissingClientVersion() throws SoneException {
543                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-version.xml");
544                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
545         }
546
547         @Test
548         public void parsingASoneSucceedsWithClientInfo() throws SoneException {
549                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-client-info.xml");
550                 assertThat(soneParser.parseSone(sone, inputStream).getClient(), is(new Client("some-client", "some-version")));
551         }
552
553         @Test
554         public void parsingASoneSucceedsWithProfile() throws SoneException,
555         MalformedURLException {
556                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-profile.xml");
557                 final Profile profile = soneParser.parseSone(sone, inputStream).getProfile();
558                 assertThat(profile.getFirstName(), is("first"));
559                 assertThat(profile.getMiddleName(), is("middle"));
560                 assertThat(profile.getLastName(), is("last"));
561                 assertThat(profile.getBirthDay(), is(18));
562                 assertThat(profile.getBirthMonth(), is(12));
563                 assertThat(profile.getBirthYear(), is(1976));
564         }
565
566         @Test
567         public void parsingASoneSucceedsWithoutProfileFields() throws SoneException, MalformedURLException {
568                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-fields.xml");
569                 assertThat(soneParser.parseSone(sone, inputStream), notNullValue());
570         }
571
572         @Test
573         public void parsingASoneFailsWithoutPostId() throws SoneException, MalformedURLException {
574                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-id.xml");
575                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
576         }
577
578         @Test
579         public void parsingASoneFailsWithoutPostTime() throws SoneException, MalformedURLException {
580                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-time.xml");
581                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
582         }
583
584         @Test
585         public void parsingASoneFailsWithoutPostText() throws SoneException, MalformedURLException {
586                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-text.xml");
587                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
588         }
589
590         @Test
591         public void parsingASoneFailsWithInvalidPostTime() throws SoneException, MalformedURLException {
592                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-time.xml");
593                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
594         }
595
596         @Test
597         public void parsingASoneSucceedsWithValidPostTime() throws SoneException, MalformedURLException {
598                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-time.xml");
599                 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
600                 assertThat(posts, is(createdPosts));
601                 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
602                 assertThat(posts.get(0).getId(), is("post-id"));
603                 assertThat(posts.get(0).getTime(), is(1407197508000L));
604                 assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
605                 assertThat(posts.get(0).getText(), is("text"));
606         }
607
608         @Test
609         public void parsingASoneSucceedsWithRecipient() throws SoneException, MalformedURLException {
610                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-recipient.xml");
611                 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
612                 assertThat(posts, is(createdPosts));
613                 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
614                 assertThat(posts.get(0).getId(), is("post-id"));
615                 assertThat(posts.get(0).getTime(), is(1407197508000L));
616                 assertThat(posts.get(0).getRecipientId(), is(of(
617                                 "1234567890123456789012345678901234567890123")));
618                 assertThat(posts.get(0).getText(), is("text"));
619         }
620
621         @Test
622         public void parsingASoneSucceedsWithInvalidRecipient() throws SoneException, MalformedURLException {
623                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-recipient.xml");
624                 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
625                 assertThat(posts, is(createdPosts));
626                 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
627                 assertThat(posts.get(0).getId(), is("post-id"));
628                 assertThat(posts.get(0).getTime(), is(1407197508000L));
629                 assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
630                 assertThat(posts.get(0).getText(), is("text"));
631         }
632
633         @Test
634         public void parsingASoneFailsWithoutPostReplyId() throws SoneException, MalformedURLException {
635                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-id.xml");
636                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
637         }
638
639         @Test
640         public void parsingASoneFailsWithoutPostReplyPostId() throws SoneException, MalformedURLException {
641                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-post-id.xml");
642                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
643         }
644
645         @Test
646         public void parsingASoneFailsWithoutPostReplyTime() throws SoneException, MalformedURLException {
647                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-time.xml");
648                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
649         }
650
651         @Test
652         public void parsingASoneFailsWithoutPostReplyText() throws SoneException, MalformedURLException {
653                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-text.xml");
654                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
655         }
656
657         @Test
658         public void parsingASoneFailsWithInvalidPostReplyTime() throws SoneException, MalformedURLException {
659                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-reply-time.xml");
660                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
661         }
662
663         @Test
664         public void parsingASoneSucceedsWithValidPostReplyTime() throws SoneException, MalformedURLException {
665                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-reply-time.xml");
666                 final Set<PostReply> postReplies = soneParser.parseSone(sone, inputStream).getReplies();
667                 assertThat(postReplies, is(createdPostReplies));
668                 PostReply postReply = createdPostReplies.iterator().next();
669                 assertThat(postReply.getId(), is("reply-id"));
670                 assertThat(postReply.getPostId(), is("post-id"));
671                 assertThat(postReply.getPost().get().getId(), is("post-id"));
672                 assertThat(postReply.getSone().getId(), is("identity"));
673                 assertThat(postReply.getTime(), is(1407197508000L));
674                 assertThat(postReply.getText(), is("reply-text"));
675         }
676
677         @Test
678         public void parsingASoneSucceedsWithoutLikedPostIds() throws SoneException, MalformedURLException {
679                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-ids.xml");
680                 assertThat(soneParser.parseSone(sone, inputStream), not(
681                                 nullValue()));
682         }
683
684         @Test
685         public void parsingASoneSucceedsWithLikedPostIds() throws SoneException, MalformedURLException {
686                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-ids.xml");
687                 assertThat(soneParser.parseSone(sone, inputStream).getLikedPostIds(), is(
688                                 (Set<String>) ImmutableSet.of("liked-post-id")));
689         }
690
691         @Test
692         public void parsingASoneSucceedsWithoutLikedPostReplyIds() throws SoneException, MalformedURLException {
693                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-reply-ids.xml");
694                 assertThat(soneParser.parseSone(sone, inputStream), not(
695                                 nullValue()));
696         }
697
698         @Test
699         public void parsingASoneSucceedsWithLikedPostReplyIds() throws SoneException, MalformedURLException {
700                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-reply-ids.xml");
701                 assertThat(soneParser.parseSone(sone, inputStream).getLikedReplyIds(), is(
702                                 (Set<String>) ImmutableSet.of("liked-post-reply-id")));
703         }
704
705         @Test
706         public void parsingASoneSucceedsWithoutAlbums() throws SoneException, MalformedURLException {
707                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-albums.xml");
708                 assertThat(soneParser.parseSone(sone, inputStream), not(
709                                 nullValue()));
710         }
711
712         @Test
713         public void parsingASoneFailsWithoutAlbumId() throws SoneException, MalformedURLException {
714                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-id.xml");
715                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
716         }
717
718         @Test
719         public void parsingASoneFailsWithoutAlbumTitle() throws SoneException, MalformedURLException {
720                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-title.xml");
721                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
722         }
723
724         @Test
725         public void parsingASoneSucceedsWithNestedAlbums() throws SoneException, MalformedURLException {
726                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-multiple-albums.xml");
727                 final Sone parsedSone = soneParser.parseSone(sone, inputStream);
728                 assertThat(parsedSone, not(nullValue()));
729                 assertThat(parsedSone.getRootAlbum().getAlbums(), hasSize(1));
730                 Album album = parsedSone.getRootAlbum().getAlbums().get(0);
731                 assertThat(album.getId(), is("album-id-1"));
732                 assertThat(album.getTitle(), is("album-title"));
733                 assertThat(album.getDescription(), is("album-description"));
734                 assertThat(album.getAlbums(), hasSize(1));
735                 Album nestedAlbum = album.getAlbums().get(0);
736                 assertThat(nestedAlbum.getId(), is("album-id-2"));
737                 assertThat(nestedAlbum.getTitle(), is("album-title-2"));
738                 assertThat(nestedAlbum.getDescription(), is("album-description-2"));
739                 assertThat(nestedAlbum.getAlbums(), hasSize(0));
740         }
741
742         @Test
743         public void parsingASoneFailsWithInvalidParentAlbumId() throws SoneException, MalformedURLException {
744                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-parent-album-id.xml");
745                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
746         }
747
748         @Test
749         public void parsingASoneSucceedsWithoutImages() throws SoneException, MalformedURLException {
750                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-images.xml");
751                 assertThat(soneParser.parseSone(sone, inputStream), not(
752                                 nullValue()));
753         }
754
755         @Test
756         public void parsingASoneFailsWithoutImageId() throws SoneException, MalformedURLException {
757                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-id.xml");
758                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
759         }
760
761         @Test
762         public void parsingASoneFailsWithoutImageTime() throws SoneException, MalformedURLException {
763                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-time.xml");
764                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
765         }
766
767         @Test
768         public void parsingASoneFailsWithoutImageKey() throws SoneException, MalformedURLException {
769                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-key.xml");
770                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
771         }
772
773         @Test
774         public void parsingASoneFailsWithoutImageTitle() throws SoneException, MalformedURLException {
775                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-title.xml");
776                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
777         }
778
779         @Test
780         public void parsingASoneFailsWithoutImageWidth() throws SoneException, MalformedURLException {
781                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-width.xml");
782                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
783         }
784
785         @Test
786         public void parsingASoneFailsWithoutImageHeight() throws SoneException, MalformedURLException {
787                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-height.xml");
788                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
789         }
790
791         @Test
792         public void parsingASoneFailsWithInvalidImageWidth() throws SoneException, MalformedURLException {
793                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-width.xml");
794                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
795         }
796
797         @Test
798         public void parsingASoneFailsWithInvalidImageHeight() throws SoneException, MalformedURLException {
799                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-height.xml");
800                 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
801         }
802
803         @Test
804         public void parsingASoneSucceedsWithImage() throws SoneException, MalformedURLException {
805                 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-image.xml");
806                 final Sone sone = soneParser.parseSone(this.sone, inputStream);
807                 assertThat(sone, not(nullValue()));
808                 assertThat(sone.getRootAlbum().getAlbums(), hasSize(1));
809                 assertThat(sone.getRootAlbum().getAlbums().get(0).getImages(), hasSize(1));
810                 Image image = sone.getRootAlbum().getAlbums().get(0).getImages().get(0);
811                 assertThat(image.getId(), is("image-id"));
812                 assertThat(image.getCreationTime(), is(1407197508000L));
813                 assertThat(image.getKey(), is("KSK@GPLv3.txt"));
814                 assertThat(image.getTitle(), is("image-title"));
815                 assertThat(image.getDescription(), is("image-description"));
816                 assertThat(image.getWidth(), is(1920));
817                 assertThat(image.getHeight(), is(1080));
818                 assertThat(sone.getProfile().getAvatar(), is("image-id"));
819         }
820
821
822 }