1 package net.pterodactylus.sone.core
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.test.*
6 import org.hamcrest.MatcherAssert.*
7 import org.hamcrest.Matchers.equalTo
9 import org.mockito.ArgumentMatchers.any
10 import org.mockito.ArgumentMatchers.eq
11 import org.mockito.Mockito.doAnswer
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
16 * Unit test for [SoneRescuer].
18 class SoneRescuerTest {
20 private val core = mock<Core>()
21 private val soneDownloader = mock<SoneDownloader>()
22 private val sone = mock<Sone>().apply {
23 val soneUri = mock<FreenetURI>()
24 whenever(soneUri.edition).thenReturn(currentEdition)
25 whenever(requestUri).thenReturn(soneUri)
27 private val soneRescuer = SoneRescuer(core, soneDownloader, sone)
30 fun newSoneRescuerIsNotFetchingAnything() {
31 assertThat(soneRescuer.isFetching, equalTo(false))
35 fun newSoneRescuerStartsAtCurrentEditionOfSone() {
36 assertThat(soneRescuer.currentEdition, equalTo(currentEdition))
40 fun newSoneRescuerHasANextEditionToGet() {
41 assertThat(soneRescuer.hasNextEdition(), equalTo(true))
45 fun soneRescuerDoesNotHaveANextEditionIfCurrentEditionIsZero() {
46 whenever(sone.requestUri.edition).thenReturn(0L)
47 val soneRescuer = SoneRescuer(core, soneDownloader, sone)
48 assertThat(soneRescuer.hasNextEdition(), equalTo(false))
52 fun nextEditionIsOneSmallerThanTheCurrentEdition() {
53 assertThat(soneRescuer.nextEdition, equalTo(currentEdition - 1))
57 fun lastFetchOfANewSoneRescuerWasSuccessful() {
58 assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true))
62 fun mainLoopStopsWhenItShould() {
64 soneRescuer.serviceRun()
68 fun successfulInsert() {
69 val fetchedSone = mock<Sone>()
70 returnUriOnInsert(fetchedSone)
71 soneRescuer.startNextFetch()
72 soneRescuer.serviceRun()
73 verify(core).lockSone(eq(sone))
74 verify(core).updateSone(eq(fetchedSone), eq(true))
75 assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true))
76 assertThat(soneRescuer.isFetching, equalTo(false))
80 fun nonSuccessfulInsertIsRecognized() {
81 returnUriOnInsert(null)
82 soneRescuer.startNextFetch()
83 soneRescuer.serviceRun()
84 verify(core).lockSone(eq(sone))
85 verify(core, never()).updateSone(any(Sone::class.java), eq(true))
86 assertThat(soneRescuer.isLastFetchSuccessful, equalTo(false))
87 assertThat(soneRescuer.isFetching, equalTo(false))
90 private fun returnUriOnInsert(fetchedSone: Sone?) {
91 val keyWithMetaStrings = setupFreenetUri()
95 }.`when`(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true))
98 private fun setupFreenetUri(): FreenetURI {
99 val sskKey = mock<FreenetURI>()
100 val keyWithDocName = mock<FreenetURI>()
101 val keyWithMetaStrings = mock<FreenetURI>()
102 whenever(keyWithDocName.setMetaString(eq(arrayOf("sone.xml")))).thenReturn(keyWithMetaStrings)
103 whenever(sskKey.setDocName(eq("Sone-" + (currentEdition - 1)))).thenReturn(keyWithDocName)
104 whenever(sone.requestUri.setKeyType(eq("SSK"))).thenReturn(sskKey)
105 return keyWithMetaStrings
110 private const val currentEdition = 12L