192725b99da5ecee5089f0ce9529ef8b2bce3a37
[dcaegen2/collectors/hv-ves.git] /
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 com.nhaarman.mockitokotlin2.any
25 import com.nhaarman.mockitokotlin2.mock
26 import com.nhaarman.mockitokotlin2.whenever
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.it
30 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
31 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters.VesHvClient
32 import org.onap.dcae.collectors.veshv.tests.utils.Assertions.assertThat
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
34 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
35 import org.onap.dcae.collectors.veshv.ves.message.generator.factory.MessageGeneratorFactory
36 import java.io.ByteArrayInputStream
37
38 /**
39  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
40  * @since September 2018
41  */
42 internal class XnfSimulatorTest : Spek({
43     lateinit var cut: XnfSimulator
44     lateinit var vesClient: VesHvClient
45     lateinit var messageParametersParser: MessageParametersParser
46     lateinit var generatorFactory: MessageGeneratorFactory
47
48     beforeEachTest {
49         vesClient = mock()
50         messageParametersParser = mock()
51         generatorFactory = mock()
52         cut = XnfSimulator(vesClient, generatorFactory, messageParametersParser)
53     }
54
55     describe("startSimulation") {
56         it("should fail when empty input stream") {
57             // given
58             val emptyInputStream = ByteArrayInputStream(byteArrayOf())
59
60             // when
61             val result = cut.startSimulation(emptyInputStream)
62
63             // then
64             assertThat(result).isLeft()
65         }
66
67         it("should fail when invalid JSON") {
68             // given
69             val invalidJson = "invalid json".byteInputStream()
70
71             // when
72             val result = cut.startSimulation(invalidJson)
73
74             // then
75             assertThat(result).isLeft()
76         }
77
78         it("should fail when JSON syntax is valid but content is invalid") {
79             // given
80             val json = "[1,2,3]".byteInputStream()
81             val cause = ParsingError("epic fail", None)
82             whenever(messageParametersParser.parse(any())).thenReturn(
83                     Left(cause))
84
85             // when
86             val result = cut.startSimulation(json)
87
88             // then
89             assertThat(result).left().isEqualTo(cause)
90         }
91
92         // TODO uncomment and fix this test after introducing HvVesProducer from onap SDK in XnfSimulator
93 //        it("should return generated messages") {
94 //            // given
95 //            val json = "[true]".byteInputStream()
96 //            val messageParams = listOf<MessageParameters>()
97 //            val generatedMessages = Flux.empty<WireFrameMessage>()
98 //            val sendingIo = IO {}
99 //            whenever(messageParametersParser.parse(any())).thenReturn(Right(messageParams))
100 //            whenever(messageGenerator.createMessageFlux(messageParams)).thenReturn(generatedMessages)
101 //            whenever(vesClient.sendIo(generatedMessages)).thenReturn(sendingIo)
102 //
103 //            // when
104 //            val result = cut.startSimulation(json)
105 //
106 //            // then
107 //            assertThat(result).right().isSameAs(sendingIo)
108 //        }
109     }
110 })