Don’t set the request URI anymore, either.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneDownloaderTest.java
index 28879bf..127837a 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.core;
 
 import static com.google.common.base.Optional.of;
+import static freenet.keys.InsertableClientSSK.createRandom;
 import static java.lang.System.currentTimeMillis;
 import static java.util.UUID.randomUUID;
 import static java.util.concurrent.TimeUnit.DAYS;
@@ -56,7 +57,10 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import freenet.client.ClientMetadata;
 import freenet.client.FetchResult;
 import freenet.client.async.USKCallback;
+import freenet.crypt.DummyRandomSource;
+import freenet.keys.ClientSSK;
 import freenet.keys.FreenetURI;
+import freenet.keys.InsertableClientSSK;
 import freenet.support.api.Bucket;
 
 import com.google.common.base.Optional;
@@ -79,7 +83,7 @@ public class SoneDownloaderTest {
        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 FreenetURI requestUri = mock(FreenetURI.class);
        private Sone sone = mock(Sone.class);
        private final PostBuilder postBuilder = mock(PostBuilder.class);
        private final List<Post> createdPosts = new ArrayList<Post>();
@@ -100,10 +104,19 @@ public class SoneDownloaderTest {
        public void setupSone() {
                Sone sone = SoneDownloaderTest.this.sone;
                Identity identity = mock(Identity.class);
+               InsertableClientSSK clientSSK = createRandom(new DummyRandomSource(), "WoT");
+               when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString());
                when(identity.getId()).thenReturn("identity");
                when(sone.getId()).thenReturn("identity");
                when(sone.getIdentity()).thenReturn(identity);
-               when(sone.getRequestUri()).thenReturn(requestUri);
+               requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone");
+               when(sone.getRequestUri()).thenAnswer(new Answer<FreenetURI>() {
+                       @Override
+                       public FreenetURI answer(InvocationOnMock invocation)
+                       throws Throwable {
+                               return requestUri;
+                       }
+               });
                when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1));
        }
 
@@ -112,15 +125,6 @@ public class SoneDownloaderTest {
        }
 
        @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 setupSoneBuilder() {
                when(core.soneBuilder()).thenAnswer(new Answer<SoneBuilder>() {
                        @Override
@@ -196,30 +200,42 @@ public class SoneDownloaderTest {
                                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.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 {
@@ -345,7 +361,8 @@ public class SoneDownloaderTest {
        public void setupAlbums() {
                when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
                        @Override
-                       public Album answer(InvocationOnMock invocation) throws Throwable {
+                       public Album answer(InvocationOnMock invocation)
+                       throws Throwable {
                                return albums.get(invocation.getArguments()[0]);
                        }
                });
@@ -536,13 +553,15 @@ public class SoneDownloaderTest {
        @Test
        public void parsingASoneSucceedsWithoutPayload() throws SoneException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream).getTime(), is(1407197508000L));
+               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()));
+               assertThat(soneDownloader.parseSone(sone, inputStream), not(
+                               nullValue()));
        }
 
        @Test
@@ -564,18 +583,6 @@ public class SoneDownloaderTest {
        }
 
        @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();
@@ -637,7 +644,8 @@ public class SoneDownloaderTest {
                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).getRecipientId(), is(of(
+                               "1234567890123456789012345678901234567890123")));
                assertThat(posts.get(0).getText(), is("text"));
        }
 
@@ -700,31 +708,36 @@ public class SoneDownloaderTest {
        @Test
        public void parsingASoneSucceedsWithoutLikedPostIds() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-ids.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+               assertThat(soneDownloader.parseSone(sone, inputStream), not(
+                               nullValue()));
        }
 
        @Test
        public void parsingASoneSucceedsWithLikedPostIds() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-ids.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream).getLikedPostIds(), is((Set<String>) ImmutableSet.of("liked-post-id")));
+               assertThat(soneDownloader.parseSone(sone, inputStream).getLikedPostIds(), is(
+                               (Set<String>) ImmutableSet.of("liked-post-id")));
        }
 
        @Test
        public void parsingASoneSucceedsWithoutLikedPostReplyIds() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-reply-ids.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+               assertThat(soneDownloader.parseSone(sone, inputStream), not(
+                               nullValue()));
        }
 
        @Test
        public void parsingASoneSucceedsWithLikedPostReplyIds() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-reply-ids.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream).getLikedReplyIds(), is((Set<String>) ImmutableSet.of("liked-post-reply-id")));
+               assertThat(soneDownloader.parseSone(sone, inputStream).getLikedReplyIds(), is(
+                               (Set<String>) ImmutableSet.of("liked-post-reply-id")));
        }
 
        @Test
        public void parsingASoneSucceedsWithoutAlbums() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-albums.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+               assertThat(soneDownloader.parseSone(sone, inputStream), not(
+                               nullValue()));
        }
 
        @Test
@@ -766,7 +779,8 @@ public class SoneDownloaderTest {
        @Test
        public void parsingASoneSucceedsWithoutImages() throws SoneException, MalformedURLException {
                InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-images.xml");
-               assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue()));
+               assertThat(soneDownloader.parseSone(sone, inputStream), not(
+                               nullValue()));
        }
 
        @Test
@@ -844,9 +858,11 @@ public class SoneDownloaderTest {
 
        @Test
        public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() {
+               FreenetURI finalRequestUri = requestUri.sskForUSK()
+                               .setMetaString(new String[] { "sone.xml" });
                setupSoneAsUnknown();
                soneDownloader.fetchSoneAction(sone).run();
-               verify(freenetInterface).fetchUri(requestUri);
+               verify(freenetInterface).fetchUri(finalRequestUri);
                verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
                verify(core, never()).updateSone(any(Sone.class));
        }
@@ -860,20 +876,24 @@ public class SoneDownloaderTest {
 
        @Test
        public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() {
+               FreenetURI finalRequestUri = requestUri.sskForUSK()
+                               .setMetaString(new String[] { "sone.xml" });
                soneDownloader.fetchSoneAction(sone).run();
-               verify(freenetInterface).fetchUri(requestUri);
+               verify(freenetInterface).fetchUri(finalRequestUri);
                verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
                verify(core, never()).updateSone(any(Sone.class));
        }
 
        @Test(expected = NullPointerException.class)
        public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() {
+               FreenetURI finalRequestUri = requestUri.sskForUSK()
+                               .setMetaString(new String[] { "sone.xml" });
                setupSoneAsUnknown();
-               when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
+               when(freenetInterface.fetchUri(finalRequestUri)).thenThrow(NullPointerException.class);
                try {
                        soneDownloader.fetchSoneAction(sone).run();
                } finally {
-                       verify(freenetInterface).fetchUri(requestUri);
+                       verify(freenetInterface).fetchUri(finalRequestUri);
                        verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
                        verify(core, never()).updateSone(any(Sone.class));
                }
@@ -881,11 +901,13 @@ public class SoneDownloaderTest {
 
        @Test(expected = NullPointerException.class)
        public void exceptionWhileFetchingAKnownSoneDoesNotUpdateCore() {
-               when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
+               FreenetURI finalRequestUri = requestUri.sskForUSK()
+                               .setMetaString(new String[] { "sone.xml" });
+               when(freenetInterface.fetchUri(finalRequestUri)).thenThrow( NullPointerException.class);
                try {
                        soneDownloader.fetchSoneAction(sone).run();
                } finally {
-                       verify(freenetInterface).fetchUri(requestUri);
+                       verify(freenetInterface).fetchUri(finalRequestUri);
                        verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
                        verify(core, never()).updateSone(any(Sone.class));
                }
@@ -893,8 +915,11 @@ public class SoneDownloaderTest {
 
        @Test
        public void successfulFetchingOfSoneWithUskRequestUriUpdatesTheCoreWithASone() throws IOException {
-               final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
-               when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
+               FreenetURI finalRequestUri = requestUri.sskForUSK()
+                               .setMetaString(new String[] { "sone.xml" });
+               final Fetched fetchResult = createFetchResult(finalRequestUri,
+                               getClass().getResourceAsStream("sone-parser-no-payload.xml"));
+               when(freenetInterface.fetchUri(finalRequestUri)).thenReturn(fetchResult);
                soneDownloader.fetchSoneAction(sone).run();
                verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
        }
@@ -906,24 +931,6 @@ public class SoneDownloaderTest {
        }
 
        @Test
-       public void successfulFetchingOfSoneWithSskRequestUriUpdatesTheCoreWithASone() throws IOException {
-               when(requestUri.getKeyType()).thenReturn("SSK");
-               final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
-               when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
-               soneDownloader.fetchSoneAction(sone).run();
-               verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
-       }
-
-       @Test
-       public void successfulFetchingAnUnknownSoneWithSskRequestUriUpdatesTheCoreWithASone() throws IOException {
-               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.fetchSoneAction(sone).run();
-               verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
-       }
-
-       @Test
        public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException {
                final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml"));
                when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);