blob: fe6e4028d1b585ba3f9dfa7e4285963627f1590e [file] [log] [blame]
package org.apache.fineract.online
import android.content.Context
import io.reactivex.Observable
import org.apache.fineract.FakeRemoteDataSource
import org.apache.fineract.R
import org.apache.fineract.data.datamanager.api.DataManagerCustomer
import org.apache.fineract.data.models.customer.identification.Identification
import org.apache.fineract.exceptions.NoConnectivityException
import org.apache.fineract.ui.online.identification.identificationlist.IdentificationsContract
import org.apache.fineract.ui.online.identification.identificationlist.IdentificationsPresenter
import org.apache.fineract.util.RxSchedulersOverrideRule
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class)
class IdentificationPresenterTest {
@Rule
@JvmField
val overrideSchedulersRule = RxSchedulersOverrideRule()
@Mock
lateinit var mockedView: IdentificationsContract.View
@Mock
lateinit var mockedDataManagerCustomer: DataManagerCustomer
@Mock
lateinit var mockedContext: Context
lateinit var identificationPresenter: IdentificationsPresenter
lateinit var identifications: List<Identification>
val customerIdentifier: String = "identifier1"
@Before
fun setup() {
identificationPresenter = IdentificationsPresenter(mockedContext,
mockedDataManagerCustomer)
identificationPresenter.attachView(mockedView)
identifications = FakeRemoteDataSource.getIdentifications()
}
@Test
fun testGetIdentifications() {
`when`(mockedDataManagerCustomer.fetchIdentifications(customerIdentifier))
.thenReturn(Observable.just(identifications))
identificationPresenter.fetchIdentifications(customerIdentifier)
verify(mockedView).showProgressbar()
verify(mockedView).hideProgressbar()
verify(mockedView).showIdentification(identifications)
}
@Test
fun testGetIdentificationsFail() {
`when`(mockedDataManagerCustomer.fetchIdentifications(customerIdentifier))
.thenReturn(Observable.error(RuntimeException()))
`when`(mockedContext.getString(R.string.error_fetching_identification_list))
.thenReturn("Error while fetching identification cards")
identificationPresenter.fetchIdentifications(customerIdentifier)
verify(mockedView).showProgressbar()
verify(mockedView).hideProgressbar()
verify(mockedView).showError("Error while fetching identification cards")
}
@Test
fun testGetIdentificationsFailDueToInternet() {
`when`(mockedDataManagerCustomer.fetchIdentifications(customerIdentifier))
.thenReturn(Observable.error(NoConnectivityException()))
identificationPresenter.fetchIdentifications(customerIdentifier)
verify(mockedView).showProgressbar()
verify(mockedView).hideProgressbar()
verify(mockedView).showNoInternetConnection()
}
@After
fun tearDown() {
identificationPresenter.detachView()
}
}