X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserterTest.java;h=f9905000f7f3622c0535c7f0a933d9916922b1cf;hb=b5773d0578a01a950e0ff48e1f6631667ddfa998;hp=b303a1c9436466168601397bee871f3748e02e46;hpb=c74a6b50636ab4c13967ac90fa4155eea028b836;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java
index b303a1c..f990500 100644
--- a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java
@@ -1,32 +1,44 @@
package net.pterodactylus.sone.core;
+import static com.google.common.io.ByteStreams.toByteArray;
+import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
+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.mockito.Matchers.any;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.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;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+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.SetInsertionDelay;
+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;
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.keys.FreenetURI;
+import freenet.support.api.ManifestElement;
+import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
+import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.junit.Before;
import org.junit.Test;
@@ -36,8 +48,6 @@ import org.mockito.stubbing.Answer;
/**
* Unit test for {@link SoneInserter} and its subclasses.
- *
- * @author David âBombeâ Roden
*/
public class SoneInserterTest {
@@ -49,80 +59,51 @@ public class SoneInserterTest {
public void setupCore() {
UpdateChecker updateChecker = mock(UpdateChecker.class);
when(core.getUpdateChecker()).thenReturn(updateChecker);
+ when(core.getSone(anyString())).thenReturn(null);
}
@Test
public void insertionDelayIsForwardedToSoneInserter() {
- SetInsertionDelay setInsertionDelay = new SetInsertionDelay();
- setInsertionDelay.optionChanged(null, null, 15);
+ EventBus eventBus = new AsyncEventBus(directExecutor());
+ eventBus.register(new SoneInserter(core, eventBus, freenetInterface, "SoneId"));
+ eventBus.post(new InsertionDelayChangedEvent(15));
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, sone);
- InsertInformation insertInformation = soneInserter.new InsertInformation(sone);
- HashMap manifestEntries = insertInformation.generateManifestEntries();
- assertThat(manifestEntries.keySet(), containsInAnyOrder("index.html", "sone.xml"));
- assertThat(insertInformation.getInsertUri(), is(insertUri));
- assertThat(insertInformation.getFingerprint(), is(fingerprint));
- }
-
private Sone createSone(FreenetURI insertUri, String fingerprint) {
Sone sone = mock(Sone.class);
when(sone.getInsertUri()).thenReturn(insertUri);
when(sone.getFingerprint()).thenReturn(fingerprint);
when(sone.getRootAlbum()).thenReturn(mock(Album.class));
+ when(core.getSone(anyString())).thenReturn(sone);
return sone;
}
- @Test(expected = IllegalArgumentException.class)
- public void soneOfSoneInserterCanNotBeSetToADifferentSone() {
- Sone sone = mock(Sone.class);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone);
- soneInserter.setSone(mock(Sone.class));
- }
-
- @Test
- public void soneCanBeSetToEqualSone() {
- Sone sone = mock(Sone.class);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone);
- soneInserter.setSone(sone);
- }
-
@Test
public void isModifiedIsTrueIfModificationDetectorSaysSo() {
- Sone sone = mock(Sone.class);
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
when(soneModificationDetector.isModified()).thenReturn(true);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
assertThat(soneInserter.isModified(), is(true));
}
@Test
public void isModifiedIsFalseIfModificationDetectorSaysSo() {
- Sone sone = mock(Sone.class);
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
assertThat(soneInserter.isModified(), is(false));
}
@Test
public void lastFingerprintIsStoredCorrectly() {
- Sone sone = mock(Sone.class);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId");
soneInserter.setLastInsertFingerprint("last-fingerprint");
assertThat(soneInserter.getLastInsertFingerprint(), is("last-fingerprint"));
}
@Test
public void soneInserterStopsWhenItShould() {
- Sone sone = mock(Sone.class);
- SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId");
soneInserter.stop();
soneInserter.serviceRun();
}
@@ -136,7 +117,7 @@ public class SoneInserterTest {
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenReturn(finalUri);
- final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
@@ -149,9 +130,9 @@ public class SoneInserterTest {
verify(freenetInterface).insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"));
verify(eventBus, times(2)).post(soneEvents.capture());
assertThat(soneEvents.getAllValues().get(0), instanceOf(SoneInsertingEvent.class));
- assertThat(soneEvents.getAllValues().get(0).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(0).getSone(), is(sone));
assertThat(soneEvents.getAllValues().get(1), instanceOf(SoneInsertedEvent.class));
- assertThat(soneEvents.getAllValues().get(1).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(1).getSone(), is(sone));
}
@Test
@@ -162,7 +143,7 @@ public class SoneInserterTest {
Sone sone = createSone(insertUri, fingerprint);
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
- final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenAnswer(new Answer() {
@Override
public FreenetURI answer(InvocationOnMock invocation) throws Throwable {
@@ -175,9 +156,9 @@ public class SoneInserterTest {
verify(freenetInterface).insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"));
verify(eventBus, times(2)).post(soneEvents.capture());
assertThat(soneEvents.getAllValues().get(0), instanceOf(SoneInsertingEvent.class));
- assertThat(soneEvents.getAllValues().get(0).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(0).getSone(), is(sone));
assertThat(soneEvents.getAllValues().get(1), instanceOf(SoneInsertedEvent.class));
- assertThat(soneEvents.getAllValues().get(1).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(1).getSone(), is(sone));
verify(core, never()).touchConfiguration();
}
@@ -187,7 +168,7 @@ public class SoneInserterTest {
String fingerprint = "fingerprint";
Sone sone = createSone(insertUri, fingerprint);
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
- final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
new Thread(new Runnable() {
@Override
public void run() {
@@ -211,7 +192,7 @@ public class SoneInserterTest {
Sone sone = createSone(insertUri, fingerprint);
SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
- final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, sone, soneModificationDetector);
+ final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
final SoneException soneException = new SoneException(new Exception());
when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenAnswer(new Answer() {
@Override
@@ -225,10 +206,81 @@ public class SoneInserterTest {
verify(freenetInterface).insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"));
verify(eventBus, times(2)).post(soneEvents.capture());
assertThat(soneEvents.getAllValues().get(0), instanceOf(SoneInsertingEvent.class));
- assertThat(soneEvents.getAllValues().get(0).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(0).getSone(), is(sone));
assertThat(soneEvents.getAllValues().get(1), instanceOf(SoneInsertAbortedEvent.class));
- assertThat(soneEvents.getAllValues().get(1).sone(), is(sone));
+ assertThat(soneEvents.getAllValues().get(1).getSone(), is(sone));
verify(core, never()).touchConfiguration();
}
+ @Test
+ public void soneInserterExitsIfSoneIsUnknown() {
+ SoneModificationDetector soneModificationDetector =
+ mock(SoneModificationDetector.class);
+ SoneInserter soneInserter =
+ new SoneInserter(core, eventBus, freenetInterface, "SoneId",
+ soneModificationDetector, 1);
+ when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
+ when(core.getSone("SoneId")).thenReturn(null);
+ soneInserter.serviceRun();
+ }
+
+ @Test
+ public void soneInserterCatchesExceptionAndContinues() {
+ SoneModificationDetector soneModificationDetector =
+ mock(SoneModificationDetector.class);
+ final SoneInserter soneInserter =
+ new SoneInserter(core, eventBus, freenetInterface, "SoneId",
+ soneModificationDetector, 1);
+ Answer> stopInserterAndThrowException =
+ new Answer>() {
+ @Override
+ public Optional answer(
+ InvocationOnMock invocation) {
+ soneInserter.stop();
+ throw new NullPointerException();
+ }
+ };
+ when(soneModificationDetector.isEligibleForInsert()).thenAnswer(
+ stopInserterAndThrowException);
+ soneInserter.serviceRun();
+ }
+
+ @Test
+ public void templateIsRenderedCorrectlyForManifestElement()
+ throws IOException {
+ Map soneProperties = new HashMap<>();
+ 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.getPluginVersion() + "\n"));
+ assertThat(templateContent, containsString("Core Startup: " + now + "\n"));
+ assertThat(templateContent, containsString("Sone ID: " + "SoneId" + "\n"));
+ }
+
+ @Test
+ public void invalidTemplateReturnsANullManifestElement() {
+ Map soneProperties = new HashMap<>();
+ 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 soneProperties = new HashMap<>();
+ 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());
+ }
+
}