Bump checkstyle version
[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.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
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since May 2018
33  */
34 object LoggerTest : Spek({
35
36     lateinit var slf4jLogger: org.slf4j.Logger
37     lateinit var cut: Logger
38
39     beforeEachTest {
40         slf4jLogger = mock()
41         cut = Logger(slf4jLogger)
42     }
43
44     afterEachTest {
45         verifyNoMoreInteractions(slf4jLogger)
46     }
47
48     describe("Thin Kotlin logging facade for Slf4j") {
49         val message = "sample message"
50         val exception = Exception("fail")
51
52         describe("debug levels") {
53             it("should log message") {
54                 cut.debug(message)
55                 verify(slf4jLogger).debug(message)
56             }
57
58             it("should log message with exception") {
59                 cut.debug(message, exception)
60                 verify(slf4jLogger).debug(message, exception)
61             }
62
63             describe("lazy logging message") {
64
65                 it("should log when debug is ON") {
66                     whenever(slf4jLogger.isDebugEnabled).thenReturn(true)
67                     cut.debug { message }
68                     verify(slf4jLogger).isDebugEnabled
69                     verify(slf4jLogger).debug(message)
70                 }
71
72                 it("should not log when debug is OFF") {
73                     whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
74                     cut.debug { message }
75                     verify(slf4jLogger).isDebugEnabled
76                 }
77             }
78
79             describe("lazy logging message with exception") {
80
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)
86                 }
87
88                 it("should not log when debug is OFF") {
89                     whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
90                     cut.debug(exception) { message }
91                     verify(slf4jLogger).isDebugEnabled
92                 }
93             }
94         }
95
96         describe("info levels") {
97             it("should log message") {
98                 cut.info(message)
99                 verify(slf4jLogger).info(message)
100             }
101
102             it("should log message with exception") {
103                 cut.info(message, exception)
104                 verify(slf4jLogger).info(message, exception)
105             }
106
107             describe("lazy logging message") {
108
109                 it("should log when debug is ON") {
110                     whenever(slf4jLogger.isInfoEnabled).thenReturn(true)
111                     cut.info { message }
112                     verify(slf4jLogger).isInfoEnabled
113                     verify(slf4jLogger).info(message)
114                 }
115
116                 it("should not log when debug is OFF") {
117                     whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
118                     cut.info { message }
119                     verify(slf4jLogger).isInfoEnabled
120                 }
121             }
122
123             describe("lazy logging message with exception") {
124
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)
130                 }
131
132                 it("should not log when debug is OFF") {
133                     whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
134                     cut.info(exception) { message }
135                     verify(slf4jLogger).isInfoEnabled
136                 }
137             }
138         }
139
140         describe("warning levels") {
141             it("should log message") {
142                 cut.warn(message)
143                 verify(slf4jLogger).warn(message)
144             }
145
146             it("should log message with exception") {
147                 cut.warn(message, exception)
148                 verify(slf4jLogger).warn(message, exception)
149             }
150
151             describe("lazy logging message") {
152
153                 it("should log when debug is ON") {
154                     whenever(slf4jLogger.isWarnEnabled).thenReturn(true)
155                     cut.warn { message }
156                     verify(slf4jLogger).isWarnEnabled
157                     verify(slf4jLogger).warn(message)
158                 }
159
160                 it("should not log when debug is OFF") {
161                     whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
162                     cut.warn { message }
163                     verify(slf4jLogger).isWarnEnabled
164                 }
165             }
166
167             describe("lazy logging message with exception") {
168
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)
174                 }
175
176                 it("should not log when debug is OFF") {
177                     whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
178                     cut.warn(exception) { message }
179                     verify(slf4jLogger).isWarnEnabled
180                 }
181             }
182         }
183
184         describe("error levels") {
185             it("should log message") {
186                 cut.error(message)
187                 verify(slf4jLogger).error(message)
188             }
189
190             it("should log message with exception") {
191                 cut.error(message, exception)
192                 verify(slf4jLogger).error(message, exception)
193             }
194
195             describe("lazy logging message") {
196
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)
202                 }
203
204                 it("should not log when debug is OFF") {
205                     whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
206                     cut.error { message }
207                     verify(slf4jLogger).isErrorEnabled
208                 }
209             }
210
211             describe("lazy logging message with exception") {
212
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)
218                 }
219
220                 it("should not log when debug is OFF") {
221                     whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
222                     cut.error(exception) { message }
223                     verify(slf4jLogger).isErrorEnabled
224                 }
225             }
226         }
227
228
229     }
230 })