Advertisement
frogiii

tests

Apr 22nd, 2022
1,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.51 KB | None | 0 0
  1. package com.trycentsemployeeapplication.data.repositories
  2.  
  3. import com.haroldadmin.cnradapter.NetworkResponse
  4. import com.trycentsemployeeapplication.data.resource.local.datasource.AuthSessionLocalDatasource
  5. import com.trycentsemployeeapplication.data.resource.local.datasource.LocalDataSource
  6. import com.trycentsemployeeapplication.data.resource.remote.AuthApi
  7. import com.trycentsemployeeapplication.data.resource.remote.response.AuthSessionRemoteModel
  8. import com.trycentsemployeeapplication.data.resource.remote.response.ResponseErrorMessage
  9. import com.trycentsemployeeapplication.domain.results.LoginResult
  10. import kotlinx.coroutines.runBlocking
  11. import okhttp3.MediaType.Companion.toMediaTypeOrNull
  12. import okhttp3.ResponseBody.Companion.toResponseBody
  13. import org.junit.Assert.assertEquals
  14. import org.junit.Before
  15. import org.junit.Test
  16. import org.mockito.kotlin.*
  17. import retrofit2.Response
  18. import java.net.HttpURLConnection.*
  19.  
  20. class LoginRepositoryTest {
  21.  
  22.     private lateinit var api: AuthApi
  23.     private lateinit var authSessionLocalDatasource: AuthSessionLocalDatasource
  24.     private lateinit var localDatasource: LocalDataSource
  25.     private lateinit var loginRepository: LoginRepository
  26.  
  27.     //Test credentials
  28.     private val locationId = "13"
  29.     private val password = "adc"
  30.  
  31.     @Before
  32.     fun setUp() {
  33.         api = mock()
  34.         authSessionLocalDatasource = mock()
  35.         localDatasource = mock()
  36.         loginRepository = LoginRepositoryImpl(api, authSessionLocalDatasource, localDatasource)
  37.     }
  38.  
  39.     @Test
  40.     fun `login() should save AuthSession and return LoginResultError if login is Success`() {
  41.         runBlocking {
  42.             // Given api.login() returns the given NetworkResponse
  43.             val authSessionBody = createTestAuthSessionRemoteModel()
  44.             given(
  45.                 api.login(locationId, password)
  46.             ).willReturn(
  47.                 NetworkResponse.Success(
  48.                     authSessionBody,
  49.                     Response.success(HTTP_OK, authSessionBody)
  50.                 )
  51.             )
  52.  
  53.             // When loginRepository.login() called
  54.             val result = loginRepository.login(locationId, password)
  55.  
  56.             //Then call authSessionLocalDatasource.saveAuthSession(any()) only once and return LoginResult.Success
  57.             verify(authSessionLocalDatasource, times(1)).saveAuthSession(any())
  58.             assertEquals(result, LoginResult.Success)
  59.         }
  60.     }
  61.  
  62.     @Test
  63.     fun `login() should not save AuthSession and return LoginResultError if login is Error`() {
  64.         runBlocking {
  65.             // Given api.login() returns the given NetworkResponse
  66.             val responseErrorMessage = createTestResponseErrorMessage()
  67.             given(
  68.                 api.login(locationId, password)
  69.             ).willReturn(
  70.                 NetworkResponse.ServerError(
  71.                     responseErrorMessage,
  72.                     Response.error<String>(
  73.                         HTTP_UNAUTHORIZED,
  74.                         createErrorResponseBody()
  75.                     )
  76.                 )
  77.             )
  78.  
  79.             // When loginRepository.login() called
  80.             val result = loginRepository.login(locationId, password)
  81.  
  82.             //Then never call authSessionLocalDatasource.saveAuthSession() and return LoginResult.Error
  83.             verify(authSessionLocalDatasource, never()).saveAuthSession(any())
  84.             assertEquals(result, LoginResult.Error(createTestResponseErrorMessage().error))
  85.         }
  86.     }
  87.  
  88.     @Test
  89.     fun `login() should not save AuthSession and return LoginResultCredentialsError if credentials is invalid`() {
  90.         runBlocking {
  91.             // Given api.login() returns the given NetworkResponse
  92.             val responseErrorMessage = createTestResponseErrorMessage()
  93.             given(
  94.                 api.login(locationId, password)
  95.             ).willReturn(
  96.                 NetworkResponse.ServerError(
  97.                     responseErrorMessage,
  98.                     Response.error<String>(
  99.                         HTTP_FORBIDDEN,
  100.                         createErrorResponseBody()
  101.                     )
  102.                 )
  103.             )
  104.  
  105.             // When loginRepository.login() called
  106.             val result = loginRepository.login(locationId, password)
  107.  
  108.             // Then never call authSessionLocalDatasource.saveAuthSession() and return LoginResult.CredentialsError
  109.             verify(authSessionLocalDatasource, never()).saveAuthSession(any())
  110.             assertEquals(
  111.                 result,
  112.                 LoginResult.CredentialsError(createTestResponseErrorMessage().error)
  113.             )
  114.         }
  115.     }
  116.  
  117.     @Test
  118.     fun `logout() should call clearRoomDB() only once`() {
  119.         runBlocking {
  120.             // When loginRepository.logout() called
  121.             loginRepository.logout()
  122.  
  123.             //Then never call localDataSource.clearRoomDB() only once
  124.             verify(localDatasource, times(1)).clearRoomDB()
  125.         }
  126.     }
  127.  
  128. }
  129.  
  130. private fun createTestAuthSessionRemoteModel() = AuthSessionRemoteModel(
  131.     address = "Abc",
  132.     city = "Mexico",
  133.     id = 1,
  134.     isHub = false,
  135.     name = "Name",
  136.     processingCapability = "",
  137.     state = "US",
  138.     subsidiaryCode = 1,
  139.     success = true,
  140.     token = "qwerty",
  141.     type = "us",
  142.     zipCode = "12345"
  143. )
  144.  
  145. private fun createTestResponseErrorMessage() = ResponseErrorMessage(
  146.     error = "error"
  147. )
  148.  
  149. private fun createErrorResponseBody() = "{\"error\":[\"error\"]}"
  150.     .toResponseBody("application/json".toMediaTypeOrNull())
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement