Don’t set insert URI of a Sone, let it be generated from the identity.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneDownloaderTest.java
index 31e83f7..c910233 100644 (file)
@@ -1,6 +1,9 @@
 package net.pterodactylus.sone.core;
 
 import static com.google.common.base.Optional.of;
+import static java.lang.System.currentTimeMillis;
+import static java.util.UUID.randomUUID;
+import static java.util.concurrent.TimeUnit.DAYS;
 import static net.pterodactylus.sone.data.Sone.SoneStatus.downloading;
 import static net.pterodactylus.sone.data.Sone.SoneStatus.idle;
 import static net.pterodactylus.sone.data.Sone.SoneStatus.unknown;
@@ -12,10 +15,10 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.ArgumentCaptor.forClass;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.anyLong;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
@@ -33,29 +36,31 @@ import java.util.Map;
 import java.util.Set;
 
 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
-import net.pterodactylus.sone.core.SoneDownloader.FetchSone;
-import net.pterodactylus.sone.core.SoneDownloader.FetchSoneWithUri;
 import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.AlbumImpl;
+import net.pterodactylus.sone.data.Album.Modifier;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.ImageImpl;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.ImageBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 
 import freenet.client.ClientMetadata;
 import freenet.client.FetchResult;
+import freenet.client.async.USKCallback;
 import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ListMultimap;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -63,7 +68,7 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 /**
- * Unit test for {@link SoneDownloader} and its subclasses.
+ * Unit test for {@link SoneDownloaderImpl} and its subclasses.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -71,7 +76,7 @@ public class SoneDownloaderTest {
 
        private final Core core = mock(Core.class);
        private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
-       private final SoneDownloader soneDownloader = new SoneDownloader(core, freenetInterface);
+       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);
@@ -80,7 +85,14 @@ public class SoneDownloaderTest {
        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() {
@@ -89,6 +101,11 @@ public class SoneDownloaderTest {
                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
@@ -210,34 +227,233 @@ public class SoneDownloaderTest {
        }
 
        @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() {
-               albums.put("album-id-1", new AlbumImpl("album-id-1"));
-               albums.put("album-id-2", new AlbumImpl("album-id-2"));
                when(core.getAlbum(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>() {
+       }
+
+       @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 Album answer(InvocationOnMock invocation) throws Throwable {
-                               return albums.get(invocation.getArguments()[0]);
+                       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() {
-               Image image = new ImageImpl("image-id");
-               when(core.getImage("image-id")).thenReturn(image);
-               when(core.getImage(eq("image-id"), anyBoolean())).thenReturn(image);
+               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).registerUsk(sone, soneDownloader);
+               verify(freenetInterface).registerActiveUsk(eq(sone.getRequestUri()), any(
+                               USKCallback.class));
                verify(freenetInterface, never()).unregisterUsk(sone);
        }
 
@@ -245,7 +461,8 @@ public class SoneDownloaderTest {
        public void addingASoneTwiceWillAlsoDeregisterItsKey() {
                soneDownloader.addSone(sone);
                soneDownloader.addSone(sone);
-               verify(freenetInterface, times(2)).registerUsk(sone, soneDownloader);
+               verify(freenetInterface, times(2)).registerActiveUsk(eq(
+                               sone.getRequestUri()), any(USKCallback.class));
                verify(freenetInterface).unregisterUsk(sone);
        }
 
@@ -346,18 +563,6 @@ public class SoneDownloaderTest {
        }
 
        @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();
@@ -625,28 +830,9 @@ public class SoneDownloaderTest {
        }
 
        @Test
-       public void fetchSoneWithUriDownloadsSoneWithUri() {
-               SoneDownloader soneDownloader = mock(SoneDownloader.class);
-               Sone sone = mock(Sone.class);
-               FreenetURI soneUri = mock(FreenetURI.class);
-               when(sone.getRequestUri()).thenReturn(soneUri);
-               FetchSoneWithUri fetchSoneWithUri = soneDownloader.new FetchSoneWithUri(sone);
-               fetchSoneWithUri.run();
-               verify(soneDownloader).fetchSone(eq(sone), eq(soneUri));
-       }
-
-       @Test
-       public void fetchSoneDownloadsSone() {
-               SoneDownloader soneDownloader = mock(SoneDownloader.class);
-               Sone sone = mock(Sone.class);
-               FetchSone fetchSone = soneDownloader.new FetchSone(sone);
-               fetchSone.run();
-               verify(soneDownloader).fetchSone(eq(sone));
-       }
-
-       @Test
        public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() {
-               soneDownloader.fetchSone(sone);
+               setupSoneAsUnknown();
+               soneDownloader.fetchSoneAction(sone).run();
                verify(freenetInterface).fetchUri(requestUri);
                verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
                verify(core, never()).updateSone(any(Sone.class));
@@ -661,8 +847,7 @@ public class SoneDownloaderTest {
 
        @Test
        public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() {
-               when(sone.getTime()).thenReturn(1000L);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verify(freenetInterface).fetchUri(requestUri);
                verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
                verify(core, never()).updateSone(any(Sone.class));
@@ -670,9 +855,10 @@ public class SoneDownloaderTest {
 
        @Test(expected = NullPointerException.class)
        public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() {
+               setupSoneAsUnknown();
                when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
                try {
-                       soneDownloader.fetchSone(sone);
+                       soneDownloader.fetchSoneAction(sone).run();
                } finally {
                        verify(freenetInterface).fetchUri(requestUri);
                        verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
@@ -682,10 +868,9 @@ public class SoneDownloaderTest {
 
        @Test(expected = NullPointerException.class)
        public void exceptionWhileFetchingAKnownSoneDoesNotUpdateCore() {
-               when(sone.getTime()).thenReturn(1000L);
                when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
                try {
-                       soneDownloader.fetchSone(sone);
+                       soneDownloader.fetchSoneAction(sone).run();
                } finally {
                        verify(freenetInterface).fetchUri(requestUri);
                        verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
@@ -697,7 +882,7 @@ public class SoneDownloaderTest {
        public void successfulFetchingOfSoneWithUskRequestUriUpdatesTheCoreWithASone() throws IOException {
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
        }
 
@@ -712,7 +897,7 @@ public class SoneDownloaderTest {
                when(requestUri.getKeyType()).thenReturn("SSK");
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
        }
 
@@ -721,7 +906,7 @@ public class SoneDownloaderTest {
                when(requestUri.getKeyType()).thenReturn("SSK");
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-with-zero-time.xml"));
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
        }
 
@@ -729,7 +914,7 @@ public class SoneDownloaderTest {
        public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException {
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml"));
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verify(core, never()).updateSone(any(Sone.class));
        }
 
@@ -738,7 +923,7 @@ public class SoneDownloaderTest {
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
                when(sone.getId()).thenThrow(NullPointerException.class);
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSone(sone);
+               soneDownloader.fetchSoneAction(sone).run();
                verify(core, never()).updateSone(any(Sone.class));
        }
 
@@ -748,7 +933,7 @@ public class SoneDownloaderTest {
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
                soneDownloader.fetchSone(sone, sone.getRequestUri(), true);
                verify(core, never()).updateSone(any(Sone.class));
-               verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
+               verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
        }
 
        private Fetched createFetchResult(FreenetURI uri, InputStream inputStream) throws IOException {