This file contains hidden or 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
interface CustomerView { | |
fun showCustomError() | |
fun showError(error: ErrorModel) | |
fun showLoader() | |
fun hideLoader() | |
fun populateData(data: CustomerDetail) | |
} |
This file contains hidden or 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
private fun getCustomerDataFromApi() { | |
customerUsecase.getCustomerData() | |
.observeOn(AndroidSchedulers.mainThread()) | |
.doOnSubscribe { view.showLoader() } | |
.doOnEvent { _, _ -> view.hideLoader() } | |
.subscribe( | |
{ | |
view.populateData(it) | |
}, | |
{ e -> |
This file contains hidden or 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
interface CustomerUsecase { | |
fun getCustomerData(): Single<CustomerDetail> | |
} | |
class CustomerInteractor(private val repo: CustomerRepo) : CustomerUsecase { | |
override fun getCustomerData(): Single<CustomerDetail> = repo.fetchCustomerDetail() | |
} |
This file contains hidden or 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
interface CustomerRepo { | |
fun fetchCustomerDetail(): Single<CustomerDetail> | |
} | |
class CustomerRepoImpl( | |
private val customerEndPoint: CustomerEndPoint): CustomerRepo | |
{ | |
override fun fetchCustomerDetail(): Single<CustomerDetail> { | |
return customerEndPoint | |
.getCustomerData() |
This file contains hidden or 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
interface CustomerEndPoint { | |
@GET(CUSTOMER_API_CALL) | |
fun getCustomerData(): Single<Response<CustomerDetailResponse>> | |
} |
This file contains hidden or 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
data class CustomerDetailResponse( | |
@SerializedName("data") val data: CustomerDetail, | |
@SerializedName("success") val success: Boolean = false, | |
@SerializedName("errors") val errors: List<Error>?) | |
data class CustomerDetail( | |
@SerializedName("first_name") val firstName: String, | |
@SerializedName("last_name") val lastName: String, | |
@SerializedName("email") val email: String, | |
@SerializedName("phone_number") val phoneNumber: Int) |
This file contains hidden or 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
@Mock | |
private lateinit var view: CustomerView | |
@Mock | |
private lateinit var usecase: CustomerUsecase | |
private lateinit var presenter: CustomerPresenter | |
@Test | |
fun `should show customer data`() { | |
val expectedResponse = mock(CustomerDetail::class.java) | |
`when`(usecase.getCustomerData()).thenReturn(Single.just(expectedResponse)) | |
This file contains hidden or 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
@Mock | |
private lateinit var repo: CustomerRepo | |
private lateinit var customerUsecase: CustomerUsecase | |
@Before | |
fun setUp() { | |
customerUsecase = CustomerInteractor(repo) | |
} | |
@Test |
This file contains hidden or 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
@Mock | |
private lateinit var endPoint: CustomerEndPoint | |
private lateinit var repo: CustomerRepoImpl | |
@Test | |
fun `should return customer data`() { | |
val expectedCustomerResponse = mock(CustomerDetailResponse::class.java) | |
`when`(endPoint.getCustomerData()).thenReturn(Single.create { it.onSuccess(Response.success(expectedCustomerResponse)) }) | |
val expectedCustomerDetail = mock(CustomerDetail::class.java) | |
`when`(expectedexpectedCustomerResponse.data).thenReturn(expectedCustomerDetail) |