Skip to content

Commit

Permalink
Added kotlin decode test & removed unsupported date-time
Browse files Browse the repository at this point in the history
Archdoog committed Aug 7, 2024
1 parent 8af942f commit fd81f00
Showing 11 changed files with 2,097 additions and 15 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ publishing {

groupId = "com.osrm"
artifactId = "api"
version = "0.0.8"
version = "0.0.9"
}
}
}
10 changes: 10 additions & 0 deletions android/src/test/kotlin/TestFixture.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TestFixture {
companion object {
fun loadAsset(fileName: String): String {
val stream =
this.javaClass.classLoader.getResource(fileName)?.openStream()
?: throw IllegalArgumentException("Asset not found: $fileName")
return stream.bufferedReader().use { it.readText() }
}
}
}
27 changes: 27 additions & 0 deletions android/src/test/kotlin/TestModel.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import com.osrm.api.models.RouteResponse
import kotlin.test.Test
import com.osrm.api.models.Waypoint
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.fail

class ModelTest {

private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()

@Test
fun testWaypoint() {
val waypoint = Waypoint("name", listOf(1.0, 2.0))
assertEquals("name", waypoint.name)
assertEquals(listOf(1.0, 2.0), waypoint.location)
}

@Test
fun testDecode() {
val jsonData = TestFixture.loadAsset("route.json")
val response = moshi.adapter(RouteResponse::class.java).fromJson(jsonData)

assertNotNull(response?.routes)
assertEquals(RouteResponse.Code.Ok, response?.code)

val route = response?.routes?.first() ?: fail("response must have at least one route")

assertEquals(1, route.legs?.size)
assertEquals(4, route.legs?.first()?.steps?.size)

val step = route.legs?.first()?.steps?.first() ?: fail("route leg must have at least one step")

assertEquals("State Highway 65", step.name)
assertEquals("tezuoAezllgIfhAdStpAzV`hAxQlxA|X|i@vIdQpCnxClh@laCjd@dRdDr}@zOxgApR`pBf^jk@hKvmAnUfJ|AdDj@tCR", step.geometry)
}
}
2,044 changes: 2,044 additions & 0 deletions android/src/test/resources/route.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/ApiResponse.swift
Original file line number Diff line number Diff line change
@@ -29,9 +29,9 @@ public struct ApiResponse: Codable, Hashable {

public var code: Code
public var message: String?
public var dataVersion: Date?
public var dataVersion: String?

public init(code: Code, message: String? = nil, dataVersion: Date? = nil) {
public init(code: Code, message: String? = nil, dataVersion: String? = nil) {
self.code = code
self.message = message
self.dataVersion = dataVersion
4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/NearestResponse.swift
Original file line number Diff line number Diff line change
@@ -29,10 +29,10 @@ public struct NearestResponse: Codable, Hashable {

public var code: Code
public var message: String?
public var dataVersion: Date?
public var dataVersion: String?
public var waypoints: [NearestWaypoint]?

public init(code: Code, message: String? = nil, dataVersion: Date? = nil, waypoints: [NearestWaypoint]? = nil) {
public init(code: Code, message: String? = nil, dataVersion: String? = nil, waypoints: [NearestWaypoint]? = nil) {
self.code = code
self.message = message
self.dataVersion = dataVersion
4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/RouteResponse.swift
Original file line number Diff line number Diff line change
@@ -29,11 +29,11 @@ public struct RouteResponse: Codable, Hashable {

public var code: Code
public var message: String?
public var dataVersion: Date?
public var dataVersion: String?
public var waypoints: [Waypoint]?
public var routes: [Route]?

public init(code: Code, message: String? = nil, dataVersion: Date? = nil, waypoints: [Waypoint]? = nil, routes: [Route]? = nil) {
public init(code: Code, message: String? = nil, dataVersion: String? = nil, waypoints: [Waypoint]? = nil, routes: [Route]? = nil) {
self.code = code
self.message = message
self.dataVersion = dataVersion
4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/TableResponse.swift
Original file line number Diff line number Diff line change
@@ -29,15 +29,15 @@ public struct TableResponse: Codable, Hashable {

public var code: Code
public var message: String?
public var dataVersion: Date?
public var dataVersion: String?
/** array of arrays that stores the matrix in row-major order. durations[i][j] gives the travel time from the i-th waypoint to the j-th waypoint. Values are given in seconds. */
public var durations: [[Double]]?
public var distances: [[Double]]?
public var sources: [Waypoint]?
public var destinations: [Waypoint]?
public var fallbackSpeedCells: [[Int]]?

public init(code: Code, message: String? = nil, dataVersion: Date? = nil, durations: [[Double]]? = nil, distances: [[Double]]? = nil, sources: [Waypoint]? = nil, destinations: [Waypoint]? = nil, fallbackSpeedCells: [[Int]]? = nil) {
public init(code: Code, message: String? = nil, dataVersion: String? = nil, durations: [[Double]]? = nil, distances: [[Double]]? = nil, sources: [Waypoint]? = nil, destinations: [Waypoint]? = nil, fallbackSpeedCells: [[Int]]? = nil) {
self.code = code
self.message = message
self.dataVersion = dataVersion
4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/TripResponse.swift
Original file line number Diff line number Diff line change
@@ -29,11 +29,11 @@ public struct TripResponse: Codable, Hashable {

public var code: Code
public var message: String?
public var dataVersion: Date?
public var dataVersion: String?
public var waypoints: [TripWaypoint]?
public var trips: [Route]?

public init(code: Code, message: String? = nil, dataVersion: Date? = nil, waypoints: [TripWaypoint]? = nil, trips: [Route]? = nil) {
public init(code: Code, message: String? = nil, dataVersion: String? = nil, waypoints: [TripWaypoint]? = nil, trips: [Route]? = nil) {
self.code = code
self.message = message
self.dataVersion = dataVersion
4 changes: 2 additions & 2 deletions apple/Sources/OSRM/Models/ViaWaypoint.swift
Original file line number Diff line number Diff line change
@@ -12,10 +12,10 @@ import Foundation

public struct ViaWaypoint: Codable, Hashable {
public var distanceFromStart: Double?
public var geometryIndex: Double?
public var geometryIndex: Int?
public var waypointIndex: Int?

public init(distanceFromStart: Double? = nil, geometryIndex: Double? = nil, waypointIndex: Int? = nil) {
public init(distanceFromStart: Double? = nil, geometryIndex: Int? = nil, waypointIndex: Int? = nil) {
self.distanceFromStart = distanceFromStart
self.geometryIndex = geometryIndex
self.waypointIndex = waypointIndex
5 changes: 3 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ components:
type: string
data_version:
type: string
format: date-time
# format: date-time # TODO: Disabled because of missing swagger codegen kotlin adapter
NearestResponse:
allOf:
- $ref: '#/components/schemas/ApiResponse'
@@ -363,8 +363,9 @@ components:
properties:
distance_from_start:
type: number
format: double
geometry_index:
type: number
type: integer
waypoint_index:
type: integer
NearestWaypoint:

0 comments on commit fd81f00

Please sign in to comment.