2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.collectors.veshv.utils.logging
22 import com.nhaarman.mockitokotlin2.mock
23 import com.nhaarman.mockitokotlin2.verify
24 import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
25 import com.nhaarman.mockitokotlin2.whenever
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.it
31 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
34 object LoggerTest : Spek({
36 lateinit var slf4jLogger: org.slf4j.Logger
37 fun cut() = Logger(slf4jLogger).also {
38 verify(slf4jLogger).isTraceEnabled
39 verify(slf4jLogger).isDebugEnabled
40 verify(slf4jLogger).isInfoEnabled
41 verify(slf4jLogger).isWarnEnabled
42 verify(slf4jLogger).isErrorEnabled
50 verifyNoMoreInteractions(slf4jLogger)
53 describe("Thin Kotlin logging facade for Slf4j") {
54 val message = "sample message"
55 val exception = Exception("fail")
57 describe("debug levels") {
59 describe("lazy logging message") {
61 it("should log when debug is ON") {
62 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
63 cut().debug { message }
64 verify(slf4jLogger).isDebugEnabled
65 verify(slf4jLogger).debug(message)
68 it("should not log when debug is OFF") {
69 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
70 cut().debug { message }
71 verify(slf4jLogger).isDebugEnabled
75 describe("lazy logging message with exception") {
77 it("should log when debug is ON") {
78 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
79 cut().withDebug { log(message, exception) }
80 verify(slf4jLogger).isDebugEnabled
81 verify(slf4jLogger).debug(message, exception)
84 it("should not log when debug is OFF") {
85 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
86 cut().withDebug { log(message, exception) }
87 verify(slf4jLogger).isDebugEnabled
92 describe("info levels") {
94 describe("lazy logging message") {
96 it("should log when debug is ON") {
97 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
98 cut().info { message }
99 verify(slf4jLogger).isInfoEnabled
100 verify(slf4jLogger).info(message)
103 it("should not log when debug is OFF") {
104 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
105 cut().info { message }
106 verify(slf4jLogger).isInfoEnabled
110 describe("lazy logging message with exception") {
112 it("should log when debug is ON") {
113 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
114 cut().withInfo { log(message, exception) }
115 verify(slf4jLogger).isInfoEnabled
116 verify(slf4jLogger).info(message, exception)
119 it("should not log when debug is OFF") {
120 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
121 cut().withInfo { log(message, exception) }
122 verify(slf4jLogger).isInfoEnabled
127 describe("warning levels") {
128 describe("lazy logging message") {
130 it("should log when debug is ON") {
131 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
132 cut().warn { message }
133 verify(slf4jLogger).isWarnEnabled
134 verify(slf4jLogger).warn(message)
137 it("should not log when debug is OFF") {
138 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
139 cut().warn { message }
140 verify(slf4jLogger).isWarnEnabled
144 describe("lazy logging message with exception") {
146 it("should log when debug is ON") {
147 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
148 cut().withWarn { log(message, exception) }
149 verify(slf4jLogger).isWarnEnabled
150 verify(slf4jLogger).warn(message, exception)
153 it("should not log when debug is OFF") {
154 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
155 cut().withWarn { log(message, exception) }
156 verify(slf4jLogger).isWarnEnabled
161 describe("error levels") {
163 describe("lazy logging message") {
165 it("should log when debug is ON") {
166 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
167 cut().error { message }
168 verify(slf4jLogger).isErrorEnabled
169 verify(slf4jLogger).error(message)
172 it("should not log when debug is OFF") {
173 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
174 cut().error { message }
175 verify(slf4jLogger).isErrorEnabled
179 describe("lazy logging message with exception") {
181 it("should log when debug is ON") {
182 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
183 cut().withError { log(message, exception) }
184 verify(slf4jLogger).isErrorEnabled
185 verify(slf4jLogger).error(message, exception)
188 it("should not log when debug is OFF") {
189 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
190 cut().withError { log(message, exception) }
191 verify(slf4jLogger).isErrorEnabled