X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderTest.java;h=b05d2ca86807c1c0463ceffac3164ea3235f7c86;hb=ec06ae64c86f0b06bb0cf9f8b289e7907e81dffa;hp=31e83f793811f3a90ffea39a5296f3610147b686;hpb=b2a3147c056a532558c9bfe676431f4597b7f8eb;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index 31e83f7..b05d2ca 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -1,6 +1,7 @@ package net.pterodactylus.sone.core; import static com.google.common.base.Optional.of; +import static java.util.UUID.randomUUID; 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; @@ -16,6 +17,7 @@ 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,10 +35,8 @@ 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; @@ -45,6 +45,7 @@ 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.PostBuilder; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.freenet.wot.Identity; @@ -55,7 +56,9 @@ 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 +66,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 David ‘Bombe’ Roden */ @@ -71,7 +74,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,6 +83,10 @@ public class SoneDownloaderTest { private final PostReplyBuilder postReplyBuilder = mock(PostReplyBuilder.class); private final Set createdPostReplies = new HashSet(); private PostReply postReply = mock(PostReply.class); + private final AlbumBuilder albumBuilder = mock(AlbumBuilder.class); + private final ListMultimap nestedAlbums = ArrayListMultimap.create(); + private final ListMultimap albumImages = ArrayListMultimap.create(); + private Album album = mock(Album.class); private final Map albums = new HashMap(); @Before @@ -210,16 +217,113 @@ public class SoneDownloaderTest { } @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() { + public void setupAlbum() { + setupAlbum(album); + } + + private void setupAlbum(final Album album) { + when(album.getAlbumImage()).thenReturn(mock(Image.class)); + doAnswer(new Answer() { @Override - public Album answer(InvocationOnMock invocation) throws Throwable { - return albums.get(invocation.getArguments()[0]); + public Void answer(InvocationOnMock invocation) { + nestedAlbums.put(album, (Album) invocation.getArguments()[0]); + return null; + } + }).when(album).addAlbum(any(Album.class)); + doAnswer(new Answer() { + @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>() { + @Override + public List answer(InvocationOnMock invocation) { + return nestedAlbums.get(album); + } + }); + when(album.getImages()).thenAnswer(new Answer>() { + @Override + public List answer(InvocationOnMock invocation) { + return albumImages.get(album); } }); - when(core.getAlbum(anyString(), anyBoolean())).thenAnswer(new Answer() { + 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() { + @Override + public AlbumBuilder answer(InvocationOnMock invocation) { + when(album.getId()).thenReturn((String) invocation.getArguments()[0]); + return albumBuilder; + } + }); + when(albumBuilder.randomId()).thenAnswer(new Answer() { + @Override + public AlbumBuilder answer(InvocationOnMock invocation) { + when(album.getId()).thenReturn(randomUUID().toString()); + return albumBuilder; + } + }); + when(albumBuilder.by(any(Sone.class))).thenAnswer(new Answer() { + @Override + public AlbumBuilder answer(InvocationOnMock invocation) { + when(album.getSone()).thenReturn((Sone) invocation.getArguments()[0]); + return albumBuilder; + } + }); + when(albumBuilder.build()).thenAnswer(new Answer() { + @Override + public Album answer(InvocationOnMock invocation) { + Album album = SoneDownloaderTest.this.album; + albums.put(album.getId(), album); + SoneDownloaderTest.this.album = mock(Album.class); + setupAlbum(SoneDownloaderTest.this.album); + return album; + } + }); + when(core.albumBuilder()).thenReturn(albumBuilder); + } + + @Before + public void setupAlbums() { + when(core.getAlbum(anyString())).thenAnswer(new Answer() { @Override public Album answer(InvocationOnMock invocation) throws Throwable { return albums.get(invocation.getArguments()[0]); @@ -625,26 +729,6 @@ 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); verify(freenetInterface).fetchUri(requestUri);