+ @Test
+ public void isModifiedIsTrueIfModificationDetectorSaysSo() {
+ SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
+ when(soneModificationDetector.isModified()).thenReturn(true);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
+ assertThat(soneInserter.isModified(), is(true));
+ }
+
+ @Test
+ public void isModifiedIsFalseIfModificationDetectorSaysSo() {
+ SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
+ assertThat(soneInserter.isModified(), is(false));
+ }
+
+ @Test
+ public void lastFingerprintIsStoredCorrectly() {
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId");
+ soneInserter.setLastInsertFingerprint("last-fingerprint");
+ assertThat(soneInserter.getLastInsertFingerprint(), is("last-fingerprint"));
+ }
+
+ @Test
+ public void soneInserterStopsWhenItShould() {
+ SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId");
+ soneInserter.stop();
+ soneInserter.serviceRun();
+ }
+
+ @Test
+ public void soneInserterInsertsASoneIfItIsEligible() throws SoneException {
+ FreenetURI insertUri = mock(FreenetURI.class);
+ final FreenetURI finalUri = mock(FreenetURI.class);
+ String fingerprint = "fingerprint";
+ Sone sone = createSone(insertUri, fingerprint);
+ 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, "SoneId", soneModificationDetector, 1);
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Throwable {
+ soneInserter.stop();
+ return null;
+ }
+ }).when(core).touchConfiguration();
+ soneInserter.serviceRun();
+ ArgumentCaptor<SoneEvent> soneEvents = ArgumentCaptor.forClass(SoneEvent.class);
+ 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(1), instanceOf(SoneInsertedEvent.class));
+ assertThat(soneEvents.getAllValues().get(1).sone(), is(sone));
+ }
+
+ @Test
+ public void soneInserterBailsOutIfItIsStoppedWhileInserting() throws SoneException {
+ FreenetURI insertUri = mock(FreenetURI.class);
+ final FreenetURI finalUri = mock(FreenetURI.class);
+ String fingerprint = "fingerprint";
+ Sone sone = createSone(insertUri, fingerprint);
+ SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
+ when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
+ 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<FreenetURI>() {
+ @Override
+ public FreenetURI answer(InvocationOnMock invocation) throws Throwable {
+ soneInserter.stop();
+ return finalUri;
+ }
+ });
+ soneInserter.serviceRun();
+ ArgumentCaptor<SoneEvent> soneEvents = ArgumentCaptor.forClass(SoneEvent.class);
+ 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(1), instanceOf(SoneInsertedEvent.class));
+ assertThat(soneEvents.getAllValues().get(1).sone(), is(sone));
+ verify(core, never()).touchConfiguration();
+ }
+
+ @Test
+ public void soneInserterDoesNotInsertSoneIfItIsNotEligible() throws SoneException {
+ FreenetURI insertUri = mock(FreenetURI.class);
+ String fingerprint = "fingerprint";
+ Sone sone = createSone(insertUri, fingerprint);
+ SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
+ final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1);
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException ie1) {
+ throw new RuntimeException(ie1);
+ }
+ soneInserter.stop();
+ }
+ }).start();
+ soneInserter.serviceRun();
+ verify(freenetInterface, never()).insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"));
+ verify(eventBus, never()).post(argThat(org.hamcrest.Matchers.any(SoneEvent.class)));
+ }
+
+ @Test
+ public void soneInserterPostsAbortedEventIfAnExceptionOccurs() throws SoneException {
+ FreenetURI insertUri = mock(FreenetURI.class);
+ String fingerprint = "fingerprint";
+ Sone sone = createSone(insertUri, fingerprint);
+ SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class);
+ when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
+ 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<FreenetURI>() {
+ @Override
+ public FreenetURI answer(InvocationOnMock invocation) throws Throwable {
+ soneInserter.stop();
+ throw soneException;
+ }
+ });
+ soneInserter.serviceRun();
+ ArgumentCaptor<SoneEvent> soneEvents = ArgumentCaptor.forClass(SoneEvent.class);
+ 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(1), instanceOf(SoneInsertAbortedEvent.class));
+ assertThat(soneEvents.getAllValues().get(1).sone(), 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<Optional<Sone>> stopInserterAndThrowException =
+ new Answer<Optional<Sone>>() {
+ @Override
+ public Optional<Sone> answer(
+ InvocationOnMock invocation) {
+ soneInserter.stop();
+ throw new NullPointerException();
+ }
+ };
+ when(soneModificationDetector.isEligibleForInsert()).thenAnswer(
+ stopInserterAndThrowException);
+ 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.getPluginVersion() + "\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());
+ }
+