Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 03143181 authored by Yannick Li's avatar Yannick Li
Browse files

Update the CServiceAdapter to the new C-Service using crdtlib v1

parent 578ded06
No related branches found
No related tags found
1 merge request!6Resolve "Update to c-crdtlib v1.0.0"
......@@ -19,11 +19,15 @@
package client.utils
import crdtlib.crdt.DeltaCRDT
import crdtlib.crdt.*
import io.ktor.client.HttpClient
import io.ktor.client.request.url
import io.ktor.client.request.post
import io.ktor.http.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
*
......@@ -43,6 +47,7 @@ class CServiceAdapter {
}
client.close()
return resp == "\"OK\""
}
/**
......@@ -115,13 +120,13 @@ class CServiceAdapter {
* @param objectUId CRDT id
* @param crdt new crdt
*/
suspend fun updateObject(dbName: String, objectUId: CObjectUId, crdt: DeltaCRDT): Boolean{
suspend fun updateObject(dbName: String, myid: String, crdt: DeltaCRDT): Boolean{
val client = HttpClient()
val crdtJson = crdt.toJson()
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":"$objectUId.name", "document":"$crdtJson"}"""
body = """{"appName":"$dbName","id":"$myid", "document":"$crdtJson"}"""
}
client.close()
return resp == "\"OK\""
......@@ -134,5 +139,20 @@ class CServiceAdapter {
suspend fun close(dbName: String): Boolean{
return true
}
/**
* Delete the database
* @param dbName database name
*/
suspend fun delete(dbName: String): Boolean{
val client = HttpClient()
val resp = client.post<String> {
url("http://127.0.0.1:4000/api/delete-app")
contentType(ContentType.Application.Json)
body = """{"appName":"$dbName"}"""
}
client.close()
return resp == "\"OK\""
}
}
}
......@@ -19,8 +19,13 @@
package client.utils
import crdtlib.crdt.PNCounter
import crdtlib.crdt.DeltaCRDTFactory
import crdtlib.utils.ClientUId
import crdtlib.utils.SimpleEnvironment
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldMatch
import kotlinx.coroutines.delay
......@@ -28,16 +33,28 @@ class CServiceAdapterTest : StringSpec({
"connect to c-service create, write twice, read and delete" {
CServiceAdapter.connect("myapp").shouldBeTrue()
CServiceAdapter.updateObject("myapp", "myid", """{\"key\":\"value1\"}""")
delay(500)
val regex1 = """\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"key\\\":\\\"value1\\\"\}\"\]""".toRegex()
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()
delay(200)
val regex1 = """\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\],\\\"decrement\\\":\[\]\},\\\"value\\\":0\}\"\]""".toRegex()
CServiceAdapter.getObjects("myapp").shouldMatch(regex1)
delay(200)
delay(500)
if (my_crdt is PNCounter) {
my_crdt.increment(10)
CServiceAdapter.updateObject("myapp", "myid", my_crdt)
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)
delay(200)
}
CServiceAdapter.updateObject("myapp", "myid", """{\"key\":\"value2\"}""")
delay(500)
val regex2 = """\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"key\\\":\\\"value2\\\"\}\"""".toRegex()
CServiceAdapter.getObject("myapp", "myid").shouldMatch(regex2)
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