From c7daee5747ad12b1ea9e51707c3de5bcde940703 Mon Sep 17 00:00:00 2001 From: Kitselyuk Egor Date: Thu, 26 Sep 2024 14:20:25 +0300 Subject: [PATCH] MBX-3636: Fix error logs for empty monitoring --- .../MobileConfigSerializationManagerImpl.kt | 11 ++-- .../ConfigSerializationManagerTest.kt | 63 +++++++++++++++++++ ...ingMobileConfigSerializationManagerTest.kt | 10 ++- .../Config/EmptyConfigs/EmptyConfig.json | 1 + .../EmptyConfigs/EmptyLogsMonitoring.json | 3 + .../Config/EmptyConfigs/EmptyMonitoring.json | 3 + .../Config/EmptyConfigs/NoMonitoring.json | 20 ++++++ 7 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyConfig.json create mode 100644 sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyLogsMonitoring.json create mode 100644 sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyMonitoring.json create mode 100644 sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/NoMonitoring.json diff --git a/sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/data/managers/MobileConfigSerializationManagerImpl.kt b/sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/data/managers/MobileConfigSerializationManagerImpl.kt index 96596b07f..f4bcbeaff 100644 --- a/sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/data/managers/MobileConfigSerializationManagerImpl.kt +++ b/sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/data/managers/MobileConfigSerializationManagerImpl.kt @@ -6,7 +6,6 @@ import cloud.mindbox.mobile_sdk.inapp.data.dto.* import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.MobileConfigSerializationManager import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl import cloud.mindbox.mobile_sdk.logger.mindboxLogE -import cloud.mindbox.mobile_sdk.logger.mindboxLogW import cloud.mindbox.mobile_sdk.models.TreeTargetingDto import cloud.mindbox.mobile_sdk.models.operation.response.* import cloud.mindbox.mobile_sdk.models.operation.response.InAppConfigResponseBlank.InAppDtoBlank @@ -55,10 +54,10 @@ internal class MobileConfigSerializationManagerImpl(private val gson: Gson) : return null } - val inApps = deserializeInApps(jsonObject.getOrNull("inapps")) - val settings = deserializeSettings(jsonObject.getOrNull("settings")) - val abtests = deserializeAbtests(jsonObject.getOrNull("abtests")) - val monitoring = deserializeMonitoring(jsonObject.getOrNull("monitoring")) + val inApps = jsonObject.getOrNull("inapps")?.let { deserializeInApps(it) } + val settings = jsonObject.getOrNull("settings")?.let { deserializeSettings(it) } + val abtests = jsonObject.getOrNull("abtests")?.let { deserializeAbtests(it) } + val monitoring = jsonObject.getOrNull("monitoring")?.let { deserializeMonitoring(it) } return InAppConfigResponseBlank(inApps, monitoring, settings, abtests) } @@ -70,7 +69,7 @@ internal class MobileConfigSerializationManagerImpl(private val gson: Gson) : runCatching { gson.fromJson(log, LogRequestDtoBlank::class.java)?.copy() }.getOrNull { - mindboxLogW("Failed to parse log block", it) + mindboxLogE("Failed to parse logs block", it) } }) }.getOrNull { diff --git a/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/ConfigSerializationManagerTest.kt b/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/ConfigSerializationManagerTest.kt index 9f64b28e0..7bbe7281e 100644 --- a/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/ConfigSerializationManagerTest.kt +++ b/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/ConfigSerializationManagerTest.kt @@ -4,8 +4,11 @@ import android.app.Application import cloud.mindbox.mobile_sdk.di.MindboxDI import cloud.mindbox.mobile_sdk.di.mindboxInject import cloud.mindbox.mobile_sdk.inapp.data.managers.MobileConfigSerializationManagerImpl +import cloud.mindbox.mobile_sdk.logger.mindboxLogE import io.mockk.every import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.verify import junit.framework.TestCase.assertNotNull import junit.framework.TestCase.assertNull import org.junit.Before @@ -157,4 +160,64 @@ internal class ConfigSerializationManagerTest { assertNotNull(config.settings) assertNotNull(config.inApps) } + + @Test + fun `empty config`() { + // Key is `abtestsTest` instead of `abtests` + mockkStatic(::mindboxLogE) + val json = getJson("ConfigParsing/Config/EmptyConfigs/EmptyConfig.json") + val config = manager.deserializeToConfigDtoBlank(json.toString())!! + + assertNull(config.abtests) + assertNull(config.monitoring) + assertNull(config.settings) + assertNull(config.inApps) + + verify(exactly = 0) { mindboxLogE(any(), any()) } + } + + @Test + fun `no monitoring in config`() { + // Key is `abtestsTest` instead of `abtests` + mockkStatic(::mindboxLogE) + val json = getJson("ConfigParsing/Config/EmptyConfigs/NoMonitoring.json") + val config = manager.deserializeToConfigDtoBlank(json.toString())!! + + assertNull(config.monitoring) + assertNotNull(config.abtests) + assertNotNull(config.settings) + assertNotNull(config.inApps) + + verify(exactly = 0) { mindboxLogE(any(), any()) } + } + + @Test + fun `empty monitoring in config`() { + // Key is `abtestsTest` instead of `abtests` + mockkStatic(::mindboxLogE) + val json = getJson("ConfigParsing/Config/EmptyConfigs/EmptyMonitoring.json") + val config = manager.deserializeToConfigDtoBlank(json.toString())!! + + assertNull(config.abtests) + assertNull(config.monitoring) + assertNull(config.settings) + assertNull(config.inApps) + + verify(exactly = 0) { mindboxLogE(any(), any()) } + } + + @Test + fun `empty monitoring logs in config`() { + // Key is `abtestsTest` instead of `abtests` + mockkStatic(::mindboxLogE) + val json = getJson("ConfigParsing/Config/EmptyConfigs/EmptyLogsMonitoring.json") + val config = manager.deserializeToConfigDtoBlank(json.toString())!! + + assertNull(config.abtests) + assertNull(config.monitoring) + assertNull(config.settings) + assertNull(config.inApps) + + verify(exactly = 0) { mindboxLogE(any(), any()) } + } } \ No newline at end of file diff --git a/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/MonitoringMobileConfigSerializationManagerTest.kt b/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/MonitoringMobileConfigSerializationManagerTest.kt index d1b75606a..c639caa04 100644 --- a/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/MonitoringMobileConfigSerializationManagerTest.kt +++ b/sdk/src/test/java/cloud/mindbox/mobile_sdk/inapp/data/managers/serialization/MonitoringMobileConfigSerializationManagerTest.kt @@ -4,10 +4,11 @@ import android.app.Application import cloud.mindbox.mobile_sdk.di.MindboxDI import cloud.mindbox.mobile_sdk.di.mindboxInject import cloud.mindbox.mobile_sdk.inapp.data.managers.MobileConfigSerializationManagerImpl -import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.MobileConfigSerializationManager -import com.google.gson.Gson +import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl import io.mockk.every import io.mockk.mockk +import io.mockk.mockkObject +import io.mockk.verify import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before @@ -68,19 +69,24 @@ class MonitoringMobileConfigSerializationManagerTest { @Test fun monitoringConfig_withLogsOneElementTypeError_shouldParseSuccessfullyRemainsElements() { // Type of `requestId` is Int instead of String + mockkObject(MindboxLoggerImpl) val json = getJson("ConfigParsing/Monitoring/MonitoringLogsOneElementTypeError.json") val config = manager.deserializeMonitoring(json)!! assertEquals(1, config.logs?.size) + verify(exactly = 1) { MindboxLoggerImpl.e(any(),"Failed to parse logs block", any()) } } @Test fun monitoringConfig_withLogsTwoElementsError_shouldParseSuccessfullyRemainsElements() { // Key is `request` instead `requestId` and key is `device` instead of `deviceUUID` + mockkObject(MindboxLoggerImpl) + val json = getJson("ConfigParsing/Monitoring/MonitoringLogsTwoElementsError.json") val config = manager.deserializeMonitoring(json)!! assertEquals(0, config.logs?.size) + verify(exactly = 2) { MindboxLoggerImpl.e(any(),"Failed to parse logs block", any()) } } @Test diff --git a/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyConfig.json b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyConfig.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyConfig.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyLogsMonitoring.json b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyLogsMonitoring.json new file mode 100644 index 000000000..54e903be5 --- /dev/null +++ b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyLogsMonitoring.json @@ -0,0 +1,3 @@ +{ + "monitoring": {} +} \ No newline at end of file diff --git a/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyMonitoring.json b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyMonitoring.json new file mode 100644 index 000000000..54e903be5 --- /dev/null +++ b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/EmptyMonitoring.json @@ -0,0 +1,3 @@ +{ + "monitoring": {} +} \ No newline at end of file diff --git a/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/NoMonitoring.json b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/NoMonitoring.json new file mode 100644 index 000000000..d26fda9c6 --- /dev/null +++ b/sdk/src/test/resources/ConfigParsing/Config/EmptyConfigs/NoMonitoring.json @@ -0,0 +1,20 @@ +{ + "settings": { + "operations": { + "viewProduct": { + "systemName": "ViewProduct" + }, + "viewCategory": { + "systemName": "ViewCategory" + }, + "setCart": { + "systemName": "SetCart" + } + }, + "ttl": { + "inapps": "1.00:00:00" + } + }, + "abtests": [], + "inapps": [] +} \ No newline at end of file