Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.trycentsemployeeapplication.data.repositories
- import com.haroldadmin.cnradapter.NetworkResponse
- import com.trycentsemployeeapplication.data.resource.local.datasource.AuthSessionLocalDatasource
- import com.trycentsemployeeapplication.data.resource.local.datasource.LocalDataSource
- import com.trycentsemployeeapplication.data.resource.remote.AuthApi
- import com.trycentsemployeeapplication.data.resource.remote.response.AuthSessionRemoteModel
- import com.trycentsemployeeapplication.data.resource.remote.response.ResponseErrorMessage
- import com.trycentsemployeeapplication.domain.results.LoginResult
- import kotlinx.coroutines.runBlocking
- import okhttp3.MediaType.Companion.toMediaTypeOrNull
- import okhttp3.ResponseBody.Companion.toResponseBody
- import org.junit.Assert.assertEquals
- import org.junit.Before
- import org.junit.Test
- import org.mockito.kotlin.*
- import retrofit2.Response
- import java.net.HttpURLConnection.*
- class LoginRepositoryTest {
- private lateinit var api: AuthApi
- private lateinit var authSessionLocalDatasource: AuthSessionLocalDatasource
- private lateinit var localDatasource: LocalDataSource
- private lateinit var loginRepository: LoginRepository
- //Test credentials
- private val locationId = "13"
- private val password = "adc"
- @Before
- fun setUp() {
- api = mock()
- authSessionLocalDatasource = mock()
- localDatasource = mock()
- loginRepository = LoginRepositoryImpl(api, authSessionLocalDatasource, localDatasource)
- }
- @Test
- fun `login() should save AuthSession and return LoginResultError if login is Success`() {
- runBlocking {
- // Given api.login() returns the given NetworkResponse
- val authSessionBody = createTestAuthSessionRemoteModel()
- given(
- api.login(locationId, password)
- ).willReturn(
- NetworkResponse.Success(
- authSessionBody,
- Response.success(HTTP_OK, authSessionBody)
- )
- )
- // When loginRepository.login() called
- val result = loginRepository.login(locationId, password)
- //Then call authSessionLocalDatasource.saveAuthSession(any()) only once and return LoginResult.Success
- verify(authSessionLocalDatasource, times(1)).saveAuthSession(any())
- assertEquals(result, LoginResult.Success)
- }
- }
- @Test
- fun `login() should not save AuthSession and return LoginResultError if login is Error`() {
- runBlocking {
- // Given api.login() returns the given NetworkResponse
- val responseErrorMessage = createTestResponseErrorMessage()
- given(
- api.login(locationId, password)
- ).willReturn(
- NetworkResponse.ServerError(
- responseErrorMessage,
- Response.error<String>(
- HTTP_UNAUTHORIZED,
- createErrorResponseBody()
- )
- )
- )
- // When loginRepository.login() called
- val result = loginRepository.login(locationId, password)
- //Then never call authSessionLocalDatasource.saveAuthSession() and return LoginResult.Error
- verify(authSessionLocalDatasource, never()).saveAuthSession(any())
- assertEquals(result, LoginResult.Error(createTestResponseErrorMessage().error))
- }
- }
- @Test
- fun `login() should not save AuthSession and return LoginResultCredentialsError if credentials is invalid`() {
- runBlocking {
- // Given api.login() returns the given NetworkResponse
- val responseErrorMessage = createTestResponseErrorMessage()
- given(
- api.login(locationId, password)
- ).willReturn(
- NetworkResponse.ServerError(
- responseErrorMessage,
- Response.error<String>(
- HTTP_FORBIDDEN,
- createErrorResponseBody()
- )
- )
- )
- // When loginRepository.login() called
- val result = loginRepository.login(locationId, password)
- // Then never call authSessionLocalDatasource.saveAuthSession() and return LoginResult.CredentialsError
- verify(authSessionLocalDatasource, never()).saveAuthSession(any())
- assertEquals(
- result,
- LoginResult.CredentialsError(createTestResponseErrorMessage().error)
- )
- }
- }
- @Test
- fun `logout() should call clearRoomDB() only once`() {
- runBlocking {
- // When loginRepository.logout() called
- loginRepository.logout()
- //Then never call localDataSource.clearRoomDB() only once
- verify(localDatasource, times(1)).clearRoomDB()
- }
- }
- }
- private fun createTestAuthSessionRemoteModel() = AuthSessionRemoteModel(
- address = "Abc",
- city = "Mexico",
- id = 1,
- isHub = false,
- name = "Name",
- processingCapability = "",
- state = "US",
- subsidiaryCode = 1,
- success = true,
- token = "qwerty",
- type = "us",
- zipCode = "12345"
- )
- private fun createTestResponseErrorMessage() = ResponseErrorMessage(
- error = "error"
- )
- private fun createErrorResponseBody() = "{\"error\":[\"error\"]}"
- .toResponseBody("application/json".toMediaTypeOrNull())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement