Skip to content

Commit

Permalink
Unit test for viewmodels, staff and offlice sdk implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Aug 28, 2023
1 parent 7e55631 commit 168da5a
Show file tree
Hide file tree
Showing 14 changed files with 465 additions and 15 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ext {
jUnitVersion = '4.13.2'

// mockito version
mockitoVersion = '5.4.0'
mockitoVersion = '5.5.0'

//jsr version
jsrVersion = '1.0'
Expand Down Expand Up @@ -129,4 +129,7 @@ ext {

// sdk client
clientVersion = '2.0.3'

// arch core testing
archCoreVersion = '2.2.0'
}
1 change: 1 addition & 0 deletions mifosng-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ dependencies {
androidTestImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
androidTestImplementation "org.mockito:mockito-android:$rootProject.mockitoVersion"
testImplementation "org.junit.jupiter:junit-jupiter:$rootProject.junitJupiterVersion"
testImplementation "androidx.arch.core:core-testing:$rootProject.archCoreVersion"

//Android-Jobs
implementation "com.evernote:android-job:$rootProject.androidJobVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mifos.api.datamanager

import com.mifos.api.BaseApiManager
import com.mifos.api.local.databasehelper.DatabaseHelperOffices
import com.mifos.mappers.offices.GetOfficeResponseMapper
import com.mifos.objects.organisation.Office
import com.mifos.utils.PrefManager.userStatus
import rx.Observable
Expand All @@ -18,18 +19,16 @@ import javax.inject.Singleton
@Singleton
class DataManagerOffices @Inject constructor(
val mBaseApiManager: BaseApiManager,
val mDatabaseHelperOffices: DatabaseHelperOffices
private val mDatabaseHelperOffices: DatabaseHelperOffices,
private val baseApiManager: org.mifos.core.apimanager.BaseApiManager
) {
/**
* return all List of Offices from DatabaseHelperOffices
*/
val offices: Observable<List<Office>>
get() = when (userStatus) {
false -> mBaseApiManager.officeApi.allOffices
.concatMap { offices ->
mDatabaseHelperOffices.saveAllOffices(offices)
Observable.just(offices)
}
false -> baseApiManager.getOfficeApi().retrieveOffices(null, null, null)
.map(GetOfficeResponseMapper::mapFromEntityList)

true ->
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.mifos.api.datamanager

import com.mifos.api.BaseApiManager
import com.mifos.api.local.databasehelper.DatabaseHelperStaff
import com.mifos.mappers.staffs.StaffMapper
import com.mifos.objects.organisation.Staff
import com.mifos.utils.PrefManager.userStatus
import rx.Observable
import rx.functions.Func1
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -15,19 +15,17 @@ import javax.inject.Singleton
@Singleton
class DataManagerStaff @Inject constructor(
val mBaseApiManager: BaseApiManager,
private val mDatabaseHelperStaff: DatabaseHelperStaff
private val mDatabaseHelperStaff: DatabaseHelperStaff,
private val baseApiManager: org.mifos.core.apimanager.BaseApiManager
) {
/**
* @param officeId
* @return
*/
fun getStaffInOffice(officeId: Int): Observable<List<Staff>> {
return when (userStatus) {
false -> mBaseApiManager.staffApi.getStaffForOffice(officeId)
.concatMap { staffs ->
mDatabaseHelperStaff.saveAllStaffOfOffices(staffs)
Observable.just(staffs)
}
false -> baseApiManager.getStaffApi().retrieveAll16(officeId.toLong(), null, null, null)
.map(StaffMapper::mapFromEntityList)

true ->
/**
Expand Down
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
}
}
}
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mifos.objects.organisation.Staff
import com.mifos.objects.templates.clients.ClientsTemplate
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import org.apache.fineract.client.models.RetrieveOneResponse
import rx.Observable

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.mifos.objects.organisation.Staff
import com.mifos.objects.templates.clients.ClientsTemplate
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import org.apache.fineract.client.models.RetrieveOneResponse
import rx.Observable
import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class LoginActivity : MifosBaseActivity(){
}

private fun onLoginSuccessful(user: PostAuthenticationResponse) {

PrefManager.usernamePassword = Pair(username,password)
// Saving userID
PrefManager.setUserId(user.userId!!.toInt())
// Saving user's token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class LoginViewModel @Inject constructor(private val loginRepository: LoginRepos

override fun onNext(user: PostAuthenticationResponse) {
_loginUiState.value = LoginUiState.ShowLoginSuccessful(user)
PrefManager.usernamePassword = Pair(username,password)
}
})
}
Expand Down
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)
}
}
Loading

0 comments on commit 168da5a

Please sign in to comment.