Skip to content

Commit

Permalink
feat: add ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Sonzogni committed Jan 31, 2024
1 parent 962bd51 commit 0eceb37
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 276 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*]
ktlint_standard_no-wildcard-imports = disabled
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
// Apply to all projects, used to check unused dependencies, run `./gradlew lintGradle` to check dependencies.
// Be aware that automatic fixing with `fixLintGradle` might break something.
id('nebula.lint') version "17.1.0"
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
}

// Do not delete, this will get updated automatically when running the version command
Expand All @@ -22,6 +23,8 @@ allprojects {
}

subprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint"

// setup junit5 and remove all api imports of junit 4
configurations {
implementation {
Expand Down
15 changes: 7 additions & 8 deletions commons/src/main/kotlin/eu/miaplatform/commons/Serialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package eu.miaplatform.commons
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.registerKotlinModule

object Serialization {
val defaultRetrofitMapper = ObjectMapper().apply {
setSerializationInclusion(JsonInclude.Include.NON_NULL)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
registerKotlinModule()
}
val defaultRetrofitMapper =
ObjectMapper().apply {
setSerializationInclusion(JsonInclude.Include.NON_NULL)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
registerKotlinModule()
}

fun ObjectMapper.defaultKtorLiteral() {
setSerializationInclusion(JsonInclude.Include.NON_NULL)
registerModule(JavaTimeModule())
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class StatusService {
init {
try {
versionProperties.load(this.javaClass.getResourceAsStream("/version.properties"))
} catch (e: Exception) {}
} catch (e: Exception) {
}
}

fun getVersion() : String = versionProperties.getProperty("version") ?: "no version"

}
fun getVersion(): String = versionProperties.getProperty("version") ?: "no version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package eu.miaplatform.commons.client
import retrofit2.http.*

interface CrudClientInterface {

@GET("v2/books")
suspend fun getBooks(@HeaderMap headers: Map<String, String>): List<String>

}
suspend fun getBooks(
@HeaderMap headers: Map<String, String>,
): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import retrofit2.converter.jackson.JacksonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitClient {

inline fun <reified T> build(
basePath: String,
logLevel: HttpLoggingInterceptor.Level,
objectMapper: ObjectMapper = Serialization.defaultRetrofitMapper
objectMapper: ObjectMapper = Serialization.defaultRetrofitMapper,
): T {
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = logLevel
}
val loggingInterceptor =
HttpLoggingInterceptor().apply {
level = logLevel
}

val client = OkHttpClient.Builder()
.callTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build()
val client =
OkHttpClient.Builder()
.callTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build()

return Retrofit.Builder()
.baseUrl(basePath)
Expand All @@ -34,4 +35,4 @@ object RetrofitClient {
.build()
.create(T::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ fun Application.install(app: CustomApplication) {

fun Application.install(app: CustomApiApplication) {
apiRouting { app.install(this) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eu.miaplatform.commons.ktor

import io.ktor.server.application.*


fun ApplicationCall.headersToProxy(additionalHeaderToProxy: String = ""): Map<String, String> {
val requestIdHeaderKey = "x-request-id"
val userIdHeaderKey = System.getenv("USERID_HEADER_KEY") ?: "miauserid"
Expand All @@ -23,9 +22,9 @@ fun ApplicationCall.headersToProxy(additionalHeaderToProxy: String = ""): Map<St
additionalHeaderToProxy.split(",").forEach { key ->
val headerValue = request.headers[key]
headerValue?.let { header ->
headers[key] = header
headers[key] = header
}
}

return headers
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package eu.miaplatform.commons.model

data class BadRequestException(val code: Int, val errorMessage: String): Exception(errorMessage)
data class InternalServerErrorException(val code: Int, val errorMessage: String): Exception(errorMessage)
data class NotFoundException(val code: Int, val errorMessage: String): Exception(errorMessage)
data class UnauthorizedException(val code: Int, val errorMessage: String): Exception(errorMessage)
data class BadRequestException(val code: Int, val errorMessage: String) : Exception(errorMessage)

data class InternalServerErrorException(val code: Int, val errorMessage: String) : Exception(errorMessage)

data class NotFoundException(val code: Int, val errorMessage: String) : Exception(errorMessage)

data class UnauthorizedException(val code: Int, val errorMessage: String) : Exception(errorMessage)
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package eu.miaplatform.commons.model

import com.fasterxml.jackson.annotation.JsonProperty

data class HealthBodyResponse (
data class HealthBodyResponse(
@JsonProperty("name")
val name: String,

@JsonProperty("version")
val version: String,

@JsonProperty("status")
val status: String
val status: String,
) {
enum class Status(val value: String) {
OK("OK"),
KO("KO")
KO("KO"),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package eu.miaplatform.commons.ktor

import assertk.assertThat
import assertk.assertions.*
import eu.miaplatform.commons.ktor.headersToProxy
import io.kotest.core.spec.style.DescribeSpec
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.Test

class HeadersToProxyTest : DescribeSpec ({
class HeadersToProxyTest : DescribeSpec({

it("returns empty header map when no header is present") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
}.apply {
val headers = headersToProxy()
assertThat(mapOf<String, String>()).isEqualTo(headers)
Expand All @@ -22,7 +20,7 @@ class HeadersToProxyTest : DescribeSpec ({

it("returns the correct header map when x-request-id has a value") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("x-request-id", "1234abcd")
}.apply {
val headers = headersToProxy()
Expand All @@ -33,7 +31,7 @@ class HeadersToProxyTest : DescribeSpec ({

it("returns the correct header map when miauserid has a value") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("miauserid", "userid")
}.apply {
val headers = headersToProxy()
Expand All @@ -44,7 +42,7 @@ class HeadersToProxyTest : DescribeSpec ({

it("returns the correct header map when miausergroups has a value") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("miausergroups", "group")
}.apply {
assertThat(headersToProxy()).isEqualTo(mapOf("miausergroups" to "group"))
Expand All @@ -55,7 +53,7 @@ class HeadersToProxyTest : DescribeSpec ({
it("returns the correct header map when client-type has a value") {
withTestApplication {

handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("client-type", "type")
}.apply {
assertThat(headersToProxy()).isEqualTo(mapOf("client-type" to "type"))
Expand All @@ -66,7 +64,7 @@ class HeadersToProxyTest : DescribeSpec ({
it("returns the correct header map when isbackoffice has a value") {
withTestApplication {

handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("isbackoffice", "true")
}.apply {
assertThat(headersToProxy()).isEqualTo(mapOf("isbackoffice" to "true"))
Expand All @@ -76,7 +74,7 @@ class HeadersToProxyTest : DescribeSpec ({

it("returns the correct header map when miauserproperties has a value") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("miauserproperties", "property")
}.apply {
assertThat(headersToProxy()).isEqualTo(mapOf("miauserproperties" to "property"))
Expand All @@ -86,7 +84,7 @@ class HeadersToProxyTest : DescribeSpec ({

it("returns the correct header map when all platform headers have value") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("x-request-id", "1234abcd")
addHeader("miauserid", "userid")
addHeader("miausergroups", "group")
Expand All @@ -101,16 +99,16 @@ class HeadersToProxyTest : DescribeSpec ({
"miausergroups" to "group",
"miauserproperties" to "property",
"client-type" to "type",
"miauserproperties" to "property"
)
"miauserproperties" to "property",
),
)
}
}
}

it("returns the header map with only headers to proxy when there are more") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("x-request-id", "1234abcd")
addHeader("miauserid", "userid")
addHeader("miausergroups", "group")
Expand All @@ -126,16 +124,16 @@ class HeadersToProxyTest : DescribeSpec ({
"miausergroups" to "group",
"miauserproperties" to "property",
"client-type" to "type",
"miauserproperties" to "property"
)
"miauserproperties" to "property",
),
)
}
}
}

it("returns the header map with additional headers to proxy if present") {
withTestApplication {
handleRequest(HttpMethod.Get, "/proxy-headers"){
handleRequest(HttpMethod.Get, "/proxy-headers") {
addHeader("x-request-id", "1234abcd")
addHeader("miauserid", "userid")
addHeader("miausergroups", "group")
Expand All @@ -154,10 +152,10 @@ class HeadersToProxyTest : DescribeSpec ({
"miauserproperties" to "property",
"client-type" to "type",
"miauserproperties" to "property",
"some-other-header-to-proxy" to "other"
)
"some-other-header-to-proxy" to "other",
),
)
}
}
}
})
})
Loading

0 comments on commit 0eceb37

Please sign in to comment.