|
|
@ -21,6 +21,7 @@ package org.isoron.uhabits.sync.app
|
|
|
|
|
|
|
|
|
|
|
|
import io.ktor.http.*
|
|
|
|
import io.ktor.http.*
|
|
|
|
import io.ktor.server.testing.*
|
|
|
|
import io.ktor.server.testing.*
|
|
|
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import org.isoron.uhabits.sync.*
|
|
|
|
import org.isoron.uhabits.sync.*
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.Test
|
|
|
|
import org.mockito.Mockito.*
|
|
|
|
import org.mockito.Mockito.*
|
|
|
@ -31,8 +32,8 @@ class StorageModuleTest : BaseApplicationTest() {
|
|
|
|
private val data2 = SyncData(2, "Hello new world")
|
|
|
|
private val data2 = SyncData(2, "Hello new world")
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when get succeeds should return data`() {
|
|
|
|
fun `when get succeeds should return data`(): Unit = runBlocking {
|
|
|
|
`when`(server.get("k1")).thenReturn(data1)
|
|
|
|
`when`(server.getData("k1")).thenReturn(data1)
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handleGet("/db/k1").apply {
|
|
|
|
handleGet("/db/k1").apply {
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
@ -42,19 +43,19 @@ class StorageModuleTest : BaseApplicationTest() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when get version succeeds should return version`() {
|
|
|
|
fun `when get version succeeds should return version`(): Unit = runBlocking {
|
|
|
|
`when`(server.get("k1")).thenReturn(data1)
|
|
|
|
`when`(server.getDataVersion("k1")).thenReturn(30)
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handleGet("/db/k1/version").apply {
|
|
|
|
handleGet("/db/k1/version").apply {
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
|
assertEquals("1", response.content)
|
|
|
|
assertEquals(GetDataVersionResponse(30).toJson(), response.content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when get with invalid key should return 404`() {
|
|
|
|
fun `when get with invalid key should return 404`(): Unit = runBlocking {
|
|
|
|
`when`(server.get("k1")).thenThrow(KeyNotFoundException())
|
|
|
|
`when`(server.getData("k1")).thenThrow(KeyNotFoundException())
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handleGet("/db/k1").apply {
|
|
|
|
handleGet("/db/k1").apply {
|
|
|
|
assertEquals(HttpStatusCode.NotFound, response.status())
|
|
|
|
assertEquals(HttpStatusCode.NotFound, response.status())
|
|
|
@ -64,17 +65,19 @@ class StorageModuleTest : BaseApplicationTest() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when put succeeds should return OK`() {
|
|
|
|
fun `when put succeeds should return OK`(): Unit = runBlocking {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
|
runBlocking {
|
|
|
|
verify(server).put("k1", data1)
|
|
|
|
assertEquals(HttpStatusCode.OK, response.status())
|
|
|
|
|
|
|
|
verify(server).put("k1", data1)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when put with invalid key should return 404`() {
|
|
|
|
fun `when put with invalid key should return 404`(): Unit = runBlocking {
|
|
|
|
`when`(server.put("k1", data1)).thenThrow(KeyNotFoundException())
|
|
|
|
`when`(server.put("k1", data1)).thenThrow(KeyNotFoundException())
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
@ -84,13 +87,12 @@ class StorageModuleTest : BaseApplicationTest() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `when put with invalid version should return 409 and current data`() {
|
|
|
|
fun `when put with invalid version should return 409 and current data`(): Unit = runBlocking {
|
|
|
|
`when`(server.put("k1", data1)).thenThrow(EditConflictException())
|
|
|
|
`when`(server.put("k1", data1)).thenThrow(EditConflictException())
|
|
|
|
`when`(server.get("k1")).thenReturn(data2)
|
|
|
|
`when`(server.getData("k1")).thenReturn(data2)
|
|
|
|
withTestApplication(app()) {
|
|
|
|
withTestApplication(app()) {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
|
handlePut("/db/k1", data1).apply {
|
|
|
|
assertEquals(HttpStatusCode.Conflict, response.status())
|
|
|
|
assertEquals(HttpStatusCode.Conflict, response.status())
|
|
|
|
assertEquals(data2.toJson(), response.content)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|