1 package net.pterodactylus.sone.core;
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;
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;
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;
48 import freenet.crypt.DummyRandomSource;
49 import freenet.keys.FreenetURI;
50 import freenet.keys.InsertableClientSSK;
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;
62 * Unit test for {@link SoneParser}.
64 public class SoneParserTest {
66 private final Database database = mock(Database.class);
67 private final SoneParser soneParser = new SoneParser(database);
68 private final Sone sone = mock(Sone.class);
69 private FreenetURI requestUri = mock(FreenetURI.class);
70 private final PostBuilder postBuilder = mock(PostBuilder.class);
71 private final List<Post> createdPosts = new ArrayList<>();
72 private Post post = mock(Post.class);
73 private final PostReplyBuilder postReplyBuilder = mock(PostReplyBuilder.class);
74 private final Set<PostReply> createdPostReplies = new HashSet<>();
75 private PostReply postReply = mock(PostReply.class);
76 private final AlbumBuilder albumBuilder = mock(AlbumBuilder.class);
77 private final ListMultimap<Album, Album>
78 nestedAlbums = ArrayListMultimap.create();
79 private final ListMultimap<Album, Image> albumImages = ArrayListMultimap.create();
80 private Album album = mock(Album.class);
81 private final Map<String, Album> albums = new HashMap<>();
82 private final ImageBuilder imageBuilder = mock(ImageBuilder.class);
83 private Image image = mock(Image.class);
84 private final Map<String, Image> images = new HashMap<>();
87 public void setupSone() {
88 setupSone(this.sone, Identity.class);
91 private void setupSone(Sone sone, Class<? extends Identity> identityClass) {
92 Identity identity = mock(identityClass);
93 InsertableClientSSK clientSSK =
94 createRandom(new DummyRandomSource(), "WoT");
95 when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString());
96 when(identity.getId()).thenReturn("identity");
97 when(sone.getId()).thenReturn("identity");
98 when(sone.getIdentity()).thenReturn(identity);
99 requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone");
100 when(sone.getRequestUri()).thenAnswer(new Answer<FreenetURI>() {
102 public FreenetURI answer(InvocationOnMock invocation)
108 .thenReturn(currentTimeMillis() - DAYS.toMillis(1));
112 public void setupSoneBuilder() {
113 when(database.newSoneBuilder()).thenAnswer(new Answer<SoneBuilder>() {
115 public SoneBuilder answer(InvocationOnMock invocation) {
116 return new MemorySoneBuilder(null);
122 public void setupPost() {
123 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
127 public void setupPostBuilder() {
128 when(postBuilder.withId(anyString())).thenAnswer(new Answer<PostBuilder>() {
130 public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
131 when(post.getId()).thenReturn((String) invocation.getArguments()[0]);
135 when(postBuilder.from(anyString())).thenAnswer(new Answer<PostBuilder>() {
137 public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
138 final Sone sone = mock(Sone.class);
139 when(sone.getId()).thenReturn((String) invocation.getArguments()[0]);
140 when(post.getSone()).thenReturn(sone);
144 when(postBuilder.withTime(anyLong())).thenAnswer(new Answer<PostBuilder>() {
146 public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
147 when(post.getTime()).thenReturn((Long) invocation.getArguments()[0]);
151 when(postBuilder.withText(anyString())).thenAnswer(new Answer<PostBuilder>() {
153 public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
154 when(post.getText()).thenReturn((String) invocation.getArguments()[0]);
158 when(postBuilder.to(anyString())).thenAnswer(new Answer<PostBuilder>() {
160 public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
161 when(post.getRecipientId()).thenReturn(of((String) invocation.getArguments()[0]));
165 when(postBuilder.build()).thenAnswer(new Answer<Post>() {
167 public Post answer(InvocationOnMock invocation) throws Throwable {
168 Post post = SoneParserTest.this.post;
169 SoneParserTest.this.post = mock(Post.class);
171 createdPosts.add(post);
175 when(database.newPostBuilder()).thenReturn(postBuilder);
179 public void setupPostReplyBuilder() {
180 when(postReplyBuilder.withId(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
182 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
183 when(postReply.getId()).thenReturn((String) invocation.getArguments()[0]);
184 return postReplyBuilder;
187 when(postReplyBuilder.from(anyString())).thenAnswer(
188 new Answer<PostReplyBuilder>() {
190 public PostReplyBuilder answer(
191 InvocationOnMock invocation) throws Throwable {
192 Sone sone = when(mock(Sone.class).getId()).thenReturn(
193 (String) invocation.getArguments()[0])
195 when(postReply.getSone()).thenReturn(sone);
196 return postReplyBuilder;
199 when(postReplyBuilder.to(anyString())).thenAnswer(
200 new Answer<PostReplyBuilder>() {
202 public PostReplyBuilder answer(
203 InvocationOnMock invocation) throws Throwable {
204 when(postReply.getPostId()).thenReturn(
205 (String) invocation.getArguments()[0]);
206 Post post = when(mock(Post.class).getId()).thenReturn(
207 (String) invocation.getArguments()[0])
209 when(postReply.getPost()).thenReturn(of(post));
210 return postReplyBuilder;
213 when(postReplyBuilder.withTime(anyLong())).thenAnswer(
214 new Answer<PostReplyBuilder>() {
216 public PostReplyBuilder answer(
217 InvocationOnMock invocation) throws Throwable {
218 when(postReply.getTime()).thenReturn(
219 (Long) invocation.getArguments()[0]);
220 return postReplyBuilder;
223 when(postReplyBuilder.withText(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
225 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
226 when(postReply.getText()).thenReturn((String) invocation.getArguments()[0]);
227 return postReplyBuilder;
230 when(postReplyBuilder.build()).thenAnswer(new Answer<PostReply>() {
232 public PostReply answer(InvocationOnMock invocation) throws Throwable {
233 PostReply postReply = SoneParserTest.this.postReply;
234 createdPostReplies.add(postReply);
235 SoneParserTest.this.postReply = mock(PostReply.class);
239 when(database.newPostReplyBuilder()).thenReturn(postReplyBuilder);
243 public void setupAlbum() {
244 final Album album = SoneParserTest.this.album;
245 doAnswer(new Answer<Void>() {
247 public Void answer(InvocationOnMock invocation) {
248 nestedAlbums.put(album, (Album) invocation.getArguments()[0]);
251 }).when(album).addAlbum(any(Album.class));
252 doAnswer(new Answer<Void>() {
254 public Void answer(InvocationOnMock invocation) {
255 albumImages.put(album, (Image) invocation.getArguments()[0]);
258 }).when(album).addImage(any(Image.class));
259 when(album.getAlbums()).thenAnswer(new Answer<List<Album>>() {
261 public List<Album> answer(InvocationOnMock invocation) {
262 return nestedAlbums.get(album);
265 when(album.getImages()).thenAnswer(new Answer<List<Image>>() {
267 public List<Image> answer(InvocationOnMock invocation) {
268 return albumImages.get(album);
271 final Modifier albumModifier = new Modifier() {
272 private String title = album.getTitle();
273 private String description = album.getDescription();
276 public Modifier setTitle(String title) {
282 public Modifier setDescription(String description) {
283 this.description = description;
288 public Album update() throws IllegalStateException {
289 when(album.getTitle()).thenReturn(title);
290 when(album.getDescription()).thenReturn(description);
294 when(album.modify()).thenReturn(albumModifier);
298 public void setupAlbumBuilder() {
299 when(albumBuilder.withId(anyString())).thenAnswer(new Answer<AlbumBuilder>() {
301 public AlbumBuilder answer(InvocationOnMock invocation) {
302 when(album.getId()).thenReturn((String) invocation.getArguments()[0]);
306 when(albumBuilder.randomId()).thenAnswer(new Answer<AlbumBuilder>() {
308 public AlbumBuilder answer(InvocationOnMock invocation) {
309 when(album.getId()).thenReturn(randomUUID().toString());
313 when(albumBuilder.by(any(Sone.class))).thenAnswer(new Answer<AlbumBuilder>() {
315 public AlbumBuilder answer(InvocationOnMock invocation) {
316 when(album.getSone()).thenReturn((Sone) invocation.getArguments()[0]);
320 when(albumBuilder.build()).thenAnswer(new Answer<Album>() {
322 public Album answer(InvocationOnMock invocation) {
323 Album album = SoneParserTest.this.album;
324 albums.put(album.getId(), album);
325 SoneParserTest.this.album = mock(Album.class);
330 when(database.newAlbumBuilder()).thenReturn(albumBuilder);
334 public void setupAlbums() {
335 when(database.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
337 public Album answer(InvocationOnMock invocation)
339 return albums.get(invocation.getArguments()[0]);
345 public void setupImage() {
346 final Image image = SoneParserTest.this.image;
347 Image.Modifier modifier = new Image.Modifier() {
348 private Sone sone = image.getSone();
349 private long creationTime = image.getCreationTime();
350 private String key = image.getKey();
351 private String title = image.getTitle();
352 private String description = image.getDescription();
353 private int width = image.getWidth();
354 private int height = image.getHeight();
357 public Image.Modifier setSone(Sone sone) {
363 public Image.Modifier setCreationTime(long creationTime) {
364 this.creationTime = creationTime;
369 public Image.Modifier setKey(String key) {
375 public Image.Modifier setTitle(String title) {
381 public Image.Modifier setDescription(String description) {
382 this.description = description;
387 public Image.Modifier setWidth(int width) {
393 public Image.Modifier setHeight(int height) {
394 this.height = height;
399 public Image update() throws IllegalStateException {
400 when(image.getSone()).thenReturn(sone);
401 when(image.getCreationTime()).thenReturn(creationTime);
402 when(image.getKey()).thenReturn(key);
403 when(image.getTitle()).thenReturn(title);
404 when(image.getDescription()).thenReturn(description);
405 when(image.getWidth()).thenReturn(width);
406 when(image.getHeight()).thenReturn(height);
410 when(image.getSone()).thenReturn(sone);
411 when(image.modify()).thenReturn(modifier);
415 public void setupImageBuilder() {
416 when(imageBuilder.randomId()).thenAnswer(new Answer<ImageBuilder>() {
418 public ImageBuilder answer(InvocationOnMock invocation) {
419 when(image.getId()).thenReturn(randomUUID().toString());
423 when(imageBuilder.withId(anyString())).thenAnswer(new Answer<ImageBuilder>() {
425 public ImageBuilder answer(InvocationOnMock invocation) {
426 when(image.getId()).thenReturn(
427 (String) invocation.getArguments()[0]);
431 when(imageBuilder.build()).thenAnswer(new Answer<Image>() {
433 public Image answer(InvocationOnMock invocation) {
434 Image image = SoneParserTest.this.image;
435 images.put(image.getId(), image);
436 SoneParserTest.this.image = mock(Image.class);
441 when(database.newImageBuilder()).thenReturn(imageBuilder);
445 public void setupImages() {
446 when(database.getImage(anyString())).thenAnswer(new Answer<Image>() {
448 public Image answer(InvocationOnMock invocation)
450 return images.get(invocation.getArguments()[0]);
455 public void parsingASoneFailsWhenDocumentIsNotXml() throws SoneException {
456 InputStream inputStream = getClass().getResourceAsStream("sone-parser-not-xml.xml");
457 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
461 public void parsingASoneFailsWhenDocumentHasNegativeProtocolVersion() throws SoneException {
462 InputStream inputStream = getClass().getResourceAsStream("sone-parser-negative-protocol-version.xml");
463 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
467 public void parsingASoneFailsWhenProtocolVersionIsTooLarge() throws SoneException {
468 InputStream inputStream = getClass().getResourceAsStream("sone-parser-too-large-protocol-version.xml");
469 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
473 public void parsingASoneFailsWhenThereIsNoTime() throws SoneException {
474 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-time.xml");
475 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
479 public void parsingASoneFailsWhenTimeIsNotNumeric() throws SoneException {
480 InputStream inputStream = getClass().getResourceAsStream("sone-parser-time-not-numeric.xml");
481 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
485 public void parsingASoneFailsWhenProfileIsMissing() throws SoneException {
486 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-profile.xml");
487 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
491 public void parsingASoneFailsWhenProfileFieldIsMissingAFieldName() throws SoneException {
492 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-missing-field-name.xml");
493 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
497 public void parsingASoneFailsWhenProfileFieldNameIsEmpty() throws SoneException {
498 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-empty-field-name.xml");
499 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
503 public void parsingASoneFailsWhenProfileFieldNameIsNotUnique() throws SoneException {
504 InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-duplicate-field-name.xml");
505 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
509 public void parsingASoneSucceedsWithoutPayload() throws SoneException {
510 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
511 assertThat(soneParser.parseSone(sone, inputStream).getTime(), is(
516 public void parsingALocalSoneSucceedsWithoutPayload() throws SoneException {
517 InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
518 Sone localSone = mock(Sone.class);
519 setupSone(localSone, OwnIdentity.class);
520 when(localSone.isLocal()).thenReturn(true);
521 Sone parsedSone = soneParser.parseSone(localSone, inputStream);
522 assertThat(parsedSone.getTime(), is(1407197508000L));
523 assertThat(parsedSone.isLocal(), is(true));
527 public void parsingASoneSucceedsWithoutProtocolVersion() throws SoneException {
528 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-protocol-version.xml");
529 assertThat(soneParser.parseSone(sone, inputStream), not(
534 public void parsingASoneFailsWithMissingClientName() throws SoneException {
535 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-name.xml");
536 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
540 public void parsingASoneFailsWithMissingClientVersion() throws SoneException {
541 InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-version.xml");
542 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
546 public void parsingASoneSucceedsWithClientInfo() throws SoneException {
547 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-client-info.xml");
548 assertThat(soneParser.parseSone(sone, inputStream).getClient(), is(new Client("some-client", "some-version")));
552 public void parsingASoneSucceedsWithProfile() throws SoneException,
553 MalformedURLException {
554 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-profile.xml");
555 final Profile profile = soneParser.parseSone(sone, inputStream).getProfile();
556 assertThat(profile.getFirstName(), is("first"));
557 assertThat(profile.getMiddleName(), is("middle"));
558 assertThat(profile.getLastName(), is("last"));
559 assertThat(profile.getBirthDay(), is(18));
560 assertThat(profile.getBirthMonth(), is(12));
561 assertThat(profile.getBirthYear(), is(1976));
565 public void parsingASoneSucceedsWithoutProfileFields() throws SoneException, MalformedURLException {
566 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-fields.xml");
567 assertThat(soneParser.parseSone(sone, inputStream), notNullValue());
571 public void parsingASoneFailsWithoutPostId() throws SoneException, MalformedURLException {
572 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-id.xml");
573 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
577 public void parsingASoneFailsWithoutPostTime() throws SoneException, MalformedURLException {
578 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-time.xml");
579 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
583 public void parsingASoneFailsWithoutPostText() throws SoneException, MalformedURLException {
584 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-text.xml");
585 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
589 public void parsingASoneFailsWithInvalidPostTime() throws SoneException, MalformedURLException {
590 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-time.xml");
591 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
595 public void parsingASoneSucceedsWithValidPostTime() throws SoneException, MalformedURLException {
596 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-time.xml");
597 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
598 assertThat(posts, is(createdPosts));
599 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
600 assertThat(posts.get(0).getId(), is("post-id"));
601 assertThat(posts.get(0).getTime(), is(1407197508000L));
602 assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
603 assertThat(posts.get(0).getText(), is("text"));
607 public void parsingASoneSucceedsWithRecipient() throws SoneException, MalformedURLException {
608 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-recipient.xml");
609 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
610 assertThat(posts, is(createdPosts));
611 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
612 assertThat(posts.get(0).getId(), is("post-id"));
613 assertThat(posts.get(0).getTime(), is(1407197508000L));
614 assertThat(posts.get(0).getRecipientId(), is(of(
615 "1234567890123456789012345678901234567890123")));
616 assertThat(posts.get(0).getText(), is("text"));
620 public void parsingASoneSucceedsWithInvalidRecipient() throws SoneException, MalformedURLException {
621 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-recipient.xml");
622 final List<Post> posts = soneParser.parseSone(sone, inputStream).getPosts();
623 assertThat(posts, is(createdPosts));
624 assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
625 assertThat(posts.get(0).getId(), is("post-id"));
626 assertThat(posts.get(0).getTime(), is(1407197508000L));
627 assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
628 assertThat(posts.get(0).getText(), is("text"));
632 public void parsingASoneFailsWithoutPostReplyId() throws SoneException, MalformedURLException {
633 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-id.xml");
634 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
638 public void parsingASoneFailsWithoutPostReplyPostId() throws SoneException, MalformedURLException {
639 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-post-id.xml");
640 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
644 public void parsingASoneFailsWithoutPostReplyTime() throws SoneException, MalformedURLException {
645 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-time.xml");
646 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
650 public void parsingASoneFailsWithoutPostReplyText() throws SoneException, MalformedURLException {
651 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-text.xml");
652 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
656 public void parsingASoneFailsWithInvalidPostReplyTime() throws SoneException, MalformedURLException {
657 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-reply-time.xml");
658 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
662 public void parsingASoneSucceedsWithValidPostReplyTime() throws SoneException, MalformedURLException {
663 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-reply-time.xml");
664 final Set<PostReply> postReplies = soneParser.parseSone(sone, inputStream).getReplies();
665 assertThat(postReplies, is(createdPostReplies));
666 PostReply postReply = createdPostReplies.iterator().next();
667 assertThat(postReply.getId(), is("reply-id"));
668 assertThat(postReply.getPostId(), is("post-id"));
669 assertThat(postReply.getPost().get().getId(), is("post-id"));
670 assertThat(postReply.getSone().getId(), is("identity"));
671 assertThat(postReply.getTime(), is(1407197508000L));
672 assertThat(postReply.getText(), is("reply-text"));
676 public void parsingASoneSucceedsWithoutLikedPostIds() throws SoneException, MalformedURLException {
677 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-ids.xml");
678 assertThat(soneParser.parseSone(sone, inputStream), not(
683 public void parsingASoneSucceedsWithLikedPostIds() throws SoneException, MalformedURLException {
684 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-ids.xml");
685 assertThat(soneParser.parseSone(sone, inputStream).getLikedPostIds(), is(
686 (Set<String>) ImmutableSet.of("liked-post-id")));
690 public void parsingASoneSucceedsWithoutLikedPostReplyIds() throws SoneException, MalformedURLException {
691 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-reply-ids.xml");
692 assertThat(soneParser.parseSone(sone, inputStream), not(
697 public void parsingASoneSucceedsWithLikedPostReplyIds() throws SoneException, MalformedURLException {
698 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-reply-ids.xml");
699 assertThat(soneParser.parseSone(sone, inputStream).getLikedReplyIds(), is(
700 (Set<String>) ImmutableSet.of("liked-post-reply-id")));
704 public void parsingASoneSucceedsWithoutAlbums() throws SoneException, MalformedURLException {
705 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-albums.xml");
706 assertThat(soneParser.parseSone(sone, inputStream), not(
711 public void parsingASoneFailsWithoutAlbumId() throws SoneException, MalformedURLException {
712 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-id.xml");
713 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
717 public void parsingASoneFailsWithoutAlbumTitle() throws SoneException, MalformedURLException {
718 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-title.xml");
719 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
723 public void parsingASoneSucceedsWithNestedAlbums() throws SoneException, MalformedURLException {
724 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-multiple-albums.xml");
725 final Sone parsedSone = soneParser.parseSone(sone, inputStream);
726 assertThat(parsedSone, not(nullValue()));
727 assertThat(parsedSone.getRootAlbum().getAlbums(), hasSize(1));
728 Album album = parsedSone.getRootAlbum().getAlbums().get(0);
729 assertThat(album.getId(), is("album-id-1"));
730 assertThat(album.getTitle(), is("album-title"));
731 assertThat(album.getDescription(), is("album-description"));
732 assertThat(album.getAlbums(), hasSize(1));
733 Album nestedAlbum = album.getAlbums().get(0);
734 assertThat(nestedAlbum.getId(), is("album-id-2"));
735 assertThat(nestedAlbum.getTitle(), is("album-title-2"));
736 assertThat(nestedAlbum.getDescription(), is("album-description-2"));
737 assertThat(nestedAlbum.getAlbums(), hasSize(0));
741 public void parsingASoneFailsWithInvalidParentAlbumId() throws SoneException, MalformedURLException {
742 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-parent-album-id.xml");
743 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
747 public void parsingASoneSucceedsWithoutImages() throws SoneException, MalformedURLException {
748 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-images.xml");
749 assertThat(soneParser.parseSone(sone, inputStream), not(
754 public void parsingASoneFailsWithoutImageId() throws SoneException, MalformedURLException {
755 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-id.xml");
756 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
760 public void parsingASoneFailsWithoutImageTime() throws SoneException, MalformedURLException {
761 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-time.xml");
762 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
766 public void parsingASoneFailsWithoutImageKey() throws SoneException, MalformedURLException {
767 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-key.xml");
768 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
772 public void parsingASoneFailsWithoutImageTitle() throws SoneException, MalformedURLException {
773 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-title.xml");
774 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
778 public void parsingASoneFailsWithoutImageWidth() throws SoneException, MalformedURLException {
779 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-width.xml");
780 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
784 public void parsingASoneFailsWithoutImageHeight() throws SoneException, MalformedURLException {
785 InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-height.xml");
786 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
790 public void parsingASoneFailsWithInvalidImageWidth() throws SoneException, MalformedURLException {
791 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-width.xml");
792 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
796 public void parsingASoneFailsWithInvalidImageHeight() throws SoneException, MalformedURLException {
797 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-height.xml");
798 assertThat(soneParser.parseSone(sone, inputStream), nullValue());
802 public void parsingASoneSucceedsWithImage() throws SoneException, MalformedURLException {
803 InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-image.xml");
804 final Sone sone = soneParser.parseSone(this.sone, inputStream);
805 assertThat(sone, not(nullValue()));
806 assertThat(sone.getRootAlbum().getAlbums(), hasSize(1));
807 assertThat(sone.getRootAlbum().getAlbums().get(0).getImages(), hasSize(1));
808 Image image = sone.getRootAlbum().getAlbums().get(0).getImages().get(0);
809 assertThat(image.getId(), is("image-id"));
810 assertThat(image.getCreationTime(), is(1407197508000L));
811 assertThat(image.getKey(), is("KSK@GPLv3.txt"));
812 assertThat(image.getTitle(), is("image-title"));
813 assertThat(image.getDescription(), is("image-description"));
814 assertThat(image.getWidth(), is(1920));
815 assertThat(image.getHeight(), is(1080));
816 assertThat(sone.getProfile().getAvatar(), is("image-id"));