From 11b8ab41057526931ad9fbe84dea46a69f4d59c9 Mon Sep 17 00:00:00 2001 From: Andrew Haisting <142518658+ahaisting-livefront@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:20:32 -0500 Subject: [PATCH] BIT-587 Add ConfigService layer to wrap ConfigApi (#80) --- .../datasource/network/di/NetworkModule.kt | 7 +- .../network/service/ConfigService.kt | 14 ++++ .../network/service/ConfigServiceImpl.kt | 8 +++ .../network/service/ConfigServiceTest.kt | 65 +++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigService.kt create mode 100644 app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceImpl.kt create mode 100644 app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceTest.kt diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/di/NetworkModule.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/di/NetworkModule.kt index efcf29c49..0ea5bbc81 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/di/NetworkModule.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/di/NetworkModule.kt @@ -1,9 +1,10 @@ package com.x8bit.bitwarden.data.platform.datasource.network.di import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory -import com.x8bit.bitwarden.data.platform.datasource.network.api.ConfigApi import com.x8bit.bitwarden.data.platform.datasource.network.core.ResultCallAdapterFactory import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.AuthTokenInterceptor +import com.x8bit.bitwarden.data.platform.datasource.network.service.ConfigService +import com.x8bit.bitwarden.data.platform.datasource.network.service.ConfigServiceImpl import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -30,8 +31,8 @@ object NetworkModule { @Provides @Singleton - fun providesConfigApiService(@Named(UNAUTHORIZED) retrofit: Retrofit): ConfigApi = - retrofit.create() + fun providesConfigService(@Named(UNAUTHORIZED) retrofit: Retrofit): ConfigService = + ConfigServiceImpl(retrofit.create()) fun provideOkHttpClient(): OkHttpClient { return OkHttpClient.Builder() diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigService.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigService.kt new file mode 100644 index 000000000..3d28dc1ba --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigService.kt @@ -0,0 +1,14 @@ +package com.x8bit.bitwarden.data.platform.datasource.network.service + +import com.x8bit.bitwarden.data.platform.datasource.network.model.ConfigResponseJson + +/** + * Provides an API for querying config endpoints. + */ +interface ConfigService { + + /** + * Fetch app configuration. + */ + suspend fun getConfig(): Result +} diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceImpl.kt new file mode 100644 index 000000000..f90b01c6c --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceImpl.kt @@ -0,0 +1,8 @@ +package com.x8bit.bitwarden.data.platform.datasource.network.service + +import com.x8bit.bitwarden.data.platform.datasource.network.api.ConfigApi +import com.x8bit.bitwarden.data.platform.datasource.network.model.ConfigResponseJson + +class ConfigServiceImpl(private val configApi: ConfigApi) : ConfigService { + override suspend fun getConfig(): Result = configApi.getConfig() +} diff --git a/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceTest.kt new file mode 100644 index 000000000..8db616ec3 --- /dev/null +++ b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/network/service/ConfigServiceTest.kt @@ -0,0 +1,65 @@ +package com.x8bit.bitwarden.data.platform.datasource.network.service + +import com.x8bit.bitwarden.data.platform.base.BaseServiceTest +import com.x8bit.bitwarden.data.platform.datasource.network.api.ConfigApi +import com.x8bit.bitwarden.data.platform.datasource.network.model.ConfigResponseJson +import kotlinx.coroutines.test.runTest +import okhttp3.mockwebserver.MockResponse +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import retrofit2.create + +class ConfigServiceTest : BaseServiceTest() { + + private val api: ConfigApi = retrofit.create() + private val service = ConfigServiceImpl(api) + + @Test + fun `getConfig should call ConfigApi`() = runTest { + server.enqueue(MockResponse().setBody(CONFIG_RESPONSE_JSON)) + assertEquals(Result.success(CONFIG_RESPONSE), service.getConfig()) + } +} + +private const val CONFIG_RESPONSE_JSON = """ +{ + "object": "config", + "version": "1", + "gitHash": "gitHash", + "server": { + "name": "default", + "url": "url" + }, + "environment": { + "cloudRegion": "US", + "vault": "vaultUrl", + "api": "apiUrl", + "identity": "identityUrl", + "notifications": "notificationsUrl", + "sso": "ssoUrl" + }, + "featureStates": { + "feature one": false + } +} +""" +private val CONFIG_RESPONSE = ConfigResponseJson( + type = "config", + version = "1", + gitHash = "gitHash", + server = ConfigResponseJson.ServerJson( + name = "default", + url = "url", + ), + environment = ConfigResponseJson.EnvironmentJson( + cloudRegion = "US", + vaultUrl = "vaultUrl", + apiUrl = "apiUrl", + notificationsUrl = "notificationsUrl", + identityUrl = "identityUrl", + ssoUrl = "ssoUrl", + ), + featureStates = mapOf( + "feature one" to false, + ), +)