package de.geeksfactory.opacclient.apis

import de.geeksfactory.opacclient.i18n.DummyStringProvider
import de.geeksfactory.opacclient.objects.Account
import de.geeksfactory.opacclient.objects.AccountData
import de.geeksfactory.opacclient.utils.html
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.mockito.Mockito
import org.mockito.Mockito.any
import org.mockito.Mockito.doNothing
import java.util.*

@RunWith(Parameterized::class)
class OPENAccountTest(private val file: String) : BaseHtmlTest() {
    val open = Mockito.spy(OpenAccountScraper::class.java)

    init {
        open.stringProvider = DummyStringProvider()
        doNothing().`when`(open).fetchProlongability(any(), any(), any())
    }

    @Test
    fun testParseLent() {
        val doc = readResource("/open/$file")?.html
        val adata = AccountData(0L)
        open.parse_lent(adata, doc, Account())

        val lent = adata.lent
        assertTrue(lent.isNotEmpty())
        for (item in lent) {
            assertContainsData(item.title)
            assertNotNull(item.deadline)
            assertNotNull(item.id)
            assertContainsData(item.format)
        }
    }

    @Test
    fun testParseReservations() {
        val doc = readResource("/open/$file")?.html
        val adata = AccountData(0L)
        open.parse_reservations(adata, doc)

        val reservations = adata.reservations
        assertTrue(reservations.isNotEmpty())
        for (item in reservations) {
            assertContainsData(item.title)
            assertNotNull(item.id)
            assertContainsData(item.format)
            if (item.status == "Bereitgestellt") {
                assertNotNull(item.expirationDate)
            }
        }
    }

    companion object {

        private val FILES = arrayOf("erlangen.html")

        @JvmStatic
        @Parameterized.Parameters(name = "{0}")
        fun files(): Collection<Array<String>> {
            val files = ArrayList<Array<String>>()
            for (file in FILES) {
                files.add(arrayOf(file))
            }
            return files
        }
    }
}
