f8c84c3905b9fc60d09b08b0575184f919ad51b3
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2010 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.ves.message.generator.impl.wireframe
21
22 import com.google.protobuf.InvalidProtocolBufferException
23 import org.assertj.core.api.Assertions
24 import org.assertj.core.api.Assertions.assertThat
25 import org.jetbrains.spek.api.Spek
26 import org.jetbrains.spek.api.dsl.it
27 import org.jetbrains.spek.api.dsl.on
28 import org.onap.dcae.collectors.veshv.domain.ByteData
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameParameters
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameType
32 import org.onap.ves.VesEventOuterClass
33 import reactor.test.test
34 import kotlin.test.assertTrue
35
36 /**
37  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
38  * @since February 2019
39  */
40 object WireFrameGeneratorTest : Spek({
41
42     val maxPayloadSizeBytes = 1024
43     val cut = WireFrameGenerator()
44
45     on("message type requesting invalid GPB data ") {
46         it("should createVesEventGenerator flux of messages with invalid payload") {
47             cut
48                     .createMessageFlux(WireFrameParameters(
49                             WireFrameType.INVALID_GPB_DATA, 1
50                     ))
51                     .test()
52                     .assertNext {
53                         assertTrue(it.validate().isRight())
54                         assertThat(it.payloadSize).isLessThan(maxPayloadSizeBytes)
55                         Assertions.assertThatExceptionOfType(InvalidProtocolBufferException::class.java)
56                                 .isThrownBy { extractCommonEventHeader(it.payload) }
57                     }
58                     .verifyComplete()
59         }
60     }
61
62     on("message type requesting invalid wire frame ") {
63         it("should createVesEventGenerator flux of messages with invalid version") {
64             cut
65                     .createMessageFlux(WireFrameParameters(
66                             WireFrameType.INVALID_WIRE_FRAME, 1
67                     ))
68                     .test()
69                     .assertNext {
70                         assertTrue(it.validate().isLeft())
71                         assertThat(it.payloadSize).isLessThan(maxPayloadSizeBytes)
72                         assertThat(it.versionMajor).isNotEqualTo(WireFrameMessage.SUPPORTED_VERSION_MINOR)
73                     }
74                     .verifyComplete()
75         }
76     }
77
78 })
79
80 fun extractCommonEventHeader(bytes: ByteData): VesEventOuterClass.CommonEventHeader =
81         VesEventOuterClass.VesEvent.parseFrom(bytes.unsafeAsArray()).commonEventHeader