✨ Add way to start fetch while skipping one version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2019 18:46:12 +0000 (19:46 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2019 18:46:12 +0000 (19:46 +0100)
src/main/java/net/pterodactylus/sone/core/SoneRescuer.java
src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt

index adc0925..6045127 100644 (file)
@@ -130,6 +130,15 @@ public class SoneRescuer extends AbstractService {
                notifySyncObject();
        }
 
+       /**
+        * Starts the next fetch.
+        */
+       public void startNextFetchWithSkip() {
+               currentEdition--;
+               fetching = true;
+               notifySyncObject();
+       }
+
        //
        // SERVICE METHODS
        //
index af8a402..1fd2553 100644 (file)
@@ -5,12 +5,12 @@ 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
+import kotlin.test.Test
 
 /**
  * Unit test for [SoneRescuer].
@@ -67,7 +67,7 @@ class SoneRescuerTest {
        @Test
        fun successfulInsert() {
                val fetchedSone = mock<Sone>()
-               returnUriOnInsert(fetchedSone)
+               returnUriOnInsert(fetchedSone, currentEdition - 1)
                soneRescuer.startNextFetch()
                soneRescuer.serviceRun()
                verify(core).lockSone(eq(sone))
@@ -78,8 +78,21 @@ class SoneRescuerTest {
        }
 
        @Test
+       fun `starting fetch with skipping one edition skips one edition`() {
+               val fetchedSone = mock<Sone>()
+               returnUriOnInsert(fetchedSone, currentEdition - 2)
+               soneRescuer.startNextFetchWithSkip()
+               soneRescuer.serviceRun()
+               verify(core).lockSone(eq(sone))
+               verify(core).updateSone(eq(fetchedSone), eq(true))
+               assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true))
+               assertThat(soneRescuer.isFetching, equalTo(false))
+               assertThat(soneRescuer.currentEdition, equalTo(currentEdition - 2))
+       }
+
+       @Test
        fun nonSuccessfulInsertIsRecognized() {
-               returnUriOnInsert(null)
+               returnUriOnInsert(null, (currentEdition - 1))
                soneRescuer.startNextFetch()
                soneRescuer.serviceRun()
                verify(core).lockSone(eq(sone))
@@ -89,20 +102,20 @@ class SoneRescuerTest {
                assertThat(soneRescuer.currentEdition, equalTo(currentEdition))
        }
 
-       private fun returnUriOnInsert(fetchedSone: Sone?) {
-               val keyWithMetaStrings = setupFreenetUri()
+       private fun returnUriOnInsert(fetchedSone: Sone?, edition: Long) {
+               val keyWithMetaStrings = setupFreenetUri(edition)
                doAnswer {
                        soneRescuer.stop()
                        fetchedSone
                }.whenever(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true))
        }
 
-       private fun setupFreenetUri(): FreenetURI {
+       private fun setupFreenetUri(edition: Long): 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(sskKey.setDocName(eq("Sone-" + edition))).thenReturn(keyWithDocName)
                whenever(sone.requestUri.setKeyType(eq("SSK"))).thenReturn(sskKey)
                return keyWithMetaStrings
        }