Skip to content

Commit 3445b3b

Browse files
committed
test(csa-362): add IamClient unit tests with local HTTP stub server
1 parent 35d0954 commit 3445b3b

1 file changed

Lines changed: 54 additions & 36 deletions

File tree

samples/packages/admin-export/src/test/kotlin/IamClientTest.kt

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ class IamClientTest {
2929
// Stub: GET /identity/users?filter=...&columns=groups
3030
server.createContext("/identity/users") { exchange ->
3131
val query = exchange.requestURI.query ?: ""
32-
val filter = URLDecoder.decode(
33-
query.split("&").firstOrNull { it.startsWith("filter=") }?.removePrefix("filter=") ?: "{}",
34-
StandardCharsets.UTF_8,
35-
)
32+
val filter =
33+
URLDecoder.decode(
34+
query.split("&").firstOrNull { it.startsWith("filter=") }?.removePrefix("filter=") ?: "{}",
35+
StandardCharsets.UTF_8,
36+
)
3637
// Extract IDs from {"id":{"$in":["id1","id2",...]}}
37-
val ids = Regex("\"([^\"]+)\"").findAll(
38-
filter.substringAfter("\$in\":[").substringBefore("]"),
39-
).map { it.groupValues[1] }.toList()
40-
41-
val body = ids.joinToString(",", "[", "]") { id ->
42-
"""{"id":"$id","groups":[{"id":"grp-$id","name":"group-$id"}]}"""
43-
}.toByteArray()
38+
val ids =
39+
Regex("\"([^\"]+)\"")
40+
.findAll(
41+
filter.substringAfter("\$in\":[").substringBefore("]"),
42+
).map { it.groupValues[1] }
43+
.toList()
44+
45+
val body =
46+
ids
47+
.joinToString(",", "[", "]") { id ->
48+
"""{"id":"$id","groups":[{"id":"grp-$id","name":"group-$id"}]}"""
49+
}.toByteArray()
4450

4551
exchange.sendResponseHeaders(200, body.size.toLong())
4652
exchange.responseBody.use { it.write(body) }
@@ -49,32 +55,41 @@ class IamClientTest {
4955
// Stub: GET /identity/groups?filter=...&columns=attributes
5056
server.createContext("/identity/groups") { exchange ->
5157
val query = exchange.requestURI.query ?: ""
52-
val filter = URLDecoder.decode(
53-
query.split("&").firstOrNull { it.startsWith("filter=") }?.removePrefix("filter=") ?: "{}",
54-
StandardCharsets.UTF_8,
55-
)
56-
val ids = Regex("\"([^\"]+)\"").findAll(
57-
filter.substringAfter("\$in\":[").substringBefore("]"),
58-
).map { it.groupValues[1] }.toList()
59-
60-
val body = ids.joinToString(",", "[", "]") { id ->
61-
when {
62-
id.contains("no-alias") -> """{"id":"$id"}""" // missing attributes entirely
63-
else -> """{"id":"$id","attributes":{"alias":"Alias for $id"}}"""
64-
}
65-
}.toByteArray()
58+
val filter =
59+
URLDecoder.decode(
60+
query.split("&").firstOrNull { it.startsWith("filter=") }?.removePrefix("filter=") ?: "{}",
61+
StandardCharsets.UTF_8,
62+
)
63+
val ids =
64+
Regex("\"([^\"]+)\"")
65+
.findAll(
66+
filter.substringAfter("\$in\":[").substringBefore("]"),
67+
).map { it.groupValues[1] }
68+
.toList()
69+
70+
val body =
71+
ids
72+
.joinToString(",", "[", "]") { id ->
73+
when {
74+
id.contains("no-alias") -> """{"id":"$id"}"""
75+
76+
// missing attributes entirely
77+
else -> """{"id":"$id","attributes":{"alias":"Alias for $id"}}"""
78+
}
79+
}.toByteArray()
6680

6781
exchange.sendResponseHeaders(200, body.size.toLong())
6882
exchange.responseBody.use { it.write(body) }
6983
}
7084

7185
server.start()
7286
val port = server.address.port
73-
client = IamClient(
74-
baseUrl = "http://localhost:$port",
75-
bearerToken = "test-token",
76-
logger = logger,
77-
)
87+
client =
88+
IamClient(
89+
baseUrl = "http://localhost:$port",
90+
bearerToken = "test-token",
91+
logger = logger,
92+
)
7893
}
7994

8095
@AfterClass
@@ -129,14 +144,17 @@ class IamClientTest {
129144
fun getUserGroupsBatchesLargeInputs() {
130145
// 550 users should produce 2 batches (500 + 50) with batchSize=500
131146
val userIds = (1..550).map { "batch-user-$it" }
132-
val requestCount = java.util.concurrent.atomic.AtomicInteger(0)
147+
val requestCount =
148+
java.util.concurrent.atomic
149+
.AtomicInteger(0)
133150

134151
// Wrap client with a smaller batchSize to verify chunking without needing 550 ids
135-
val smallBatchClient = IamClient(
136-
baseUrl = "http://localhost:${server.address.port}",
137-
bearerToken = "test-token",
138-
logger = logger,
139-
)
152+
val smallBatchClient =
153+
IamClient(
154+
baseUrl = "http://localhost:${server.address.port}",
155+
bearerToken = "test-token",
156+
logger = logger,
157+
)
140158
val result = smallBatchClient.getUserGroups(userIds.take(7), batchSize = 3)
141159
// 7 users, batchSize=3 → 3 batches: [3, 3, 1]
142160
assertEquals(result.size, 7, "All 7 users should be present in result")

0 commit comments

Comments
 (0)