-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf2c7ce
commit 0b72e7e
Showing
14 changed files
with
271 additions
and
11 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
8 changes: 8 additions & 0 deletions
8
...in/java/com/zsoltbertalan/flickslate/account/data/network/model/AccountDetailsReplyDto.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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
package com.zsoltbertalan.flickslate.account.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.shared.model.Account | ||
import kotlinx.serialization.Serializable | ||
|
||
@Suppress("ConstructorParameterNaming") | ||
@Serializable | ||
data class AccountDetailsReplyDto(val name: String?, val username: String) | ||
|
||
fun AccountDetailsReplyDto.toAccount(): Account { | ||
val accountName = | ||
if (name.isNullOrEmpty()) username | ||
else name | ||
return Account(accountName) | ||
} |
99 changes: 99 additions & 0 deletions
99
...est/java/com/zsoltbertalan/flickslate/account/data/network/AccountRemoteDataSourceTest.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,99 @@ | ||
package com.zsoltbertalan.flickslate.account.data.network | ||
|
||
import com.github.michaelbull.result.Err | ||
import com.zsoltbertalan.flickslate.account.data.network.model.CreateRequestTokenReplyDto | ||
import com.zsoltbertalan.flickslate.account.data.network.model.CreateSessionReplyDto | ||
import com.zsoltbertalan.flickslate.movies.data.network.model.CreateRequestTokenReplyDtoMother | ||
import com.zsoltbertalan.flickslate.movies.data.network.model.CreateSessionReplyDtoMother | ||
import com.zsoltbertalan.flickslate.shared.data.network.model.ErrorBody | ||
import com.zsoltbertalan.flickslate.shared.model.Failure | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.ResponseBody.Companion.toResponseBody | ||
import org.junit.Before | ||
import org.junit.Test | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
import java.net.HttpURLConnection | ||
|
||
class AccountRemoteDataSourceTest { | ||
|
||
private val accountService: AccountService = mockk() | ||
private lateinit var accountRemoteDataSource: AccountRemoteDataSource | ||
|
||
@Before | ||
fun setup() { | ||
coEvery { accountService.createRequestToken() } returns | ||
CreateRequestTokenReplyDtoMother.createCreateRequestTokenReplyDto() | ||
coEvery { accountService.validateRequestTokenWithLogin(any()) } returns | ||
CreateRequestTokenReplyDtoMother.createCreateRequestTokenReplyDto() | ||
coEvery { accountService.createSession(any()) } returns | ||
CreateSessionReplyDtoMother.createCreateSessionReplyDto() | ||
accountRemoteDataSource = AccountRemoteDataSource(accountService) | ||
} | ||
|
||
@Test | ||
fun `when createSessionId called and service returns result then returns correct result`() = runTest { | ||
val result = accountRemoteDataSource.createSessionId("", "") | ||
result.value shouldBeEqual "session123abc" | ||
} | ||
|
||
@Test | ||
fun `when createSessionId called and service returns create request failure then returns correct result`() = | ||
runTest { | ||
coEvery { accountService.createRequestToken() } throws createRequestTokenException() | ||
accountRemoteDataSource.createSessionId("", "") shouldBeEqual | ||
Err(Failure.ServerError("Invalid service: this service does not exist.")) | ||
} | ||
|
||
@Test | ||
fun `when createSessionId called and service returns create 2 failure then returns correct result`() = | ||
runTest { | ||
coEvery { accountService.validateRequestTokenWithLogin(any()) } throws createRequestTokenException() | ||
accountRemoteDataSource.createSessionId("", "") shouldBeEqual | ||
Err(Failure.ServerError("Invalid service: this service does not exist.")) | ||
} | ||
|
||
@Test | ||
fun `when createSessionId called and service returns create 3 failure then returns correct result`() = | ||
runTest { | ||
coEvery { accountService.createSession(any()) } throws createSessionException() | ||
accountRemoteDataSource.createSessionId("", "") shouldBeEqual | ||
Err(Failure.ServerError("Invalid service: this service does not exist.")) | ||
} | ||
|
||
@Suppress("unused") | ||
private fun createRequestTokenException(): HttpException { | ||
val errorBody = ErrorBody( | ||
success = false, | ||
status_code = 2, | ||
status_message = "Invalid service: this service does not exist." | ||
) | ||
return HttpException( | ||
Response.error<CreateRequestTokenReplyDto>( | ||
HttpURLConnection.HTTP_NOT_IMPLEMENTED, | ||
Json.encodeToString(errorBody).toResponseBody() | ||
) | ||
) | ||
} | ||
|
||
@Suppress("unused") | ||
private fun createSessionException(): HttpException { | ||
val errorBody = ErrorBody( | ||
success = false, | ||
status_code = 2, | ||
status_message = "Invalid service: this service does not exist." | ||
) | ||
return HttpException( | ||
Response.error<CreateSessionReplyDto>( | ||
HttpURLConnection.HTTP_NOT_IMPLEMENTED, | ||
Json.encodeToString(errorBody).toResponseBody() | ||
) | ||
) | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...ava/com/zsoltbertalan/flickslate/account/data/network/model/AccountDetailsReplyDtoTest.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,27 @@ | ||
package com.zsoltbertalan.flickslate.account.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.movies.data.network.model.AccountReplyDtoMother | ||
import com.zsoltbertalan.flickslate.shared.model.Account | ||
import com.zsoltbertalan.flickslate.shared.model.Movie | ||
import com.zsoltbertalan.flickslate.shared.model.PagingReply | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class AccountDetailsReplyDtoTest { | ||
|
||
@Test | ||
fun `when there is a name in response then name is mapped`() { | ||
val responseDto = AccountReplyDtoMother.createAccountReplyDto() | ||
val mappedResponse = responseDto.toAccount() | ||
mappedResponse.name shouldBe "John Doe" | ||
} | ||
|
||
@Test | ||
fun `when there is no name in response then username is mapped`() { | ||
val responseDto = AccountReplyDtoMother.createAccountReplyDtoWithoutName() | ||
val mappedResponse = responseDto.toAccount() | ||
mappedResponse.name shouldBe "Jane Doe" | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...src/test/java/com/zsoltbertalan/flickslate/account/data/repository/AccountAccessorTest.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,66 @@ | ||
package com.zsoltbertalan.flickslate.account.data.repository | ||
|
||
import com.github.michaelbull.result.Ok | ||
import com.zsoltbertalan.flickslate.account.data.api.AccountDataSource | ||
import com.zsoltbertalan.flickslate.account.data.network.AccountService | ||
import com.zsoltbertalan.flickslate.movies.domain.model.AccountMother | ||
import com.zsoltbertalan.flickslate.shared.model.Account | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class AccountAccessorTest { | ||
|
||
private val accountLocalDataSource: AccountDataSource.Local = mockk() | ||
private val accountRemoteDataSource: AccountDataSource.Remote = mockk() | ||
|
||
private lateinit var accountAccessor: AccountAccessor | ||
|
||
@Before | ||
fun setup() { | ||
coEvery { accountLocalDataSource.getAccount() } returns null | ||
coEvery { accountLocalDataSource.getAccessToken() } returns "" | ||
coEvery { accountLocalDataSource.saveAccount(any()) } returns Unit | ||
coEvery { accountLocalDataSource.saveAccessToken(any()) } returns Unit | ||
coEvery { accountRemoteDataSource.createSessionId(any(), any()) } returns Ok( | ||
"session123abc" | ||
) | ||
coEvery { accountRemoteDataSource.getAccountDetails(any()) } returns Ok( | ||
Account("John Doe") | ||
) | ||
coEvery { accountRemoteDataSource.deleteSessionId(any()) } returns Ok( | ||
true | ||
) | ||
accountAccessor = AccountAccessor( | ||
accountRemoteDataSource, | ||
accountLocalDataSource, | ||
) | ||
} | ||
|
||
@Test | ||
fun `when login called then returns correct account`() = runTest { | ||
val accountOutcome = accountAccessor.login("john.doe", "password123") | ||
val account = AccountMother.createAccount() | ||
accountOutcome.value shouldBeEqual account | ||
} | ||
|
||
@Test | ||
fun `when getAccount called then returns correct account`() = runTest { | ||
val accountOutcome = accountAccessor.getAccount() | ||
val account = AccountMother.createAccount() | ||
if (accountOutcome != null) { | ||
accountOutcome shouldBeEqual account | ||
} | ||
} | ||
|
||
@Test | ||
fun `when logout called then session is deleted`() = runTest { | ||
accountAccessor.logout() | ||
coVerify { accountRemoteDataSource.deleteSessionId(any()) } | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...es/kotlin/com/zsoltbertalan/flickslate/movies/data/network/model/AccountReplyDtoMother.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,17 @@ | ||
package com.zsoltbertalan.flickslate.movies.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.account.data.network.model.AccountDetailsReplyDto | ||
|
||
object AccountReplyDtoMother { | ||
|
||
fun createAccountReplyDto() = AccountDetailsReplyDto( | ||
name = "John Doe", | ||
username = "Jane Doe" | ||
) | ||
|
||
fun createAccountReplyDtoWithoutName() = AccountDetailsReplyDto( | ||
name = null, | ||
username = "Jane Doe" | ||
) | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...om/zsoltbertalan/flickslate/movies/data/network/model/CreateRequestTokenReplyDtoMother.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,13 @@ | ||
package com.zsoltbertalan.flickslate.movies.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.account.data.network.model.CreateRequestTokenReplyDto | ||
|
||
object CreateRequestTokenReplyDtoMother { | ||
|
||
fun createCreateRequestTokenReplyDto(success: Boolean = true) = CreateRequestTokenReplyDto( | ||
success = success, | ||
expires_at = "", | ||
request_token = "request123abc" | ||
) | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...lin/com/zsoltbertalan/flickslate/movies/data/network/model/CreateSessionReplyDtoMother.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,12 @@ | ||
package com.zsoltbertalan.flickslate.movies.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.account.data.network.model.CreateSessionReplyDto | ||
|
||
object CreateSessionReplyDtoMother { | ||
|
||
fun createCreateSessionReplyDto(success: Boolean = true) = CreateSessionReplyDto( | ||
success = success, | ||
session_id = "session123abc" | ||
) | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...lin/com/zsoltbertalan/flickslate/movies/data/network/model/DeleteSessionReplyDtoMother.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,11 @@ | ||
package com.zsoltbertalan.flickslate.movies.data.network.model | ||
|
||
import com.zsoltbertalan.flickslate.account.data.network.model.DeleteSessionReplyDto | ||
|
||
object DeleteSessionReplyDtoMother { | ||
|
||
fun createDeleteSessionReplyDto(success: Boolean = true) = DeleteSessionReplyDto( | ||
success = success, | ||
) | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...src/testFixtures/kotlin/com/zsoltbertalan/flickslate/movies/domain/model/AccountMother.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,11 @@ | ||
package com.zsoltbertalan.flickslate.movies.domain.model | ||
|
||
import com.zsoltbertalan.flickslate.shared.model.Account | ||
|
||
object AccountMother { | ||
|
||
fun createAccount() = Account( | ||
name = "John Doe" | ||
) | ||
|
||
} |
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