+ public void addingASoneWillRegisterItsKey() {
+ soneDownloader.addSone(sone);
+ verify(freenetInterface).registerUsk(sone, soneDownloader);
+ verify(freenetInterface, never()).unregisterUsk(sone);
+ }
+
+ @Test
+ public void addingASoneTwiceWillAlsoDeregisterItsKey() {
+ soneDownloader.addSone(sone);
+ soneDownloader.addSone(sone);
+ verify(freenetInterface, times(2)).registerUsk(sone, soneDownloader);
+ verify(freenetInterface).unregisterUsk(sone);
+ }
+
+ @Test
+ public void parsingASoneFailsWhenDocumentIsNotXml() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-not-xml.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenDocumentHasNegativeProtocolVersion() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-negative-protocol-version.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenProtocolVersionIsTooLarge() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-too-large-protocol-version.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenThereIsNoTime() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenTimeIsNotNumeric() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-time-not-numeric.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenProfileIsMissing() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-profile.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenProfileFieldIsMissingAFieldName() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-missing-field-name.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenProfileFieldNameIsEmpty() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-empty-field-name.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWhenProfileFieldNameIsNotUnique() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-profile-duplicate-field-name.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutPayload() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream).getTime(), is(1407197508000L));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutProtocolVersion() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-protocol-version.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+ }
+
+ @Test
+ public void parsingASoneFailsWithMissingClientName() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-name.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithMissingClientVersion() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-client-version.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithClientInfo() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-client-info.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream).getClient(), is(new Client("some-client", "some-version")));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithRequestUri() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-request-uri.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream).getRequestUri().toString(), is("USK@w~RyTGmv12Lg9oO91q1Untupi7my9qczT1RheGkEkVE,E8ElVfUgukSCPHxIEJp-gHMiR80wpM7sID3Jo5O7w1s,AQACAAE/Sone/0"));
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidRequestUri() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-request-uri.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void soneInsertUriIsCopiedToNewSone() throws SoneException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
+ FreenetURI insertUri = mock(FreenetURI.class);
+ when(insertUri.setKeyType(anyString())).thenReturn(insertUri);
+ when(insertUri.setDocName(anyString())).thenReturn(insertUri);
+ when(insertUri.setMetaString(any(String[].class))).thenReturn(insertUri);
+ when(insertUri.setSuggestedEdition(anyLong())).thenReturn(insertUri);
+ when(sone.getInsertUri()).thenReturn(insertUri);
+ assertThat(soneDownloader.parseSone(sone, inputStream).getInsertUri(), is(insertUri));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithProfile() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-profile.xml");
+ final Profile profile = soneDownloader.parseSone(sone, inputStream).getProfile();
+ assertThat(profile.getFirstName(), is("first"));
+ assertThat(profile.getMiddleName(), is("middle"));
+ assertThat(profile.getLastName(), is("last"));
+ assertThat(profile.getBirthDay(), is(18));
+ assertThat(profile.getBirthMonth(), is(12));
+ assertThat(profile.getBirthYear(), is(1976));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutProfileFields() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-fields.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), notNullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostText() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-text.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidPostTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithValidPostTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-time.xml");
+ final List<Post> posts = soneDownloader.parseSone(sone, inputStream).getPosts();
+ assertThat(posts, is(createdPosts));
+ assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
+ assertThat(posts.get(0).getId(), is("post-id"));
+ assertThat(posts.get(0).getTime(), is(1407197508000L));
+ assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
+ assertThat(posts.get(0).getText(), is("text"));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithRecipient() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-recipient.xml");
+ final List<Post> posts = soneDownloader.parseSone(sone, inputStream).getPosts();
+ assertThat(posts, is(createdPosts));
+ assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
+ assertThat(posts.get(0).getId(), is("post-id"));
+ assertThat(posts.get(0).getTime(), is(1407197508000L));
+ assertThat(posts.get(0).getRecipientId(), is(of("1234567890123456789012345678901234567890123")));
+ assertThat(posts.get(0).getText(), is("text"));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithInvalidRecipient() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-recipient.xml");
+ final List<Post> posts = soneDownloader.parseSone(sone, inputStream).getPosts();
+ assertThat(posts, is(createdPosts));
+ assertThat(posts.get(0).getSone().getId(), is(sone.getId()));
+ assertThat(posts.get(0).getId(), is("post-id"));
+ assertThat(posts.get(0).getTime(), is(1407197508000L));
+ assertThat(posts.get(0).getRecipientId(), is(Optional.<String>absent()));
+ assertThat(posts.get(0).getText(), is("text"));
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostReplyId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostReplyPostId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-post-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostReplyTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutPostReplyText() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-post-reply-text.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidPostReplyTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-post-reply-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithValidPostReplyTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-valid-post-reply-time.xml");
+ final Set<PostReply> postReplies = soneDownloader.parseSone(sone, inputStream).getReplies();
+ assertThat(postReplies, is(createdPostReplies));
+ PostReply postReply = createdPostReplies.iterator().next();
+ assertThat(postReply.getId(), is("reply-id"));
+ assertThat(postReply.getPostId(), is("post-id"));
+ assertThat(postReply.getPost().get().getId(), is("post-id"));
+ assertThat(postReply.getSone().getId(), is("identity"));
+ assertThat(postReply.getTime(), is(1407197508000L));
+ assertThat(postReply.getText(), is("reply-text"));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutLikedPostIds() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-ids.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithLikedPostIds() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-ids.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream).getLikedPostIds(), is((Set<String>) ImmutableSet.of("liked-post-id")));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutLikedPostReplyIds() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-reply-ids.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithLikedPostReplyIds() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-reply-ids.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream).getLikedReplyIds(), is((Set<String>) ImmutableSet.of("liked-post-reply-id")));
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutAlbums() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-albums.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutAlbumId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutAlbumTitle() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-album-title.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithNestedAlbums() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-multiple-albums.xml");
+ final Sone parsedSone = soneDownloader.parseSone(sone, inputStream);
+ assertThat(parsedSone, not(nullValue()));
+ assertThat(parsedSone.getRootAlbum().getAlbums(), hasSize(1));
+ Album album = parsedSone.getRootAlbum().getAlbums().get(0);
+ assertThat(album.getId(), is("album-id-1"));
+ assertThat(album.getTitle(), is("album-title"));
+ assertThat(album.getDescription(), is("album-description"));
+ assertThat(album.getAlbums(), hasSize(1));
+ Album nestedAlbum = album.getAlbums().get(0);
+ assertThat(nestedAlbum.getId(), is("album-id-2"));
+ assertThat(nestedAlbum.getTitle(), is("album-title-2"));
+ assertThat(nestedAlbum.getDescription(), is("album-description-2"));
+ assertThat(nestedAlbum.getAlbums(), hasSize(0));
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidParentAlbumId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-parent-album-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithoutImages() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-images.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageId() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-id.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageTime() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-time.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageKey() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-key.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageTitle() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-title.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageWidth() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-width.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithoutImageHeight() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-image-height.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidImageWidth() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-width.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneFailsWithInvalidImageHeight() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-image-height.xml");
+ assertThat(soneDownloader.parseSone(sone, inputStream), nullValue());
+ }
+
+ @Test
+ public void parsingASoneSucceedsWithImage() throws SoneException, MalformedURLException {
+ InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-image.xml");
+ final Sone sone = soneDownloader.parseSone(this.sone, inputStream);
+ assertThat(sone, not(nullValue()));
+ assertThat(sone.getRootAlbum().getAlbums(), hasSize(1));
+ assertThat(sone.getRootAlbum().getAlbums().get(0).getImages(), hasSize(1));
+ Image image = sone.getRootAlbum().getAlbums().get(0).getImages().get(0);
+ assertThat(image.getId(), is("image-id"));
+ assertThat(image.getCreationTime(), is(1407197508000L));
+ assertThat(image.getKey(), is("KSK@GPLv3.txt"));
+ assertThat(image.getTitle(), is("image-title"));
+ assertThat(image.getDescription(), is("image-description"));
+ assertThat(image.getWidth(), is(1920));
+ assertThat(image.getHeight(), is(1080));
+ assertThat(sone.getProfile().getAvatar(), is("image-id"));
+ }
+
+ @Test
+ public void stoppingTheSoneDownloaderUnregistersTheSone() {
+ soneDownloader.addSone(sone);
+ soneDownloader.stop();
+ verify(freenetInterface).unregisterUsk(sone);
+ }
+
+ @Test
+ public void fetchSoneWithUriDownloadsSoneWithUri() {