Skip to content

Commit 636db43

Browse files
Tests (#5)
* move tests to starter folder * advice tests * exclude dtos from sonar * move tests into the correct package
1 parent 11b55b1 commit 636db43

File tree

8 files changed

+80
-34
lines changed

8 files changed

+80
-34
lines changed

build.gradle.kts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import io.hndrs.gradle.plugin.Developer
2+
import io.hndrs.gradle.plugin.License
3+
import io.hndrs.gradle.plugin.Organization
4+
import io.hndrs.gradle.plugin.Scm
15
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
26

37
buildscript {
@@ -40,7 +44,8 @@ sonarqube {
4044
property("sonar.projectKey", "hndrs_jsonapi-spring-boot-starter")
4145
property("sonar.organization", "hndrs")
4246
property("sonar.host.url", "https://sonarcloud.io")
43-
property("sonar.exclusions", "**/sample/**")
47+
property("sonar.exclusions", "**/sample/**, **/response/Meta.kt," +
48+
" **/response/Response.kt, **/response/ErrorResponse.kt")
4449
}
4550
}
4651

@@ -60,15 +65,15 @@ subprojects {
6065

6166
publishingInfo {
6267
url = "https://github.com/hndrs/jsonapi-spring-boot-starter"
63-
license = io.hndrs.gradle.plugin.License(
68+
license = License(
6469
"https://github.com/hndrs/jsonapi-spring-boot-starter/blob/main/LICENSE",
6570
"MIT License"
6671
)
6772
developers = listOf(
68-
io.hndrs.gradle.plugin.Developer("marvinschramm", "Marvin Schramm", "marvin.schramm@gmail.com")
73+
Developer("marvinschramm", "Marvin Schramm", "marvin.schramm@gmail.com")
6974
)
70-
organization = io.hndrs.gradle.plugin.Organization("hndrs", "https://oss.hndrs.io")
71-
scm = io.hndrs.gradle.plugin.Scm(
75+
organization = Organization("hndrs", "https://oss.hndrs.io")
76+
scm = Scm(
7277
"scm:git:git://github.com/hndrs/jsonapi-spring-boot-starter",
7378
"https://github.com/hndrs/jsonapi-spring-boot-starter"
7479
)
@@ -99,8 +104,6 @@ subprojects {
99104
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
100105
testImplementation(platform("org.junit:junit-bom:5.7.0"))
101106
testImplementation("org.junit.jupiter:junit-jupiter")
102-
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
103-
104107
}
105108

106109
tasks.withType<KotlinCompile> {

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rootProject.name = "jsonapi-spring-boot-starter"
1+
rootProject.name = "hndrs-jsonapi-spring-boot-starter"
22

33
include("spring-json-api")
44
project(":spring-json-api").projectDir = File("spring-json-api")

spring-json-api-starter/build.gradle.kts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ dependencies {
22
api(project(":spring-json-api"))
33
api(group = "org.springframework.boot", name = "spring-boot-autoconfigure")
44

5-
annotationProcessor(group = "org.springframework.boot", name = "spring-boot-configuration-processor")
6-
kapt(group = "org.springframework.boot", name = "spring-boot-configuration-processor")
7-
85
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
96
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
107
}

spring-json-api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies {
22
optional(group = "org.springframework.boot", name = "spring-boot-starter-web")
3+
34
testImplementation("io.mockk:mockk:1.10.6")
45
}

spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.hndrs.api.exception
22

33
import io.hndrs.api.response.ErrorResponse
4-
import org.slf4j.Logger
5-
import org.slf4j.LoggerFactory
64
import org.springframework.http.HttpHeaders
75
import org.springframework.http.HttpStatus
86
import org.springframework.http.ResponseEntity
@@ -16,12 +14,12 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
1614
class ExceptionHandler : ResponseEntityExceptionHandler() {
1715

1816
@ExceptionHandler(ResponseStatusException::class)
19-
fun handleResponseStatusExceptions(ex: ResponseStatusException, request: WebRequest): ResponseEntity<Any> {
17+
fun handleResponseStatusExceptions(ex: ResponseStatusException): ResponseEntity<Any> {
2018
return ResponseEntity.status(ex.status).body(ErrorResponse(ex.status, ex.reason))
2119
}
2220

2321
@ExceptionHandler(Exception::class)
24-
fun handleExceptions(ex: Exception, request: WebRequest): ResponseEntity<Any> {
22+
fun handleExceptions(ex: Exception): ResponseEntity<Any> {
2523
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
2624
.body(ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.message))
2725
}
@@ -36,8 +34,5 @@ class ExceptionHandler : ResponseEntityExceptionHandler() {
3634
return ResponseEntity.status(status).body(ErrorResponse(status, ex.message))
3735
}
3836

39-
companion object {
40-
private val LOG: Logger = LoggerFactory.getLogger(ExceptionHandler::class.java)
41-
}
4237
}
4338

spring-json-api/src/main/kotlin/io/hndrs/api/response/ResponseAdvice.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class ResponseAdvice : ResponseBodyAdvice<Any> {
2121
response: ServerHttpResponse
2222
): Any? {
2323
return when (value) {
24-
is ResponseEntity<*> -> ResponseEntity.status(value.statusCode).headers(value.headers).body(value.body)
24+
is ResponseEntity<*> -> ResponseEntity
25+
.status(value.statusCode)
26+
.headers(value.headers)
27+
.body(Response(value.body))
2528
null -> null
2629
else -> Response(value)
2730
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
11
package io.hndrs.api.exception
22

3-
import com.nhaarman.mockitokotlin2.mock
43
import io.hndrs.api.response.ErrorResponse
5-
import org.junit.jupiter.api.Assertions
4+
import io.mockk.mockk
5+
import org.junit.jupiter.api.Assertions.assertEquals
66
import org.junit.jupiter.api.Test
77
import org.springframework.http.HttpHeaders
88
import org.springframework.http.HttpStatus
99
import org.springframework.web.server.ResponseStatusException
1010

1111
internal class ExceptionHandlerTest {
1212

13-
1413
@Test
1514
fun handleResponseStatusExceptions() {
1615
val expectedStatus = HttpStatus.BAD_REQUEST
1716
val expectedMessage = "Error Message"
1817

1918
val resonse = ExceptionHandler()
20-
.handleResponseStatusExceptions(ResponseStatusException(expectedStatus, expectedMessage), mock())
19+
.handleResponseStatusExceptions(ResponseStatusException(expectedStatus, expectedMessage))
2120

22-
Assertions.assertEquals(expectedStatus, resonse.statusCode)
23-
Assertions.assertEquals(resonse.body, ErrorResponse(expectedStatus, expectedMessage))
21+
assertEquals(expectedStatus, resonse.statusCode)
22+
assertEquals(resonse.body, ErrorResponse(expectedStatus, expectedMessage))
2423
}
2524

2625
@Test
2726
fun handleExceptions() {
2827
val expectedMessage = "Error Message"
2928

30-
val resonse = ExceptionHandler()
31-
.handleExceptions(IllegalStateException(expectedMessage), mock())
29+
val response = ExceptionHandler()
30+
.handleExceptions(IllegalStateException(expectedMessage))
3231

33-
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, resonse.statusCode)
34-
Assertions.assertEquals(resonse.body, ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, expectedMessage))
32+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.statusCode)
33+
assertEquals(response.body, ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, expectedMessage))
3534
}
3635

3736
@Test
3837
fun handleExceptionsInternal() {
3938
val expectedStatus = HttpStatus.BAD_REQUEST
4039
val expectedMessage = "Error Message"
4140

42-
val resonse = ExceptionHandler()
41+
val response = ExceptionHandler()
4342
.handleExceptionInternal(
4443
IllegalStateException(expectedMessage),
4544
null,
4645
HttpHeaders(),
4746
expectedStatus,
48-
mock()
47+
mockk()
4948
)
5049

51-
Assertions.assertEquals(expectedStatus, resonse!!.statusCode)
52-
Assertions.assertEquals(resonse!!.body, ErrorResponse(HttpStatus.BAD_REQUEST, expectedMessage))
50+
assertEquals(expectedStatus, response.statusCode)
51+
assertEquals(response.body, ErrorResponse(HttpStatus.BAD_REQUEST, expectedMessage))
5352
}
5453
}

spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,67 @@
11
package io.hndrs.api.response
22

33
import io.mockk.mockk
4+
import org.junit.jupiter.api.Assertions.assertEquals
45
import org.junit.jupiter.api.Assertions.assertFalse
6+
import org.junit.jupiter.api.Assertions.assertNull
57
import org.junit.jupiter.api.Assertions.assertTrue
68
import org.junit.jupiter.api.Test
79
import org.springframework.core.MethodParameter
10+
import org.springframework.http.MediaType
11+
import org.springframework.http.ResponseEntity
812
import org.springframework.http.converter.HttpMessageConverter
913
import kotlin.reflect.jvm.javaMethod
1014

1115
internal class ResponseAdviceTest {
1216

17+
private val mockConverterType = mockk<HttpMessageConverter<String>>()::class.java
18+
19+
@Test
20+
fun beforeBodyWriteSimpleValue() {
21+
val beforeValue = "value"
22+
val beforeBodyWrite = ResponseAdvice()
23+
.beforeBodyWrite(
24+
beforeValue,
25+
supportedMethod,
26+
MediaType.APPLICATION_JSON,
27+
mockConverterType,
28+
mockk(),
29+
mockk()
30+
)
31+
assertEquals(Response(beforeValue), beforeBodyWrite)
32+
}
33+
34+
@Test
35+
fun beforeBodyWriteResponseEntityValue() {
36+
val beforeValue = ResponseEntity.ok("value")
37+
val beforeBodyWrite = ResponseAdvice()
38+
.beforeBodyWrite(
39+
beforeValue,
40+
supportedMethod,
41+
MediaType.APPLICATION_JSON,
42+
mockConverterType,
43+
mockk(),
44+
mockk()
45+
)
46+
assertEquals(ResponseEntity.ok(Response("value")), beforeBodyWrite)
47+
}
48+
49+
@Test
50+
fun beforeBodyWriteNullValue() {
51+
val beforeBodyWrite = ResponseAdvice()
52+
.beforeBodyWrite(
53+
null,
54+
supportedMethod,
55+
MediaType.APPLICATION_JSON,
56+
mockConverterType,
57+
mockk(),
58+
mockk()
59+
)
60+
assertNull(beforeBodyWrite)
61+
}
1362

1463
@Test
1564
fun supports() {
16-
val mockConverterType = mockk<HttpMessageConverter<String>>()::class.java
1765
assertTrue(ResponseAdvice().supports(supportedMethod, mockConverterType))
1866
assertFalse(ResponseAdvice().supports(unsupportedMethod, mockConverterType))
1967
}

0 commit comments

Comments
 (0)