Apply ktlint to uhabits-server

feature/sync2
Alinson S. Xavier 4 years ago
parent bb8b742dc4
commit 2ea6fe0570
No known key found for this signature in database
GPG Key ID: DCA0DAD4D2F58624

@ -23,9 +23,9 @@ plugins {
application application
id("kotlin") id("kotlin")
id("com.github.johnrengelman.shadow") version "7.1.0" id("com.github.johnrengelman.shadow") version "7.1.0"
id("org.jlleitschuh.gradle.ktlint")
} }
application { application {
group = "org.isoron.uhabits" group = "org.isoron.uhabits"
version = "0.0.1" version = "0.0.1"

@ -19,7 +19,7 @@
package org.isoron.uhabits.sync package org.isoron.uhabits.sync
import com.fasterxml.jackson.databind.* import com.fasterxml.jackson.databind.ObjectMapper
data class SyncData( data class SyncData(
val version: Long, val version: Long,
@ -32,4 +32,4 @@ data class GetDataVersionResponse(val version: Long)
val defaultMapper = ObjectMapper() val defaultMapper = ObjectMapper()
fun SyncData.toJson(): String = defaultMapper.writeValueAsString(this) fun SyncData.toJson(): String = defaultMapper.writeValueAsString(this)
fun GetDataVersionResponse.toJson(): String = defaultMapper.writeValueAsString(this) fun GetDataVersionResponse.toJson(): String = defaultMapper.writeValueAsString(this)

@ -19,10 +19,10 @@
package org.isoron.uhabits.sync package org.isoron.uhabits.sync
open class SyncException: RuntimeException() open class SyncException : RuntimeException()
class KeyNotFoundException: SyncException() class KeyNotFoundException : SyncException()
class ServiceUnavailable: SyncException() class ServiceUnavailable : SyncException()
class EditConflictException: SyncException() class EditConflictException : SyncException()

@ -19,15 +19,15 @@
package org.isoron.uhabits.sync.app package org.isoron.uhabits.sync.app
import io.ktor.application.* import io.ktor.application.call
import io.ktor.http.* import io.ktor.http.HttpStatusCode
import io.ktor.response.* import io.ktor.response.respond
import io.ktor.routing.* import io.ktor.routing.Routing
import io.prometheus.client.* import io.ktor.routing.get
import io.prometheus.client.exporter.common.* import io.prometheus.client.CollectorRegistry
import io.prometheus.client.hotspot.* import io.prometheus.client.exporter.common.TextFormat
import java.io.* import io.prometheus.client.hotspot.DefaultExports
import java.io.StringWriter
fun Routing.metrics(app: SyncApplication) { fun Routing.metrics(app: SyncApplication) {
// Register JVM metrics // Register JVM metrics

@ -19,11 +19,13 @@
package org.isoron.uhabits.sync.app package org.isoron.uhabits.sync.app
import io.ktor.application.* import io.ktor.application.call
import io.ktor.http.* import io.ktor.http.HttpStatusCode
import io.ktor.response.* import io.ktor.response.respond
import io.ktor.routing.* import io.ktor.routing.Routing
import org.isoron.uhabits.sync.* import io.ktor.routing.post
import org.isoron.uhabits.sync.RegisterReponse
import org.isoron.uhabits.sync.ServiceUnavailable
fun Routing.registration(app: SyncApplication) { fun Routing.registration(app: SyncApplication) {
post("/register") { post("/register") {

@ -19,12 +19,18 @@
package org.isoron.uhabits.sync.app package org.isoron.uhabits.sync.app
import io.ktor.application.* import io.ktor.application.call
import io.ktor.http.* import io.ktor.http.HttpStatusCode
import io.ktor.request.* import io.ktor.request.receive
import io.ktor.response.* import io.ktor.response.respond
import io.ktor.routing.* import io.ktor.routing.Routing
import org.isoron.uhabits.sync.* import io.ktor.routing.get
import io.ktor.routing.put
import io.ktor.routing.route
import org.isoron.uhabits.sync.EditConflictException
import org.isoron.uhabits.sync.GetDataVersionResponse
import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.SyncData
fun Routing.storage(app: SyncApplication) { fun Routing.storage(app: SyncApplication) {
route("/db/{key}") { route("/db/{key}") {
@ -33,7 +39,7 @@ fun Routing.storage(app: SyncApplication) {
try { try {
val data = app.server.getData(key) val data = app.server.getData(key)
call.respond(HttpStatusCode.OK, data) call.respond(HttpStatusCode.OK, data)
} catch(e: KeyNotFoundException) { } catch (e: KeyNotFoundException) {
call.respond(HttpStatusCode.NotFound) call.respond(HttpStatusCode.NotFound)
} }
} }
@ -54,9 +60,9 @@ fun Routing.storage(app: SyncApplication) {
try { try {
val version = app.server.getDataVersion(key) val version = app.server.getDataVersion(key)
call.respond(HttpStatusCode.OK, GetDataVersionResponse(version)) call.respond(HttpStatusCode.OK, GetDataVersionResponse(version))
} catch(e: KeyNotFoundException) { } catch (e: KeyNotFoundException) {
call.respond(HttpStatusCode.NotFound) call.respond(HttpStatusCode.NotFound)
} }
} }
} }
} }

@ -19,13 +19,18 @@
package org.isoron.uhabits.sync.app package org.isoron.uhabits.sync.app
import io.ktor.application.* import io.ktor.application.Application
import io.ktor.features.* import io.ktor.application.install
import io.ktor.jackson.* import io.ktor.features.CallLogging
import io.ktor.routing.* import io.ktor.features.ContentNegotiation
import org.isoron.uhabits.sync.repository.* import io.ktor.features.DefaultHeaders
import org.isoron.uhabits.sync.server.* import io.ktor.jackson.jackson
import java.nio.file.* import io.ktor.routing.routing
import org.isoron.uhabits.sync.repository.FileRepository
import org.isoron.uhabits.sync.server.AbstractSyncServer
import org.isoron.uhabits.sync.server.RepositorySyncServer
import java.nio.file.Path
import java.nio.file.Paths
fun Application.main() = SyncApplication().apply { main() } fun Application.main() = SyncApplication().apply { main() }

@ -19,8 +19,8 @@
package org.isoron.uhabits.sync.links package org.isoron.uhabits.sync.links
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.utils.* import org.isoron.uhabits.sync.utils.randomString
class LinkManager( class LinkManager(
private val timeoutInMillis: Long = 900_000, private val timeoutInMillis: Long = 900_000,
@ -46,4 +46,4 @@ class LinkManager(
} }
return link return link
} }
} }

@ -19,9 +19,10 @@
package org.isoron.uhabits.sync.repository package org.isoron.uhabits.sync.repository
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.KeyNotFoundException
import java.io.* import org.isoron.uhabits.sync.SyncData
import java.nio.file.* import java.io.PrintWriter
import java.nio.file.Path
class FileRepository( class FileRepository(
private val basepath: Path, private val basepath: Path,
@ -70,4 +71,4 @@ class FileRepository(
private fun String.toDataPath(): Path { private fun String.toDataPath(): Path {
return basepath.resolve("${this[0]}/${this[1]}/${this[2]}/${this[3]}/$this") return basepath.resolve("${this[0]}/${this[1]}/${this[2]}/${this[3]}/$this")
} }
} }

@ -43,4 +43,3 @@ interface Repository {
*/ */
suspend fun contains(key: String): Boolean suspend fun contains(key: String): Boolean
} }

@ -19,8 +19,11 @@
package org.isoron.uhabits.sync.server package org.isoron.uhabits.sync.server
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.EditConflictException
import org.isoron.uhabits.sync.links.* import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.ServiceUnavailable
import org.isoron.uhabits.sync.SyncData
import org.isoron.uhabits.sync.links.Link
interface AbstractSyncServer { interface AbstractSyncServer {
/** /**

@ -19,11 +19,14 @@
package org.isoron.uhabits.sync.server package org.isoron.uhabits.sync.server
import io.prometheus.client.* import io.prometheus.client.Counter
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.EditConflictException
import org.isoron.uhabits.sync.links.* import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.repository.* import org.isoron.uhabits.sync.SyncData
import org.isoron.uhabits.sync.utils.* import org.isoron.uhabits.sync.links.Link
import org.isoron.uhabits.sync.links.LinkManager
import org.isoron.uhabits.sync.repository.Repository
import org.isoron.uhabits.sync.utils.randomString
/** /**
* An AbstractSyncServer that stores all data in a [Repository]. * An AbstractSyncServer that stores all data in a [Repository].

@ -19,8 +19,8 @@
package org.isoron.uhabits.sync.utils package org.isoron.uhabits.sync.utils
import java.util.* import java.util.Random
import kotlin.streams.* import kotlin.streams.asSequence
fun randomString(length: Long): String { fun randomString(length: Long): String {
val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
@ -28,4 +28,4 @@ fun randomString(length: Long): String {
.asSequence() .asSequence()
.map(chars::get) .map(chars::get)
.joinToString("") .joinToString("")
} }

@ -20,8 +20,8 @@
package org.isoron.uhabits.sync.app package org.isoron.uhabits.sync.app
import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.mock
import io.ktor.application.* import io.ktor.application.Application
import org.isoron.uhabits.sync.server.* import org.isoron.uhabits.sync.server.AbstractSyncServer
open class BaseApplicationTest { open class BaseApplicationTest {

@ -21,12 +21,23 @@ package org.isoron.uhabits.sync.app
import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever import com.nhaarman.mockitokotlin2.whenever
import io.ktor.http.* import io.ktor.http.ContentType
import io.ktor.server.testing.* import io.ktor.http.HttpHeaders
import kotlinx.coroutines.* import io.ktor.http.HttpMethod
import org.isoron.uhabits.sync.* import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.TestApplicationCall
import io.ktor.server.testing.TestApplicationEngine
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.setBody
import io.ktor.server.testing.withTestApplication
import kotlinx.coroutines.runBlocking
import org.isoron.uhabits.sync.EditConflictException
import org.isoron.uhabits.sync.GetDataVersionResponse
import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.SyncData
import org.isoron.uhabits.sync.toJson
import org.junit.Test import org.junit.Test
import kotlin.test.* import kotlin.test.assertEquals
class StorageModuleTest : BaseApplicationTest() { class StorageModuleTest : BaseApplicationTest() {
private val data1 = SyncData(1, "Hello world") private val data1 = SyncData(1, "Hello world")
@ -64,7 +75,6 @@ class StorageModuleTest : BaseApplicationTest() {
} }
} }
@Test @Test
fun `when put succeeds should return OK`(): Unit = runBlocking { fun `when put succeeds should return OK`(): Unit = runBlocking {
withTestApplication(app()) { withTestApplication(app()) {

@ -17,17 +17,18 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@file:Suppress("BlockingMethodInNonBlockingContext") @file:Suppress("BlockingMethodInNonBlockingContext")
package org.isoron.uhabits.sync.repository package org.isoron.uhabits.sync.repository
import kotlinx.coroutines.* import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.* import org.hamcrest.CoreMatchers.equalTo
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.SyncData
import org.junit.* import org.junit.Assert.assertEquals
import org.junit.Assert.* import org.junit.Assert.assertThat
import java.nio.file.* import org.junit.Assert.assertTrue
import org.junit.Test
import java.nio.file.Files
class FileRepositoryTest { class FileRepositoryTest {
@ -50,4 +51,4 @@ class FileRepositoryTest {
val retrieved = repo.get("abcdefg") val retrieved = repo.get("abcdefg")
assertThat(retrieved, equalTo(original)) assertThat(retrieved, equalTo(original))
} }
} }

@ -19,12 +19,15 @@
package org.isoron.uhabits.sync.server package org.isoron.uhabits.sync.server
import kotlinx.coroutines.* import kotlinx.coroutines.runBlocking
import org.isoron.uhabits.sync.* import org.isoron.uhabits.sync.EditConflictException
import org.isoron.uhabits.sync.repository.* import org.isoron.uhabits.sync.KeyNotFoundException
import org.isoron.uhabits.sync.SyncData
import org.isoron.uhabits.sync.repository.FileRepository
import org.junit.Test import org.junit.Test
import java.nio.file.* import java.nio.file.Files
import kotlin.test.* import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class RepositorySyncServerTest { class RepositorySyncServerTest {
@ -57,4 +60,4 @@ class RepositorySyncServerTest {
server.put("INVALID", data0) server.put("INVALID", data0)
} }
} }
} }

Loading…
Cancel
Save