Skip to content

Commit

Permalink
fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisives committed Nov 16, 2023
1 parent 3dadc30 commit bd8e39f
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import okhttp3.OkHttpClient

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import io.jsonwebtoken.Jwts
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.ClearanceStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.network.AccessTokenRequests
import com.highmobility.hmkitfleet.network.Cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
*/
package com.highmobility.hmkitfleet.network

import com.highmobility.hmkitfleet.com.highmobility.hmkitfleet.HMKitCredentials
import com.highmobility.hmkitfleet.HMKitCredentials
import com.highmobility.hmkitfleet.model.AccessToken
import com.highmobility.hmkitfleet.utils.await
import kotlinx.serialization.json.Json
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.Logger
import com.highmobility.hmkitfleet.utils.await
import java.net.HttpURLConnection
import java.util.UUID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.ClearanceStatus
import com.highmobility.hmkitfleet.model.ControlMeasure
import com.highmobility.hmkitfleet.model.RequestClearanceResponse
import com.highmobility.hmkitfleet.utils.await
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
Expand All @@ -41,7 +42,6 @@ import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.Logger
import com.highmobility.hmkitfleet.utils.await
import java.net.HttpURLConnection

internal class ClearanceRequests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package com.highmobility.hmkitfleet.network

import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.EligibilityStatus
import com.highmobility.hmkitfleet.utils.await
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
Expand All @@ -35,7 +36,6 @@ import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.Logger
import com.highmobility.hmkitfleet.utils.await
import java.net.HttpURLConnection

internal class UtilityRequests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
package com.highmobility.hmkitfleet.network

import com.highmobility.hmkitfleet.utils.await
import okhttp3.OkHttpClient
import okhttp3.Request
import org.slf4j.Logger
import com.highmobility.hmkitfleet.utils.await
import java.net.HttpURLConnection

internal class VehicleDataRequests(
Expand All @@ -45,7 +45,7 @@ internal class VehicleDataRequests(
val authToken = accessTokenRequests.getAccessToken()

if (authToken.error != null) return Response(null, authToken.error)
println("auth: Bearer ${authToken.response?.accessToken}")

val request = Request.Builder()
.url("$baseUrl/vehicle-data/autoapi-13/$vin")
.header("Content-Type", "application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.com.highmobility.hmkitfleet.HMKitConfiguration
import com.highmobility.hmkitfleet.com.highmobility.hmkitfleet.HMKitOAuthCredentials
import com.highmobility.hmkitfleet.model.AccessToken
import io.mockk.CapturingSlot
import io.mockk.Runs
Expand All @@ -34,6 +32,9 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.jupiter.api.BeforeEach
import org.koin.test.KoinTest
import org.slf4j.Logger
Expand All @@ -48,7 +49,25 @@ internal fun notExpiredAccessToken(): AccessToken {
}

open class BaseTest : KoinTest {
val privateKeyConfiguration = readPrivateKeyConfiguration()
val oauthCredentials = readOauthCredentials()
val privateKeyCredentials = readPrivateKeyCredentials()

private fun readPrivateKeyCredentials(): HMKitPrivateKeyCredentials {
val jsonString = readPrivateKeyJsonString()
val json = Json.parseToJsonElement(jsonString) as JsonObject

val privateKey = json["private_key"]!!.jsonPrimitive.content
val privateKeyId = json["id"]!!.jsonPrimitive.content

val credentials =
HMKitPrivateKeyCredentials(
"client_id",
privateKey,
privateKeyId
)

return credentials
}

internal fun readPrivateKeyJsonString(): String {
val homeDir = System.getProperty("user.home")
Expand All @@ -67,7 +86,7 @@ open class BaseTest : KoinTest {
return credentialsContent
}

private fun readPrivateKeyConfiguration(): HMKitConfiguration {
private fun readOauthCredentials(): HMKitConfiguration {
val credentialsContent = readPrivateKeyJsonString()

val configuration = spyk(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.BaseTest
import io.mockk.every
import io.mockk.mockk
import okhttp3.Call
Expand Down Expand Up @@ -51,7 +50,8 @@ class HMKitConfigurationTest : BaseTest() {
}
val hmkitConf = HMKitConfiguration.Builder()
.client(client)
.credentials(privateKeyConfiguration.credentials)
.credentials(oauthCredentials.credentials)
.environment(HMKitFleet.Environment.SANDBOX)
.build()

val hmkit = HMKitFleet(hmkitConf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.com.highmobility.hmkitfleet
package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.BaseTest
import io.jsonwebtoken.Jwts
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package com.highmobility.hmkitfleet

import com.highmobility.hmkitfleet.com.highmobility.hmkitfleet.HMKitFleet
import com.highmobility.hmkitfleet.com.highmobility.hmkitfleet.Koin
import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.ClearanceStatus
import com.highmobility.hmkitfleet.model.RequestClearanceResponse
Expand Down Expand Up @@ -90,7 +88,7 @@ class HMKitFleetTest : BaseTest() {
clearanceRequests.deleteClearance(any())
} returns Response(RequestClearanceResponse("vin1", ClearanceStatus.Status.REVOKING), null)

val hmkit = HMKitFleet(privateKeyConfiguration)
val hmkit = HMKitFleet(oauthCredentials)
val delete = hmkit.deleteClearance("vin1").get()
assertTrue(delete.response?.vin == "vin1")
assertTrue(delete.response?.status == ClearanceStatus.Status.REVOKING)
Expand All @@ -102,12 +100,35 @@ class HMKitFleetTest : BaseTest() {
clearanceRequests.getClearanceStatus("vin1")
} returns Response(ClearanceStatus("vin1", ClearanceStatus.Status.REVOKING), null)

val hmkit = HMKitFleet(privateKeyConfiguration)
val hmkit = HMKitFleet(oauthCredentials)
val clearance = hmkit.getClearanceStatus("vin1").get()
assertTrue(clearance.response?.vin == "vin1")
assertTrue(clearance.response?.status == ClearanceStatus.Status.REVOKING)
}

@Test
fun requestClearance() = runBlocking {
coEvery {
clearanceRequests.requestClearance("vin1", Brand.SANDBOX, null)
} returns Response(RequestClearanceResponse("vin1", ClearanceStatus.Status.APPROVED), null)

val hmkit = HMKitFleet(oauthCredentials)
val clearance = hmkit.requestClearance("vin1", Brand.SANDBOX).get()
assertTrue(clearance.response?.vin == "vin1")
assertTrue(clearance.response?.status == ClearanceStatus.Status.APPROVED)
}

@Test
fun requestClearanceStatuses() = runBlocking {
coEvery {
clearanceRequests.getClearanceStatuses()
} returns Response(listOf(), null)

val hmkit = HMKitFleet(oauthCredentials)
val clearance = hmkit.getClearanceStatuses().get()
assertTrue(clearance.response?.isEmpty() == true)
}

@Test
@Suppress("MaximumLineLength", "MaxLineLength")
fun getVehicleStatus() = runTest {
Expand All @@ -118,7 +139,7 @@ class HMKitFleetTest : BaseTest() {
null
)

val hmkit = HMKitFleet(privateKeyConfiguration)
val hmkit = HMKitFleet(oauthCredentials)
val vehicleStatus = hmkit.getVehicleState("vin1").get()
val json = Json.decodeFromString<JsonObject>(vehicleStatus.response ?: "")
assertTrue(json["vin"]?.jsonPrimitive?.content == "vin1")
Expand All @@ -142,7 +163,7 @@ class HMKitFleetTest : BaseTest() {
HMKitFleet.Environment.webUrl = fakeUrl
assertTrue(HMKitFleet.Environment.webUrl == fakeUrl)

val hmkit = HMKitFleet(privateKeyConfiguration)
val hmkit = HMKitFleet(oauthCredentials)
hmkit.getEligibility("vin1", Brand.SANDBOX).get()

val recordedRequest = mockWebServer.takeRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal class AccessTokenRequestsTest : BaseTest() {
every { cache getProperty "accessToken" } returnsMany listOf(null, responseAccessToken)

val response = runBlocking {
mockSuccessfulRequest(responseAccessToken).getAccessToken()
mockSuccessfulRequest(responseAccessToken, 200).getAccessToken()
}

val recordedRequest: RecordedRequest = mockWebServer.takeRequest()
Expand All @@ -93,7 +93,7 @@ internal class AccessTokenRequestsTest : BaseTest() {
val responseAccessToken = notExpiredAccessToken()
// return null from cache at first, then on next call a new one
every { cache getProperty "accessToken" } returns responseAccessToken
val response = runBlocking { mockSuccessfulRequest(responseAccessToken).getAccessToken() }
val response = runBlocking { mockSuccessfulRequest(responseAccessToken, 201).getAccessToken() }

// this means request is not made
verify(exactly = 0) { cache setProperty "accessToken" value any<AccessToken>() }
Expand All @@ -120,7 +120,7 @@ internal class AccessTokenRequestsTest : BaseTest() {
client,
mockLogger,
baseUrl.toString(),
privateKeyConfiguration.credentials,
oauthCredentials.credentials,
cache
)

Expand Down Expand Up @@ -151,7 +151,7 @@ internal class AccessTokenRequestsTest : BaseTest() {
client,
mockLogger,
baseUrl.toString(),
privateKeyConfiguration.credentials,
oauthCredentials.credentials,
cache
)

Expand All @@ -163,12 +163,12 @@ internal class AccessTokenRequestsTest : BaseTest() {
assertTrue(status.error!!.title == genericError.title)
}

private fun mockSuccessfulRequest(responseAccessToken: AccessToken): AccessTokenRequests {
private fun mockSuccessfulRequest(responseAccessToken: AccessToken, responseCode: Int = 201): AccessTokenRequests {
// return null from cache at first, then on next call a new one
val json = Json.encodeToString(responseAccessToken)

val mockResponse = MockResponse()
.setResponseCode(HttpURLConnection.HTTP_CREATED)
.setResponseCode(responseCode)
.setBody(json)

mockWebServer.enqueue(mockResponse)
Expand All @@ -178,7 +178,7 @@ internal class AccessTokenRequestsTest : BaseTest() {
client,
mockLogger,
baseUrl.toString(),
privateKeyConfiguration.credentials,
privateKeyCredentials,
cache
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlinx.serialization.json.addJsonObject
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.junit.Test
import org.junit.jupiter.api.Test

internal class RequestsTest {
@Test
Expand Down Expand Up @@ -89,4 +89,13 @@ internal class RequestsTest {
assert(response.error?.source == null)
assert(response.error?.detail == "detail")
}

@Test
fun parsesUnknownResponse() {
val requests = Requests(mockk(), mockk(), "mockk()")
val body = "unknown"
val response = requests.parseError<Any>(body)

assert(response.error?.title == "Unknown server response")
}
}
Loading

0 comments on commit bd8e39f

Please sign in to comment.