Skip to content

Commit d6674b1

Browse files
committed
feat: Enhance TimeBand router to support organisation-specific endpoints and add tests
1 parent e09d9cd commit d6674b1

File tree

5 files changed

+141
-5
lines changed

5 files changed

+141
-5
lines changed

src/main/kotlin/com/ctrlhub/core/settings/timebands/TimeBandsRouter.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class TimeBandsRouter(httpClient: HttpClient) : Router(httpClient) {
2020
* @return A list of all time bands
2121
*/
2222
suspend fun all(
23+
organisationId: String,
2324
requestParameters: TimeBandsRequestParameters = TimeBandsRequestParameters()
24-
): java.util.List<TimeBand> {
25-
val endpoint = "/settings/time-bands"
26-
return fetchJsonApiResource(endpoint, requestParameters.toMap())
25+
): List<TimeBand> {
26+
val endpoint = "/orgs/$organisationId/settings/time-bands"
27+
return fetchJsonApiResources(endpoint, requestParameters.toMap())
2728
}
2829

2930
/**
@@ -33,10 +34,11 @@ class TimeBandsRouter(httpClient: HttpClient) : Router(httpClient) {
3334
* @return The time band with the given ID
3435
*/
3536
suspend fun one(
37+
organisationId: String,
3638
timeBandId: String,
3739
requestParameters: TimeBandsRequestParameters = TimeBandsRequestParameters()
3840
): TimeBand {
39-
val endpoint = "/settings/time-bands/$timeBandId"
41+
val endpoint = "/orgs/$organisationId/settings/time-bands/$timeBandId"
4042
return fetchJsonApiResource(endpoint, requestParameters.toMap(), TimeBand::class.java)
4143
}
4244
}

src/main/kotlin/com/ctrlhub/core/settings/timebands/response/TimeBand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package com.ctrlhub.core.settings.timebands.response
33
import com.fasterxml.jackson.annotation.JsonCreator
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
55
import com.fasterxml.jackson.annotation.JsonProperty
6+
import com.github.jasminb.jsonapi.StringIdHandler
67
import com.github.jasminb.jsonapi.annotations.Id
78
import com.github.jasminb.jsonapi.annotations.Meta
89
import com.github.jasminb.jsonapi.annotations.Type
910

1011
@Type("time-bands")
1112
@JsonIgnoreProperties(ignoreUnknown = true)
1213
data class TimeBand @JsonCreator constructor(
13-
@Id var id: String = "",
14+
@Id(StringIdHandler::class) var id: String = "",
1415
@JsonProperty("end") var end: String = "",
1516
@JsonProperty("name") var name: String = "",
1617
@JsonProperty("start") var start: String = "",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.ctrlhub.core.settings.timebands
2+
3+
import com.ctrlhub.core.configureForTest
4+
import com.ctrlhub.core.settings.timebands.response.TimeBand
5+
import io.ktor.client.*
6+
import io.ktor.client.engine.mock.*
7+
import io.ktor.http.*
8+
import kotlinx.coroutines.runBlocking
9+
import org.junit.jupiter.api.Test
10+
import java.nio.file.Files
11+
import java.nio.file.Paths
12+
import kotlin.test.assertEquals
13+
import kotlin.test.assertIs
14+
import kotlin.test.assertNotNull
15+
16+
class TimeBandsRouterTest {
17+
@Test
18+
fun `can retrieve all time bands`() {
19+
val jsonFilePath = Paths.get("src/test/resources/settings/time-bands/all-response.json")
20+
val jsonContent = Files.readString(jsonFilePath)
21+
22+
val mockEngine = MockEngine { request ->
23+
respond(
24+
content = jsonContent,
25+
status = HttpStatusCode.OK,
26+
headers = headersOf(HttpHeaders.ContentType, "application/json")
27+
)
28+
}
29+
30+
val router = TimeBandsRouter(httpClient = HttpClient(mockEngine).configureForTest())
31+
32+
runBlocking {
33+
val response = router.all("org-123")
34+
assertIs<java.util.List<TimeBand>>(response)
35+
assertEquals(3, response.size)
36+
assertNotNull(response[0].id)
37+
assertNotNull(response[0].name)
38+
}
39+
}
40+
41+
@Test
42+
fun `can retrieve a single time band`() {
43+
val jsonFilePath = Paths.get("src/test/resources/settings/time-bands/one-response.json")
44+
val jsonContent = Files.readString(jsonFilePath)
45+
46+
val mockEngine = MockEngine { request ->
47+
respond(
48+
content = jsonContent,
49+
status = HttpStatusCode.OK,
50+
headers = headersOf(HttpHeaders.ContentType, "application/json")
51+
)
52+
}
53+
54+
val router = TimeBandsRouter(httpClient = HttpClient(mockEngine).configureForTest())
55+
56+
runBlocking {
57+
val response = router.one("org-123", "b1a1a111-1111-1111-1111-111111111111")
58+
assertIs<TimeBand>(response)
59+
assertEquals("b1a1a111-1111-1111-1111-111111111111", response.id)
60+
assertNotNull(response.name)
61+
}
62+
}
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"data": [
3+
{
4+
"id": "b1a1a111-1111-1111-1111-111111111111",
5+
"type": "time-bands",
6+
"attributes": {
7+
"end": "11:00+0000",
8+
"name": "Lorem",
9+
"start": "07:00+0000"
10+
},
11+
"meta": {
12+
"created_at": "2025-01-01T00:00:00.000Z",
13+
"updated_at": "2025-01-01T00:00:00.000Z",
14+
"modified_at": "2025-01-01T00:00:00.000Z"
15+
}
16+
},
17+
{
18+
"id": "b2a2a222-2222-2222-2222-222222222222",
19+
"type": "time-bands",
20+
"attributes": {
21+
"end": "15:00+0000",
22+
"name": "Ipsum",
23+
"start": "11:00+0000"
24+
},
25+
"meta": {
26+
"created_at": "2025-01-01T00:00:00.000Z",
27+
"updated_at": "2025-01-01T00:00:00.000Z",
28+
"modified_at": "2025-01-01T00:00:00.000Z"
29+
}
30+
},
31+
{
32+
"id": "b3a3a333-3333-3333-3333-333333333333",
33+
"type": "time-bands",
34+
"attributes": {
35+
"end": "07:00+0000",
36+
"name": "Dolor",
37+
"start": "15:00+0000"
38+
},
39+
"meta": {
40+
"created_at": "2025-01-01T00:00:00.000Z",
41+
"updated_at": "2025-01-01T00:00:00.000Z",
42+
"modified_at": "2025-01-01T00:00:00.000Z"
43+
}
44+
}
45+
],
46+
"jsonapi": {
47+
"version": "1.0"
48+
}
49+
}
50+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"data": {
3+
"id": "b1a1a111-1111-1111-1111-111111111111",
4+
"type": "time-bands",
5+
"attributes": {
6+
"end": "11:00+0000",
7+
"name": "Lorem",
8+
"start": "07:00+0000"
9+
},
10+
"meta": {
11+
"created_at": "2025-01-01T00:00:00.000Z",
12+
"updated_at": "2025-01-01T00:00:00.000Z",
13+
"modified_at": "2025-01-01T00:00:00.000Z"
14+
}
15+
},
16+
"jsonapi": {
17+
"version": "1.0"
18+
}
19+
}
20+

0 commit comments

Comments
 (0)