Extract manifest element creation into its own class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 3 Oct 2014 18:45:29 +0000 (20:45 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 3 Oct 2014 18:45:29 +0000 (20:45 +0200)
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java
src/test/resources/net/pterodactylus/sone/core/sone-inserter-faulty-manifest.txt [new file with mode: 0644]
src/test/resources/net/pterodactylus/sone/core/sone-inserter-invalid-manifest.txt [new file with mode: 0644]
src/test/resources/net/pterodactylus/sone/core/sone-inserter-manifest.txt [new file with mode: 0644]

index 8c30bfd..f544ef2 100644 (file)
@@ -283,9 +283,7 @@ public class SoneInserter extends AbstractService {
        class InsertInformation {
 
                private final String fingerprint;
-
-               /** All properties of the Sone, copied for thread safety. */
-               private final Map<String, Object> soneProperties = new HashMap<String, Object>();
+               private final ManifestCreator manifestCreator;
 
                /**
                 * Creates a new insert information container.
@@ -295,6 +293,7 @@ public class SoneInserter extends AbstractService {
                 */
                public InsertInformation(Sone sone) {
                        this.fingerprint = sone.getFingerprint();
+                       Map<String, Object> soneProperties = new HashMap<String, Object>();
                        soneProperties.put("id", sone.getId());
                        soneProperties.put("name", sone.getName());
                        soneProperties.put("time", currentTimeMillis());
@@ -305,6 +304,7 @@ public class SoneInserter extends AbstractService {
                        soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
                        soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
                        soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
+                       manifestCreator = new ManifestCreator(core, soneProperties);
                }
 
                //
@@ -329,31 +329,37 @@ public class SoneInserter extends AbstractService {
                        HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
 
                        /* first, create an index.html. */
-                       manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html"));
+                       manifestEntries.put("index.html", manifestCreator.createManifestElement(
+                                       "index.html", "text/html; charset=utf-8",
+                                       "/templates/insert/index.html"));
 
                        /* now, store the sone. */
-                       manifestEntries.put("sone.xml", createManifestElement("sone.xml", "text/xml; charset=utf-8", "/templates/insert/sone.xml"));
+                       manifestEntries.put("sone.xml", manifestCreator.createManifestElement(
+                                       "sone.xml", "text/xml; charset=utf-8",
+                                       "/templates/insert/sone.xml"));
 
                        return manifestEntries;
                }
 
-               //
-               // PRIVATE METHODS
-               //
+       }
 
-               /**
-                * Creates a new manifest element.
-                *
-                * @param name
-                *            The name of the file
-                * @param contentType
-                *            The content type of the file
-                * @param templateName
-                *            The name of the template to render
-                * @return The manifest element
-                */
-               @SuppressWarnings("synthetic-access")
-               private ManifestElement createManifestElement(String name, String contentType, String templateName) {
+       /**
+        * Creates manifest elements for an insert by rendering a template.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       @VisibleForTesting
+       static class ManifestCreator {
+
+               private final Core core;
+               private final Map<String, Object> soneProperties;
+
+               ManifestCreator(Core core, Map<String, Object> soneProperties) {
+                       this.core = core;
+                       this.soneProperties = soneProperties;
+               }
+
+               public ManifestElement createManifestElement(String name, String contentType, String templateName) {
                        InputStreamReader templateInputStreamReader = null;
                        InputStream templateInputStream = null;
                        Template template;
index c44ced6..e0ff3a5 100644 (file)
@@ -1,27 +1,32 @@
 package net.pterodactylus.sone.core;
 
-import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.of;
+import static com.google.common.io.ByteStreams.toByteArray;
 import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
+import static java.lang.System.currentTimeMillis;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.instanceOf;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.io.IOException;
 import java.util.HashMap;
+import java.util.Map;
 
 import net.pterodactylus.sone.core.SoneInserter.InsertInformation;
+import net.pterodactylus.sone.core.SoneInserter.ManifestCreator;
 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
 import net.pterodactylus.sone.core.event.SoneEvent;
 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
@@ -29,9 +34,12 @@ import net.pterodactylus.sone.core.event.SoneInsertedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.main.SonePlugin;
 
+import freenet.client.async.ManifestElement;
 import freenet.keys.FreenetURI;
 
+import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 import com.google.common.eventbus.AsyncEventBus;
 import com.google.common.eventbus.EventBus;
@@ -67,19 +75,6 @@ public class SoneInserterTest {
                assertThat(SoneInserter.getInsertionDelay().get(), is(15));
        }
 
-       @Test
-       /* this test is hilariously bad. */
-       public void manifestEntriesAreCreated() {
-               FreenetURI insertUri = mock(FreenetURI.class);
-               String fingerprint = "fingerprint";
-               Sone sone = createSone(insertUri, fingerprint);
-               SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId");
-               InsertInformation insertInformation = soneInserter.new InsertInformation(sone);
-               HashMap<String, Object> manifestEntries = insertInformation.generateManifestEntries();
-               assertThat(manifestEntries.keySet(), containsInAnyOrder("index.html", "sone.xml"));
-               assertThat(insertInformation.getFingerprint(), is(fingerprint));
-       }
-
        private Sone createSone(FreenetURI insertUri, String fingerprint) {
                Sone sone = mock(Sone.class);
                when(sone.getInsertUri()).thenReturn(insertUri);
@@ -255,4 +250,42 @@ public class SoneInserterTest {
                soneInserter.serviceRun();
        }
 
+       @Test
+       public void templateIsRenderedCorrectlyForManifestElement()
+       throws IOException {
+               Map<String, Object> soneProperties = new HashMap<String, Object>();
+               soneProperties.put("id", "SoneId");
+               ManifestCreator manifestCreator = new ManifestCreator(core, soneProperties);
+               long now = currentTimeMillis();
+               when(core.getStartupTime()).thenReturn(now);
+               ManifestElement manifestElement = manifestCreator.createManifestElement("test.txt", "plain/text; charset=utf-8", "sone-inserter-manifest.txt");
+               assertThat(manifestElement.getName(), is("test.txt"));
+               assertThat(manifestElement.getMimeTypeOverride(), is("plain/text; charset=utf-8"));
+               String templateContent = new String(toByteArray(manifestElement.getData().getInputStream()), Charsets.UTF_8);
+               assertThat(templateContent, containsString("Sone Version: " + SonePlugin.VERSION.toString() + "\n"));
+               assertThat(templateContent, containsString("Core Startup: " + now + "\n"));
+               assertThat(templateContent, containsString("Sone ID: " + "SoneId" + "\n"));
+       }
+
+       @Test
+       public void invalidTemplateReturnsANullManifestElement() {
+               Map<String, Object> soneProperties = new HashMap<String, Object>();
+               ManifestCreator manifestCreator = new ManifestCreator(core, soneProperties);
+               assertThat(manifestCreator.createManifestElement("test.txt",
+                               "plain/text; charset=utf-8",
+                               "sone-inserter-invalid-manifest.txt"),
+                               nullValue());
+       }
+
+       @Test
+       public void errorWhileRenderingTemplateReturnsANullManifestElement() {
+               Map<String, Object> soneProperties = new HashMap<String, Object>();
+               ManifestCreator manifestCreator = new ManifestCreator(core, soneProperties);
+               when(core.toString()).thenThrow(NullPointerException.class);
+               assertThat(manifestCreator.createManifestElement("test.txt",
+                               "plain/text; charset=utf-8",
+                               "sone-inserter-faulty-manifest.txt"),
+                               nullValue());
+       }
+
 }
diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-inserter-faulty-manifest.txt b/src/test/resources/net/pterodactylus/sone/core/sone-inserter-faulty-manifest.txt
new file mode 100644 (file)
index 0000000..7d39bdc
--- /dev/null
@@ -0,0 +1 @@
+<%include stuff>
diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-inserter-invalid-manifest.txt b/src/test/resources/net/pterodactylus/sone/core/sone-inserter-invalid-manifest.txt
new file mode 100644 (file)
index 0000000..625c86f
--- /dev/null
@@ -0,0 +1 @@
+Sone Version: <% version %>
diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-inserter-manifest.txt b/src/test/resources/net/pterodactylus/sone/core/sone-inserter-manifest.txt
new file mode 100644 (file)
index 0000000..a5818bf
--- /dev/null
@@ -0,0 +1,3 @@
+Sone Version: <% version>
+Core Startup: <% core.startupTime>
+Sone ID: <% currentSone.id>