|
| 1 | +package io.customer.datapipelines.util |
| 2 | + |
| 3 | +import io.customer.datapipelines.extensions.sanitizeForJson |
| 4 | +import io.customer.datapipelines.testutils.core.JUnitTest |
| 5 | +import io.customer.sdk.core.util.CioLogLevel |
| 6 | +import io.customer.sdk.core.util.Logger |
| 7 | +import io.mockk.every |
| 8 | +import io.mockk.mockk |
| 9 | +import io.mockk.verify |
| 10 | +import kotlinx.serialization.json.JsonNull |
| 11 | +import org.amshove.kluent.shouldBeEqualTo |
| 12 | +import org.junit.jupiter.api.Test |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests for the sanitizeForJson function which sanitizes data for JSON serialization by: |
| 16 | + * 1. Replacing null values with JsonNull |
| 17 | + * 2. Removing entries with NaN or infinity values from maps |
| 18 | + */ |
| 19 | +class SanitizeForJsonTest : JUnitTest() { |
| 20 | + @Test |
| 21 | + fun sanitize_givenNull_expectJsonNull() { |
| 22 | + val input = mapOf("key" to null) |
| 23 | + val expected = mapOf("key" to JsonNull) |
| 24 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun sanitize_givenFlatMapWithNull_expectJsonNullReplacement() { |
| 29 | + val input = mapOf("a" to 1, "b" to null) |
| 30 | + val expected = mapOf("a" to 1, "b" to JsonNull) |
| 31 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun sanitize_givenNestedMapWithNull_expectJsonNullReplacement() { |
| 36 | + val input = mapOf("meta" to mapOf("os" to "android", "root" to false, "vendor" to null, "version" to 13)) |
| 37 | + val expected = mapOf("meta" to mapOf("os" to "android", "root" to false, "vendor" to JsonNull, "version" to 13)) |
| 38 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun sanitize_givenListWithNull_expectJsonNullReplacement() { |
| 43 | + val input = mapOf("key" to listOf("a", null, "b")) |
| 44 | + val expected = mapOf("key" to listOf("a", JsonNull, "b")) |
| 45 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun sanitize_givenNestedStructureWithNull_expectJsonNullReplacement() { |
| 50 | + val input = mapOf( |
| 51 | + "maps" to listOf( |
| 52 | + mapOf("key" to null), |
| 53 | + mapOf("key" to "value") |
| 54 | + ), |
| 55 | + "colors" to listOf("red", null, "blue") |
| 56 | + ) |
| 57 | + val expected = mapOf( |
| 58 | + "maps" to listOf( |
| 59 | + mapOf("key" to JsonNull), |
| 60 | + mapOf("key" to "value") |
| 61 | + ), |
| 62 | + "colors" to listOf("red", JsonNull, "blue") |
| 63 | + ) |
| 64 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun sanitize_givenMapWithoutNull_expectReturnSameStructure() { |
| 69 | + val input = mapOf("x" to 42, "y" to "hello") |
| 70 | + input.sanitizeForJson() shouldBeEqualTo input |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun sanitize_givenListWithoutNull_expectReturnSameStructure() { |
| 75 | + val input = mapOf("key" to listOf("apple", "banana", "cherry")) |
| 76 | + input.sanitizeForJson() shouldBeEqualTo input |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun sanitize_givenMapWithNaN_expectFilteredMap() { |
| 81 | + val input = mapOf("x" to 42, "y" to Float.NaN, "z" to "hello") |
| 82 | + val expected = mapOf("x" to 42, "z" to "hello") |
| 83 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun sanitize_givenMapWithPositiveInfinity_expectFilteredMap() { |
| 88 | + val input = mapOf("x" to 42, "y" to Double.POSITIVE_INFINITY, "z" to "hello") |
| 89 | + val expected = mapOf("x" to 42, "z" to "hello") |
| 90 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun sanitize_givenMapWithNegativeInfinity_expectFilteredMap() { |
| 95 | + val input = mapOf("x" to 42, "y" to Float.NEGATIVE_INFINITY, "z" to "hello") |
| 96 | + val expected = mapOf("x" to 42, "z" to "hello") |
| 97 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun sanitize_givenNestedMapWithInvalidNumbers_expectFilteredMap() { |
| 102 | + val input = mapOf( |
| 103 | + "meta" to mapOf( |
| 104 | + "os" to "android", |
| 105 | + "version" to 13, |
| 106 | + "temperature" to Double.NaN, |
| 107 | + "battery" to Float.POSITIVE_INFINITY |
| 108 | + ), |
| 109 | + "data" to "valid", |
| 110 | + "list" to listOf(20, Double.NaN) |
| 111 | + ) |
| 112 | + val expected = mapOf( |
| 113 | + "meta" to mapOf( |
| 114 | + "os" to "android", |
| 115 | + "version" to 13 |
| 116 | + ), |
| 117 | + "data" to "valid", |
| 118 | + "list" to listOf(20) |
| 119 | + ) |
| 120 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun sanitize_givenComplexStructureWithNullsAndInvalidNumbers_expectCorrectFiltering() { |
| 125 | + val input = mapOf( |
| 126 | + "user" to mapOf( |
| 127 | + "name" to "John", |
| 128 | + "age" to null, |
| 129 | + "score" to Double.NaN |
| 130 | + ), |
| 131 | + "metrics" to listOf( |
| 132 | + mapOf("key" to "views", "value" to 100), |
| 133 | + listOf(15.0, Float.POSITIVE_INFINITY), |
| 134 | + mapOf("key" to "conversions", "value" to null) |
| 135 | + ), |
| 136 | + "settings" to mapOf( |
| 137 | + "enabled" to true, |
| 138 | + "timeout" to Float.NEGATIVE_INFINITY, |
| 139 | + "fallback" to null |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + val expected = mapOf( |
| 144 | + "user" to mapOf( |
| 145 | + "name" to "John", |
| 146 | + "age" to JsonNull |
| 147 | + ), |
| 148 | + "metrics" to listOf( |
| 149 | + mapOf("key" to "views", "value" to 100), |
| 150 | + listOf(15.0), |
| 151 | + mapOf("key" to "conversions", "value" to JsonNull) |
| 152 | + ), |
| 153 | + "settings" to mapOf( |
| 154 | + "enabled" to true, |
| 155 | + "fallback" to JsonNull |
| 156 | + ) |
| 157 | + ) |
| 158 | + |
| 159 | + input.sanitizeForJson() shouldBeEqualTo expected |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + fun sanitize_givenInvalidNumericValues_expectLogMessages() { |
| 164 | + // Create a mock logger |
| 165 | + val mockLogger = mockk<Logger>(relaxed = true) |
| 166 | + every { mockLogger.logLevel } returns CioLogLevel.DEBUG |
| 167 | + |
| 168 | + // Map with multiple invalid numeric values |
| 169 | + val input = mapOf( |
| 170 | + "nan" to Float.NaN, |
| 171 | + "posInf" to Double.POSITIVE_INFINITY, |
| 172 | + "negInf" to Float.NEGATIVE_INFINITY, |
| 173 | + "valid" to 42 |
| 174 | + ) |
| 175 | + |
| 176 | + // Sanitize with the mock logger |
| 177 | + input.sanitizeForJson(mockLogger) |
| 178 | + |
| 179 | + // Verify that debug was called 3 times (once for each invalid value) |
| 180 | + verify { |
| 181 | + mockLogger.error("Removed invalid JSON numeric value (NaN or infinity) for key: nan") |
| 182 | + mockLogger.error("Removed invalid JSON numeric value (NaN or infinity) for key: posInf") |
| 183 | + mockLogger.error("Removed invalid JSON numeric value (NaN or infinity) for key: negInf") |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments