Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / 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 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 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
42
43 /**
44  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
45  * @since September 2018
46  */
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
52
53     beforeEachTest {
54         vesClient = mock()
55         messageParametersParser = mock()
56         messageGenerator = mock()
57         cut = XnfSimulator(vesClient, messageGenerator, messageParametersParser)
58     }
59
60     describe("startSimulation") {
61         it("should fail when empty input stream") {
62             // given
63             val emptyInputStream = ByteArrayInputStream(byteArrayOf())
64
65             // when
66             val result = cut.startSimulation(emptyInputStream)
67
68             // then
69             assertThat(result).isLeft()
70         }
71
72         it("should fail when invalid JSON") {
73             // given
74             val invalidJson = "invalid json".byteInputStream()
75
76             // when
77             val result = cut.startSimulation(invalidJson)
78
79             // then
80             assertThat(result).isLeft()
81         }
82
83         it("should fail when JSON syntax is valid but content is invalid") {
84             // given
85             val json = "[1,2,3]".byteInputStream()
86             val cause = ParsingError("epic fail", None)
87             whenever(messageParametersParser.parse(any())).thenReturn(
88                     Left(cause))
89
90             // when
91             val result = cut.startSimulation(json)
92
93             // then
94             assertThat(result).left().isEqualTo(cause)
95         }
96
97         it("should return generated messages") {
98             // given
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)
106
107             // when
108             val result = cut.startSimulation(json)
109
110             // then
111             assertThat(result).right().isSameAs(sendingIo)
112         }
113     }
114 })