Skip to content

Commit

Permalink
add brand and changelog to ClearanceStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisives committed Apr 8, 2022
1 parent daa6f62 commit 3639707
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ext {
coroutinesVersion = "1.4.0"
koinVersion = '3.1.4'
ver = [
"hmkit-fleet" : "0.3.2",
"hmkit-fleet" : "0.4.0",
"hmkit-crypto-telematics": "0.1",
"hmkit-auto-api" : "3.12.1",
]
Expand Down
2 changes: 1 addition & 1 deletion hmkit-fleet-consumer
2 changes: 1 addition & 1 deletion src/main/kotlin/HMKitFleet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object HMKitFleet {
vin: String,
brand: Brand,
controlMeasures: List<ControlMeasure>? = null
): CompletableFuture<Response<ClearanceStatus>> = GlobalScope.future {
): CompletableFuture<Response<RequestClearanceResponse>> = GlobalScope.future {
logger.debug("HMKitFleet: requestClearance: $vin")
koin.get<ClearanceRequests>().requestClearance(vin, brand, controlMeasures)
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/model/ClearanceStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ClearanceStatus(val vin: String, val status: Status) {
data class RequestClearanceResponse(val vin: String, val status: ClearanceStatus.Status)

@Serializable
data class ClearanceStatus(val vin: String, val status: Status, val brand:Brand? = null, val changelog:List<ChangeLogItem> = emptyList()) {
@Serializable
enum class Status {
@SerialName("approved")
Expand All @@ -43,6 +46,15 @@ data class ClearanceStatus(val vin: String, val status: Status) {
REVOKED,

@SerialName("rejected")
REJECTED
REJECTED,

@SerialName("canceling")
CANCELING,

@SerialName("canceled")
CANCELED
}
}
}

@Serializable
data class ChangeLogItem(val status: ClearanceStatus.Status, val timestamp:String)
5 changes: 3 additions & 2 deletions src/main/kotlin/network/ClearanceRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.serialization.json.*
import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.ControlMeasure
import com.highmobility.hmkitfleet.model.ClearanceStatus
import com.highmobility.hmkitfleet.model.RequestClearanceResponse
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
Expand All @@ -48,7 +49,7 @@ internal class ClearanceRequests(
vin: String,
brand: Brand,
controlMeasures: List<ControlMeasure>?
): Response<ClearanceStatus> {
): Response<RequestClearanceResponse> {
val body = requestBody(vin, brand, controlMeasures)
val authToken = authTokenRequests.getAuthToken()

Expand All @@ -71,7 +72,7 @@ internal class ClearanceRequests(
val statuses = jsonElement["vehicles"] as JsonArray
for (statusElement in statuses) {
val status =
Json.decodeFromJsonElement<ClearanceStatus>(statusElement)
Json.decodeFromJsonElement<RequestClearanceResponse>(statusElement)
if (status.vin == vin) {
return Response(status, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@ internal class ClearanceRequestsTest : BaseTest() {
"""
[
{
"brand":"bmw",
"vin": "WBADT43452G296403",
"status": "pending"
},
{
"brand":"bmw",
"vin": "WBADT43452G296404",
"status": "pending"
}
Expand Down Expand Up @@ -191,6 +193,81 @@ internal class ClearanceRequestsTest : BaseTest() {
assertTrue(status.response!![1].vin == "WBADT43452G296404")
}

@Test
fun parsesChangeLog() {
val mockResponse = MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
"""[
{
"brand":"bmw",
"changelog":[
{
"status":"pending",
"timestamp":"2022-03-30T12:27:25"
},
{
"status":"approved",
"timestamp":"2022-03-30T12:27:27"
},
{
"status":"revoked",
"timestamp":"2022-04-30T12:27:27"
}
],
"status":"revoked",
"vin":"WBY8P210X07J49112"
},
{
"brand":"bmw",
"changelog":[
{
"status":"pending",
"timestamp":"2022-04-01T12:27:25"
},
{
"status":"approved",
"timestamp":"2022-04-01T12:27:27"
}
],
"status":"approved",
"vin":"WBY8P210X07J49112"
},
{
"brand":"bmw",
"changelog":[],
"status":"approved",
"vin":"WBY8P210X07J49112"
},
{
"brand":"bmw",
"status":"approved",
"vin":"WBY8P210X07J49112"
}
]""".trimIndent()
)

mockWebServer.enqueue(mockResponse)
val mockUrl = mockWebServer.url("").toString()
val webService = ClearanceRequests(client, mockLogger, mockUrl, authTokenRequests)

val status = runBlocking {
webService.getClearanceStatuses()
}

assertTrue(status.response!![0].status == ClearanceStatus.Status.REVOKED)
assertTrue(status.response!![0].vin == "WBY8P210X07J49112")
assertTrue(status.response!![0].brand == Brand.BMW)
assertTrue(status.response!![0].changelog.size == 3)

assertTrue(status.response!![1].status == ClearanceStatus.Status.APPROVED)
assertTrue(status.response!![1].vin == "WBY8P210X07J49112")
assertTrue(status.response!![1].changelog.size == 2)

assertTrue(status.response!![2].changelog.isEmpty())
assertTrue(status.response!![2].changelog.isEmpty())
}

@Test
fun getClearanceStatusesAuthTokenError() = runBlocking {
testAuthTokenErrorReturned(mockWebServer, authTokenRequests) { mockUrl ->
Expand Down

0 comments on commit 3639707

Please sign in to comment.