Skip to content

Commit d7cf7dd

Browse files
author
Robert Winkler
committed
Updated code
1 parent 2e3569b commit d7cf7dd

File tree

17 files changed

+122
-45
lines changed

17 files changed

+122
-45
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ subprojects {
1313
version = "1.0-SNAPSHOT"
1414

1515
dependencies {
16-
1716
testImplementation(kotlin("test"))
1817
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test")
1918
testImplementation("io.mockk:mockk:1.13.13")

kotlin-wot-binding-http/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ dependencies {
1818
implementation("io.ktor:ktor-server-auto-head-response")
1919
//implementation("io.insert-koin:koin-ktor:4.0.0")
2020
testImplementation("io.ktor:ktor-server-test-host")
21-
testImplementation("org.slf4j:slf4j-simple:2.0.16")
21+
testImplementation("ch.qos.logback:logback-classic:1.5.12")
2222
}

kotlin-wot-binding-http/src/main/kotlin/http/HttpProtocolClientFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ open class HttpProtocolClientFactory : ProtocolClientFactory {
1515
get() = HttpProtocolClient()
1616

1717
override suspend fun init() {
18-
TODO("Not yet implemented")
18+
// TODO
1919
}
2020

2121
override suspend fun destroy() {
22-
TODO("Not yet implemented")
22+
// TODO
2323
}
2424
}

kotlin-wot-binding-http/src/main/kotlin/http/HttpProtocolServer.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import ai.ancf.lmos.wot.content.Content
55
import ai.ancf.lmos.wot.content.ContentCodecException
66
import ai.ancf.lmos.wot.content.ContentManager
77
import ai.ancf.lmos.wot.thing.ExposedThing
8+
import ai.ancf.lmos.wot.thing.action.ExposedThingAction
89
import ai.ancf.lmos.wot.thing.form.Form
910
import ai.ancf.lmos.wot.thing.form.Operation
10-
import ai.ancf.lmos.wot.thing.schema.InteractionAffordance
11+
import ai.ancf.lmos.wot.thing.property.ExposedThingProperty
12+
import ai.ancf.lmos.wot.thing.property.ExposedThingProperty.*
13+
import ai.ancf.lmos.wot.thing.schema.*
1114
import ai.anfc.lmos.wot.binding.ProtocolServer
1215
import ai.anfc.lmos.wot.binding.ProtocolServerException
1316
import com.fasterxml.jackson.databind.DeserializationFeature
@@ -17,6 +20,7 @@ import io.ktor.serialization.jackson.*
1720
import io.ktor.server.application.*
1821
import io.ktor.server.engine.*
1922
import io.ktor.server.netty.*
23+
import io.ktor.server.plugins.*
2024
import io.ktor.server.plugins.contentnegotiation.*
2125
import io.ktor.server.request.*
2226
import io.ktor.server.routing.*
@@ -212,7 +216,15 @@ fun Application.setupRouting(servient: Servient) {
212216
val value = property.read()
213217
//contentType = getOrDefaultRequestContentType(call.request)
214218
//val content = ContentManager.valueToContent(value, contentType.toString())
215-
call.respond(value, typeInfo<Any>())
219+
when (property) {
220+
is ExposedStringProperty -> call.respond(property.read(), typeInfo<String>())
221+
is ExposedIntProperty -> call.respond(property.read(), typeInfo<Int>())
222+
is ExposedBooleanProperty -> call.respond(property.read(), typeInfo<Boolean>())
223+
is ExposedNumberProperty -> call.respond(property.read(), typeInfo<Double>())
224+
is ExposedObjectProperty -> call.respond(property.read(), typeInfo<Map<*,*>>())
225+
is ExposedNullProperty -> call.respond(property.read(), typeInfo<Any>())
226+
is ExposedArrayProperty -> call.respond(property.read(), typeInfo<List<*>>())
227+
}
216228
}
217229
catch (e: ContentCodecException) {
218230
call.response.status(HttpStatusCode.InternalServerError)
@@ -235,8 +247,16 @@ fun Application.setupRouting(servient: Servient) {
235247
if (!property.readOnly) {
236248
//val contentType = getOrDefaultRequestContentType(call.request)
237249
//val content = Content(contentType.toString(), call.receiveChannel().toByteArray())
238-
//val newValue = property.write(call.receive())
239-
//call.respond(newValue, typeInfo<Any>())
250+
251+
when (property) {
252+
is ExposedStringProperty -> call.respond(property.write(call.receive<String>()), typeInfo<String>())
253+
is ExposedIntProperty -> call.respond(property.write(call.receive<Int>()), typeInfo<Int>())
254+
is ExposedBooleanProperty -> call.respond(property.write(call.receive<Boolean>()), typeInfo<Boolean>())
255+
is ExposedNumberProperty -> call.respond(property.write(call.receive<Double>()), typeInfo<Double>())
256+
is ExposedObjectProperty -> call.respond(property.write(call.receive<Map<*,*>>()), typeInfo<Map<*,*>>())
257+
is ExposedNullProperty -> call.respond(property.write(call.receive<Any>()), typeInfo<Any>())
258+
is ExposedArrayProperty -> call.respond(property.write(call.receive<List<*>>()), typeInfo<List<*>>())
259+
}
240260
} else {
241261
call.response.status(HttpStatusCode.BadRequest)
242262
}
@@ -256,7 +276,7 @@ fun Application.setupRouting(servient: Servient) {
256276

257277
if(action.input != null){
258278
val input = ContentManager.contentToValue(content, action.input!!)
259-
val newValue = action.invokeAction(null, null)
279+
val newValue = action.invokeAction(input, null)
260280
call.respond(newValue, typeInfo<Any>())
261281
}else{
262282
val newValue = action.invokeAction(null, null)

kotlin-wot-binding-http/src/test/kotlin/http/HttpProtocolServerTest.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ class HttpProtocolServerTest {
4040
private val servient: Servient = mockk()
4141
private val mockServer: EmbeddedServer<*, *> = mockk()
4242
private val exposedThing: ExposedThing = exposedThing("test") {
43-
intProperty(PROPERTY_NAME, PropertyState(initialValue = 0, readHandler = { 2 })){
43+
intProperty(PROPERTY_NAME, PropertyState(initialValue = 0, readHandler = { 2 }, writeHandler = { input -> input })){
4444
title = "title"
45-
readOnly = true
4645
}
47-
action<String, String>(ACTION_NAME, state = ActionState(handler = ActionHandler { input, _ ->
48-
// Your action logic here
49-
println("Input: $input")
50-
return@ActionHandler input?.let { "Output: $it" }
46+
action<String, String>(ACTION_NAME, state = ActionState(handler = { input, _ ->
47+
input
5148
}))
5249
{
50+
title = ACTION_NAME
5351
input = stringSchema {
5452
title = "Action Input"
5553
minLength = 10
@@ -149,11 +147,12 @@ class HttpProtocolServerTest {
149147
// Perform GET request on "/"
150148
val response = client.get("/")
151149

152-
val things : List<ExposedThing> = response.body()
150+
assertEquals(HttpStatusCode.OK, response.status)
153151

152+
val things : List<ExposedThing> = response.body()
154153
assertEquals(1, things.size)
155154

156-
assertEquals(HttpStatusCode.OK, response.status)
155+
157156
}
158157

159158
@Test
@@ -204,11 +203,8 @@ class HttpProtocolServerTest {
204203
contentType(ContentType.Application.Json)
205204
}
206205

207-
val propertyValue : String = response.body()
208-
209-
println(propertyValue)
210-
211206
assertEquals(HttpStatusCode.OK, response.status)
207+
assertEquals(2, response.body<Int>())
212208

213209
}
214210

@@ -225,10 +221,11 @@ class HttpProtocolServerTest {
225221
// Perform PUT request on property endpoint
226222
val response = client.put("/test/properties/$propertyName") {
227223
contentType(ContentType.Application.Json)
228-
setBody("""{ "value": "newValue" }""") // JSON payload to update the property
224+
setBody(2) // JSON payload to update the property
229225
}
230226

231227
assertEquals(HttpStatusCode.OK, response.status)
228+
assertEquals(2, response.body<Int>())
232229

233230
}
234231

@@ -255,10 +252,12 @@ class HttpProtocolServerTest {
255252
// Perform POST request on action endpoint
256253
val response = client.post("/test/actions/action1") {
257254
contentType(ContentType.Application.Json)
258-
setBody("""{ "input": "someValue" }""") // JSON payload for action input
255+
setBody(""""input"""") // JSON payload for action input"
259256
}
260257

261258
assertEquals(HttpStatusCode.OK, response.status)
259+
260+
assertEquals("input", response.body<String>())
262261
}
263262

264263
@Test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
<root level="debug">
8+
<appender-ref ref="STDOUT"/>
9+
</root>
10+
<logger name="io.netty" level="INFO"/>
11+
</configuration>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies {
2+
api(project(":kotlin-wot"))
3+
api(project(":kotlin-wot-binding-http"))
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package integration
2+
3+
import kotlinx.coroutines.test.runTest
4+
import ai.ancf.lmos.wot.Servient
5+
import ai.ancf.lmos.wot.Wot
6+
import ai.ancf.lmos.wot.binding.http.HttpProtocolClientFactory
7+
import ai.ancf.lmos.wot.binding.http.HttpProtocolServer
8+
import kotlin.test.Test
9+
10+
class WoTIntegrationTest {
11+
12+
13+
@Test
14+
fun `Should start http server`() = runTest {
15+
16+
val servient = Servient(
17+
servers = listOf(HttpProtocolServer()),
18+
clientFactories = listOf(HttpProtocolClientFactory())
19+
)
20+
val wot = Wot.create(servient)
21+
22+
val exposedThing = wot.produce {
23+
id = "myid"
24+
title = "MyThing"
25+
}
26+
27+
servient.start()
28+
servient.addThing(exposedThing)
29+
servient.expose("myid")
30+
31+
//val url = servient.fetchDirectory()
32+
33+
//val consumedThing = servient.fetch(url)
34+
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
<root level="debug">
8+
<appender-ref ref="STDOUT"/>
9+
</root>
10+
<logger name="io.netty" level="INFO"/>
11+
</configuration>

0 commit comments

Comments
 (0)