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