From d00acee05c05c7e3146abf7d13b78953f9a0d3f9 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Fri, 24 Aug 2018 12:51:14 +0200 Subject: 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 --- .../onap/dcae/collectors/veshv/utils/arrow/core.kt | 5 +- .../dcae/collectors/veshv/utils/arrow/effects.kt | 16 ++++++ .../collectors/veshv/utils/arrow/CoreKtTest.kt | 63 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/CoreKtTest.kt (limited to 'hv-collector-utils') 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 * @since July 2018 */ - fun Either.flatten() = fold(::identity, ::identity) + +fun AtomicReference.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>.unsafeRunEitherSync(onError: (Throwable) -> ExitC fun IO.void() = map { Unit } + +fun Mono.asIo() = IO.async { proc -> + subscribe({ proc(Right(it)) }, { proc(Left(it)) }) +} + +fun Flux>.evaluateIo(): Flux = + flatMap { io -> + io.attempt().unsafeRunSync().fold( + { Flux.error(it) }, + { Flux.just(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 @nokia.com> + * @since August 2018 + */ +internal class CoreKtTest: Spek({ + describe("AtomicReference.getOption") { + given("empty atomic reference") { + val atomicReference = AtomicReference() + + 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 -- cgit 1.2.3-korg