forked from openMF/android-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit test for viewmodels, staff and offlice sdk implementation
- Loading branch information
1 parent
7e55631
commit 168da5a
Showing
14 changed files
with
465 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
mifosng-android/src/main/java/com/mifos/mappers/offices/GetOfficeResponseMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.mifos.mappers.offices | ||
|
||
import com.mifos.objects.organisation.Office | ||
import org.apache.fineract.client.models.GetOfficesResponse | ||
import org.mifos.core.data.AbstractMapper | ||
import java.util.Date | ||
|
||
object GetOfficeResponseMapper : AbstractMapper<GetOfficesResponse, Office>() { | ||
|
||
override fun mapFromEntity(entity: GetOfficesResponse): Office { | ||
return Office().apply { | ||
id = entity.id?.toInt() | ||
name = entity.name | ||
nameDecorated = entity.nameDecorated | ||
openingDate = listOf( | ||
entity.openingDate?.year, | ||
entity.openingDate?.month, | ||
entity.openingDate?.year | ||
) | ||
externalId = entity.externalId | ||
} | ||
} | ||
|
||
override fun mapToEntity(domainModel: Office): GetOfficesResponse { | ||
return GetOfficesResponse().apply { | ||
id = domainModel.id?.toLong() | ||
name = domainModel.name | ||
nameDecorated = domainModel.nameDecorated | ||
openingDate = Date().apply { | ||
year = domainModel.openingDate[0]!! | ||
month = domainModel.openingDate[1]!! | ||
date = domainModel.openingDate[2]!! | ||
} | ||
externalId = domainModel.externalId | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
mifosng-android/src/main/java/com/mifos/mappers/staffs/StaffMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mifos.mappers.staffs | ||
|
||
import com.mifos.objects.organisation.Staff | ||
import org.apache.fineract.client.models.RetrieveOneResponse | ||
import org.mifos.core.data.AbstractMapper | ||
|
||
object StaffMapper: AbstractMapper<RetrieveOneResponse, Staff>() { | ||
override fun mapFromEntity(entity: RetrieveOneResponse): Staff { | ||
return Staff().apply { | ||
id = entity.id!!.toInt() | ||
firstname = entity.firstname | ||
lastname = entity.lastname | ||
displayName = entity.displayName | ||
officeId = entity.officeId!!.toInt() | ||
officeName = entity.officeName | ||
isLoanOfficer = entity.isLoanOfficer | ||
isActive = entity.isActive | ||
} | ||
} | ||
|
||
override fun mapToEntity(domainModel: Staff): RetrieveOneResponse { | ||
return RetrieveOneResponse().apply { | ||
id = domainModel.id?.toLong() | ||
firstname = domainModel.firstname | ||
lastname = domainModel.lastname | ||
displayName = domainModel.displayName | ||
officeId = domainModel.officeId?.toLong() | ||
officeName = domainModel.officeName | ||
isLoanOfficer = domainModel.isLoanOfficer | ||
isActive = domainModel.isActive | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
mifosng-android/src/test/java/com/mifos/viewmodels/ClientChargeViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.mifos.viewmodels | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.lifecycle.Observer | ||
import com.mifos.mifosxdroid.util.RxSchedulersOverrideRule | ||
import com.mifos.objects.client.Charges | ||
import com.mifos.objects.client.Page | ||
import com.mifos.repositories.ClientChargeRepository | ||
import com.mifos.states.ClientChargeUiState | ||
import org.junit.After | ||
import org.junit.Assert.* | ||
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 | ||
import org.mockito.Mockito.mock | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import rx.Observable | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class ClientChargeViewModelTest { | ||
|
||
@JvmField | ||
@Rule | ||
val overrideSchedulersRule = RxSchedulersOverrideRule() | ||
|
||
@JvmField | ||
@Rule | ||
val instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Mock | ||
private lateinit var clientChargeRepository: ClientChargeRepository | ||
|
||
@Mock | ||
private lateinit var clientUiStateObserver: Observer<ClientChargeUiState> | ||
|
||
private lateinit var clientChargeViewModel: ClientChargeViewModel | ||
|
||
@Mock | ||
lateinit var mockChargesPage: Page<Charges> | ||
|
||
|
||
@Before | ||
fun setUp() { | ||
MockitoAnnotations.openMocks(this) | ||
clientChargeViewModel = ClientChargeViewModel(clientChargeRepository) | ||
clientChargeViewModel.clientChargeUiState.observeForever(clientUiStateObserver) | ||
} | ||
|
||
@Test | ||
fun testLoadChargesEmptyList_SuccessfulChargesReceivedFromRepository_ReturnsSuccess() { | ||
|
||
Mockito.`when`( | ||
clientChargeRepository.getClientCharges( | ||
Mockito.anyInt(), | ||
Mockito.anyInt(), | ||
Mockito.anyInt() | ||
) | ||
).thenReturn(Observable.just(mockChargesPage)) | ||
clientChargeViewModel.loadCharges(1, 1, 1) | ||
Mockito.verify(clientUiStateObserver).onChanged(ClientChargeUiState.ShowProgressbar) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowFetchingErrorCharges("some error message")) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowChargesList(mockChargesPage)) | ||
Mockito.verify(clientUiStateObserver).onChanged(ClientChargeUiState.ShowEmptyCharges) | ||
Mockito.verifyNoMoreInteractions(clientUiStateObserver) | ||
} | ||
|
||
@Test | ||
fun testLoadChargesList_SuccessfulChargesReceivedFromRepository_ReturnsSuccess() { | ||
|
||
val list1 = mock(Charges::class.java) | ||
val list2 = mock(Charges::class.java) | ||
val list = listOf(list1, list2) | ||
val mockPage = Page(2, list) | ||
|
||
Mockito.`when`( | ||
clientChargeRepository.getClientCharges( | ||
Mockito.anyInt(), | ||
Mockito.anyInt(), | ||
Mockito.anyInt() | ||
) | ||
).thenReturn(Observable.just(mockPage)) | ||
clientChargeViewModel.loadCharges(1, 1, 1) | ||
Mockito.verify(clientUiStateObserver).onChanged(ClientChargeUiState.ShowProgressbar) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowFetchingErrorCharges("some error message")) | ||
Mockito.verify(clientUiStateObserver) | ||
.onChanged(ClientChargeUiState.ShowChargesList(mockPage)) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowEmptyCharges) | ||
Mockito.verifyNoMoreInteractions(clientUiStateObserver) | ||
} | ||
|
||
@Test | ||
fun testLoadCharges_UnsuccessfulChargesReceivedFromRepository_ReturnsError() { | ||
|
||
Mockito.`when`( | ||
clientChargeRepository.getClientCharges( | ||
Mockito.anyInt(), | ||
Mockito.anyInt(), | ||
Mockito.anyInt() | ||
) | ||
).thenReturn(Observable.error(RuntimeException("some error message"))) | ||
clientChargeViewModel.loadCharges(1, 1, 1) | ||
Mockito.verify(clientUiStateObserver).onChanged(ClientChargeUiState.ShowProgressbar) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowChargesList(mockChargesPage)) | ||
Mockito.verify(clientUiStateObserver, Mockito.never()) | ||
.onChanged(ClientChargeUiState.ShowEmptyCharges) | ||
Mockito.verifyNoMoreInteractions(clientUiStateObserver) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
clientChargeViewModel.clientChargeUiState.removeObserver(clientUiStateObserver) | ||
} | ||
} |
Oops, something went wrong.