aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-utils/src/test/kotlin
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-12-07 14:41:39 +0100
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-12-10 14:46:23 +0100
commit8b8c37c296e55644063e0332fd455437168e78da (patch)
tree36e9d96217346dd4296677cfd8af584c69a0ad05 /sources/hv-collector-utils/src/test/kotlin
parent73293332b2244b66083dc5d3910801c1b1058105 (diff)
Add log diagnostic context
As it's not trivial to use MDC directly from logging framework in reactive application, we need to do some work manually. The approach proposed is an explicit MDC handling, which means that context is kept as an object created after establishing client connection. Next, new instance of HvVesCollector (and its dependencies) is created. Every object is propagated with ClientContext so it can use it when calling logger methods. In the future ClientContext might be used to support other use-cases, ie. per-topic access control. As a by-product I had to refactor our Logger wrapper, too. It already had too many functions and after adding MDC number would be doubled. Change-Id: I9c5d3f5e1d1be1db66d28d292eb0e1c38d8d0ffe Issue-ID: DCAEGEN2-671 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'sources/hv-collector-utils/src/test/kotlin')
-rw-r--r--sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt78
-rw-r--r--sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/ReactiveLoggingTest.kt12
2 files changed, 29 insertions, 61 deletions
diff --git a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
index c27fb8c8..10fc8d8f 100644
--- a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
+++ b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
@@ -34,11 +34,16 @@ import org.jetbrains.spek.api.dsl.it
object LoggerTest : Spek({
lateinit var slf4jLogger: org.slf4j.Logger
- lateinit var cut: Logger
+ fun cut() = Logger(slf4jLogger).also {
+ verify(slf4jLogger).isTraceEnabled
+ verify(slf4jLogger).isDebugEnabled
+ verify(slf4jLogger).isInfoEnabled
+ verify(slf4jLogger).isWarnEnabled
+ verify(slf4jLogger).isErrorEnabled
+ }
beforeEachTest {
slf4jLogger = mock()
- cut = Logger(slf4jLogger)
}
afterEachTest {
@@ -50,28 +55,19 @@ object LoggerTest : Spek({
val exception = Exception("fail")
describe("debug levels") {
- it("should log message") {
- cut.debug(message)
- verify(slf4jLogger).debug(message)
- }
-
- it("should log message with exception") {
- cut.debug(message, exception)
- verify(slf4jLogger).debug(message, exception)
- }
describe("lazy logging message") {
it("should log when debug is ON") {
whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
- cut.debug { message }
+ cut().debug { message }
verify(slf4jLogger).isDebugEnabled
verify(slf4jLogger).debug(message)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
- cut.debug { message }
+ cut().debug { message }
verify(slf4jLogger).isDebugEnabled
}
}
@@ -80,42 +76,33 @@ object LoggerTest : Spek({
it("should log when debug is ON") {
whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
- cut.debug(exception) { message }
+ cut().withDebug { log(message, exception) }
verify(slf4jLogger).isDebugEnabled
verify(slf4jLogger).debug(message, exception)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
- cut.debug(exception) { message }
+ cut().withDebug { log(message, exception) }
verify(slf4jLogger).isDebugEnabled
}
}
}
describe("info levels") {
- it("should log message") {
- cut.info(message)
- verify(slf4jLogger).info(message)
- }
-
- it("should log message with exception") {
- cut.info(message, exception)
- verify(slf4jLogger).info(message, exception)
- }
describe("lazy logging message") {
it("should log when debug is ON") {
whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
- cut.info { message }
+ cut().info { message }
verify(slf4jLogger).isInfoEnabled
verify(slf4jLogger).info(message)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
- cut.info { message }
+ cut().info { message }
verify(slf4jLogger).isInfoEnabled
}
}
@@ -124,42 +111,32 @@ object LoggerTest : Spek({
it("should log when debug is ON") {
whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
- cut.info(exception) { message }
+ cut().withInfo { log(message, exception) }
verify(slf4jLogger).isInfoEnabled
verify(slf4jLogger).info(message, exception)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
- cut.info(exception) { message }
+ cut().withInfo { log(message, exception) }
verify(slf4jLogger).isInfoEnabled
}
}
}
describe("warning levels") {
- it("should log message") {
- cut.warn(message)
- verify(slf4jLogger).warn(message)
- }
-
- it("should log message with exception") {
- cut.warn(message, exception)
- verify(slf4jLogger).warn(message, exception)
- }
-
describe("lazy logging message") {
it("should log when debug is ON") {
whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
- cut.warn { message }
+ cut().warn { message }
verify(slf4jLogger).isWarnEnabled
verify(slf4jLogger).warn(message)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
- cut.warn { message }
+ cut().warn { message }
verify(slf4jLogger).isWarnEnabled
}
}
@@ -168,42 +145,33 @@ object LoggerTest : Spek({
it("should log when debug is ON") {
whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
- cut.warn(exception) { message }
+ cut().withWarn { log(message, exception) }
verify(slf4jLogger).isWarnEnabled
verify(slf4jLogger).warn(message, exception)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
- cut.warn(exception) { message }
+ cut().withWarn { log(message, exception) }
verify(slf4jLogger).isWarnEnabled
}
}
}
describe("error levels") {
- it("should log message") {
- cut.error(message)
- verify(slf4jLogger).error(message)
- }
-
- it("should log message with exception") {
- cut.error(message, exception)
- verify(slf4jLogger).error(message, exception)
- }
describe("lazy logging message") {
it("should log when debug is ON") {
whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
- cut.error { message }
+ cut().error { message }
verify(slf4jLogger).isErrorEnabled
verify(slf4jLogger).error(message)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
- cut.error { message }
+ cut().error { message }
verify(slf4jLogger).isErrorEnabled
}
}
@@ -212,14 +180,14 @@ object LoggerTest : Spek({
it("should log when debug is ON") {
whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
- cut.error(exception) { message }
+ cut().withError { log(message, exception) }
verify(slf4jLogger).isErrorEnabled
verify(slf4jLogger).error(message, exception)
}
it("should not log when debug is OFF") {
whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
- cut.error(exception) { message }
+ cut().withError { log(message, exception) }
verify(slf4jLogger).isErrorEnabled
}
}
diff --git a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/ReactiveLoggingTest.kt b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/ReactiveLoggingTest.kt
index 0f359df3..da956bec 100644
--- a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/ReactiveLoggingTest.kt
+++ b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/ReactiveLoggingTest.kt
@@ -42,7 +42,7 @@ class ReactiveLoggingTest : Spek({
val cut = Try.just(event)
it("should not filter stream event and log accepted message") {
- cut.filterFailedWithLog(logger, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
+ cut.filterFailedWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
.test()
.expectNext(event)
.verifyComplete()
@@ -53,7 +53,7 @@ class ReactiveLoggingTest : Spek({
val e = Exception()
val cut = Failure(e)
it("should filter stream event and log rejected message") {
- cut.filterFailedWithLog(logger, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
+ cut.filterFailedWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
.test()
.verifyComplete()
}
@@ -65,7 +65,7 @@ class ReactiveLoggingTest : Spek({
val cut = Option.just(event)
it("should not filter stream event and log accepted message") {
- cut.filterEmptyWithLog(logger, ACCEPTED_MESSAGE, FAILED_MESSAGE)
+ cut.filterEmptyWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_MESSAGE)
.test()
.expectNext(event)
.verifyComplete()
@@ -75,7 +75,7 @@ class ReactiveLoggingTest : Spek({
given("empty Option") {
val cut = Option.empty<Int>()
it("should filter stream event and log rejected message") {
- cut.filterEmptyWithLog(logger, ACCEPTED_MESSAGE, FAILED_MESSAGE)
+ cut.filterEmptyWithLog(logger,::emptyMap, ACCEPTED_MESSAGE, FAILED_MESSAGE)
.test()
.verifyComplete()
}
@@ -88,7 +88,7 @@ class ReactiveLoggingTest : Spek({
val cut = Flux.just(event)
it("should not filter stream event and log accepted message") {
- cut.filterFailedWithLog(logger, right())
+ cut.filterFailedWithLog(logger,::emptyMap, right())
.test()
.expectNext(event)
.verifyComplete()
@@ -99,7 +99,7 @@ class ReactiveLoggingTest : Spek({
val cut = Flux.just(event)
it("should filter stream event and log rejected message") {
- cut.filterFailedWithLog(logger, left())
+ cut.filterFailedWithLog(logger,::emptyMap, left())
.test()
.verifyComplete()
}