Skip to content

Commit

Permalink
test: Adapt Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Oct 23, 2023
1 parent f1f42c8 commit 65f08e9
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/

package org.eclipse.kuksa

import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.types.instanceOf
import io.mockk.mockk
import io.mockk.verify
import org.eclipse.kuksa.databroker.DataBrokerConnectorProvider
import org.eclipse.kuksa.extensions.setRandomFloatValue
import org.eclipse.kuksa.proto.v1.KuksaValV1
import org.eclipse.kuksa.proto.v1.KuksaValV1.SetResponse
import org.eclipse.kuksa.proto.v1.Types
import org.eclipse.kuksa.proto.v1.Types.Datapoint
import org.eclipse.kuksa.test.kotest.Insecure
import org.eclipse.kuksa.test.kotest.Integration
import kotlin.random.Random

class DataBrokerApiInteractionTest : BehaviorSpec({
tags(Integration, Insecure)

given("An active Connection to the DataBroker") {
val dataBrokerConnectorProvider = DataBrokerConnectorProvider()
val connector = dataBrokerConnectorProvider.createInsecure()
connector.connect()

and("An Instance of DataBrokerApiInteraction") {
val classUnderTest = DataBrokerApiInteraction(dataBrokerConnectorProvider.managedChannel)

and("Some VSS-related data") {
val vssPath = "Vehicle.ADAS.CruiseControl.SpeedSet"
val fields = listOf(Types.Field.FIELD_VALUE)
val random = Random(System.currentTimeMillis())
val valueToSet = random.nextInt(250).toFloat()

`when`("Updating the $fields of $vssPath to $valueToSet km/h") {
val updatedDatapoint = Datapoint.newBuilder().setFloat(valueToSet).build()
val result = kotlin.runCatching {
classUnderTest.updateProperty(vssPath, fields, updatedDatapoint)
}

then("No Exception should be thrown") {
result.exceptionOrNull() shouldBe null
}

then("It should return a valid SetResponse") {
val response = result.getOrNull()
response shouldNotBe null
response shouldBe instanceOf(SetResponse::class)
}
}

`when`("Fetching the Value of Vehicle.ADAS.CruiseControl.SpeedSet") {
val property = classUnderTest.fetchProperty(vssPath, fields)

then("It should return the correct value") {
val dataEntry = property.getEntries(0)
val value = dataEntry.value.float
value shouldBe valueToSet
}
}

`when`("Trying to fetch the $fields from an invalid VSS Path") {
val invalidVssPath = "Vehicle.This.Path.Is.Invalid"

val result = kotlin.runCatching {
classUnderTest.fetchProperty(invalidVssPath, fields)
}

then("No Exception should be thrown") {
result.exceptionOrNull() shouldBe null
}

then("It should return a GetResponse with no entries and one error") {
val response = result.getOrNull()
response shouldNotBe null
response shouldBe instanceOf(KuksaValV1.GetResponse::class)
response?.entriesList?.size shouldBe 0
response?.errorsList?.size shouldBe 1
}
}

`when`("Subscribing to the vssPath using FIELD_VALUE") {
val subscription = classUnderTest.subscribe(vssPath, Types.Field.FIELD_VALUE)

val propertyObserver = mockk<PropertyObserver>(relaxed = true)
subscription.observers.register(propertyObserver)

and("The value of the vssPath is updated") {
classUnderTest.setRandomFloatValue(vssPath)

then("The PropertyObserver should be notified") {
verify {
propertyObserver.onPropertyChanged(vssPath, Types.Field.FIELD_VALUE, any())
}
}
}
}

`when`("Subscribing to an invalid vssPath") {
val subscription = classUnderTest.subscribe("Vehicle.Some.Invalid.Path", Types.Field.FIELD_VALUE)

val propertyObserver = mockk<PropertyObserver>(relaxed = true)
subscription.observers.register(propertyObserver)

then("An Error should be triggered") {
verify(timeout = 100L) {
propertyObserver.onError(any())
}
}
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import io.mockk.clearMocks
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.eclipse.kuksa.databroker.DataBrokerConnectorProvider
import org.eclipse.kuksa.model.Property
Expand Down Expand Up @@ -55,7 +54,7 @@ class DataBrokerConnectionTest : BehaviorSpec({
dataBrokerConnection.subscribe(property, propertyObserver)

then("The #onPropertyChanged method is triggered") {
verify { propertyObserver.onPropertyChanged(any(), any(), any()) }
verify(timeout = 100L) { propertyObserver.onPropertyChanged(any(), any(), any()) }
}

`when`("The observed Property changes") {
Expand Down Expand Up @@ -162,10 +161,8 @@ class DataBrokerConnectionTest : BehaviorSpec({
val propertyObserver = mockk<VssSpecificationObserver<VssDriver>>(relaxed = true)
dataBrokerConnection.subscribe(specification, observer = propertyObserver)

delay(100)

then("The #onSpecificationChanged method is triggered") {
verify { propertyObserver.onSpecificationChanged(any()) }
verify(timeout = 100L) { propertyObserver.onSpecificationChanged(any()) }
}

and("The initial value is different from the default for a child") {
Expand Down Expand Up @@ -214,11 +211,9 @@ class DataBrokerConnectionTest : BehaviorSpec({
val propertyObserver = mockk<PropertyObserver>(relaxed = true)
dataBrokerConnection.subscribe(property, propertyObserver)

delay(100)

then("The PropertyObserver#onError method should be triggered with 'NOT_FOUND' (Path not found)") {
val capturingSlot = slot<Throwable>()
verify { propertyObserver.onError(capture(capturingSlot)) }
verify(timeout = 100L) { propertyObserver.onError(capture(capturingSlot)) }
val capturedThrowable = capturingSlot.captured
capturedThrowable.message shouldContain "NOT_FOUND"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/

package org.eclipse.kuksa.extensions

import io.kotest.assertions.fail
import org.eclipse.kuksa.DataBrokerApiInteraction
import org.eclipse.kuksa.proto.v1.Types
import kotlin.random.Random

internal suspend fun DataBrokerApiInteraction.setRandomFloatValue(vssPath: String, maxValue: Int = 300): Float {
val random = Random(System.nanoTime())
val randomValue = random.nextInt(maxValue)
val randomFloat = randomValue.toFloat()
val updatedDatapoint = Types.Datapoint.newBuilder().setFloat(randomFloat).build()

try {
updateProperty(vssPath, listOf(Types.Field.FIELD_VALUE), updatedDatapoint)
} catch (e: Exception) {
fail("Updating $vssPath to $randomFloat failed: $e")
}

return randomFloat
}

internal suspend fun DataBrokerApiInteraction.setRandomUint32Value(vssPath: String, maxValue: Int = 300): Int {
val random = Random(System.nanoTime())
val randomValue = random.nextInt(maxValue)
val updatedDatapoint = Types.Datapoint.newBuilder().setUint32(randomValue).build()

try {
updateProperty(vssPath, listOf(Types.Field.FIELD_VALUE), updatedDatapoint)
} catch (e: Exception) {
fail("Updating $vssPath to $randomValue failed: $e")
}

return randomValue
}
Loading

0 comments on commit 65f08e9

Please sign in to comment.