|
| 1 | +package com.kvs.samsunghealthreporter.decorator |
| 2 | + |
| 3 | +import com.kvs.samsunghealthreporter.END |
| 4 | +import com.kvs.samsunghealthreporter.START |
| 5 | +import com.kvs.samsunghealthreporter.SamsungHealthException |
| 6 | +import com.kvs.samsunghealthreporter.model.DataSource |
| 7 | +import com.kvs.samsunghealthreporter.model.SourceFilter |
| 8 | +import com.kvs.samsunghealthreporter.model.TimeGroup |
| 9 | +import com.kvs.samsunghealthreporter.model.TimeRange |
| 10 | +import com.kvs.samsunghealthreporter.model.type.Aggregation |
| 11 | +import com.kvs.samsunghealthreporter.model.type.HealthType |
| 12 | +import com.kvs.samsunghealthreporter.model.type.MealType |
| 13 | +import com.kvs.samsunghealthreporter.model.type.TimeGroupUnit |
| 14 | +import com.samsung.android.sdk.health.data.data.AggregatedData |
| 15 | +import com.samsung.android.sdk.health.data.data.HealthDataPoint |
| 16 | +import com.samsung.android.sdk.health.data.device.AccessoryType |
| 17 | +import com.samsung.android.sdk.health.data.device.DeviceGroup |
| 18 | +import com.samsung.android.sdk.health.data.device.DeviceType |
| 19 | +import com.samsung.android.sdk.health.data.error.HealthDataException |
| 20 | +import com.samsung.android.sdk.health.data.error.PlatformInternalException |
| 21 | +import com.samsung.android.sdk.health.data.request.DataType |
| 22 | +import com.samsung.android.sdk.health.data.request.LocalDateGroupUnit |
| 23 | +import io.mockk.every |
| 24 | +import io.mockk.mockk |
| 25 | +import kotlinx.serialization.json.JsonArray |
| 26 | +import kotlinx.serialization.json.JsonNull |
| 27 | +import kotlinx.serialization.json.JsonObject |
| 28 | +import kotlinx.serialization.json.JsonPrimitive |
| 29 | +import org.junit.Test |
| 30 | +import java.time.Instant |
| 31 | +import kotlin.test.assertEquals |
| 32 | +import kotlin.test.assertFailsWith |
| 33 | +import kotlin.test.assertIs |
| 34 | +import kotlin.test.assertNull |
| 35 | +import com.samsung.android.sdk.health.data.data.DataSource as OriginalDataSource |
| 36 | +import com.samsung.android.sdk.health.data.device.Device as OriginalDevice |
| 37 | + |
| 38 | +class DecoratorTest { |
| 39 | + @Test |
| 40 | + fun `map values of every supported kind become json`() { |
| 41 | + val sut = |
| 42 | + mapOf( |
| 43 | + "null" to null, |
| 44 | + "json" to JsonPrimitive("raw"), |
| 45 | + "string" to "text", |
| 46 | + "number" to 1.5, |
| 47 | + "boolean" to true, |
| 48 | + "enum" to MealType.LUNCH, |
| 49 | + "map" to mapOf(1 to "one"), |
| 50 | + "list" to listOf(1, 2), |
| 51 | + "array" to arrayOf("a", "b"), |
| 52 | + ).asJsonElement |
| 53 | + |
| 54 | + assertIs<JsonObject>(sut) |
| 55 | + assertEquals(JsonNull, sut["null"]) |
| 56 | + assertEquals(JsonPrimitive("raw"), sut["json"]) |
| 57 | + assertEquals(JsonPrimitive(true), sut["boolean"]) |
| 58 | + assertEquals(JsonPrimitive("LUNCH"), sut["enum"]) |
| 59 | + assertEquals(JsonObject(mapOf("1" to JsonPrimitive("one"))), sut["map"]) |
| 60 | + assertEquals(JsonArray(listOf(JsonPrimitive(1), JsonPrimitive(2))), sut["list"]) |
| 61 | + assertEquals(JsonArray(listOf(JsonPrimitive("a"), JsonPrimitive("b"))), sut["array"]) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `unsupported map value throws`() { |
| 66 | + assertFailsWith<SamsungHealthException.InvalidValue> { mapOf("date" to Instant.EPOCH).asJsonElement } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `any other Samsung Health error becomes Platform with its code`() { |
| 71 | + val internal = PlatformInternalException(9000, "database") |
| 72 | + val sut = assertIs<SamsungHealthException.Platform>(internal.wrapped) |
| 73 | + assertEquals(9000, sut.errorCode) |
| 74 | + assertEquals(internal, sut.cause) |
| 75 | + assertNull(assertIs<SamsungHealthException.Platform>(HealthDataException(null, "unknown").wrapped).errorCode) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `devices keep their group or accessory type`() { |
| 80 | + fun device(type: DeviceType) = |
| 81 | + mockk<OriginalDevice> { |
| 82 | + every { id } returns "id" |
| 83 | + every { deviceType } returns type |
| 84 | + every { manufacturer } returns null |
| 85 | + every { model } returns null |
| 86 | + every { name } returns null |
| 87 | + } |
| 88 | + assertEquals("WEIGHT_SCALE", device(AccessoryType.WEIGHT_SCALE).harmonized.type) |
| 89 | + assertEquals("RING", device(DeviceGroup.RING).harmonized.type) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `enum without a matching entry throws`() { |
| 94 | + assertFailsWith<SamsungHealthException.InvalidValue> { |
| 95 | + DataType.SleepType.StageType.UNDEFINED |
| 96 | + .mapped<TimeGroupUnit>() |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `unsupported aggregate value throws`() { |
| 102 | + val data = |
| 103 | + mockk<AggregatedData<Any>> { |
| 104 | + every { startTime } returns Instant.ofEpochMilli(START) |
| 105 | + every { endTime } returns Instant.ofEpochMilli(END) |
| 106 | + every { value } returns "text" |
| 107 | + } |
| 108 | + assertFailsWith<SamsungHealthException.InvalidValue> { data.harmonize(Aggregation.STEPS_TOTAL) } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `instantaneous data point has no end`() { |
| 113 | + // Samsung Health rejects heart rate without an end time, but not instantaneous types like body temperature. |
| 114 | + val point = |
| 115 | + HealthDataPoint |
| 116 | + .builder() |
| 117 | + .setStartTime(Instant.ofEpochMilli(START)) |
| 118 | + .addFieldData(DataType.BodyTemperatureType.BODY_TEMPERATURE, 36.6f) |
| 119 | + .build() |
| 120 | + assertEquals(START, point.startTimestamp) |
| 121 | + assertNull(point.endTimestamp) |
| 122 | + assertFailsWith<SamsungHealthException.InvalidValue> { point.requiredEndTimestamp } |
| 123 | + assertFailsWith<SamsungHealthException.InvalidValue> { HealthType.HEART_RATE.sample(point) } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `data source is copied from Samsung Health`() { |
| 128 | + val point = |
| 129 | + mockk<HealthDataPoint> { |
| 130 | + every { dataSource } returns |
| 131 | + mockk<OriginalDataSource> { |
| 132 | + every { appId } returns "com.example" |
| 133 | + every { deviceId } returns "device-1" |
| 134 | + } |
| 135 | + every { zoneOffset } returns null |
| 136 | + } |
| 137 | + assertEquals(DataSource("com.example", "device-1"), point.source) |
| 138 | + assertNull(point.zoneOffsetSeconds) |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun `every date unit maps to a local date group`() { |
| 143 | + val units = |
| 144 | + mapOf( |
| 145 | + TimeGroupUnit.DAILY to LocalDateGroupUnit.DAILY, |
| 146 | + TimeGroupUnit.WEEKLY to LocalDateGroupUnit.WEEKLY, |
| 147 | + TimeGroupUnit.MONTHLY to LocalDateGroupUnit.MONTHLY, |
| 148 | + TimeGroupUnit.YEARLY to LocalDateGroupUnit.YEARLY, |
| 149 | + ) |
| 150 | + units.keys.forEach { TimeGroup(it, 2).asLocalDateGroup } |
| 151 | + assertEquals(4, units.size) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `source filters map to Samsung Health filters`() { |
| 156 | + SourceFilter.LocalDevice.asOriginal |
| 157 | + SourceFilter.SamsungHealth.asOriginal |
| 158 | + SourceFilter.App("com.example").asOriginal |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + fun `today is the default day`() { |
| 163 | + val sut = TimeRange.day() |
| 164 | + assertEquals(TimeRange.day(System.currentTimeMillis()).start, sut.start) |
| 165 | + } |
| 166 | +} |
0 commit comments