Don’t set the request URI anymore, either.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 11:56:36 +0000 (13:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 11:56:36 +0000 (13:56 +0200)
It can also be calculcated from the identity’s key, together with the
latest edition of the Sone.

src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/SoneImpl.java
src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java
src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java
src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml [deleted file]
src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml [deleted file]

index ef05eba..26f9def 100644 (file)
@@ -736,7 +736,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
                boolean newSone = !existingSone.isPresent();
                Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
-               sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
                sone.setLatestEdition(latestEdition);
                if (newSone) {
                        synchronized (knownSones) {
index 28d4521..d4ed927 100644 (file)
@@ -236,11 +236,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                        Sone parsedSone = parseSone(originalSone, soneInputStream);
                        if (parsedSone != null) {
                                parsedSone.setLatestEdition(requestUri.getEdition());
-                               if (requestUri.getKeyType().equals("USK")) {
-                                       parsedSone.setRequestUri(requestUri.setMetaString(new String[0]));
-                               } else {
-                                       parsedSone.setRequestUri(requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]));
-                               }
                        }
                        return parsedSone;
                } catch (Exception e1) {
@@ -340,17 +335,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                        sone.setClient(new Client(clientName, clientVersion));
                }
 
-               String soneRequestUri = soneXml.getValue("request-uri", null);
-               if (soneRequestUri != null) {
-                       try {
-                               sone.setRequestUri(new FreenetURI(soneRequestUri));
-                       } catch (MalformedURLException mue1) {
-                               /* TODO - mark Sone as bad. */
-                               logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid request URI: %s", sone, soneRequestUri), mue1);
-                               return null;
-                       }
-               }
-
                SimpleXML profileXml = soneXml.getNode("profile");
                if (profileXml == null) {
                        /* TODO - mark Sone as bad. */
index 3b36f46..b23e48e 100644 (file)
@@ -228,15 +228,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        FreenetURI getRequestUri();
 
        /**
-        * Sets the request URI of this Sone.
-        *
-        * @param requestUri
-        *              The request URI of this Sone
-        * @return This Sone (for method chaining)
-        */
-       Sone setRequestUri(FreenetURI requestUri);
-
-       /**
         * Returns the insert URI of this Sone.
         *
         * @return The insert URI of this Sone
index 08c104f..86c5935 100644 (file)
@@ -167,26 +167,17 @@ public class SoneImpl implements Sone {
         * @return The request URI of this Sone
         */
        public FreenetURI getRequestUri() {
-               return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
-       }
-
-       /**
-        * Sets the request URI of this Sone.
-        *
-        * @param requestUri
-        *              The request URI of this Sone
-        * @return This Sone (for method chaining)
-        */
-       public Sone setRequestUri(FreenetURI requestUri) {
-               if (this.requestUri == null) {
-                       this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
-                       return this;
-               }
-               if (!this.requestUri.equalsKeypair(requestUri)) {
-                       logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
-                       return this;
+               try {
+                       return new FreenetURI(getIdentity().getRequestUri())
+                                       .setKeyType("USK")
+                                       .setDocName("Sone")
+                                       .setMetaString(new String[0])
+                                       .setSuggestedEdition(latestEdition);
+               } catch (MalformedURLException e) {
+                       throw new IllegalStateException(
+                                       format("Identity %s's request URI is incorrect.",
+                                                       getIdentity()), e);
                }
-               return this;
        }
 
        /**
index 1cc1801..fa7aace 100644 (file)
@@ -53,11 +53,6 @@ public class IdOnlySone implements Sone {
        }
 
        @Override
-       public Sone setRequestUri(FreenetURI requestUri) {
-               return null;
-       }
-
-       @Override
        public FreenetURI getInsertUri() {
                return null;
        }
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);
diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml b/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml
deleted file mode 100644 (file)
index 5bcb0cc..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<sone>
-       <protocol-version>0</protocol-version>
-       <time>1407197508000</time>
-       <request-uri>invalid-request-uri</request-uri>
-       <profile></profile>
-</sone>
diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml b/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml
deleted file mode 100644 (file)
index 5271c34..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<sone>
-       <protocol-version>0</protocol-version>
-       <time>1407197508000</time>
-       <request-uri>USK@w~RyTGmv12Lg9oO91q1Untupi7my9qczT1RheGkEkVE,E8ElVfUgukSCPHxIEJp-gHMiR80wpM7sID3Jo5O7w1s,AQACAAE/Sone/0</request-uri>
-       <profile></profile>
-</sone>