-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
16 changed files
with
562 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/model/PaginationMetaApiModel.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,18 @@ | ||
package co.nimblehq.blisskmmic.data.model | ||
|
||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
class PaginationMetaApiModel( | ||
val page: Int, | ||
val pages: Int, | ||
@SerialName("page_size") | ||
val pageSize: Int, | ||
val records: Int | ||
) | ||
|
||
fun PaginationMetaApiModel.toPaginationMeta(): PaginationMeta { | ||
return PaginationMeta(page, pages, pageSize, records) | ||
} |
29 changes: 29 additions & 0 deletions
29
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/model/SurveyApiModel.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,29 @@ | ||
package co.nimblehq.blisskmmic.data.model | ||
|
||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.jsonapi.model.ApiJson | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SurveyApiModel( | ||
val title: String, | ||
val description: String, | ||
@SerialName("is_active") | ||
val isActive: Boolean, | ||
@SerialName("cover_image_url") | ||
val coverImageUrl: String, | ||
@SerialName("survey_type") | ||
val surveyType: String | ||
) | ||
|
||
fun SurveyApiModel.toSurvey(): Survey { | ||
return Survey( | ||
title, | ||
description, | ||
isActive, | ||
coverImageUrl, | ||
surveyType | ||
) | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
...src/commonMain/kotlin/co/nimblehq/blisskmmic/data/network/datasource/NetworkDataSource.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,18 +1,31 @@ | ||
package co.nimblehq.blisskmmic.data.network.datasource | ||
|
||
import co.nimblehq.blisskmmic.data.model.PaginationMetaApiModel | ||
import co.nimblehq.blisskmmic.data.model.SurveyApiModel | ||
import co.nimblehq.blisskmmic.data.network.core.NetworkClient | ||
import co.nimblehq.blisskmmic.data.network.target.LoginTargetType | ||
import co.nimblehq.blisskmmic.data.network.target.SurveySelectionTargetType | ||
import co.nimblehq.blisskmmic.data.network.target.SurveyTargetType | ||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.model.TokenApiModel | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface NetworkDataSource { | ||
|
||
fun logIn(target: LoginTargetType): Flow<TokenApiModel> | ||
fun survey(target: SurveyTargetType): Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>> | ||
} | ||
|
||
class NetworkDataSourceImpl(private val networkClient: NetworkClient): NetworkDataSource { | ||
|
||
override fun logIn(target: LoginTargetType): Flow<TokenApiModel> { | ||
return networkClient.fetch(target.requestBuilder) | ||
} | ||
|
||
override fun survey(target: SurveyTargetType): Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>> { | ||
return networkClient.fetchWithMeta<List<SurveyApiModel>, PaginationMetaApiModel>( | ||
target.requestBuilder | ||
) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/network/target/SurveyTargetType.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,24 @@ | ||
package co.nimblehq.blisskmmic.data.network.target | ||
|
||
import co.nimblehq.blisskmmic.data.network.helpers.TargetType | ||
import io.ktor.http.* | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
sealed class SurveyTargetType: TargetType | ||
|
||
class SurveySelectionTargetType(page: Int = 1, size: Int = 3): SurveyTargetType() { | ||
|
||
@Serializable | ||
data class SurveySelectionInput( | ||
@SerialName("page[number]") | ||
val pageNumber: Int, | ||
@SerialName("page[size]") | ||
val pageSize: Int | ||
) | ||
|
||
override var path = "surveys" | ||
override var method = HttpMethod.Post | ||
override var body = SurveySelectionInput(page, size) | ||
override var headers: Map<String, String>? = null | ||
} |
24 changes: 24 additions & 0 deletions
24
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryImpl.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,24 @@ | ||
package co.nimblehq.blisskmmic.data.repository | ||
|
||
import co.nimblehq.blisskmmic.data.model.* | ||
import co.nimblehq.blisskmmic.data.network.core.NetworkClient | ||
import co.nimblehq.blisskmmic.data.network.datasource.NetworkDataSource | ||
import co.nimblehq.blisskmmic.data.network.target.SurveySelectionTargetType | ||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class SurveyRepositoryImpl(private val networkDataSource: NetworkDataSource): SurveyRepository { | ||
|
||
override fun survey(page: Int): Flow<Pair<List<Survey>, PaginationMeta>> { | ||
return networkDataSource.survey(SurveySelectionTargetType(page)) | ||
.map { | ||
Pair( | ||
it.first.map { item -> item.toSurvey() }, | ||
it.second.toPaginationMeta() | ||
) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/di/koin/modules/RepositoryModule.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,9 +1,14 @@ | ||
package co.nimblehq.blisskmmic.di.koin.modules | ||
|
||
import co.nimblehq.blisskmmic.data.repository.SurveyRepositoryImpl | ||
import co.nimblehq.blisskmmic.data.repository.TokenRepositoryImpl | ||
import co.nimblehq.blisskmmic.di.koin.constants.TOKENIZED_NETWORK_CLIENT_KOIN | ||
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository | ||
import co.nimblehq.blisskmmic.domain.repository.TokenRepository | ||
import org.koin.core.qualifier.named | ||
import org.koin.dsl.module | ||
|
||
val repositoryModule = module { | ||
single<TokenRepository> { TokenRepositoryImpl(get(), get()) } | ||
factory<SurveyRepository> { SurveyRepositoryImpl(get(named(TOKENIZED_NETWORK_CLIENT_KOIN))) } | ||
} |
8 changes: 8 additions & 0 deletions
8
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/PaginationMeta.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,8 @@ | ||
package co.nimblehq.blisskmmic.domain.model | ||
|
||
data class PaginationMeta( | ||
val page: Int, | ||
val pages: Int, | ||
val pageSize: Int, | ||
val records: Int | ||
) |
11 changes: 11 additions & 0 deletions
11
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/Survey.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 co.nimblehq.blisskmmic.domain.model | ||
|
||
import kotlinx.serialization.SerialName | ||
|
||
data class Survey( | ||
val title: String, | ||
val description: String, | ||
val isActive: Boolean, | ||
val coverImageUrl: String, | ||
val surveyType: String | ||
) |
11 changes: 11 additions & 0 deletions
11
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/SurveyRepository.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 co.nimblehq.blisskmmic.domain.repository | ||
|
||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.model.Token | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SurveyRepository { | ||
|
||
fun survey(page: Int): Flow<Pair<List<Survey>, PaginationMeta>> | ||
} |
23 changes: 23 additions & 0 deletions
23
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCase.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,23 @@ | ||
package co.nimblehq.blisskmmic.domain.usecase | ||
|
||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.model.Token | ||
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface SurveyListUseCase { | ||
|
||
operator fun invoke(page: Int): Flow<List<Survey>> | ||
} | ||
|
||
class SurveyListUseCaseImpl( | ||
private val repository: SurveyRepository | ||
) : SurveyListUseCase { | ||
|
||
override operator fun invoke(page: Int): Flow<List<Survey>> { | ||
return repository | ||
.survey(page) | ||
.map { it.first } | ||
} | ||
} |
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
105 changes: 105 additions & 0 deletions
105
.../commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/TokenizedNetworkClientTest.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,105 @@ | ||
package co.nimblehq.blisskmmic.data.network.core | ||
|
||
import co.nimblehq.blisskmmic.data.database.datasource.LocalDataSource | ||
import co.nimblehq.blisskmmic.data.database.datasource.MockLocalDataSource | ||
import co.nimblehq.blisskmmic.data.database.model.TokenDatabaseModel | ||
import co.nimblehq.blisskmmic.helpers.mock.NETWORK_META_MOCK_MODEL_RESULT | ||
import co.nimblehq.blisskmmic.helpers.mock.NETWORK_MOCK_MODEL_RESULT | ||
import co.nimblehq.blisskmmic.helpers.mock.NetworkMetaMockModel | ||
import co.nimblehq.blisskmmic.helpers.mock.NetworkMockModel | ||
import co.nimblehq.blisskmmic.helpers.mock.ktor.jsonTokenizedMockEngine | ||
import co.nimblehq.jsonapi.model.JsonApiException | ||
import io.kotest.matchers.collections.shouldContain | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.request.* | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.test.runTest | ||
import org.kodein.mock.Mocker | ||
import org.kodein.mock.UsesMocks | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
import kotlin.test.fail | ||
|
||
@ExperimentalCoroutinesApi | ||
@UsesMocks(LocalDataSource::class) | ||
class TokenizedNetworkClientTest { | ||
|
||
val token = TokenDatabaseModel( | ||
"Access", | ||
"", | ||
1, | ||
"", | ||
1 | ||
) | ||
val path = "/user" | ||
val request = HttpRequestBuilder(path = path) | ||
|
||
@Test | ||
fun `when calling fetch, it returns correct object`() = runTest { | ||
val mocker = Mocker() | ||
val localDataSource = MockLocalDataSource(mocker) | ||
mocker.every { | ||
localDataSource.getToken() | ||
} returns flow { emit(token) } | ||
val engine = jsonTokenizedMockEngine( | ||
NETWORK_MOCK_MODEL_RESULT, | ||
token.accessToken, | ||
path | ||
) | ||
val networkClient = TokenizedNetworkClient(engine = engine, localDataSource) | ||
networkClient | ||
.fetch<NetworkMockModel>(request) | ||
.collect { | ||
it.title shouldBe "Hello" | ||
} | ||
} | ||
|
||
@Test | ||
fun `when calling fetchWithMeta, it returns correct object`() = runTest { | ||
val mocker = Mocker() | ||
val localDataSource = MockLocalDataSource(mocker) | ||
mocker.every { | ||
localDataSource.getToken() | ||
} returns flow { emit(token) } | ||
val engine = jsonTokenizedMockEngine( | ||
NETWORK_META_MOCK_MODEL_RESULT, | ||
token.accessToken, | ||
path | ||
) | ||
val networkClient = TokenizedNetworkClient(engine = engine, localDataSource) | ||
networkClient | ||
.fetchWithMeta<NetworkMockModel, NetworkMetaMockModel>(request) | ||
.collect { | ||
it.first.title shouldBe "Hello" | ||
it.second.page shouldBe 1 | ||
} | ||
} | ||
|
||
@Test | ||
fun `when calling fetch with incorrect token, it returns correct error`() = runTest { | ||
val mocker = Mocker() | ||
val localDataSource = MockLocalDataSource(mocker) | ||
mocker.every { | ||
localDataSource.getToken() | ||
} returns flow { emit(token) } | ||
val engine = jsonTokenizedMockEngine( | ||
NETWORK_MOCK_MODEL_RESULT, | ||
"no access", | ||
path | ||
) | ||
val networkClient = TokenizedNetworkClient(engine = engine, localDataSource) | ||
networkClient | ||
.fetch<NetworkMockModel>(request) | ||
.catch { error -> | ||
when(error) { | ||
is JsonApiException -> error.errors.map { it.code } shouldContain "unauthorized" | ||
else -> fail("Should not return incorrect error type") | ||
} | ||
} | ||
.collect { | ||
fail("Should not return object") | ||
} | ||
} | ||
} |
Oops, something went wrong.