diff --git a/infra/config-store/src/main/java/com/simprints/infra/config/store/ConfigRepositoryImpl.kt b/infra/config-store/src/main/java/com/simprints/infra/config/store/ConfigRepositoryImpl.kt index 8f6a72f276..1ffc910197 100644 --- a/infra/config-store/src/main/java/com/simprints/infra/config/store/ConfigRepositoryImpl.kt +++ b/infra/config-store/src/main/java/com/simprints/infra/config/store/ConfigRepositoryImpl.kt @@ -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( @@ -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) diff --git a/infra/config-store/src/test/java/com/simprints/infra/config/store/ConfigRepositoryImplTest.kt b/infra/config-store/src/test/java/com/simprints/infra/config/store/ConfigRepositoryImplTest.kt index 47288dae0c..2386621f05 100644 --- a/infra/config-store/src/test/java/com/simprints/infra/config/store/ConfigRepositoryImplTest.kt +++ b/infra/config-store/src/test/java/com/simprints/infra/config/store/ConfigRepositoryImplTest.kt @@ -17,9 +17,15 @@ 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 @@ -27,8 +33,10 @@ 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 { @@ -47,6 +55,7 @@ class ConfigRepositoryImplTest { @Before fun setup() { + mockkStatic(Simber::class) configServiceImpl = ConfigRepositoryImpl( localDataSource, remoteDataSource, @@ -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 @@ -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(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(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()) } + } }