|
| 1 | +package io.ably.lib.objects.unit.type |
| 2 | + |
| 3 | +import io.ably.lib.objects.* |
| 4 | +import io.ably.lib.objects.type.BaseLiveObject |
| 5 | +import io.ably.lib.objects.type.livecounter.DefaultLiveCounter |
| 6 | +import io.ably.lib.objects.type.livemap.DefaultLiveMap |
| 7 | +import io.mockk.mockk |
| 8 | +import org.junit.Test |
| 9 | +import kotlin.test.assertEquals |
| 10 | +import kotlin.test.assertFalse |
| 11 | +import kotlin.test.assertTrue |
| 12 | +import kotlin.test.assertFailsWith |
| 13 | + |
| 14 | +class BaseLiveObjectTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + fun `(RTLO1, RTLO2) BaseLiveObject should be abstract base class for LiveMap and LiveCounter`() { |
| 18 | + // RTLO2 - Check that BaseLiveObject is abstract |
| 19 | + val isAbstract = java.lang.reflect.Modifier.isAbstract(BaseLiveObject::class.java.modifiers) |
| 20 | + assertTrue(isAbstract, "BaseLiveObject should be an abstract class") |
| 21 | + |
| 22 | + // RTLO1 - Check that BaseLiveObject is the parent class of DefaultLiveMap and DefaultLiveCounter |
| 23 | + assertTrue(BaseLiveObject::class.java.isAssignableFrom(DefaultLiveMap::class.java), |
| 24 | + "DefaultLiveMap should extend BaseLiveObject") |
| 25 | + assertTrue(BaseLiveObject::class.java.isAssignableFrom(DefaultLiveCounter::class.java), |
| 26 | + "DefaultLiveCounter should extend BaseLiveObject") |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `(RTLO3) BaseLiveObject should have required properties`() { |
| 31 | + val liveMap: BaseLiveObject = DefaultLiveMap.zeroValue("map:testObject@1", mockk(), mockk()) |
| 32 | + val liveCounter: BaseLiveObject = DefaultLiveCounter.zeroValue("counter:testObject@1", mockk()) |
| 33 | + // RTLO3a - check that objectId is set correctly |
| 34 | + assertEquals("map:testObject@1", liveMap.objectId) |
| 35 | + assertEquals("counter:testObject@1", liveCounter.objectId) |
| 36 | + |
| 37 | + // RTLO3b, RTLO3b1 - check that siteTimeserials is initialized as an empty map |
| 38 | + assertEquals(emptyMap(), liveMap.siteTimeserials) |
| 39 | + assertEquals(emptyMap(), liveCounter.siteTimeserials) |
| 40 | + |
| 41 | + // RTLO3c - Create operation merged flag |
| 42 | + assertFalse(liveMap.createOperationIsMerged, "Create operation should not be merged by default") |
| 43 | + assertFalse(liveCounter.createOperationIsMerged, "Create operation should not be merged by default") |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `(RTLO4a1, RTLO4a2) canApplyOperation should accept ObjectMessage params and return boolean`() { |
| 48 | + // RTLO4a1a - Assert parameter types and return type based on method signature using reflection |
| 49 | + val method = BaseLiveObject::class.java.findMethod("canApplyOperation") |
| 50 | + |
| 51 | + // RTLO4a1a - Verify parameter types |
| 52 | + val parameters = method.parameters |
| 53 | + assertEquals(2, parameters.size, "canApplyOperation should have exactly 2 parameters") |
| 54 | + |
| 55 | + // First parameter should be String? (siteCode) |
| 56 | + assertEquals(String::class.java, parameters[0].type, "First parameter should be of type String?") |
| 57 | + assertTrue(parameters[0].isVarArgs.not(), "First parameter should not be varargs") |
| 58 | + |
| 59 | + // Second parameter should be String? (timeSerial) |
| 60 | + assertEquals(String::class.java, parameters[1].type, "Second parameter should be of type String?") |
| 61 | + assertTrue(parameters[1].isVarArgs.not(), "Second parameter should not be varargs") |
| 62 | + |
| 63 | + // RTLO4a2 - Verify return type |
| 64 | + assertEquals(Boolean::class.java, method.returnType, "canApplyOperation should return Boolean") |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `(RTLO4a3) canApplyOperation should throw error for null or empty incoming siteSerial`() { |
| 69 | + val liveMap: BaseLiveObject = DefaultLiveMap.zeroValue("map:testObject@1", mockk(), mockk()) |
| 70 | + |
| 71 | + // Test null serial |
| 72 | + assertFailsWith<Exception>("Should throw error for null serial") { |
| 73 | + liveMap.canApplyOperation("site1", null) |
| 74 | + } |
| 75 | + |
| 76 | + // Test empty serial |
| 77 | + assertFailsWith<Exception>("Should throw error for empty serial") { |
| 78 | + liveMap.canApplyOperation("site1", "") |
| 79 | + } |
| 80 | + |
| 81 | + // Test null siteCode |
| 82 | + assertFailsWith<Exception>("Should throw error for null site code") { |
| 83 | + liveMap.canApplyOperation(null, "serial1") |
| 84 | + } |
| 85 | + |
| 86 | + // Test empty siteCode |
| 87 | + assertFailsWith<Exception>("Should throw error for empty site code") { |
| 88 | + liveMap.canApplyOperation("", "serial1") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `(RTLO4a4, RTLO4a5) canApplyOperation should return true when existing siteSerial is null or empty`() { |
| 94 | + val liveMap: BaseLiveObject = DefaultLiveMap.zeroValue("map:testObject@1", mockk(), mockk()) |
| 95 | + assertTrue(liveMap.siteTimeserials.isEmpty(), "Initial siteTimeserials should be empty") |
| 96 | + |
| 97 | + // RTLO4a4 - Get siteSerial from siteTimeserials map |
| 98 | + // RTLO4a5 - Return true when siteSerial is null (no entry in map) |
| 99 | + assertTrue(liveMap.canApplyOperation("site1", "serial1"), |
| 100 | + "Should return true when no siteSerial exists for the site") |
| 101 | + |
| 102 | + // RTLO4a5 - Return true when siteSerial is empty string |
| 103 | + liveMap.siteTimeserials["site1"] = "" |
| 104 | + assertTrue(liveMap.canApplyOperation("site1", "serial1"), |
| 105 | + "Should return true when siteSerial is empty string") |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `(RTLO4a6) canApplyOperation should return true when message siteSerial is greater than existing siteSerial`() { |
| 110 | + val liveMap: BaseLiveObject = DefaultLiveMap.zeroValue("map:testObject@1", mockk(), mockk()) |
| 111 | + |
| 112 | + // Set existing siteSerial |
| 113 | + liveMap.siteTimeserials["site1"] = "serial1" |
| 114 | + |
| 115 | + // RTLO4a6 - Return true when message serial is greater (lexicographically) |
| 116 | + assertTrue(liveMap.canApplyOperation("site1", "serial2"), |
| 117 | + "Should return true when message serial 'serial2' > siteSerial 'serial1'") |
| 118 | + |
| 119 | + assertTrue(liveMap.canApplyOperation("site1", "serial10"), |
| 120 | + "Should return true when message serial 'serial10' > siteSerial 'serial1'") |
| 121 | + |
| 122 | + assertTrue(liveMap.canApplyOperation("site1", "serialA"), |
| 123 | + "Should return true when message serial 'serialA' > siteSerial 'serial1'") |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `(RTLO4a6) canApplyOperation should return false when message siteSerial is less than or equal to siteSerial`() { |
| 128 | + val liveMap: BaseLiveObject = DefaultLiveMap.zeroValue("map:testObject@1", mockk(), mockk()) |
| 129 | + |
| 130 | + // Set existing siteSerial |
| 131 | + liveMap.siteTimeserials["site1"] = "serial2" |
| 132 | + |
| 133 | + // RTLO4a6 - Return false when message serial is less than siteSerial |
| 134 | + assertFalse(liveMap.canApplyOperation("site1", "serial1"), |
| 135 | + "Should return false when message serial 'serial1' < siteSerial 'serial2'") |
| 136 | + |
| 137 | + // RTLO4a6 - Return false when message serial equals siteSerial |
| 138 | + assertFalse(liveMap.canApplyOperation("site1", "serial2"), |
| 139 | + "Should return false when message serial equals siteSerial") |
| 140 | + |
| 141 | + // RTLO4a6 - Return false when message serial is less (lexicographically) |
| 142 | + assertTrue(liveMap.canApplyOperation("site1", "serialA"), |
| 143 | + "Should return false when message serial 'serialA' < siteSerial 'serial2'") |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun `(RTLO4a) canApplyOperation should work with different site codes`() { |
| 148 | + val liveMap: BaseLiveObject = DefaultLiveCounter.zeroValue("map:testObject@1", mockk()) |
| 149 | + |
| 150 | + // Set serials for different sites |
| 151 | + liveMap.siteTimeserials["site1"] = "serial1" |
| 152 | + liveMap.siteTimeserials["site2"] = "serial5" |
| 153 | + |
| 154 | + // Test site1 |
| 155 | + assertTrue(liveMap.canApplyOperation("site1", "serial2"), |
| 156 | + "Should return true for site1 when serial2 > serial1") |
| 157 | + assertFalse(liveMap.canApplyOperation("site1", "serial1"), |
| 158 | + "Should return false for site1 when serial1 = serial1") |
| 159 | + |
| 160 | + // Test site2 |
| 161 | + assertTrue(liveMap.canApplyOperation("site2", "serial6"), |
| 162 | + "Should return true for site2 when serial6 > serial5") |
| 163 | + assertFalse(liveMap.canApplyOperation("site2", "serial4"), |
| 164 | + "Should return false for site2 when serial4 < serial5") |
| 165 | + |
| 166 | + // Test new site (should return true) |
| 167 | + assertTrue(liveMap.canApplyOperation("site3", "serial1"), |
| 168 | + "Should return true for new site with any serial") |
| 169 | + } |
| 170 | +} |
0 commit comments