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.*
23 import org.jetbrains.spek.api.Spek
24 import org.jetbrains.spek.api.dsl.describe
25 import org.jetbrains.spek.api.dsl.it
28 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
31 object LoggerTest : Spek({
33 lateinit var slf4jLogger: org.slf4j.Logger
34 fun cut() = Logger(slf4jLogger)
41 verifyNoMoreInteractions(slf4jLogger)
44 describe("Thin Kotlin logging facade for Slf4j") {
45 val message = "sample message"
46 val exception = Exception("fail")
48 describe("before first use") {
49 it("should not initialize AtLeverLoggers") {
51 verify(slf4jLogger, never()).isTraceEnabled
52 verify(slf4jLogger, never()).isDebugEnabled
53 verify(slf4jLogger, never()).isInfoEnabled
54 verify(slf4jLogger, never()).isWarnEnabled
55 verify(slf4jLogger, never()).isErrorEnabled
59 describe("debug levels") {
61 describe("lazy logging message") {
63 it("should log when debug is ON") {
64 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
65 cut().debug { message }
66 verify(slf4jLogger).isDebugEnabled
67 verify(slf4jLogger).debug(message)
70 it("should not log when debug is OFF") {
71 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
72 cut().debug { message }
73 verify(slf4jLogger).isDebugEnabled
77 describe("lazy logging message with exception") {
79 it("should log when debug is ON") {
80 whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
81 cut().withDebug { log(message, exception) }
82 verify(slf4jLogger).isDebugEnabled
83 verify(slf4jLogger).debug(message, exception)
86 it("should not log when debug is OFF") {
87 whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
88 cut().withDebug { log(message, exception) }
89 verify(slf4jLogger).isDebugEnabled
94 describe("info levels") {
96 describe("lazy logging message") {
98 it("should log when debug is ON") {
99 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
100 cut().info { message }
101 verify(slf4jLogger).isInfoEnabled
102 verify(slf4jLogger).info(message)
105 it("should not log when debug is OFF") {
106 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
107 cut().info { message }
108 verify(slf4jLogger).isInfoEnabled
112 describe("lazy logging message with exception") {
114 it("should log when debug is ON") {
115 whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
116 cut().withInfo { log(message, exception) }
117 verify(slf4jLogger).isInfoEnabled
118 verify(slf4jLogger).info(message, exception)
121 it("should not log when debug is OFF") {
122 whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
123 cut().withInfo { log(message, exception) }
124 verify(slf4jLogger).isInfoEnabled
129 describe("warning levels") {
130 describe("lazy logging message") {
132 it("should log when debug is ON") {
133 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
134 cut().warn { message }
135 verify(slf4jLogger).isWarnEnabled
136 verify(slf4jLogger).warn(message)
139 it("should not log when debug is OFF") {
140 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
141 cut().warn { message }
142 verify(slf4jLogger).isWarnEnabled
146 describe("lazy logging message with exception") {
148 it("should log when debug is ON") {
149 whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
150 cut().withWarn { log(message, exception) }
151 verify(slf4jLogger).isWarnEnabled
152 verify(slf4jLogger).warn(message, exception)
155 it("should not log when debug is OFF") {
156 whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
157 cut().withWarn { log(message, exception) }
158 verify(slf4jLogger).isWarnEnabled
163 describe("error levels") {
165 describe("lazy logging message") {
167 it("should log when debug is ON") {
168 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
169 cut().error { message }
170 verify(slf4jLogger).isErrorEnabled
171 verify(slf4jLogger).error(message)
174 it("should not log when debug is OFF") {
175 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
176 cut().error { message }
177 verify(slf4jLogger).isErrorEnabled
181 describe("lazy logging message with exception") {
183 it("should log when debug is ON") {
184 whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
185 cut().withError { log(message, exception) }
186 verify(slf4jLogger).isErrorEnabled
187 verify(slf4jLogger).error(message, exception)
190 it("should not log when debug is OFF") {
191 whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
192 cut().withError { log(message, exception) }
193 verify(slf4jLogger).isErrorEnabled