2  * ============LICENSE_START=======================================================
 
   3  * dcaegen2-collectors-veshv
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018-2019 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.main
 
  22 import arrow.core.Left
 
  23 import arrow.core.None
 
  24 import arrow.core.Right
 
  25 import com.nhaarman.mockitokotlin2.any
 
  26 import com.nhaarman.mockitokotlin2.eq
 
  27 import com.nhaarman.mockitokotlin2.mock
 
  28 import com.nhaarman.mockitokotlin2.verify
 
  29 import com.nhaarman.mockitokotlin2.whenever
 
  30 import org.jetbrains.spek.api.Spek
 
  31 import org.jetbrains.spek.api.dsl.describe
 
  32 import org.jetbrains.spek.api.dsl.it
 
  33 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
 
  34 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters.HvVesClient
 
  35 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.factory.ClientFactory
 
  36 import org.onap.dcae.collectors.veshv.tests.utils.Assertions.assertThat
 
  37 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
 
  38 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
 
  39 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventParameters
 
  40 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventType
 
  41 import org.onap.dcae.collectors.veshv.ves.message.generator.factory.MessageGeneratorFactory
 
  42 import org.onap.dcae.collectors.veshv.ves.message.generator.generators.VesEventGenerator
 
  43 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.PayloadType
 
  44 import org.onap.ves.VesEventOuterClass
 
  45 import org.onap.ves.VesEventOuterClass.CommonEventHeader
 
  46 import reactor.core.publisher.Flux
 
  47 import reactor.core.publisher.Mono
 
  48 import java.io.ByteArrayInputStream
 
  51  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 
  52  * @since September 2018
 
  54 internal class XnfSimulatorTest : Spek({
 
  55     lateinit var cut: XnfSimulator
 
  56     lateinit var clientFactory: ClientFactory
 
  57     lateinit var messageParametersParser: MessageParametersParser
 
  58     lateinit var generatorFactory: MessageGeneratorFactory
 
  61         clientFactory = mock()
 
  62         messageParametersParser = mock()
 
  63         generatorFactory = mock()
 
  64         cut = XnfSimulator(clientFactory, generatorFactory, messageParametersParser)
 
  67     describe("startSimulation") {
 
  68         it("should fail when empty input stream") {
 
  70             val emptyInputStream = ByteArrayInputStream(byteArrayOf())
 
  73             val result = cut.startSimulation(emptyInputStream)
 
  76             assertThat(result).isLeft()
 
  79         it("should fail when invalid JSON") {
 
  81             val invalidJson = "invalid json".byteInputStream()
 
  84             val result = cut.startSimulation(invalidJson)
 
  87             assertThat(result).isLeft()
 
  90         it("should fail when JSON syntax is valid but content is invalid") {
 
  92             val json = "[1,2,3]".byteInputStream()
 
  93             val cause = ParsingError("epic fail", None)
 
  94             whenever(messageParametersParser.parse(any())).thenReturn(
 
  98             val result = cut.startSimulation(json)
 
 101             assertThat(result).left().isEqualTo(cause)
 
 104         it("should return generated ves messages") {
 
 106             val vesEventGenerator: VesEventGenerator = mock()
 
 107             val vesClient: HvVesClient = mock()
 
 109             val json = "[true]".byteInputStream()
 
 111             val vesEventParams = VesEventParameters(
 
 112                     CommonEventHeader.getDefaultInstance(),
 
 116             val messageParams = listOf(vesEventParams)
 
 118             val generatedMessages = Flux.empty<VesEventOuterClass.VesEvent>()
 
 121             whenever(messageParametersParser.parse(any())).thenReturn(Right(messageParams))
 
 122             whenever(generatorFactory.createVesEventGenerator()).thenReturn(vesEventGenerator)
 
 123             whenever(vesEventGenerator.createMessageFlux(vesEventParams)).thenReturn(generatedMessages)
 
 124             whenever(clientFactory.create()).thenReturn(vesClient)
 
 126             whenever(vesClient.sendRawPayload(any(), eq(PayloadType.PROTOBUF))).thenReturn(Mono.just(Unit))
 
 129             cut.startSimulation(json).map { it.block() }
 
 132             verify(vesClient).sendRawPayload(any(), eq(PayloadType.PROTOBUF))