29281cdce855d7f528e6d3dd5c19ba57ad93890b
[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.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
49
50 /**
51  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
52  * @since September 2018
53  */
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
59
60     beforeEachTest {
61         clientFactory = mock()
62         messageParametersParser = mock()
63         generatorFactory = mock()
64         cut = XnfSimulator(clientFactory, generatorFactory, messageParametersParser)
65     }
66
67     describe("startSimulation") {
68         it("should fail when empty input stream") {
69             // given
70             val emptyInputStream = ByteArrayInputStream(byteArrayOf())
71
72             // when
73             val result = cut.startSimulation(emptyInputStream)
74
75             // then
76             assertThat(result).isLeft()
77         }
78
79         it("should fail when invalid JSON") {
80             // given
81             val invalidJson = "invalid json".byteInputStream()
82
83             // when
84             val result = cut.startSimulation(invalidJson)
85
86             // then
87             assertThat(result).isLeft()
88         }
89
90         it("should fail when JSON syntax is valid but content is invalid") {
91             // given
92             val json = "[1,2,3]".byteInputStream()
93             val cause = ParsingError("epic fail", None)
94             whenever(messageParametersParser.parse(any())).thenReturn(
95                     Left(cause))
96
97             // when
98             val result = cut.startSimulation(json)
99
100             // then
101             assertThat(result).left().isEqualTo(cause)
102         }
103
104         it("should return generated ves messages") {
105             // given
106             val vesEventGenerator: VesEventGenerator = mock()
107             val vesClient: HvVesClient = mock()
108
109             val json = "[true]".byteInputStream()
110
111             val vesEventParams = VesEventParameters(
112                     CommonEventHeader.getDefaultInstance(),
113                     VesEventType.VALID,
114                     1
115             )
116             val messageParams = listOf(vesEventParams)
117
118             val generatedMessages = Flux.empty<VesEventOuterClass.VesEvent>()
119
120
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)
125
126             whenever(vesClient.sendRawPayload(any(), eq(PayloadType.PROTOBUF))).thenReturn(Mono.just(Unit))
127
128             // when
129             cut.startSimulation(json).map { it.unsafeRunSync() }
130
131             // then
132             verify(vesClient).sendRawPayload(any(), eq(PayloadType.PROTOBUF))
133         }
134     }
135 })