10fc8d8f484c94cb7c019525e35fa6df1c5d0124
[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     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
43     }
44
45     beforeEachTest {
46         slf4jLogger = mock()
47     }
48
49     afterEachTest {
50         verifyNoMoreInteractions(slf4jLogger)
51     }
52
53     describe("Thin Kotlin logging facade for Slf4j") {
54         val message = "sample message"
55         val exception = Exception("fail")
56
57         describe("debug levels") {
58
59             describe("lazy logging message") {
60
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)
66                 }
67
68                 it("should not log when debug is OFF") {
69                     whenever(slf4jLogger.isDebugEnabled).thenReturn(false)
70                     cut().debug { message }
71                     verify(slf4jLogger).isDebugEnabled
72                 }
73             }
74
75             describe("lazy logging message with exception") {
76
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)
82                 }
83
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
88                 }
89             }
90         }
91
92         describe("info levels") {
93
94             describe("lazy logging message") {
95
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)
101                 }
102
103                 it("should not log when debug is OFF") {
104                     whenever(slf4jLogger.isInfoEnabled).thenReturn(false)
105                     cut().info { message }
106                     verify(slf4jLogger).isInfoEnabled
107                 }
108             }
109
110             describe("lazy logging message with exception") {
111
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)
117                 }
118
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
123                 }
124             }
125         }
126
127         describe("warning levels") {
128             describe("lazy logging message") {
129
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)
135                 }
136
137                 it("should not log when debug is OFF") {
138                     whenever(slf4jLogger.isWarnEnabled).thenReturn(false)
139                     cut().warn { message }
140                     verify(slf4jLogger).isWarnEnabled
141                 }
142             }
143
144             describe("lazy logging message with exception") {
145
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)
151                 }
152
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
157                 }
158             }
159         }
160
161         describe("error levels") {
162
163             describe("lazy logging message") {
164
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)
170                 }
171
172                 it("should not log when debug is OFF") {
173                     whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
174                     cut().error { message }
175                     verify(slf4jLogger).isErrorEnabled
176                 }
177             }
178
179             describe("lazy logging message with exception") {
180
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)
186                 }
187
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
192                 }
193             }
194         }
195
196
197     }
198 })