Skip to content

Commit 23a6d74

Browse files
committed
feat: form schema router support, with test
1 parent 6cebc3c commit 23a6d74

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

src/main/kotlin/com/ctrlhub/core/datacapture/response/FormSchema.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package com.ctrlhub.core.datacapture.response
33
import com.fasterxml.jackson.annotation.JsonCreator
44
import com.fasterxml.jackson.annotation.JsonProperty
55
import com.fasterxml.jackson.databind.JsonNode
6+
import com.github.jasminb.jsonapi.StringIdHandler
7+
import com.github.jasminb.jsonapi.annotations.Id
68
import com.github.jasminb.jsonapi.annotations.Type
79

8-
@Type("")
10+
@Type("form-schemas")
911
data class FormSchema @JsonCreator constructor(
10-
val id: String,
12+
@Id(StringIdHandler::class) val id: String = "",
1113

1214
@JsonProperty("model")
1315
private val modelNode: JsonNode,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ctrlhub.core.datacapture
2+
3+
import com.ctrlhub.core.configureForTest
4+
import io.ktor.client.HttpClient
5+
import io.ktor.client.engine.mock.MockEngine
6+
import io.ktor.client.engine.mock.respond
7+
import io.ktor.http.HttpHeaders
8+
import io.ktor.http.HttpStatusCode
9+
import io.ktor.http.headersOf
10+
import io.ktor.utils.io.ByteReadChannel
11+
import kotlinx.coroutines.runBlocking
12+
import java.nio.file.Files
13+
import java.nio.file.Paths
14+
import kotlin.test.Test
15+
import kotlin.test.assertEquals
16+
import kotlin.test.assertTrue
17+
18+
class FormSchemasRouterTest {
19+
20+
@Test
21+
fun `test can get all form schemas successfully`() {
22+
// Load mocked JSON response
23+
val jsonFilePath = Paths.get("src/test/resources/datacapture/all-form-schemas-response.json")
24+
val jsonContent = Files.readString(jsonFilePath)
25+
26+
// Create a mock engine to intercept HTTP calls
27+
val mockEngine = MockEngine { request ->
28+
respond(
29+
content = ByteReadChannel(jsonContent),
30+
status = HttpStatusCode.OK,
31+
headers = headersOf(HttpHeaders.ContentType, "application/json")
32+
)
33+
}
34+
35+
val formSchemasRouter = FormSchemasRouter(httpClient = HttpClient(mockEngine).configureForTest())
36+
37+
runBlocking {
38+
val organisationId = "test-org-id"
39+
val formId = "test-form-id"
40+
41+
val response = formSchemasRouter.all(organisationId, formId)
42+
43+
assertEquals(1, response.data.size)
44+
val formSchema = response.data[0]
45+
assertEquals("1.0.0", formSchema.id)
46+
assertTrue(formSchema.model.contains("properties"))
47+
}
48+
}
49+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"data": [
3+
{
4+
"id": "1.0.0",
5+
"type": "form-schemas",
6+
"attributes": {
7+
"model": {
8+
"properties": {
9+
"property": {
10+
"x-source": {
11+
"resource-type": "properties"
12+
},
13+
"format": "uuid",
14+
"type": "string"
15+
}
16+
},
17+
"type": "object"
18+
},
19+
"views": null
20+
},
21+
"relationships": {
22+
"form": {
23+
"data": {
24+
"id": "66d36697-7e35-48d2-b759-273a22baa4a9",
25+
"type": "forms"
26+
}
27+
},
28+
"organisation": {
29+
"data": {
30+
"id": "ef0fcfd6-2a18-4c27-b2c2-b2563859b3b1",
31+
"type": "organisations"
32+
}
33+
}
34+
},
35+
"meta": {
36+
"created_at": "2025-05-21T09:38:51.621Z",
37+
"updated_at": "2025-05-21T09:38:51.621Z"
38+
}
39+
}
40+
],
41+
"meta": {
42+
"pagination": {
43+
"current_page": 1,
44+
"counts": {
45+
"resources": 1,
46+
"pages": 1
47+
},
48+
"requested": {
49+
"offset": 0,
50+
"limit": 100
51+
},
52+
"offsets": {
53+
"previous": null,
54+
"next": null
55+
}
56+
},
57+
"features": {
58+
"params": {
59+
"include": {
60+
"options": null
61+
},
62+
"sort": {
63+
"default": "",
64+
"options": null
65+
}
66+
}
67+
}
68+
},
69+
"jsonapi": {
70+
"version": "1.0"
71+
}
72+
}

0 commit comments

Comments
 (0)