Skip to content

Commit c3bc772

Browse files
committed
fix: return vehicle inspection resource on create
1 parent 6539d41 commit c3bc772

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

src/main/kotlin/com/ctrlhub/core/api/ApiException.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import io.ktor.client.statement.*
55
/**
66
* Represents an exception that occurred when interacting with the API
77
*/
8-
open class ApiException(message: String, e: Throwable) : Exception(message, e)
8+
open class ApiException : Exception {
9+
constructor(message: String, e: Throwable) : super(message, e)
10+
constructor(message: String) : super(message)
11+
}
912

1013
/**
1114
* Represents an authorized error that occurs when interacting with the API.

src/main/kotlin/com/ctrlhub/core/assets/vehicles/VehicleInspectionsRouter.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.ctrlhub.core.assets.vehicles
22

3+
import com.ctrlhub.core.api.ApiException
34
import com.ctrlhub.core.api.ApiResourcePayload
45
import com.ctrlhub.core.assets.vehicles.payload.VehicleInspectionPayload
56
import com.ctrlhub.core.assets.vehicles.response.VehicleInspection
67
import com.ctrlhub.core.router.Router
78
import com.ctrlhub.core.router.request.RequestParameters
8-
import io.ktor.client.HttpClient
9-
import io.ktor.http.HttpStatusCode
10-
import kotlin.Result
9+
import io.ktor.client.*
10+
import io.ktor.http.*
1111

1212
/**
1313
* A router that interacts with the vehicle inspections realm of the Ctrl Hub API
@@ -64,18 +64,21 @@ class VehicleInspectionsRouter(httpClient: HttpClient) : Router(httpClient) {
6464
*
6565
* @return A result representing the outcome of this operation
6666
*/
67-
suspend fun create(organisationId: String, vehicleId: String, payload: VehicleInspectionPayload): Result<Boolean> {
68-
return runCatching {
69-
val endpoint = "/v3/orgs/$organisationId/assets/vehicles/$vehicleId/inspections"
67+
suspend fun create(organisationId: String, vehicleId: String, payload: VehicleInspectionPayload): VehicleInspection {
68+
val endpoint = "/v3/orgs/$organisationId/assets/vehicles/$vehicleId/inspections"
7069

71-
val response = performPost(
72-
endpoint, body = ApiResourcePayload(
73-
type = resourceType,
74-
data = payload
75-
)
70+
val response = performPost(
71+
endpoint, body = ApiResourcePayload(
72+
type = resourceType,
73+
data = payload
7674
)
77-
response.status == HttpStatusCode.Created
75+
)
76+
77+
if (response.status !== HttpStatusCode.Created) {
78+
throw ApiException("Unable to create vehicle inspection")
7879
}
80+
81+
return fetchJsonApiResource(response)
7982
}
8083
}
8184

src/main/kotlin/com/ctrlhub/core/router/Router.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ abstract class Router(val httpClient: HttpClient) {
7373
}
7474
}
7575

76+
protected suspend inline fun <reified T> fetchJsonApiResource(
77+
response: HttpResponse,
78+
vararg includedClasses: Class<*>
79+
): T {
80+
return try {
81+
val resourceConverter = ResourceConverter(getObjectMapper(), T::class.java, *includedClasses).apply {
82+
enableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES)
83+
}
84+
85+
val jsonApiResponse = resourceConverter.readDocument<T>(
86+
response.body<ByteArray>(), T::class.java
87+
)
88+
89+
jsonApiResponse.get() ?: throw ApiException("Failed to parse response", Exception())
90+
} catch (e: ClientRequestException) {
91+
if (e.response.status == HttpStatusCode.Unauthorized) {
92+
throw UnauthorizedException("Unauthorized action", e.response, e)
93+
}
94+
throw ApiClientException("Request failed", e.response, e)
95+
} catch (e: Exception) {
96+
throw ApiException("Request failed", e)
97+
}
98+
}
99+
76100
protected suspend inline fun <reified T> fetchJsonApiResources(
77101
endpoint: String,
78102
queryParameters: Map<String, String> = emptyMap(),

src/test/kotlin/com/ctrlhub/core/assets/vehicles/VehicleInspectionsRouterTest.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.runBlocking
1111
import org.junit.jupiter.api.Test
1212
import java.nio.file.Files
1313
import java.nio.file.Paths
14+
import java.time.LocalDateTime
1415
import kotlin.test.assertIs
1516
import kotlin.test.assertNotNull
1617
import kotlin.test.assertTrue
@@ -71,9 +72,12 @@ class VehicleInspectionsRouterTest {
7172

7273
@Test
7374
fun `can create a vehicle inspection`() {
75+
val jsonFilePath = Paths.get("src/test/resources/assets/vehicles/one-vehicle-inspection-response.json")
76+
val jsonContent = Files.readString(jsonFilePath)
77+
7478
val mockEngine = MockEngine { request ->
7579
respond(
76-
content = "",
80+
content = jsonContent,
7781
status = HttpStatusCode.Created,
7882
headers = headersOf(HttpHeaders.ContentType, "application/json")
7983
)
@@ -105,11 +109,12 @@ class VehicleInspectionsRouterTest {
105109
visibleDamage = listOf(true, false, null).random(),
106110
washersAndWipers = listOf(true, false, null).random(),
107111
windscreen = listOf(true, false, null).random()
108-
)
112+
),
113+
inspectedAt = LocalDateTime.now()
109114
)
110115
)
111116

112-
assertTrue(result.isSuccess)
117+
assertNotNull(result.id)
113118
}
114119
}
115120
}

0 commit comments

Comments
 (0)