🎨 Replace SoneRescuerTest with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Jul 2019 20:25:27 +0000 (22:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Jul 2019 20:25:27 +0000 (22:25 +0200)
src/test/java/net/pterodactylus/sone/core/SoneRescuerTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/core/SoneRescuerTest.java b/src/test/java/net/pterodactylus/sone/core/SoneRescuerTest.java
deleted file mode 100644 (file)
index 92748e8..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-package net.pterodactylus.sone.core;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.mockito.ArgumentMatchers.any;
-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.verify;
-import static org.mockito.Mockito.when;
-
-import net.pterodactylus.sone.data.Sone;
-
-import freenet.keys.FreenetURI;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-/**
- * Unit test for {@link SoneRescuer}.
- */
-public class SoneRescuerTest {
-
-       private static final long CURRENT_EDITION = 12L;
-       private final Core core = mock(Core.class);
-       private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
-       private final Sone sone = mock(Sone.class);
-       private SoneRescuer soneRescuer;
-
-       @Before
-       public void setupSone() {
-               FreenetURI soneUri = mock(FreenetURI.class);
-               when(soneUri.getEdition()).thenReturn(CURRENT_EDITION);
-               when(sone.getRequestUri()).thenReturn(soneUri);
-       }
-
-       @Before
-       public void setupSoneRescuer() {
-               soneRescuer = new SoneRescuer(core, soneDownloader, sone);
-       }
-
-       @Test
-       public void newSoneRescuerIsNotFetchingAnything() {
-               assertThat(soneRescuer.isFetching(), is(false));
-       }
-
-       @Test
-       public void newSoneRescuerStartsAtCurrentEditionOfSone() {
-               assertThat(soneRescuer.getCurrentEdition(), is(CURRENT_EDITION));
-       }
-
-       @Test
-       public void newSoneRescuerHasANextEditionToGet() {
-               assertThat(soneRescuer.hasNextEdition(), is(true));
-       }
-
-       @Test
-       public void soneRescuerDoesNotHaveANextEditionIfCurrentEditionIsZero() {
-               when(sone.getRequestUri().getEdition()).thenReturn(0L);
-               soneRescuer = new SoneRescuer(core, soneDownloader, sone);
-               assertThat(soneRescuer.hasNextEdition(), is(false));
-       }
-
-       @Test
-       public void nextEditionIsOneSmallerThanTheCurrentEdition() {
-               assertThat(soneRescuer.getNextEdition(), is(CURRENT_EDITION - 1));
-       }
-
-       @Test
-       public void lastFetchOfANewSoneRescuerWasSuccessful() {
-               assertThat(soneRescuer.isLastFetchSuccessful(), is(true));
-       }
-
-       @Test
-       public void mainLoopStopsWhenItShould() {
-               soneRescuer.stop();
-               soneRescuer.serviceRun();
-       }
-
-       @Test
-       public void successfulInsert() {
-               final Sone fetchedSone = mock(Sone.class);
-               returnUriOnInsert(fetchedSone);
-               soneRescuer.startNextFetch();
-               soneRescuer.serviceRun();
-               verify(core).lockSone(eq(sone));
-               verify(core).updateSone(eq(fetchedSone), eq(true));
-               assertThat(soneRescuer.isLastFetchSuccessful(), is(true));
-               assertThat(soneRescuer.isFetching(), is(false));
-       }
-
-       @Test
-       public void nonSuccessfulInsertIsRecognized() {
-               returnUriOnInsert(null);
-               soneRescuer.startNextFetch();
-               soneRescuer.serviceRun();
-               verify(core).lockSone(eq(sone));
-               verify(core, never()).updateSone(any(Sone.class), eq(true));
-               assertThat(soneRescuer.isLastFetchSuccessful(), is(false));
-               assertThat(soneRescuer.isFetching(), is(false));
-       }
-
-       private void returnUriOnInsert(final Sone fetchedSone) {
-               FreenetURI keyWithMetaStrings = setupFreenetUri();
-               doAnswer(new Answer<Sone>() {
-                       @Override
-                       public Sone answer(InvocationOnMock invocation) throws Throwable {
-                               soneRescuer.stop();
-                               return fetchedSone;
-                       }
-               }).when(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true));
-       }
-
-       private FreenetURI setupFreenetUri() {
-               FreenetURI sskKey = mock(FreenetURI.class);
-               FreenetURI keyWithDocName = mock(FreenetURI.class);
-               FreenetURI keyWithMetaStrings = mock(FreenetURI.class);
-               when(keyWithDocName.setMetaString(eq(new String[] { "sone.xml" }))).thenReturn(keyWithMetaStrings);
-               when(sskKey.setDocName(eq("Sone-" + (CURRENT_EDITION - 1)))).thenReturn(keyWithDocName);
-               when(sone.getRequestUri().setKeyType(eq("SSK"))).thenReturn(sskKey);
-               return keyWithMetaStrings;
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt
new file mode 100644 (file)
index 0000000..68e08e3
--- /dev/null
@@ -0,0 +1,110 @@
+package net.pterodactylus.sone.core
+
+import freenet.keys.*
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.equalTo
+import org.junit.*
+import org.mockito.ArgumentMatchers.any
+import org.mockito.ArgumentMatchers.eq
+import org.mockito.Mockito.doAnswer
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [SoneRescuer].
+ */
+class SoneRescuerTest {
+
+       private val core = mock<Core>()
+       private val soneDownloader = mock<SoneDownloader>()
+       private val sone = mock<Sone>().apply {
+               val soneUri = mock<FreenetURI>()
+               whenever(soneUri.edition).thenReturn(currentEdition)
+               whenever(requestUri).thenReturn(soneUri)
+       }
+       private val soneRescuer = SoneRescuer(core, soneDownloader, sone)
+
+       @Test
+       fun newSoneRescuerIsNotFetchingAnything() {
+               assertThat(soneRescuer.isFetching, equalTo(false))
+       }
+
+       @Test
+       fun newSoneRescuerStartsAtCurrentEditionOfSone() {
+               assertThat(soneRescuer.currentEdition, equalTo(currentEdition))
+       }
+
+       @Test
+       fun newSoneRescuerHasANextEditionToGet() {
+               assertThat(soneRescuer.hasNextEdition(), equalTo(true))
+       }
+
+       @Test
+       fun soneRescuerDoesNotHaveANextEditionIfCurrentEditionIsZero() {
+               whenever(sone.requestUri.edition).thenReturn(0L)
+               val soneRescuer = SoneRescuer(core, soneDownloader, sone)
+               assertThat(soneRescuer.hasNextEdition(), equalTo(false))
+       }
+
+       @Test
+       fun nextEditionIsOneSmallerThanTheCurrentEdition() {
+               assertThat(soneRescuer.nextEdition, equalTo(currentEdition - 1))
+       }
+
+       @Test
+       fun lastFetchOfANewSoneRescuerWasSuccessful() {
+               assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true))
+       }
+
+       @Test
+       fun mainLoopStopsWhenItShould() {
+               soneRescuer.stop()
+               soneRescuer.serviceRun()
+       }
+
+       @Test
+       fun successfulInsert() {
+               val fetchedSone = mock<Sone>()
+               returnUriOnInsert(fetchedSone)
+               soneRescuer.startNextFetch()
+               soneRescuer.serviceRun()
+               verify(core).lockSone(eq(sone))
+               verify(core).updateSone(eq(fetchedSone), eq(true))
+               assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true))
+               assertThat(soneRescuer.isFetching, equalTo(false))
+       }
+
+       @Test
+       fun nonSuccessfulInsertIsRecognized() {
+               returnUriOnInsert(null)
+               soneRescuer.startNextFetch()
+               soneRescuer.serviceRun()
+               verify(core).lockSone(eq(sone))
+               verify(core, never()).updateSone(any(Sone::class.java), eq(true))
+               assertThat(soneRescuer.isLastFetchSuccessful, equalTo(false))
+               assertThat(soneRescuer.isFetching, equalTo(false))
+       }
+
+       private fun returnUriOnInsert(fetchedSone: Sone?) {
+               val keyWithMetaStrings = setupFreenetUri()
+               doAnswer {
+                       soneRescuer.stop()
+                       fetchedSone
+               }.`when`(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true))
+       }
+
+       private fun setupFreenetUri(): FreenetURI {
+               val sskKey = mock<FreenetURI>()
+               val keyWithDocName = mock<FreenetURI>()
+               val keyWithMetaStrings = mock<FreenetURI>()
+               whenever(keyWithDocName.setMetaString(eq(arrayOf("sone.xml")))).thenReturn(keyWithMetaStrings)
+               whenever(sskKey.setDocName(eq("Sone-" + (currentEdition - 1)))).thenReturn(keyWithDocName)
+               whenever(sone.requestUri.setKeyType(eq("SSK"))).thenReturn(sskKey)
+               return keyWithMetaStrings
+       }
+
+}
+
+private const val currentEdition = 12L