Change AtLevelLoggers to be initialized by lazy
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / test / kotlin / org / onap / dcae / collectors / veshv / utils / logging / LoggerTest.kt
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.utils.logging
21
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
26
27 /**
28  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
29  * @since May 2018
30  */
31 object LoggerTest : Spek({
32
33     lateinit var slf4jLogger: org.slf4j.Logger
34     fun cut() = Logger(slf4jLogger)
35
36     beforeEachTest {
37         slf4jLogger = mock()
38     }
39
40     afterEachTest {
41         verifyNoMoreInteractions(slf4jLogger)
42     }
43
44     describe("Thin Kotlin logging facade for Slf4j") {
45         val message = "sample message"
46         val exception = Exception("fail")
47
48         describe("before first use") {
49             it("should not initialize AtLeverLoggers") {
50                 cut()
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
56             }
57         }
58
59         describe("debug levels") {
60
61             describe("lazy logging message") {
62
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)
68                 }
69
70                 it("should not log when debug is OFF") {
71                     whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
72                     cut().debug { message }
73                     verify(slf4jLogger).isDebugEnabled
74                 }
75             }
76
77             describe("lazy logging message with exception") {
78
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)
84                 }
85
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
90                 }
91             }
92         }
93
94         describe("info levels") {
95
96             describe("lazy logging message") {
97
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)
103                 }
104
105                 it("should not log when debug is OFF") {
106                     whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
107                     cut().info { message }
108                     verify(slf4jLogger).isInfoEnabled
109                 }
110             }
111
112             describe("lazy logging message with exception") {
113
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)
119                 }
120
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
125                 }
126             }
127         }
128
129         describe("warning levels") {
130             describe("lazy logging message") {
131
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)
137                 }
138
139                 it("should not log when debug is OFF") {
140                     whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
141                     cut().warn { message }
142                     verify(slf4jLogger).isWarnEnabled
143                 }
144             }
145
146             describe("lazy logging message with exception") {
147
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)
153                 }
154
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
159                 }
160             }
161         }
162
163         describe("error levels") {
164
165             describe("lazy logging message") {
166
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)
172                 }
173
174                 it("should not log when debug is OFF") {
175                     whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
176                     cut().error { message }
177                     verify(slf4jLogger).isErrorEnabled
178                 }
179             }
180
181             describe("lazy logging message with exception") {
182
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)
188                 }
189
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
194                 }
195             }
196         }
197
198
199     }
200 })