+ private final Core core = mock(Core.class);
+ private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
+ private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(core, freenetInterface);
+ private final FreenetURI requestUri = mock(FreenetURI.class);
+ private final Sone sone = mock(Sone.class);
+ private final PostBuilder postBuilder = mock(PostBuilder.class);
+ private final List<Post> createdPosts = new ArrayList<Post>();
+ private Post post = mock(Post.class);
+ private final PostReplyBuilder postReplyBuilder = mock(PostReplyBuilder.class);
+ private final Set<PostReply> createdPostReplies = new HashSet<PostReply>();
+ private PostReply postReply = mock(PostReply.class);
+ private final Map<String, Album> albums = new HashMap<String, Album>();
+
+ @Before
+ public void setupSone() {
+ Identity identity = mock(Identity.class);
+ when(identity.getId()).thenReturn("identity");
+ when(sone.getId()).thenReturn("identity");
+ when(sone.getIdentity()).thenReturn(identity);
+ when(sone.getRequestUri()).thenReturn(requestUri);
+ }
+
+ @Before
+ public void setupRequestUri() {
+ when(requestUri.setKeyType(anyString())).thenReturn(requestUri);
+ when(requestUri.sskForUSK()).thenReturn(requestUri);
+ when(requestUri.setDocName(anyString())).thenReturn(requestUri);
+ when(requestUri.setMetaString(any(String[].class))).thenReturn(requestUri);
+ when(requestUri.getKeyType()).thenReturn("USK");
+ }
+
+ @Before
+ public void setupPost() {
+ when(post.getRecipientId()).thenReturn(Optional.<String>absent());
+ }
+
+ @Before
+ public void setupPostBuilder() {
+ when(postBuilder.withId(anyString())).thenAnswer(new Answer<PostBuilder>() {
+ @Override
+ public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(post.getId()).thenReturn((String) invocation.getArguments()[0]);
+ return postBuilder;
+ }
+ });
+ when(postBuilder.from(anyString())).thenAnswer(new Answer<PostBuilder>() {
+ @Override
+ public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
+ final Sone sone = mock(Sone.class);
+ when(sone.getId()).thenReturn((String) invocation.getArguments()[0]);
+ when(post.getSone()).thenReturn(sone);
+ return postBuilder;
+ }
+ });
+ when(postBuilder.withTime(anyLong())).thenAnswer(new Answer<PostBuilder>() {
+ @Override
+ public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(post.getTime()).thenReturn((Long) invocation.getArguments()[0]);
+ return postBuilder;
+ }
+ });
+ when(postBuilder.withText(anyString())).thenAnswer(new Answer<PostBuilder>() {
+ @Override
+ public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(post.getText()).thenReturn((String) invocation.getArguments()[0]);
+ return postBuilder;
+ }
+ });
+ when(postBuilder.to(anyString())).thenAnswer(new Answer<PostBuilder>() {
+ @Override
+ public PostBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(post.getRecipientId()).thenReturn(of((String) invocation.getArguments()[0]));
+ return postBuilder;
+ }
+ });
+ when(postBuilder.build()).thenAnswer(new Answer<Post>() {
+ @Override
+ public Post answer(InvocationOnMock invocation) throws Throwable {
+ Post post = SoneDownloaderTest.this.post;
+ SoneDownloaderTest.this.post = mock(Post.class);
+ setupPost();
+ createdPosts.add(post);
+ return post;
+ }
+ });
+ when(core.postBuilder()).thenReturn(postBuilder);
+ }
+
+ @Before
+ public void setupPostReplyBuilder() {
+ when(postReplyBuilder.withId(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
+ @Override
+ public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(postReply.getId()).thenReturn((String) invocation.getArguments()[0]);
+ return postReplyBuilder;
+ }
+ });
+ when(postReplyBuilder.from(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
+ @Override
+ public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
+ Sone sone = when(mock(Sone.class).getId()).thenReturn((String) invocation.getArguments()[0]).getMock();
+ when(postReply.getSone()).thenReturn(sone);
+ return postReplyBuilder;
+ }
+ });
+ when(postReplyBuilder.to(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
+ @Override
+ public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(postReply.getPostId()).thenReturn((String) invocation.getArguments()[0]);
+ Post post = when(mock(Post.class).getId()).thenReturn((String) invocation.getArguments()[0]).getMock();
+ when(postReply.getPost()).thenReturn(of(post));
+ return postReplyBuilder;
+ }
+ });
+ when(postReplyBuilder.withTime(anyLong())).thenAnswer(new Answer<PostReplyBuilder>() {
+ @Override
+ public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(postReply.getTime()).thenReturn((Long) invocation.getArguments()[0]);
+ return postReplyBuilder;
+ }
+ });
+ when(postReplyBuilder.withText(anyString())).thenAnswer(new Answer<PostReplyBuilder>() {
+ @Override
+ public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
+ when(postReply.getText()).thenReturn((String) invocation.getArguments()[0]);
+ return postReplyBuilder;
+ }
+ });
+ when(postReplyBuilder.build()).thenAnswer(new Answer<PostReply>() {
+ @Override
+ public PostReply answer(InvocationOnMock invocation) throws Throwable {
+ PostReply postReply = SoneDownloaderTest.this.postReply;
+ createdPostReplies.add(postReply);
+ SoneDownloaderTest.this.postReply = mock(PostReply.class);
+ return postReply;
+ }
+ });
+ when(core.postReplyBuilder()).thenReturn(postReplyBuilder);
+ }
+
+ @Before
+ public void setupAlbums() {
+ albums.put("album-id-1", new AlbumImpl("album-id-1"));
+ albums.put("album-id-2", new AlbumImpl("album-id-2"));
+ when(core.getOrCreateAlbum(anyString())).thenAnswer(new Answer<Album>() {
+ @Override
+ public Album answer(InvocationOnMock invocation) throws Throwable {
+ return albums.get(invocation.getArguments()[0]);
+ }
+ });
+ when(core.getAlbum(anyString(), anyBoolean())).thenAnswer(new Answer<Album>() {
+ @Override
+ public Album answer(InvocationOnMock invocation) throws Throwable {
+ return albums.get(invocation.getArguments()[0]);
+ }
+ });
+ }
+
+ @Before
+ public void setupImages() {
+ Image image = new ImageImpl("image-id");
+ when(core.getImage("image-id")).thenReturn(image);
+ when(core.getImage(eq("image-id"), anyBoolean())).thenReturn(image);
+ }
+
+ @Test
+ 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());
+ }
+