+ 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 AlbumBuilder albumBuilder = mock(AlbumBuilder.class);
+ private final ListMultimap<Album, Album> nestedAlbums = ArrayListMultimap.create();
+ private final ListMultimap<Album, Image> albumImages = ArrayListMultimap.create();
+ private Album album = mock(Album.class);
+ private final Map<String, Album> albums = new HashMap<String, Album>();
+ private final ImageBuilder imageBuilder = mock(ImageBuilder.class);
+ private Image image = mock(Image.class);
+ private final Map<String, Image> images = new HashMap<String, Image>();
+
+ @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);
+ when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1));
+ }
+
+ private void setupSoneAsUnknown() {
+ when(sone.getTime()).thenReturn(0L);
+ }
+
+ @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 setupAlbum() {
+ final Album album = SoneDownloaderTest.this.album;
+ when(album.getAlbumImage()).thenReturn(mock(Image.class));
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) {
+ nestedAlbums.put(album, (Album) invocation.getArguments()[0]);
+ return null;
+ }
+ }).when(album).addAlbum(any(Album.class));
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) {
+ albumImages.put(album, (Image) invocation.getArguments()[0]);
+ return null;
+ }
+ }).when(album).addImage(any(Image.class));
+ when(album.getAlbums()).thenAnswer(new Answer<List<Album>>() {
+ @Override
+ public List<Album> answer(InvocationOnMock invocation) {
+ return nestedAlbums.get(album);
+ }
+ });
+ when(album.getImages()).thenAnswer(new Answer<List<Image>>() {
+ @Override
+ public List<Image> answer(InvocationOnMock invocation) {
+ return albumImages.get(album);
+ }
+ });
+ final Modifier albumModifier = new Modifier() {
+ private String title = album.getTitle();
+ private String description = album.getDescription();
+ private String imageId = album.getAlbumImage().getId();
+
+ @Override
+ public Modifier setTitle(String title) {
+ this.title = title;
+ return this;
+ }
+
+ @Override
+ public Modifier setDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ @Override
+ public Modifier setAlbumImage(String imageId) {
+ this.imageId = imageId;
+ return this;
+ }
+
+ @Override
+ public Album update() throws IllegalStateException {
+ when(album.getTitle()).thenReturn(title);
+ when(album.getDescription()).thenReturn(description);
+ Image image = mock(Image.class);
+ when(image.getId()).thenReturn(imageId);
+ when(album.getAlbumImage()).thenReturn(image);
+ return album;
+ }
+ };
+ when(album.modify()).thenReturn(albumModifier);
+ }
+
+ @Before
+ public void setupAlbumBuilder() {
+ when(albumBuilder.withId(anyString())).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getId()).thenReturn((String) invocation.getArguments()[0]);
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.randomId()).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getId()).thenReturn(randomUUID().toString());
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.by(any(Sone.class))).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getSone()).thenReturn((Sone) invocation.getArguments()[0]);
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.build()).thenAnswer(new Answer<Album>() {
+ @Override
+ public Album answer(InvocationOnMock invocation) {
+ Album album = SoneDownloaderTest.this.album;
+ albums.put(album.getId(), album);
+ SoneDownloaderTest.this.album = mock(Album.class);
+ setupAlbum();
+ return album;
+ }
+ });
+ when(core.albumBuilder()).thenReturn(albumBuilder);
+ }
+
+ @Before
+ public void setupAlbums() {
+ when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
+ @Override
+ public Album answer(InvocationOnMock invocation) throws Throwable {
+ return albums.get(invocation.getArguments()[0]);
+ }
+ });
+ }
+
+ @Before
+ public void setupImage() {
+ final Image image = SoneDownloaderTest.this.image;
+ Image.Modifier modifier = new Image.Modifier() {
+ private Sone sone = image.getSone();
+ private long creationTime = image.getCreationTime();
+ private String key = image.getKey();
+ private String title = image.getTitle();
+ private String description = image.getDescription();
+ private int width = image.getWidth();
+ private int height = image.getHeight();
+
+ @Override
+ public Image.Modifier setSone(Sone sone) {
+ this.sone = sone;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setCreationTime(long creationTime) {
+ this.creationTime = creationTime;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setKey(String key) {
+ this.key = key;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setTitle(String title) {
+ this.title = title;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setWidth(int width) {
+ this.width = width;
+ return this;
+ }
+
+ @Override
+ public Image.Modifier setHeight(int height) {
+ this.height = height;
+ return this;
+ }
+
+ @Override
+ public Image update() throws IllegalStateException {
+ when(image.getSone()).thenReturn(sone);
+ when(image.getCreationTime()).thenReturn(creationTime);
+ when(image.getKey()).thenReturn(key);
+ when(image.getTitle()).thenReturn(title);
+ when(image.getDescription()).thenReturn(description);
+ when(image.getWidth()).thenReturn(width);
+ when(image.getHeight()).thenReturn(height);
+ return image;
+ }
+ };
+ when(image.getSone()).thenReturn(sone);
+ when(image.modify()).thenReturn(modifier);
+ }
+
+ @Before
+ public void setupImageBuilder() {
+ when(imageBuilder.randomId()).thenAnswer(new Answer<ImageBuilder>() {
+ @Override
+ public ImageBuilder answer(InvocationOnMock invocation) {
+ when(image.getId()).thenReturn(randomUUID().toString());
+ return imageBuilder;
+ }
+ });
+ when(imageBuilder.withId(anyString())).thenAnswer(new Answer<ImageBuilder>() {
+ @Override
+ public ImageBuilder answer(InvocationOnMock invocation) {
+ when(image.getId()).thenReturn(
+ (String) invocation.getArguments()[0]);
+ return imageBuilder;
+ }
+ });
+ when(imageBuilder.build()).thenAnswer(new Answer<Image>() {
+ @Override
+ public Image answer(InvocationOnMock invocation) {
+ Image image = SoneDownloaderTest.this.image;
+ images.put(image.getId(), image);
+ SoneDownloaderTest.this.image = mock(Image.class);
+ setupImage();
+ return image;
+ }
+ });
+ when(core.imageBuilder()).thenReturn(imageBuilder);
+ }
+
+ @Before
+ public void setupImages() {
+ when(core.getImage(anyString())).thenAnswer(new Answer<Image>() {
+ @Override
+ public Image answer(InvocationOnMock invocation)
+ throws Throwable {
+ return images.get(invocation.getArguments()[0]);
+ }
+ });
+ }
+
+ @Test
+ public void addingASoneWillRegisterItsKey() {
+ soneDownloader.addSone(sone);
+ verify(freenetInterface).registerActiveUsk(eq(sone.getRequestUri()), any(
+ USKCallback.class));
+ verify(freenetInterface, never()).unregisterUsk(sone);
+ }
+
+ @Test
+ public void addingASoneTwiceWillAlsoDeregisterItsKey() {
+ soneDownloader.addSone(sone);
+ soneDownloader.addSone(sone);
+ verify(freenetInterface, times(2)).registerActiveUsk(eq(
+ sone.getRequestUri()), any(USKCallback.class));
+ 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 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"));
+ }
+