-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
444 additions
and
8 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/DataBrokerApiInteractionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/extensions/DataBrokerApiInteractionExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.