Add log diagnostic context
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / test / kotlin / org / onap / dcae / collectors / veshv / utils / logging / ReactiveLoggingTest.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 arrow.core.Either
23 import arrow.core.Failure
24 import arrow.core.Option
25 import arrow.core.Try
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.given
29 import org.jetbrains.spek.api.dsl.it
30 import reactor.core.publisher.Flux
31 import reactor.test.test
32 import kotlin.test.fail
33
34 class ReactiveLoggingTest : Spek({
35
36     describe("filtering with log message") {
37         val logger = Logger("React")
38         val event = 5
39
40         describe("Try") {
41             given("successful Try") {
42                 val cut = Try.just(event)
43
44                 it("should not filter stream event and log accepted message") {
45                     cut.filterFailedWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
46                             .test()
47                             .expectNext(event)
48                             .verifyComplete()
49                 }
50             }
51
52             given("failed Try") {
53                 val e = Exception()
54                 val cut = Failure(e)
55                 it("should filter stream event and log rejected message") {
56                     cut.filterFailedWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
57                             .test()
58                             .verifyComplete()
59                 }
60             }
61         }
62
63         describe("Option") {
64             given("Option with content") {
65                 val cut = Option.just(event)
66
67                 it("should not filter stream event and log accepted message") {
68                     cut.filterEmptyWithLog(logger, ::emptyMap, ACCEPTED_MESSAGE, FAILED_MESSAGE)
69                             .test()
70                             .expectNext(event)
71                             .verifyComplete()
72                 }
73             }
74
75             given("empty Option") {
76                 val cut = Option.empty<Int>()
77                 it("should filter stream event and log rejected message") {
78                     cut.filterEmptyWithLog(logger,::emptyMap, ACCEPTED_MESSAGE, FAILED_MESSAGE)
79                             .test()
80                             .verifyComplete()
81                 }
82             }
83         }
84
85
86         describe("Either") {
87             given("successful Either (right)") {
88                 val cut = Flux.just(event)
89
90                 it("should not filter stream event and log accepted message") {
91                     cut.filterFailedWithLog(logger,::emptyMap, right())
92                             .test()
93                             .expectNext(event)
94                             .verifyComplete()
95                 }
96             }
97
98             given("failed Either (left)") {
99                 val cut = Flux.just(event)
100
101                 it("should filter stream event and log rejected message") {
102                     cut.filterFailedWithLog(logger,::emptyMap, left())
103                             .test()
104                             .verifyComplete()
105                 }
106             }
107         }
108     }
109 })
110
111
112 val ACCEPTED_MESSAGE: (Int) -> String = { "SUCCESS" }
113 val FAILED_MESSAGE: () -> String = { "FAILED" }
114 val FAILED_WITH_EXCEPTION_MESSAGE: (Throwable) -> String = { "FAILED" }
115
116 private fun right(): (Int) -> Either<() -> String, () -> String> =
117         { Either.cond(true, { { "SUCCESS" } }, { fail() }) }
118
119 private fun left(): (Int) -> Either<() -> String, () -> String> =
120         { Either.cond(false, { fail() }, { FAILED_MESSAGE }) }