Skip to content

Commit edb1f32

Browse files
JAVA-37598 | fixed implementation and test
1 parent 9b3452a commit edb1f32

File tree

7 files changed

+42
-24
lines changed

7 files changed

+42
-24
lines changed

spring-boot-test-kotlin/src/test/kotlin/com/baeldung/validation/UserControllerUnitTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class UserControllerUnitTest(@Autowired val mockMvc: MockMvc) {
2525
).andExpect(status().isOk)
2626
}
2727

28-
//@Test
29-
//Todo: temporarily disabled it due to failure
28+
@Test
3029
fun whenPostRequestWithInvalidUserJson_thenReturnsStatus400() {
3130
mockMvc.perform(
3231
post("/users")
@@ -60,8 +59,7 @@ class UserControllerUnitTest(@Autowired val mockMvc: MockMvc) {
6059
).andExpect(status().isOk)
6160
}
6261

63-
//@Test
64-
//Todo: temporarily disabled it due to failure
62+
@Test
6563
fun whenPostRequestWithInvalidUserAddressJson_thenReturnsStatus400() {
6664
mockMvc.perform(
6765
post("/users/address")

spring-reactive-kotlin/src/main/kotlin/com/baeldung/logfilter/HttpHeadersExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fun HttpHeaders.asString(): String {
1212
entry.value
1313
) + "]"
1414
}
15-
.collect(Collectors.joining("\n"))
15+
.collect(Collectors.joining(","))
1616
}

spring-reactive-kotlin/src/main/kotlin/com/baeldung/logfilter/decorator/LoggingRequestDecorator.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ class LoggingRequestDecorator internal constructor(log: Logger, delegate: Server
2121
}
2222

2323
init {
24-
//if (log.isDebugEnabled) {
24+
if (log.isDebugEnabled) {
2525
val path = delegate.uri.path
2626
val query = delegate.uri.query
2727
val method = Optional.ofNullable(delegate.method).orElse(HttpMethod.GET).name()
2828
val headers = delegate.headers.asString()
2929
log.debug(
30-
"{} {}\n {}", method, path + (if (StringUtils.hasText(query)) "?$query" else ""), headers
30+
"{} {} {}", method, path + (if (StringUtils.hasText(query)) "?$query" else ""), headers
3131
)
3232
body = super.getBody().doOnNext { buffer: DataBuffer ->
3333
val bodyStream = ByteArrayOutputStream()
3434
Channels.newChannel(bodyStream).write(buffer.asByteBuffer().asReadOnlyBuffer())
3535
log.debug("{}: {}", "request", String(bodyStream.toByteArray()))
3636
}
37-
37+
}
38+
else {
39+
body = super.getBody()
40+
}
3841
}
3942
}

spring-reactive-kotlin/src/main/kotlin/com/baeldung/logfilter/decorator/LoggingResponseDecorator.kt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,39 @@ import reactor.core.publisher.Flux
1010
import reactor.core.publisher.Mono
1111
import java.io.ByteArrayOutputStream
1212
import java.nio.channels.Channels
13+
import java.nio.charset.StandardCharsets
1314

1415
class LoggingResponseDecorator internal constructor(val log: Logger, delegate: ServerHttpResponse) : ServerHttpResponseDecorator(delegate) {
1516

1617
override fun writeWith(body: Publisher<out DataBuffer>): Mono<Void> {
17-
return super.writeWith(Flux.from(body)
18-
.doOnNext { buffer: DataBuffer ->
19-
if (log.isDebugEnabled) {
20-
val bodyStream = ByteArrayOutputStream()
21-
Channels.newChannel(bodyStream).write(buffer.asByteBuffer().asReadOnlyBuffer())
22-
log.debug("{}: {} - {} : {}", "response", String(bodyStream.toByteArray()), "header", delegate.headers.asString())
23-
}
24-
})
18+
if(!log.isDebugEnabled){
19+
return super.writeWith(body);
20+
}
21+
22+
val responseBody = StringBuilder()
23+
24+
return super.writeWith(
25+
Flux.from(body)
26+
.doOnNext { buffer: DataBuffer ->
27+
try {
28+
val bodyStream = ByteArrayOutputStream()
29+
Channels.newChannel(bodyStream).write(buffer.asByteBuffer().asReadOnlyBuffer())
30+
responseBody.append(String(bodyStream.toByteArray(), StandardCharsets.UTF_8))
31+
} catch (e: Exception) {
32+
log.error("Error while processing response body", e)
33+
}
34+
35+
}
36+
.doOnComplete {
37+
log.debug(
38+
"{}: {} - {} : {}",
39+
"response",
40+
responseBody.toString().trim(),
41+
"header",
42+
delegate.headers.asString()
43+
)
44+
}
45+
.then(Mono.fromRunnable {})
46+
)
2547
}
2648
}

spring-reactive-kotlin/src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<root level="INFO">
1111
<appender-ref ref="STDOUT" />
1212
</root>
13+
1314
</configuration>

spring-reactive-kotlin/src/test/kotlin/com/baeldung/logfilter/LogFilterUnitTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ open class LogFilterUnitTest {
2525
`when`(log.isDebugEnabled).thenReturn(true)
2626
}
2727

28-
//@Test
29-
//Todo: temporarily disabled it due to failure
28+
@Test
3029
fun whenGet_thenLogResponseBody() {
3130
client.get()
3231
.uri("/get")
@@ -35,8 +34,7 @@ open class LogFilterUnitTest {
3534
verify(log).debug("{}: {} - {} : {}", "response", "[1,2,3]", "header", " Content-Type: [application/json]")
3635
}
3736

38-
//@Test
39-
//Todo: temporarily disabled it due to failure
37+
@Test
4038
fun whenPost_thenLogRequestAndResponseBody() {
4139
client.post()
4240
.uri("/post")

spring-reactive-kotlin/src/test/resources/application.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)