summaryrefslogtreecommitdiffstats
path: root/hv-collector-utils
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-24 12:51:14 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-31 12:16:08 +0200
commitd00acee05c05c7e3146abf7d13b78953f9a0d3f9 (patch)
tree521e0ef361bf176ce96ad6c718f198f464412b1c /hv-collector-utils
parent5bdae83e8b93cebbb84f56d5830beb726a164d76 (diff)
Improve DCAE APP Simulator coverage
Also there was a need to refactor the code, because application logic was placed inside Ratpack handlers. Change-Id: Iba3d4d039a98ba88e0dba580c1b7726b53440538 Issue-ID: DCAEGEN2-732 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'hv-collector-utils')
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt5
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt16
-rw-r--r--hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/CoreKtTest.kt63
3 files changed, 83 insertions, 1 deletions
diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
index 39964c1e..844d18d8 100644
--- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
@@ -20,12 +20,15 @@
package org.onap.dcae.collectors.veshv.utils.arrow
import arrow.core.Either
+import arrow.core.Option
import arrow.core.identity
+import java.util.concurrent.atomic.AtomicReference
/**
* @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
* @since July 2018
*/
-
fun <A> Either<A, A>.flatten() = fold(::identity, ::identity)
+
+fun <A> AtomicReference<A>.getOption() = Option.fromNullable(get())
diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt
index e37b0d7d..cef537e8 100644
--- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt
@@ -20,7 +20,11 @@
package org.onap.dcae.collectors.veshv.utils.arrow
import arrow.core.Either
+import arrow.core.Left
+import arrow.core.Right
import arrow.effects.IO
+import reactor.core.publisher.Flux
+import reactor.core.publisher.Mono
import kotlin.system.exitProcess
/**
@@ -47,3 +51,15 @@ fun Either<IO<Unit>, IO<Unit>>.unsafeRunEitherSync(onError: (Throwable) -> ExitC
fun IO<Any>.void() = map { Unit }
+
+fun <T> Mono<T>.asIo() = IO.async<T> { proc ->
+ subscribe({ proc(Right(it)) }, { proc(Left(it)) })
+}
+
+fun <T> Flux<IO<T>>.evaluateIo(): Flux<T> =
+ flatMap { io ->
+ io.attempt().unsafeRunSync().fold(
+ { Flux.error<T>(it) },
+ { Flux.just<T>(it) }
+ )
+ }
diff --git a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/CoreKtTest.kt b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/CoreKtTest.kt
new file mode 100644
index 00000000..585851be
--- /dev/null
+++ b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/CoreKtTest.kt
@@ -0,0 +1,63 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcae.collectors.veshv.utils.arrow
+
+import arrow.core.None
+import arrow.core.Some
+import org.assertj.core.api.Assertions.assertThat
+import org.jetbrains.spek.api.Spek
+import org.jetbrains.spek.api.dsl.describe
+import org.jetbrains.spek.api.dsl.given
+import org.jetbrains.spek.api.dsl.it
+import org.jetbrains.spek.api.dsl.on
+import java.util.concurrent.atomic.AtomicReference
+
+
+/**
+ * @author Piotr Jaszczyk <piotr.jaszczyk></piotr.jaszczyk>@nokia.com>
+ * @since August 2018
+ */
+internal class CoreKtTest: Spek({
+ describe("AtomicReference.getOption") {
+ given("empty atomic reference") {
+ val atomicReference = AtomicReference<String>()
+
+ on("getOption") {
+ val result = atomicReference.getOption()
+
+ it("should be None") {
+ assertThat(result).isEqualTo(None)
+ }
+ }
+ }
+ given("non-empty atomic reference") {
+ val initialValue = "reksio"
+ val atomicReference = AtomicReference(initialValue)
+
+ on("getOption") {
+ val result = atomicReference.getOption()
+
+ it("should be Some($initialValue)") {
+ assertThat(result).isEqualTo(Some(initialValue))
+ }
+ }
+ }
+ }
+}) \ No newline at end of file