Use sdk/hvves-producer in hvves/xnf-simulator
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-xnf-simulator / src / test / kotlin / org / onap / dcae / collectors / veshv / main / XnfSimulatorTest.kt
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.main
21
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
47
48 /**
49  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
50  * @since September 2018
51  */
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
57
58     beforeEachTest {
59         clientFactory = mock()
60         messageParametersParser = mock()
61         generatorFactory = mock()
62         cut = XnfSimulator(clientFactory, generatorFactory, messageParametersParser)
63     }
64
65     describe("startSimulation") {
66         it("should fail when empty input stream") {
67             // given
68             val emptyInputStream = ByteArrayInputStream(byteArrayOf())
69
70             // when
71             val result = cut.startSimulation(emptyInputStream)
72
73             // then
74             assertThat(result).isLeft()
75         }
76
77         it("should fail when invalid JSON") {
78             // given
79             val invalidJson = "invalid json".byteInputStream()
80
81             // when
82             val result = cut.startSimulation(invalidJson)
83
84             // then
85             assertThat(result).isLeft()
86         }
87
88         it("should fail when JSON syntax is valid but content is invalid") {
89             // given
90             val json = "[1,2,3]".byteInputStream()
91             val cause = ParsingError("epic fail", None)
92             whenever(messageParametersParser.parse(any())).thenReturn(
93                     Left(cause))
94
95             // when
96             val result = cut.startSimulation(json)
97
98             // then
99             assertThat(result).left().isEqualTo(cause)
100         }
101
102         it("should return generated ves messages") {
103             // given
104             val vesEventGenerator: VesEventGenerator = mock()
105             val vesClient: HvVesClient = mock()
106
107             val json = "[true]".byteInputStream()
108
109             val vesEventParams = VesEventParameters(
110                     CommonEventHeader.getDefaultInstance(),
111                     VesEventType.VALID,
112                     1
113             )
114             val messageParams = listOf(vesEventParams)
115
116             val generatedMessages = Flux.empty<VesEventOuterClass.VesEvent>()
117
118
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))
124
125             // when
126             cut.startSimulation(json).map { it.unsafeRunSync() }
127
128             // then
129             verify(vesClient).sendVesEvents(generatedMessages)
130         }
131     }
132 })