X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderTest.java;h=0f1c2a2ad790d3f97ba0b3cf20192ee4b6a2f50b;hb=fdffe49b10613e1d9caacdf62ab99bca06edf3e1;hp=6b98ace1c8bf94bab88ecd8b922cad70632f59db;hpb=471c14dabae91b2012eecb2b63be5dccb1229ff7;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 6b98ace..0f1c2a2 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -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; @@ -16,6 +19,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; @@ -31,10 +35,11 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; import net.pterodactylus.sone.core.FreenetInterface.Fetched; 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; @@ -43,17 +48,26 @@ 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; import freenet.client.ClientMetadata; import freenet.client.FetchResult; +import freenet.client.async.ClientContext; +import freenet.client.async.USKCallback; import freenet.keys.FreenetURI; +import freenet.keys.USK; +import freenet.node.RequestStarter; import freenet.support.api.Bucket; +import com.db4o.ObjectContainer; + 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; @@ -78,6 +92,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 @@ -87,6 +105,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 @@ -208,16 +231,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); + } + }); + 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(core.getAlbum(anyString(), anyBoolean())).thenAnswer(new Answer() { + 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]); @@ -235,7 +355,8 @@ public class SoneDownloaderTest { @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); } @@ -243,7 +364,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); } @@ -624,7 +746,8 @@ public class SoneDownloaderTest { @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)); @@ -639,8 +762,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)); @@ -648,9 +770,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); @@ -660,10 +783,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); @@ -675,7 +797,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(); } @@ -690,7 +812,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(); } @@ -699,7 +821,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(); } @@ -707,7 +829,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)); } @@ -716,7 +838,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)); } @@ -726,7 +848,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 {