Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 0db273d1 authored by Yannick Li's avatar Yannick Li Committed by Ludovic Le Frioux
Browse files

Make CObjectUID serializable and adapt CServiceAdapter tests

parent 1840122b
No related branches found
No related tags found
1 merge request!9Resolve "Create and use a ClientEnvironment"
......@@ -18,7 +18,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("multiplatform") version "1.4.10"
kotlin("multiplatform") version "1.4.20"
kotlin("plugin.serialization") version "1.4.20"
}
repositories {
......@@ -53,8 +54,9 @@ kotlin {
commonMain {
dependencies {
implementation("concordant:c-crdtlib:1.0.0")
implementation("concordant:c-crdtlib:1.0.1")
implementation("io.ktor:ktor-client-core:1.4.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
}
}
......
......@@ -19,9 +19,10 @@
package client.utils
import crdtlib.crdt.DeltaCRDT
import kotlinx.serialization.*
/**
* Concordant object unique identifier.
*/
@Serializable
data class CObjectUId(val collectionUId: CollectionUId, val type: String, val name: String)
......@@ -19,15 +19,15 @@
package client.utils
import crdtlib.crdt.*
import crdtlib.crdt.DeltaCRDT
import crdtlib.utils.Environment
import io.ktor.client.HttpClient
import io.ktor.client.request.url
import io.ktor.client.request.post
import io.ktor.client.request.url
import io.ktor.http.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
/**
*
......@@ -51,67 +51,34 @@ class CServiceAdapter {
}
/**
* Get all objects of the database
* @param dbName database name
*/
suspend fun getObjects(dbName: String): String {
val client = HttpClient()
val resp = client.post<String> {
url("http://127.0.0.1:4000/api/get-objects")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName"}"""
}
client.close()
return resp
}
/**
* Get a specific object of the database
* Get a CRDT from the database
* @param dbName database name
* @param myid object id
* @param objectUId crdt id
*/
suspend fun getObject(dbName: String, myid: String): String {
suspend fun getObject(dbName: String, objectUId: CObjectUId, env: Environment): DeltaCRDT {
val client = HttpClient()
val resp = client.post<String> {
val crdtJson = client.post<String>{
url("http://127.0.0.1:4000/api/get-object")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName","id":"$myid"}"""
body = """{"appName":"$dbName","id":"${Json.encodeToString(objectUId).replace("\"","\\\"")}"}"""
}
client.close()
return resp
return DeltaCRDT.fromJson(crdtJson.removePrefix("\"").removeSuffix("\"").replace("""\\"""","""\""""), env)
}
/**
* Update the object
* Get all objects of the database
* @param dbName database name
* @param myid object id
* @param mydoc object content
*/
suspend fun updateObject(dbName: String, myid: String, mydoc: String): Boolean {
suspend fun getObjects(dbName: String): String {
val client = HttpClient()
val resp = client.post<String> {
url("http://127.0.0.1:4000/api/update-object")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName","id":"$myid", "document":"$mydoc"}"""
}
client.close()
return resp == "\"OK\""
}
/**
* Get a CRDT from the database
* @param dbName database name
* @param objectUId crdt id
*/
suspend fun getObject(dbName: String, objectUId: CObjectUId): DeltaCRDT {
val client = HttpClient()
val crdtJson = client.post<String>{
url("http://127.0.0.1:4000/api/get-object")
url("http://127.0.0.1:4000/api/get-objects")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName","id":"$objectUId.name"}"""
body = """{"appName":"$dbName"}"""
}
client.close()
return DeltaCRDT.fromJson(crdtJson)
return resp
}
/**
......@@ -120,13 +87,13 @@ class CServiceAdapter {
* @param objectUId CRDT id
* @param crdt new crdt
*/
suspend fun updateObject(dbName: String, myid: String, crdt: DeltaCRDT): Boolean{
suspend fun updateObject(dbName: String, objectUId: CObjectUId, crdt: DeltaCRDT): Boolean{
val client = HttpClient()
val crdtJson = crdt.toJson().replace("\"","\\\"")
val resp = client.post<String>{
url("http://127.0.0.1:4000/api/update-object")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName","id":"$myid", "document":"$crdtJson"}"""
body = """{"appName":"$dbName","id":"${Json.encodeToString(objectUId).replace("\"","\\\"")}", "document":"$crdtJson"}"""
}
client.close()
return resp == "\"OK\""
......@@ -136,7 +103,7 @@ class CServiceAdapter {
* Close the connection to the database
* @param dbName database name
*/
suspend fun close(dbName: String): Boolean{
fun close(dbName: String): Boolean{
return true
}
......
......@@ -19,8 +19,8 @@
package client.utils
import crdtlib.crdt.DeltaCRDT
import crdtlib.crdt.PNCounter
import crdtlib.crdt.DeltaCRDTFactory
import crdtlib.utils.ClientUId
import crdtlib.utils.SimpleEnvironment
import io.kotest.core.spec.style.StringSpec
......@@ -32,28 +32,32 @@ import kotlinx.coroutines.delay
class CServiceAdapterTest : StringSpec({
"connect to c-service create, write twice, read and delete" {
CServiceAdapter.connect("myapp").shouldBeTrue()
delay(200)
val uid = ClientUId("clientid")
val my_env = SimpleEnvironment(uid)
val my_crdt = DeltaCRDTFactory.createDeltaCRDT("PNCounter", my_env)
CServiceAdapter.updateObject("myapp", "myid", my_crdt).shouldBeTrue()
val objectUId = CObjectUId("myCollection", "PNCounter", "myPNCounter")
val my_crdt : DeltaCRDT = CServiceAdapter.getObject("myapp", objectUId, my_env)
delay(200)
val regex1 = """\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\],\\\"decrement\\\":\[\]\},\\\"value\\\":0\}\"\]""".toRegex()
CServiceAdapter.getObjects("myapp").shouldMatch(regex1)
my_crdt.toJson().shouldBe("{\"type\":\"PNCounter\",\"metadata\":{\"increment\":[],\"decrement\":[]},\"value\":0}")
delay(200)
if (my_crdt is PNCounter) {
my_crdt.increment(10)
CServiceAdapter.updateObject("myapp", "myid", my_crdt)
CServiceAdapter.updateObject("myapp", objectUId, my_crdt).shouldBeTrue()
my_crdt.decrement(5)
CServiceAdapter.updateObject("myapp", objectUId, my_crdt).shouldBeTrue()
delay(200)
val regex2 = """\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\{\\\"name\\\":\\\"clientid\\\"},\{\\\"first\\\":10,\\\"second\\\":\{\\\"uid\\\":\{\\\"name\\\":\\\"clientid\\\"\},\\\"cnt\\\":-2147483647\}\}\],\\\"decrement\\\":\[\]\},\\\"value\\\":10\}\"\]""".toRegex()
CServiceAdapter.getObjects("myapp").shouldMatch(regex2)
val text = "{\"type\":\"PNCounter\",\"metadata\":{\"increment\":[{\"name\":\"clientid\"},{\"first\":10,\"second\":{\"uid\":{\"name\":\"clientid\"},\"cnt\":-2147483647}}],\"decrement\":[{\"name\":\"clientid\"},{\"first\":5,\"second\":{\"uid\":{\"name\":\"clientid\"},\"cnt\":-2147483646}}]},\"value\":5}"
CServiceAdapter.getObject("myapp", objectUId, my_env).toJson().shouldBe(text)
delay(200)
// val regex = """\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\],\\\"decrement\\\":\[\]\},\\\"value\\\":0\}\"\]""".toRegex()
// CServiceAdapter.getObjects("myapp").shouldMatch(regex)
}
CServiceAdapter.close("myapp").shouldBeTrue()
CServiceAdapter.delete("myapp").shouldBeTrue()
delay(200)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment