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].
@Test
fun successfulInsert() {
val fetchedSone = mock<Sone>()
- returnUriOnInsert(fetchedSone)
+ returnUriOnInsert(fetchedSone, currentEdition - 1)
soneRescuer.startNextFetch()
soneRescuer.serviceRun()
verify(core).lockSone(eq(sone))
}
@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))
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
}