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.logging
 
  22 import arrow.core.Either
 
  23 import arrow.core.Failure
 
  24 import arrow.core.Option
 
  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
 
  34 class ReactiveLoggingTest : Spek({
 
  36     describe("filtering with log message") {
 
  37         val logger = Logger("React")
 
  41             given("successful Try") {
 
  42                 val cut = Try.just(event)
 
  44                 it("should not filter stream event and log accepted message") {
 
  45                     cut.filterFailedWithLog(logger, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
 
  55                 it("should filter stream event and log rejected message") {
 
  56                     cut.filterFailedWithLog(logger, ACCEPTED_MESSAGE, FAILED_WITH_EXCEPTION_MESSAGE)
 
  64             given("Option with content") {
 
  65                 val cut = Option.just(event)
 
  67                 it("should not filter stream event and log accepted message") {
 
  68                     cut.filterEmptyWithLog(logger, ACCEPTED_MESSAGE, FAILED_MESSAGE)
 
  75             given("empty Option") {
 
  76                 val cut = Option.empty<Int>()
 
  77                 it("should filter stream event and log rejected message") {
 
  78                     cut.filterEmptyWithLog(logger, ACCEPTED_MESSAGE, FAILED_MESSAGE)
 
  87             given("successful Either (right)") {
 
  88                 val cut = Flux.just(event)
 
  90                 it("should not filter stream event and log accepted message") {
 
  91                     cut.filterFailedWithLog(logger, right())
 
  98             given("failed Either (left)") {
 
  99                 val cut = Flux.just(event)
 
 101                 it("should filter stream event and log rejected message") {
 
 102                     cut.filterFailedWithLog(logger, left())
 
 112 val ACCEPTED_MESSAGE: (Int) -> String = { "SUCCESS" }
 
 113 val FAILED_MESSAGE: () -> String = { "FAILED" }
 
 114 val FAILED_WITH_EXCEPTION_MESSAGE: (Throwable) -> String = { "FAILED" }
 
 116 private fun right(): (Int) -> Either<() -> String, () -> String> =
 
 117         { Either.cond(true, { { "SUCCESS" } }, { fail() }) }
 
 119 private fun left(): (Int) -> Either<() -> String, () -> String> =
 
 120         { Either.cond(false, { fail() }, { FAILED_MESSAGE }) }