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 lateinit var cut: Logger
41 cut = Logger(slf4jLogger)
45 verifyNoMoreInteractions(slf4jLogger)
48 describe("Thin Kotlin logging facade for Slf4j") {
49 val message = "sample message"
50 val exception = Exception("fail")
52 describe("debug levels") {
53 it("should log message") {
55 verify(slf4jLogger).debug(message)
58 it("should log message with exception") {
59 cut.debug(message, exception)
60 verify(slf4jLogger).debug(message, exception)
63 describe("lazy logging message") {
65 it("should log when debug is ON") {
66 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
68 verify(slf4jLogger).isDebugEnabled
69 verify(slf4jLogger).debug(message)
72 it("should not log when debug is OFF") {
73 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
75 verify(slf4jLogger).isDebugEnabled
79 describe("lazy logging message with exception") {
81 it("should log when debug is ON") {
82 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
83 cut.debug(exception) { message }
84 verify(slf4jLogger).isDebugEnabled
85 verify(slf4jLogger).debug(message, exception)
88 it("should not log when debug is OFF") {
89 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
90 cut.debug(exception) { message }
91 verify(slf4jLogger).isDebugEnabled
96 describe("info levels") {
97 it("should log message") {
99 verify(slf4jLogger).info(message)
102 it("should log message with exception") {
103 cut.info(message, exception)
104 verify(slf4jLogger).info(message, exception)
107 describe("lazy logging message") {
109 it("should log when debug is ON") {
110 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
112 verify(slf4jLogger).isInfoEnabled
113 verify(slf4jLogger).info(message)
116 it("should not log when debug is OFF") {
117 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
119 verify(slf4jLogger).isInfoEnabled
123 describe("lazy logging message with exception") {
125 it("should log when debug is ON") {
126 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
127 cut.info(exception) { message }
128 verify(slf4jLogger).isInfoEnabled
129 verify(slf4jLogger).info(message, exception)
132 it("should not log when debug is OFF") {
133 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
134 cut.info(exception) { message }
135 verify(slf4jLogger).isInfoEnabled
140 describe("warning levels") {
141 it("should log message") {
143 verify(slf4jLogger).warn(message)
146 it("should log message with exception") {
147 cut.warn(message, exception)
148 verify(slf4jLogger).warn(message, exception)
151 describe("lazy logging message") {
153 it("should log when debug is ON") {
154 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
156 verify(slf4jLogger).isWarnEnabled
157 verify(slf4jLogger).warn(message)
160 it("should not log when debug is OFF") {
161 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
163 verify(slf4jLogger).isWarnEnabled
167 describe("lazy logging message with exception") {
169 it("should log when debug is ON") {
170 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
171 cut.warn(exception) { message }
172 verify(slf4jLogger).isWarnEnabled
173 verify(slf4jLogger).warn(message, exception)
176 it("should not log when debug is OFF") {
177 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
178 cut.warn(exception) { message }
179 verify(slf4jLogger).isWarnEnabled
184 describe("error levels") {
185 it("should log message") {
187 verify(slf4jLogger).error(message)
190 it("should log message with exception") {
191 cut.error(message, exception)
192 verify(slf4jLogger).error(message, exception)
195 describe("lazy logging message") {
197 it("should log when debug is ON") {
198 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
199 cut.error { message }
200 verify(slf4jLogger).isErrorEnabled
201 verify(slf4jLogger).error(message)
204 it("should not log when debug is OFF") {
205 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
206 cut.error { message }
207 verify(slf4jLogger).isErrorEnabled
211 describe("lazy logging message with exception") {
213 it("should log when debug is ON") {
214 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
215 cut.error(exception) { message }
216 verify(slf4jLogger).isErrorEnabled
217 verify(slf4jLogger).error(message, exception)
220 it("should not log when debug is OFF") {
221 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
222 cut.error(exception) { message }
223 verify(slf4jLogger).isErrorEnabled