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.mock
27 import com.nhaarman.mockitokotlin2.verify
28 import com.nhaarman.mockitokotlin2.whenever
29 import org.jetbrains.spek.api.Spek
30 import org.jetbrains.spek.api.dsl.describe
31 import org.jetbrains.spek.api.dsl.it
32 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
33 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters.HvVesClient
34 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.factory.ClientFactory
35 import org.onap.dcae.collectors.veshv.tests.utils.Assertions.assertThat
36 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
37 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
38 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventParameters
39 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventType
40 import org.onap.dcae.collectors.veshv.ves.message.generator.factory.MessageGeneratorFactory
41 import org.onap.dcae.collectors.veshv.ves.message.generator.generators.VesEventGenerator
42 import org.onap.ves.VesEventOuterClass
43 import org.onap.ves.VesEventOuterClass.CommonEventHeader
44 import reactor.core.publisher.Flux
45 import reactor.core.publisher.Mono
46 import java.io.ByteArrayInputStream
49 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
50 * @since September 2018
52 internal class XnfSimulatorTest : Spek({
53 lateinit var cut: XnfSimulator
54 lateinit var clientFactory: ClientFactory
55 lateinit var messageParametersParser: MessageParametersParser
56 lateinit var generatorFactory: MessageGeneratorFactory
59 clientFactory = mock()
60 messageParametersParser = mock()
61 generatorFactory = mock()
62 cut = XnfSimulator(clientFactory, generatorFactory, messageParametersParser)
65 describe("startSimulation") {
66 it("should fail when empty input stream") {
68 val emptyInputStream = ByteArrayInputStream(byteArrayOf())
71 val result = cut.startSimulation(emptyInputStream)
74 assertThat(result).isLeft()
77 it("should fail when invalid JSON") {
79 val invalidJson = "invalid json".byteInputStream()
82 val result = cut.startSimulation(invalidJson)
85 assertThat(result).isLeft()
88 it("should fail when JSON syntax is valid but content is invalid") {
90 val json = "[1,2,3]".byteInputStream()
91 val cause = ParsingError("epic fail", None)
92 whenever(messageParametersParser.parse(any())).thenReturn(
96 val result = cut.startSimulation(json)
99 assertThat(result).left().isEqualTo(cause)
102 it("should return generated ves messages") {
104 val vesEventGenerator: VesEventGenerator = mock()
105 val vesClient: HvVesClient = mock()
107 val json = "[true]".byteInputStream()
109 val vesEventParams = VesEventParameters(
110 CommonEventHeader.getDefaultInstance(),
114 val messageParams = listOf(vesEventParams)
116 val generatedMessages = Flux.empty<VesEventOuterClass.VesEvent>()
119 whenever(messageParametersParser.parse(any())).thenReturn(Right(messageParams))
120 whenever(generatorFactory.createVesEventGenerator()).thenReturn(vesEventGenerator)
121 whenever(vesEventGenerator.createMessageFlux(vesEventParams)).thenReturn(generatedMessages)
122 whenever(clientFactory.create()).thenReturn(vesClient)
123 whenever(vesClient.sendVesEvents(generatedMessages)).thenReturn(Mono.just(Unit))
126 cut.startSimulation(json).map { it.unsafeRunSync() }
129 verify(vesClient).sendVesEvents(generatedMessages)