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.main
22 import arrow.core.Left
23 import arrow.core.None
24 import arrow.core.Right
25 import arrow.effects.IO
26 import com.nhaarman.mockitokotlin2.any
27 import com.nhaarman.mockitokotlin2.mock
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.domain.WireFrameMessage
33 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
34 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters.VesHvClient
35 import org.onap.dcae.collectors.veshv.tests.utils.Assertions.assertThat
36 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator
37 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
38 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
39 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
40 import reactor.core.publisher.Flux
41 import java.io.ByteArrayInputStream
44 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
45 * @since September 2018
47 internal class XnfSimulatorTest : Spek({
48 lateinit var cut: XnfSimulator
49 lateinit var vesClient: VesHvClient
50 lateinit var messageParametersParser: MessageParametersParser
51 lateinit var messageGenerator: MessageGenerator
55 messageParametersParser = mock()
56 messageGenerator = mock()
57 cut = XnfSimulator(vesClient, messageGenerator, messageParametersParser)
60 describe("startSimulation") {
61 it("should fail when empty input stream") {
63 val emptyInputStream = ByteArrayInputStream(byteArrayOf())
66 val result = cut.startSimulation(emptyInputStream)
69 assertThat(result).isLeft()
72 it("should fail when invalid JSON") {
74 val invalidJson = "invalid json".byteInputStream()
77 val result = cut.startSimulation(invalidJson)
80 assertThat(result).isLeft()
83 it("should fail when JSON syntax is valid but content is invalid") {
85 val json = "[1,2,3]".byteInputStream()
86 val cause = ParsingError("epic fail", None)
87 whenever(messageParametersParser.parse(any())).thenReturn(
91 val result = cut.startSimulation(json)
94 assertThat(result).left().isEqualTo(cause)
97 it("should return generated messages") {
99 val json = "[true]".byteInputStream()
100 val messageParams = listOf<MessageParameters>()
101 val generatedMessages = Flux.empty<WireFrameMessage>()
102 val sendingIo = IO {}
103 whenever(messageParametersParser.parse(any())).thenReturn(Right(messageParams))
104 whenever(messageGenerator.createMessageFlux(messageParams)).thenReturn(generatedMessages)
105 whenever(vesClient.sendIo(generatedMessages)).thenReturn(sendingIo)
108 val result = cut.startSimulation(json)
111 assertThat(result).right().isSameAs(sendingIo)