Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import retrofit2.HttpException
import java.net.HttpURLConnection
import javax.inject.Inject

internal class ConfigRepositoryImpl @Inject constructor(
Expand Down Expand Up @@ -125,7 +127,10 @@ internal class ConfigRepositoryImpl @Inject constructor(
localDataSource.storePrivacyNotice(projectId, language, privacyNotice)
flowCollector.emit(Succeed(language, privacyNotice))
} catch (t: Throwable) {
Simber.i("Failed to download privacy notice", t)
if ((t.cause as? HttpException)?.code() != HttpURLConnection.HTTP_NOT_FOUND) {
// Non-existence of resource isn't considered a download failure
Simber.i("Failed to download privacy notice", t)
}
flowCollector.emit(
if (t is BackendMaintenanceException) {
FailedBecauseBackendMaintenance(language, t, t.estimatedOutage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@ import com.simprints.infra.config.store.testtools.deviceState
import com.simprints.infra.config.store.testtools.project
import com.simprints.infra.config.store.testtools.projectConfiguration
import com.simprints.infra.config.store.tokenization.TokenizationProcessor
import com.simprints.infra.logging.Simber
import com.simprints.infra.network.SimNetwork
import com.simprints.infra.network.exceptions.BackendMaintenanceException
import com.simprints.testtools.common.syntax.assertThrows
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.HttpException
import retrofit2.Response
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.net.HttpURLConnection

class ConfigRepositoryImplTest {
companion object {
Expand All @@ -47,6 +55,7 @@ class ConfigRepositoryImplTest {

@Before
fun setup() {
mockkStatic(Simber::class)
configServiceImpl = ConfigRepositoryImpl(
localDataSource,
remoteDataSource,
Expand All @@ -56,6 +65,11 @@ class ConfigRepositoryImplTest {
)
}

@After
fun cleanup() {
unmockkStatic(Simber::class)
}

@Test
fun `should get the project locally if available`() = runTest {
coEvery { localDataSource.getProject() } returns project
Expand Down Expand Up @@ -260,4 +274,42 @@ class ConfigRepositoryImplTest {
assertThat(result).isEqualTo(config)
coVerify { localDataSource.getProjectConfiguration() }
}

@Test
fun `should log when failing to download privacy notice with non-404 response status code`() = runTest {
val code = HttpURLConnection.HTTP_INTERNAL_ERROR // 500
val exception = Exception("Server error").apply {
initCause(HttpException(Response.error<String>(code, "".toResponseBody(null))))
}
every { localDataSource.hasPrivacyNoticeFor(PROJECT_ID, LANGUAGE) } returns false
coEvery {
remoteDataSource.getPrivacyNotice(
PROJECT_ID,
"${PRIVACY_NOTICE_FILE}_$LANGUAGE",
)
} throws exception

configServiceImpl.getPrivacyNotice(PROJECT_ID, LANGUAGE).toList()

verify(exactly = 1) { Simber.i(eq("Failed to download privacy notice"), eq(exception)) }
}

@Test
fun `should not log when failing to download privacy notice with 404 response status code`() = runTest {
val code = HttpURLConnection.HTTP_NOT_FOUND
val exception = Exception("Resource not found").apply {
initCause(HttpException(Response.error<String>(code, "".toResponseBody(null))))
}
every { localDataSource.hasPrivacyNoticeFor(PROJECT_ID, LANGUAGE) } returns false
coEvery {
remoteDataSource.getPrivacyNotice(
PROJECT_ID,
"${PRIVACY_NOTICE_FILE}_$LANGUAGE",
)
} throws exception

configServiceImpl.getPrivacyNotice(PROJECT_ID, LANGUAGE).toList()

verify(exactly = 0) { Simber.i(any(), any()) }
}
}