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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.utils.arrow
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
35 * @author Piotr Jaszczyk <piotr.jaszczyk></piotr.jaszczyk>@nokia.com>
38 internal class CoreKtTest : Spek({
39 describe("AtomicReference.getOption") {
40 given("empty atomic reference") {
41 val atomicReference = AtomicReference<String>()
44 val result = atomicReference.getOption()
46 it("should be None") {
47 assertThat(result).isEqualTo(None)
51 given("non-empty atomic reference") {
52 val initialValue = "reksio"
53 val atomicReference = AtomicReference(initialValue)
56 val result = atomicReference.getOption()
58 it("should be Some($initialValue)") {
59 assertThat(result).isEqualTo(Some(initialValue))
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)
71 it("should return Some($just)") {
72 assertThat(result).isEqualTo(Some(just))
77 given("one null element") {
78 val just: String? = null
79 on("calling factory") {
80 val result = Option.fromNullablesChain(just)
82 it("should return None") {
83 assertThat(result).isEqualTo(None)
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 })
95 it("should return Some($first)") {
96 assertThat(result).isEqualTo(Some(first))
99 it("should have not called second provider (should be lazy)") {
100 assertThat(secondAskedForValue).isFalse()
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 })
111 it("should return Some($first)") {
112 assertThat(result).isEqualTo(Some(first))
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 })
123 it("should return None") {
124 assertThat(result).isEqualTo(None)
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 })
135 it("should return Some($second)") {
136 assertThat(result).isEqualTo(Some(second))