Skip to content

Commit 8823974

Browse files
author
Robert Winkler
committed
Fixed JSON parsing
1 parent 7e35d7a commit 8823974

File tree

8 files changed

+46
-46
lines changed

8 files changed

+46
-46
lines changed

kotlin-wot/src/main/kotlin/thing/ConsumedThing.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package ai.ancf.lmos.wot.thing
22

33
import ai.ancf.lmos.wot.Servient
4-
import ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing.ContentCodecException
5-
import ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing.ContentManager
6-
import ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing.UriTemplate
74
import ai.ancf.lmos.wot.thing.action.ConsumedThingException
85
import ai.ancf.lmos.wot.thing.form.Form
96
import ai.ancf.lmos.wot.thing.form.Operation

kotlin-wot/src/main/kotlin/ai/ancf/lmos/wot/thing/ContentManager.kt renamed to kotlin-wot/src/main/kotlin/thing/ContentManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing
1+
package ai.ancf.lmos.wot.thing
22

33
import ai.ancf.lmos.wot.ServientException
44
import ai.ancf.lmos.wot.thing.schema.DataSchema

kotlin-wot/src/main/kotlin/ai/ancf/lmos/wot/thing/UriTemplate.kt renamed to kotlin-wot/src/main/kotlin/thing/UriTemplate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing
1+
package ai.ancf.lmos.wot.thing
22

33
class UriTemplate {
44
fun expand(parameters: Map<String, Any>): String {

kotlin-wot/src/main/kotlin/thing/action/ConsumedThingAction.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package ai.ancf.lmos.wot.thing.action
22

33
import ai.ancf.lmos.wot.ServientException
4-
import ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing.ContentCodecException
5-
import ai.ancf.lmos.wot.ai.ancf.lmos.wot.thing.ContentManager
6-
import ai.ancf.lmos.wot.thing.schema.ActionAffordance
74
import ai.ancf.lmos.wot.thing.ConsumedThing
5+
import ai.ancf.lmos.wot.thing.ContentCodecException
6+
import ai.ancf.lmos.wot.thing.ContentManager
87
import ai.ancf.lmos.wot.thing.Thing
98
import ai.ancf.lmos.wot.thing.form.Form
109
import ai.ancf.lmos.wot.thing.form.Operation
10+
import ai.ancf.lmos.wot.thing.schema.ActionAffordance
1111
import ai.anfc.lmos.wot.binding.Content
1212
import ai.anfc.lmos.wot.binding.ProtocolClient
1313
import com.fasterxml.jackson.annotation.JsonIgnore

kotlin-wot/src/main/kotlin/thing/action/ExposedThingAction.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include.*
1212
import com.fasterxml.jackson.annotation.JsonProperty
1313
import kotlinx.serialization.Contextual
1414
import kotlinx.serialization.Serializable
15-
import kotlinx.serialization.Transient
1615
import org.slf4j.LoggerFactory
1716

1817
@Serializable
1918
data class ExposedThingAction<I, O> @JsonCreator constructor(
2019
private val action: ThingAction<I, O> = ThingAction(),
2120
@JsonIgnore
2221
private val thing: Thing = Thing(),
23-
@Transient private val state: ActionState<I, O> = ActionState()
22+
private val state: ActionState<I, O> = ActionState()
2423
) : ActionAffordance<I, O> by action {
2524

25+
26+
2627
/**
2728
* Invokes the method and executes the handler defined in [.state]. `input`
2829
* contains the request payload. `options` can contain additional data (for example,
@@ -44,7 +45,7 @@ data class ExposedThingAction<I, O> @JsonCreator constructor(
4445
)
4546
try {
4647
// Use the handler as a suspending function directly
47-
state.handler.invoke(input, options).also { output ->
48+
state.handler.handle(input, options).also { output ->
4849
if (output == null) {
4950
log.warn(
5051
"'{}': Called registered handler for Action '{}' returned null. This can cause problems.",
@@ -128,4 +129,6 @@ data class ThingAction<I, O>(
128129
override var titles: MutableMap<String, String>? = null
129130
) : ActionAffordance<I, O>
130131

131-
typealias ActionHandler<I, O> = suspend (input: I, options: Map<String, Map<String, Any>>) -> O?
132+
fun interface ActionHandler<I, O> {
133+
suspend fun handle(input: I, options: Map<String, Map<String, Any>>): O?
134+
}

kotlin-wot/src/main/kotlin/thing/property/ExposedThingProperty.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ sealed class ExposedThingProperty<T>(
3131
private val property: ThingProperty<T>,
3232
@JsonIgnore
3333
private val thing: Thing = Thing(),
34-
@JsonIgnore
3534
private val state: PropertyState<T> = PropertyState()
3635
) : PropertyAffordance<T> by property {
3736

3837
suspend fun read(): T? {
3938
log.debug("'{}' calls registered readHandler for Property '{}'", thing.id, title)
4039
return try {
41-
state.readHandler?.invoke()?.also { state.setValue(it) } ?: state.value
40+
state.readHandler?.handle()?.also { state.setValue(it) } ?: state.value
4241
} catch (e: Exception) {
4342
log.error("Error while reading property '{}': {}", title, e.message)
4443
throw e
@@ -49,7 +48,7 @@ sealed class ExposedThingProperty<T>(
4948
log.debug("'{}' calls registered writeHandler for Property '{}'", thing.id, title)
5049
return try {
5150
if (state.writeHandler != null) {
52-
val customValue = state.writeHandler.invoke(value)
51+
val customValue = state.writeHandler.handle(value)
5352
state.setValue(customValue)
5453
log.debug(
5554
"'{}' write handler for Property '{}' sets custom value '{}'",
@@ -143,5 +142,11 @@ fun exposedObjectProperty(initializer: ExposedObjectProperty.() -> Unit): Expose
143142
return ExposedObjectProperty().apply(initializer)
144143
}
145144

146-
typealias ReadHandler<T> = suspend () -> T?
147-
typealias WriteHandler<T> = suspend (T) -> T?
145+
146+
fun interface ReadHandler<T> {
147+
suspend fun handle(): T?
148+
}
149+
150+
fun interface WriteHandler<T> {
151+
suspend fun handle(input: T): T?
152+
}

kotlin-wot/src/test/kotlin/thing/action/ExposedThingActionTest.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ class ExposedThingActionTest {
7272
}"""
7373

7474
val parsedThingAction = JsonMapper.instance.readValue<ExposedThingAction<String, String>>(json)
75-
val action = ThingAction(
76-
title = "title",
77-
description = "blabla",
78-
input = StringSchema(),
79-
output = StringSchema()
80-
)
81-
val exposedThingAction = ExposedThingAction(action, thing)
82-
assertEquals(exposedThingAction, parsedThingAction)
75+
assertEquals("title", parsedThingAction.title)
76+
assertEquals("blabla", parsedThingAction.description)
77+
assertIs<StringSchema>(parsedThingAction.input)
78+
assertIs<StringSchema>(parsedThingAction.output)
8379
}
8480

8581
@Test
@@ -98,7 +94,7 @@ class ExposedThingActionTest {
9894

9995
@Test
10096
fun invokeWithHandlerShouldCallHandler(): Unit = runTest {
101-
val handler: (suspend (String, Map<String, Map<String, Any>>) -> String) = mockk()
97+
val handler: ActionHandler<String, String> = mockk()
10298

10399
every { state.handler } returns handler
104100

@@ -108,15 +104,15 @@ class ExposedThingActionTest {
108104
state
109105
)
110106

111-
coEvery { handler("test", any()) } returns "Result"
107+
coEvery { handler.handle("test", any()) } returns "Result"
112108

113109
val result = exposedThingAction.invokeAction("test")
114110
assertEquals("Result", result)
115111
}
116112

117113
@Test
118114
fun invokeWithBrokenHandlerShouldThrowException(): Unit = runTest {
119-
val handler: (suspend (String, Map<String, Map<String, Any>>) -> String) = mockk()
115+
val handler: ActionHandler<String, String> = mockk()
120116

121117
every { state.handler } returns handler
122118

@@ -126,7 +122,7 @@ class ExposedThingActionTest {
126122
state
127123
)
128124

129-
coEvery { handler("test", any()) } throws RuntimeException()
125+
coEvery { handler.handle("test", any()) } throws RuntimeException()
130126

131127
assertFailsWith<RuntimeException> {
132128
exposedThingAction.invokeAction("test")

kotlin-wot/src/test/kotlin/thing/property/ExposedThingPropertyTest.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@ import io.mockk.*
1010
import kotlinx.coroutines.test.runTest
1111
import net.javacrumbs.jsonunit.assertj.JsonAssertions
1212
import net.javacrumbs.jsonunit.core.Option
13-
import org.junit.jupiter.api.BeforeEach
14-
import org.junit.jupiter.api.Test
15-
import org.junit.jupiter.api.assertThrows
1613
import java.io.IOException
14+
import kotlin.test.BeforeTest
15+
import kotlin.test.Test
1716
import kotlin.test.assertEquals
17+
import kotlin.test.assertFailsWith
1818

1919
class ExposedThingPropertyTest {
2020

2121
private lateinit var thing: Thing
2222
private lateinit var state: PropertyState<String>
2323

24-
@BeforeEach
24+
@BeforeTest
2525
fun setUp() {
2626
thing = Thing(id = "testThing")
2727
state = mockk()
28+
29+
2830
}
2931

3032
@Test
3133
fun testEquals() {
32-
val property1 = ExposedStringProperty(thing = thing, state = state, property = stringProperty { title = "title" })
33-
val property2 = ExposedStringProperty(thing = thing, state = state, property = stringProperty { title = "title" })
34+
val property1 = ExposedStringProperty(thing = thing, property = stringProperty { title = "title" })
35+
val property2 = ExposedStringProperty(thing = thing, property = stringProperty { title = "title" })
3436
assertEquals(property1, property2)
3537
}
3638

3739
@Test
3840
fun testHashCode() {
3941
val thing = Thing(id = "Foo")
4042

41-
val property1 = ExposedStringProperty(thing = thing, state = state, property = stringProperty { title = "title" }).hashCode()
42-
val property2 = ExposedStringProperty(thing = thing, state = state, property = stringProperty { title = "title" }).hashCode()
43+
val property1 = ExposedStringProperty(thing = thing, property = stringProperty { title = "title" }).hashCode()
44+
val property2 = ExposedStringProperty(thing = thing, property = stringProperty { title = "title" }).hashCode()
4345
assertEquals(property1, property2)
4446
}
4547

@@ -63,13 +65,10 @@ class ExposedThingPropertyTest {
6365
val json = """{"@type":"saref:Temperature","description":"bla bla","type":"integer","observable":true,"readOnly":true}"""
6466

6567
val parsedProperty = JsonMapper.instance.readValue<ExposedIntProperty>(json)
66-
val property = exposedIntProperty {
67-
objectType=Type("saref:Temperature")
68-
description = "bla bla"
69-
observable=true
70-
readOnly=true
71-
}
72-
assertEquals(property, parsedProperty)
68+
assertEquals(Type("saref:Temperature"), parsedProperty.objectType)
69+
assertEquals("bla bla", parsedProperty.description)
70+
assertEquals(true, parsedProperty.observable)
71+
assertEquals(true, parsedProperty.readOnly)
7372
}
7473

7574
@Test
@@ -109,7 +108,7 @@ class ExposedThingPropertyTest {
109108

110109
val exposedProperty = ExposedStringProperty(thing = thing, state = state)
111110

112-
assertThrows<IOException> { exposedProperty.read() }
111+
assertFailsWith<IOException> { exposedProperty.read() }
113112
}
114113

115114
@Test
@@ -151,7 +150,7 @@ class ExposedThingPropertyTest {
151150

152151
val exposedProperty = ExposedStringProperty(thing = thing, state = state)
153152

154-
assertThrows<IOException> { exposedProperty.write("test") }
153+
assertFailsWith<IOException> { exposedProperty.write("test") }
155154
}
156155

157156
}

0 commit comments

Comments
 (0)