Skip to content

Commit

Permalink
Switch to Kover for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Aug 3, 2024
1 parent 3ed93e5 commit d6dc058
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 28 deletions.
12 changes: 3 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
id("com.github.breadmoirai.github-release")
id("org.sonarqube")
id("io.github.gradle-nexus.publish-plugin")

id("org.jetbrains.kotlinx.kover")

kotlin("jvm") apply (false)
id("org.cadixdev.licenser") apply (false)
Expand Down Expand Up @@ -47,11 +47,11 @@ allprojects {
configure(moduleNames.map { project(":sunday-$it") }) {

apply(plugin = "java-library")
apply(plugin = "jacoco")
apply(plugin = "maven-publish")
apply(plugin = "signing")

apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "org.jetbrains.kotlinx.kover")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "org.cadixdev.licenser")
apply(plugin = "org.jmailen.kotlinter")
Expand Down Expand Up @@ -105,10 +105,6 @@ configure(moduleNames.map { project(":sunday-$it") }) {
// TEST
//

configure<JacocoPluginExtension> {
toolVersion = "0.8.12"
}

tasks.named<Test>("test").configure {

useJUnitPlatform()
Expand All @@ -120,8 +116,6 @@ configure(moduleNames.map { project(":sunday-$it") }) {
}

reports.junitXml.required.set(true)

finalizedBy("jacocoTestReport")
}


Expand Down Expand Up @@ -284,7 +278,7 @@ configure(moduleNames.map { project(":sunday-$it") }) {
property("sonar.jacoco.reportPaths", "")
property(
"sonar.coverage.jacoco.xmlReportPaths",
"$rootDir/code-coverage/build/reports/jacoco/testCoverageReport/testCoverageReport.xml",
"$rootDir/code-coverage/build/reports/kover/report.xml",
)
}
}
Expand Down
21 changes: 5 additions & 16 deletions code-coverage/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@

plugins {
base
id("jacoco-report-aggregation")
id("org.jetbrains.kotlinx.kover")
}

repositories {
mavenCentral()
}

dependencies {
jacocoAggregation(project(":sunday-core"))
jacocoAggregation(project(":sunday-jdk"))
jacocoAggregation(project(":sunday-okhttp"))
}

reporting {
reports {
create<JacocoCoverageReport>("testCoverageReport") {
testType.set(TestSuiteType.UNIT_TEST)
reportTask {
reports.xml.required.set(true)
}
}
}
kover(project(":sunday-core"))
kover(project(":sunday-jdk"))
kover(project(":sunday-okhttp"))
}

tasks {

check {
finalizedBy(named<JacocoReport>("testCoverageReport"))
finalizedBy(named("koverXmlReport"), named("koverHtmlReport"))
}

}
2 changes: 1 addition & 1 deletion core/src/main/kotlin/io/outfoxx/sunday/RequestFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ abstract class RequestFactory : Closeable {
response.contentType?.let { MediaType.from(it.toString()) }
?: throw SundayError(
SundayError.Reason.InvalidContentType,
response.contentType?.value ?: "",
response.contentType?.value ?: "<none provided>",
)

val contentTypeDecoder =
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/io/outfoxx/sunday/SundayError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SundayError(
ResponseDecodingFailed("Response decoding failed"),
EventDecodingFailed("Event decoding failed"),
InvalidBaseUri("Base URL is invalid after expanding template"),
NoSupportedContentTypes("None of the provided Content-Types for the request has a registered decoder"),
NoSupportedContentTypes("None of the provided Content-Types for the request has a registered encoder"),
NoSupportedAcceptTypes("None of the provided Accept types for the request has a registered decoder"),
InvalidHeaderValue("The encoded header value contains one or more invalid characters"),
}
Expand Down
61 changes: 61 additions & 0 deletions core/src/test/kotlin/HeaderExtensionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2020 Outfox, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.outfoxx.sunday.MediaType
import io.outfoxx.sunday.http.HeaderParameters
import io.outfoxx.sunday.http.getAll
import io.outfoxx.sunday.http.getFirst
import io.outfoxx.sunday.http.toMultiMap
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.containsInAnyOrder
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.oneOf
import org.junit.jupiter.api.Test

class HeaderExtensionsTest {

@Test
fun `test getFirst`() {
val headers = HeaderParameters.encode(mapOf("test" to arrayOf(MediaType.JSON, MediaType.CBOR)))

assertThat(
headers.getFirst("test"),
`is`(oneOf(MediaType.JSON.value, MediaType.CBOR.value)),
)
}

@Test
fun `test getAll`() {
val headers = HeaderParameters.encode(mapOf("test" to arrayOf(MediaType.JSON, MediaType.CBOR)))

assertThat(
headers.getAll("test"),
containsInAnyOrder(MediaType.JSON.value, MediaType.CBOR.value),
)
}

@Test
fun `test toMultiMap`() {
val headers = HeaderParameters.encode(mapOf("test" to arrayOf(MediaType.JSON, MediaType.CBOR)))

assertThat(
headers.toMultiMap(),
equalTo(mapOf("test" to listOf(MediaType.JSON.value, MediaType.CBOR.value))),
)
}

}
33 changes: 33 additions & 0 deletions core/src/test/kotlin/MediaTypeCodecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
* limitations under the License.
*/

import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper
import io.outfoxx.sunday.MediaType
import io.outfoxx.sunday.mediatypes.codecs.BinaryDecoder
import io.outfoxx.sunday.mediatypes.codecs.BinaryEncoder
import io.outfoxx.sunday.mediatypes.codecs.MediaTypeDecoders
import io.outfoxx.sunday.mediatypes.codecs.MediaTypeEncoders
import io.outfoxx.sunday.mediatypes.codecs.TextDecoder
import io.outfoxx.sunday.mediatypes.codecs.TextEncoder
import io.outfoxx.sunday.mediatypes.codecs.decode
Expand All @@ -27,6 +32,9 @@ import okio.Source
import okio.buffer
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.not
import org.hamcrest.Matchers.nullValue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -142,4 +150,29 @@ class MediaTypeCodecTest {
}
}

@Test
fun `test encoders builder registers specific codecs`() {
val encoders =
MediaTypeEncoders
.Builder()
.registerJSON(JsonMapper())
.registerCBOR(CBORMapper())
.build()

assertThat(encoders.find(MediaType.JSON), `is`(not(nullValue())))
assertThat(encoders.find(MediaType.CBOR), `is`(not(nullValue())))
}

@Test
fun `test decoders builder registers specific codecs`() {
val decoders =
MediaTypeDecoders
.Builder()
.registerJSON(JsonMapper())
.registerCBOR(CBORMapper())
.build()

assertThat(decoders.find(MediaType.JSON), `is`(not(nullValue())))
assertThat(decoders.find(MediaType.CBOR), `is`(not(nullValue())))
}
}
7 changes: 7 additions & 0 deletions core/src/test/kotlin/MediaTypeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,11 @@ class MediaTypeTest {
assertFalse(htmlWithCharset.compatible(MediaType.JSONStructured))
assertTrue(htmlWithCharset.compatible(MediaType.Any))
}

@Test
fun `test constructor`() {
val mediaType = MediaType(Application, Vendor, "test", Zip, "charset" to "utf-8")

assertEquals(mediaType.value, "application/vnd.test+zip;charset=utf-8")
}
}
64 changes: 64 additions & 0 deletions core/src/test/kotlin/ProblemsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2020 Outfox, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.outfoxx.sunday.http.Headers
import io.outfoxx.sunday.http.Request
import io.outfoxx.sunday.http.Response
import io.outfoxx.sunday.utils.Problems
import okio.BufferedSource
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import org.zalando.problem.Status

class ProblemsTest {

@Test
fun `test forResponse`() {
val problem = Problems.forResponse(TestResponse(400, "Bad Request", listOf(), null))

assertThat(problem.status, equalTo(Status.BAD_REQUEST))
}

@Test
fun `test forStatus`() {
val problem = Problems.forStatus(400, "Bad Request")

assertThat(problem.status, equalTo(Status.BAD_REQUEST))
}

@Test
fun `test forStatus supports non-standard values`() {
val problem = Problems.forStatus(195, "AI Thinking")

assertThat(problem.status?.statusCode, equalTo(195))
assertThat(problem.status?.reasonPhrase, equalTo("AI Thinking"))
}

data class TestResponse(
override val statusCode: Int,
override val reasonPhrase: String?,
override val headers: Headers,
override val body: BufferedSource?,
) : Response {

override val trailers: Headers?
get() = null
override val request: Request
get() = TODO("Not yet implemented")
}

}
Loading

0 comments on commit d6dc058

Please sign in to comment.