Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / hv-collector-utils / src / test / kotlin / org / onap / dcae / collectors / veshv / utils / arrow / CoreKtTest.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.arrow
21
22 import arrow.core.None
23 import arrow.core.Option
24 import arrow.core.Some
25 import org.assertj.core.api.Assertions.assertThat
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 org.jetbrains.spek.api.dsl.on
31 import java.util.concurrent.atomic.AtomicReference
32
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk></piotr.jaszczyk>@nokia.com>
36  * @since August 2018
37  */
38 internal class CoreKtTest : Spek({
39     describe("AtomicReference.getOption") {
40         given("empty atomic reference") {
41             val atomicReference = AtomicReference<String>()
42
43             on("getOption") {
44                 val result = atomicReference.getOption()
45
46                 it("should be None") {
47                     assertThat(result).isEqualTo(None)
48                 }
49             }
50         }
51         given("non-empty atomic reference") {
52             val initialValue = "reksio"
53             val atomicReference = AtomicReference(initialValue)
54
55             on("getOption") {
56                 val result = atomicReference.getOption()
57
58                 it("should be Some($initialValue)") {
59                     assertThat(result).isEqualTo(Some(initialValue))
60                 }
61             }
62         }
63     }
64
65     describe("Option.fromNullablesChain") {
66         given("one non-null element") {
67             val just = "some text"
68             on("calling factory") {
69                 val result = Option.fromNullablesChain(just)
70
71                 it("should return Some($just)") {
72                     assertThat(result).isEqualTo(Some(just))
73                 }
74             }
75         }
76
77         given("one null element") {
78             val just: String? = null
79             on("calling factory") {
80                 val result = Option.fromNullablesChain(just)
81
82                 it("should return None") {
83                     assertThat(result).isEqualTo(None)
84                 }
85             }
86         }
87
88         given("first non-null element") {
89             val first = "some text"
90             val second: String? = null
91             var secondAskedForValue = false
92             on("calling factory") {
93                 val result = Option.fromNullablesChain(first, { secondAskedForValue = true; second })
94
95                 it("should return Some($first)") {
96                     assertThat(result).isEqualTo(Some(first))
97                 }
98
99                 it("should have not called second provider (should be lazy)") {
100                     assertThat(secondAskedForValue).isFalse()
101                 }
102             }
103         }
104
105         given("two non-null elements") {
106             val first = "some text"
107             val second = "another text"
108             on("calling factory") {
109                 val result = Option.fromNullablesChain(first, { second })
110
111                 it("should return Some($first)") {
112                     assertThat(result).isEqualTo(Some(first))
113                 }
114             }
115         }
116
117         given("two null elements") {
118             val first: String? = null
119             val second: String? = null
120             on("calling factory") {
121                 val result = Option.fromNullablesChain(first, { second })
122
123                 it("should return None") {
124                     assertThat(result).isEqualTo(None)
125                 }
126             }
127         }
128
129         given("second non-null element") {
130             val first: String? = null
131             val second = "another text"
132             on("calling factory") {
133                 val result = Option.fromNullablesChain(first, { second })
134
135                 it("should return Some($second)") {
136                     assertThat(result).isEqualTo(Some(second))
137                 }
138             }
139         }
140     }
141 })