Skip to content

Commit 2751cfd

Browse files
committed
[ECO-5426] Refactored ObjectId tests, kept only important ones
1 parent d7a3047 commit 2751cfd

2 files changed

Lines changed: 29 additions & 263 deletions

File tree

live-objects/src/main/kotlin/io/ably/lib/objects/ObjectId.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class ObjectId private constructor(
2727
}
2828

2929
// Parse format: type:hash@msTimestamp
30-
val parts = objectId.split(':', limit = 2)
30+
val parts = objectId.split(':')
3131
if (parts.size != 2) {
3232
throw objectError("Invalid object id: $objectId")
3333
}
@@ -41,12 +41,17 @@ internal class ObjectId private constructor(
4141
else -> throw objectError("Invalid object type in object id: $objectId")
4242
}
4343

44-
val hashAndTimestamp = rest.split('@', limit = 2)
44+
val hashAndTimestamp = rest.split('@')
4545
if (hashAndTimestamp.size != 2) {
4646
throw objectError("Invalid object id: $objectId")
4747
}
4848

4949
val hash = hashAndTimestamp[0]
50+
51+
if (hash.isEmpty()) {
52+
throw objectError("Invalid object id: $objectId")
53+
}
54+
5055
val msTimestampStr = hashAndTimestamp[1]
5156

5257
val msTimestamp = try {

live-objects/src/test/kotlin/io/ably/lib/objects/unit/ObjectIdTest.kt

Lines changed: 22 additions & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package io.ably.lib.objects.unit
33
import io.ably.lib.objects.ObjectId
44
import io.ably.lib.objects.ObjectType
55
import io.ably.lib.types.AblyException
6-
import io.ably.lib.types.ErrorInfo
76
import org.junit.Assert.assertEquals
8-
import org.junit.Assert.assertNotNull
97
import org.junit.Assert.assertThrows
108
import org.junit.Test
11-
import kotlin.test.assertFalse
129
import kotlin.test.assertTrue
1310

1411
class ObjectIdTest {
@@ -32,83 +29,23 @@ class ObjectIdTest {
3229
}
3330

3431
@Test
35-
fun testObjectIdWithComplexHash() {
36-
val objectIdString = "map:abc123-def456_ghi789@1640995200000"
37-
val objectId = ObjectId.fromString(objectIdString)
38-
39-
assertEquals(ObjectType.Map, objectId.type)
40-
assertEquals("map:abc123-def456_ghi789@1640995200000", objectId.toString())
41-
}
42-
43-
@Test
44-
fun testObjectIdWithLargeTimestamp() {
45-
val objectIdString = "counter:test@9999999999999"
46-
val objectId = ObjectId.fromString(objectIdString)
47-
48-
assertEquals(ObjectType.Counter, objectId.type)
49-
assertEquals("counter:test@9999999999999", objectId.toString())
50-
}
51-
52-
@Test
53-
fun testObjectIdWithZeroTimestamp() {
54-
val objectIdString = "map:test@0"
55-
val objectId = ObjectId.fromString(objectIdString)
56-
57-
assertEquals(ObjectType.Map, objectId.type)
58-
assertEquals("map:test@0", objectId.toString())
59-
}
60-
61-
@Test
62-
fun testObjectIdWithNegativeTimestamp() {
63-
val objectIdString = "counter:test@-1640995200000"
64-
val objectId = ObjectId.fromString(objectIdString)
65-
66-
assertEquals(ObjectType.Counter, objectId.type)
67-
assertEquals("counter:test@-1640995200000", objectId.toString())
68-
}
69-
70-
@Test
71-
fun testNullObjectId() {
72-
val exception = assertThrows(AblyException::class.java) {
73-
ObjectId.fromString(null)
74-
}
75-
76-
assertTrue(exception.message?.contains("Invalid object id: null") == true)
77-
assertEquals(92_000, exception.errorInfo?.code)
78-
assertEquals(500, exception.errorInfo?.statusCode)
79-
}
80-
81-
@Test
82-
fun testEmptyObjectId() {
32+
fun testInvalidObjectType() {
8333
val exception = assertThrows(AblyException::class.java) {
84-
ObjectId.fromString("")
34+
ObjectId.fromString("invalid:abc123@1640995200000")
8535
}
86-
87-
assertTrue(exception.message?.contains("Invalid object id: ") == true)
88-
assertEquals(92_000, exception.errorInfo?.code)
89-
assertEquals(500, exception.errorInfo?.statusCode)
36+
assertAblyExceptionError(exception)
9037
}
9138

9239
@Test
93-
fun testBlankObjectId() {
40+
fun testNullOrEmptyObjectId() {
9441
val exception = assertThrows(AblyException::class.java) {
95-
ObjectId.fromString(" ")
42+
ObjectId.fromString(null)
9643
}
97-
98-
assertTrue(exception.message?.contains("Invalid object id: ") == true)
99-
assertEquals(92_000, exception.errorInfo?.code)
100-
assertEquals(500, exception.errorInfo?.statusCode)
101-
}
102-
103-
@Test
104-
fun testObjectIdWithoutColon() {
105-
val exception = assertThrows(AblyException::class.java) {
106-
ObjectId.fromString("mapabc123@1640995200000")
44+
assertAblyExceptionError(exception)
45+
val exception1 = assertThrows(AblyException::class.java) {
46+
ObjectId.fromString("")
10747
}
108-
109-
assertTrue(exception.message?.contains("Invalid object id: mapabc123@1640995200000") == true)
110-
assertEquals(92_000, exception.errorInfo?.code)
111-
assertEquals(500, exception.errorInfo?.statusCode)
48+
assertAblyExceptionError(exception1)
11249
}
11350

11451
@Test
@@ -117,239 +54,63 @@ class ObjectIdTest {
11754
ObjectId.fromString("map:abc:123@1640995200000")
11855
}
11956

120-
assertTrue(exception.message?.contains("Invalid object id: map:abc:123@1640995200000") == true)
121-
assertEquals(92_000, exception.errorInfo?.code)
122-
assertEquals(500, exception.errorInfo?.statusCode)
123-
}
124-
125-
@Test
126-
fun testInvalidObjectType() {
127-
val exception = assertThrows(AblyException::class.java) {
128-
ObjectId.fromString("invalid:abc123@1640995200000")
129-
}
130-
131-
assertTrue(exception.message?.contains("Invalid object type in object id: invalid:abc123@1640995200000") == true)
132-
assertEquals(92_000, exception.errorInfo?.code)
133-
assertEquals(500, exception.errorInfo?.statusCode)
57+
assertAblyExceptionError(exception)
13458
}
13559

13660
@Test
13761
fun testObjectIdWithoutAtSymbol() {
13862
val exception = assertThrows(AblyException::class.java) {
13963
ObjectId.fromString("map:abc1231640995200000")
14064
}
141-
142-
assertTrue(exception.message?.contains("Invalid object id: map:abc1231640995200000") == true)
143-
assertEquals(92_000, exception.errorInfo?.code)
144-
assertEquals(500, exception.errorInfo?.statusCode)
65+
assertAblyExceptionError(exception)
14566
}
14667

14768
@Test
14869
fun testObjectIdWithMultipleAtSymbols() {
14970
val exception = assertThrows(AblyException::class.java) {
15071
ObjectId.fromString("map:abc123@1640995200000@extra")
15172
}
152-
153-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@1640995200000@extra") == true)
154-
assertEquals(92_000, exception.errorInfo?.code)
155-
assertEquals(500, exception.errorInfo?.statusCode)
73+
assertAblyExceptionError(exception)
15674
}
15775

15876
@Test
15977
fun testObjectIdWithEmptyHash() {
16078
val exception = assertThrows(AblyException::class.java) {
16179
ObjectId.fromString("map:@1640995200000")
16280
}
163-
164-
assertTrue(exception.message?.contains("Invalid object id: map:@1640995200000") == true)
165-
assertEquals(92_000, exception.errorInfo?.code)
166-
assertEquals(500, exception.errorInfo?.statusCode)
167-
}
168-
169-
@Test
170-
fun testObjectIdWithEmptyTimestamp() {
171-
val exception = assertThrows(AblyException::class.java) {
172-
ObjectId.fromString("map:abc123@")
173-
}
174-
175-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@") == true)
176-
assertEquals(92_000, exception.errorInfo?.code)
177-
assertEquals(500, exception.errorInfo?.statusCode)
178-
}
179-
180-
@Test
181-
fun testObjectIdWithNonNumericTimestamp() {
182-
val exception = assertThrows(AblyException::class.java) {
183-
ObjectId.fromString("map:abc123@invalid")
184-
}
185-
186-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@invalid") == true)
187-
assertEquals(92_000, exception.errorInfo?.code)
188-
assertEquals(500, exception.errorInfo?.statusCode)
189-
}
190-
191-
@Test
192-
fun testObjectIdWithDecimalTimestamp() {
193-
val exception = assertThrows(AblyException::class.java) {
194-
ObjectId.fromString("map:abc123@1640995200000.5")
195-
}
196-
197-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@1640995200000.5") == true)
198-
assertEquals(92_000, exception.errorInfo?.code)
199-
assertEquals(500, exception.errorInfo?.statusCode)
200-
}
201-
202-
@Test
203-
fun testObjectIdWithTimestampExceedingLongMaxValue() {
204-
val exception = assertThrows(AblyException::class.java) {
205-
ObjectId.fromString("map:abc123@9223372036854775808")
206-
}
207-
208-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@9223372036854775808") == true)
209-
assertEquals(92_000, exception.errorInfo?.code)
210-
assertEquals(500, exception.errorInfo?.statusCode)
211-
}
212-
213-
@Test
214-
fun testObjectIdWithUnicodeCharactersInHash() {
215-
val objectIdString = "counter:测试hash@1640995200000"
216-
val objectId = ObjectId.fromString(objectIdString)
217-
218-
assertEquals(ObjectType.Counter, objectId.type)
219-
assertEquals("counter:测试hash@1640995200000", objectId.toString())
220-
}
221-
222-
@Test
223-
fun testObjectIdWithVeryLongHash() {
224-
val longHash = "a".repeat(1000)
225-
val objectIdString = "map:$longHash@1640995200000"
226-
val objectId = ObjectId.fromString(objectIdString)
227-
228-
assertEquals(ObjectType.Map, objectId.type)
229-
assertEquals("map:$longHash@1640995200000", objectId.toString())
230-
}
231-
232-
@Test
233-
fun testObjectIdWithVeryLongTimestamp() {
234-
val objectIdString = "counter:test@1234567890123456789"
235-
val objectId = ObjectId.fromString(objectIdString)
236-
237-
assertEquals(ObjectType.Counter, objectId.type)
238-
assertEquals("counter:test@1234567890123456789", objectId.toString())
239-
}
240-
241-
@Test
242-
fun testObjectIdCaseSensitivity() {
243-
// Test that object types are case-sensitive
244-
val exception = assertThrows(AblyException::class.java) {
245-
ObjectId.fromString("MAP:abc123@1640995200000")
246-
}
247-
248-
assertTrue(exception.message?.contains("Invalid object type in object id: MAP:abc123@1640995200000") == true)
249-
assertEquals(92_000, exception.errorInfo?.code)
250-
assertEquals(500, exception.errorInfo?.statusCode)
251-
}
252-
253-
@Test
254-
fun testObjectIdWithWhitespace() {
255-
val exception = assertThrows(AblyException::class.java) {
256-
ObjectId.fromString(" map:abc123@1640995200000")
257-
}
258-
259-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@1640995200000") == true)
260-
assertEquals(92_000, exception.errorInfo?.code)
261-
assertEquals(500, exception.errorInfo?.statusCode)
262-
}
263-
264-
@Test
265-
fun testObjectIdWithTrailingWhitespace() {
266-
val exception = assertThrows(AblyException::class.java) {
267-
ObjectId.fromString("map:abc123@1640995200000 ")
268-
}
269-
270-
assertTrue(exception.message?.contains("Invalid object id: map:abc123@1640995200000 ") == true)
271-
assertEquals(92_000, exception.errorInfo?.code)
272-
assertEquals(500, exception.errorInfo?.statusCode)
81+
assertAblyExceptionError(exception)
27382
}
27483

27584
@Test
27685
fun testObjectIdWithOnlyType() {
27786
val exception = assertThrows(AblyException::class.java) {
27887
ObjectId.fromString("map:")
27988
}
280-
281-
assertTrue(exception.message?.contains("Invalid object id: map:") == true)
282-
assertEquals(92_000, exception.errorInfo?.code)
283-
assertEquals(500, exception.errorInfo?.statusCode)
89+
assertAblyExceptionError(exception)
28490
}
28591

28692
@Test
28793
fun testObjectIdWithOnlyTypeAndHash() {
28894
val exception = assertThrows(AblyException::class.java) {
28995
ObjectId.fromString("map:abc123")
29096
}
291-
292-
assertTrue(exception.message?.contains("Invalid object id: map:abc123") == true)
293-
assertEquals(92_000, exception.errorInfo?.code)
294-
assertEquals(500, exception.errorInfo?.statusCode)
295-
}
296-
297-
@Test
298-
fun testObjectIdWithOnlyTypeAndAtSymbol() {
299-
val exception = assertThrows(AblyException::class.java) {
300-
ObjectId.fromString("map:@")
301-
}
302-
303-
assertTrue(exception.message?.contains("Invalid object id: map:@") == true)
304-
assertEquals(92_000, exception.errorInfo?.code)
305-
assertEquals(500, exception.errorInfo?.statusCode)
97+
assertAblyExceptionError(exception)
30698
}
30799

308100
@Test
309101
fun testObjectIdWithOnlyTypeAndTimestamp() {
310102
val exception = assertThrows(AblyException::class.java) {
311103
ObjectId.fromString("map:1640995200000")
312104
}
105+
assertAblyExceptionError(exception)
106+
}
313107

314-
assertTrue(exception.message?.contains("Invalid object id: map:1640995200000") == true)
108+
private fun assertAblyExceptionError(
109+
exception: AblyException
110+
) {
111+
assertTrue(exception.errorInfo?.message?.contains("Invalid object id:") == true ||
112+
exception.errorInfo?.message?.contains("Invalid object type in object id:") == true)
315113
assertEquals(92_000, exception.errorInfo?.code)
316114
assertEquals(500, exception.errorInfo?.statusCode)
317115
}
318-
319-
@Test
320-
fun testObjectIdWithHashContainingAtSymbol() {
321-
val objectIdString = "map:abc@123@1640995200000"
322-
val objectId = ObjectId.fromString(objectIdString)
323-
324-
assertEquals(ObjectType.Map, objectId.type)
325-
assertEquals("map:abc@123@1640995200000", objectId.toString())
326-
}
327-
328-
@Test
329-
fun testObjectIdWithHashContainingColon() {
330-
val objectIdString = "map:abc:123@1640995200000"
331-
val objectId = ObjectId.fromString(objectIdString)
332-
333-
assertEquals(ObjectType.Map, objectId.type)
334-
assertEquals("map:abc:123@1640995200000", objectId.toString())
335-
}
336-
337-
@Test
338-
fun testObjectIdRoundTrip() {
339-
val originalString = "map:abc123@1640995200000"
340-
val objectId = ObjectId.fromString(originalString)
341-
val roundTripString = objectId.toString()
342-
343-
assertEquals(originalString, roundTripString)
344-
}
345-
346-
347-
@Test
348-
fun testObjectIdRoundTripWithUnicode() {
349-
val originalString = "map:测试hash@1640995200000"
350-
val objectId = ObjectId.fromString(originalString)
351-
val roundTripString = objectId.toString()
352-
353-
assertEquals(originalString, roundTripString)
354-
}
355116
}

0 commit comments

Comments
 (0)