+ @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(Optional.<Sone>absent());
+ 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.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());
+ }
+