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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.impl
22 import arrow.core.Either.Companion.left
23 import arrow.core.Either.Companion.right
24 import com.nhaarman.mockitokotlin2.doReturn
25 import com.nhaarman.mockitokotlin2.mock
26 import org.assertj.core.api.Assertions.assertThat
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.given
30 import org.jetbrains.spek.api.dsl.it
31 import org.jetbrains.spek.api.dsl.on
32 import org.onap.dcae.collectors.veshv.domain.ByteData
33 import org.onap.dcae.collectors.veshv.domain.InvalidMajorVersion
34 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
35 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
36 import org.onap.dcae.collectors.veshv.model.VesMessage
37 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
38 import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes
39 import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
40 import kotlin.test.assertTrue
42 internal object MessageValidatorTest : Spek({
44 describe("Message validator") {
45 val cut = MessageValidator
47 on("ves hv message including header with fully initialized fields") {
48 val commonHeader = commonHeader()
50 it("should accept message with fully initialized message header") {
51 val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
53 assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
57 VesEventDomain.values().forEach { domain ->
58 it("should accept message with $domain domain") {
59 val header = commonHeader(domain)
60 val vesMessage = VesMessage(header, vesEventBytes(header))
62 assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
68 on("ves hv message bytes") {
69 val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
70 it("should not accept message with default header") {
72 assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
77 val priorityTestCases = mapOf(
78 Priority.PRIORITY_NOT_PROVIDED to false,
80 Priority.MEDIUM to true,
84 priorityTestCases.forEach { value, expectedResult ->
85 on("ves hv message including header with priority $value") {
86 val commonEventHeader = commonHeader(priority = value)
87 val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))
89 it("should resolve validation result") {
91 assertThat(vesMessage.isValid()).describedAs("message validation results")
92 .isEqualTo(expectedResult)
98 on("ves hv message including header with not initialized fields") {
99 val commonHeader = newBuilder()
101 .setEventName("Sample event name")
102 .setEventId("Sample event Id")
103 .setSourceName("Sample Source")
105 val rawMessageBytes = vesEventBytes(commonHeader)
107 it("should not accept not fully initialized message header") {
108 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
110 assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
115 on("ves hv message including header.vesEventListenerVersion with non-string major part") {
116 val commonHeader = commonHeader(vesEventListenerVersion = "sample-version")
117 val rawMessageBytes = vesEventBytes(commonHeader)
120 it("should not accept message header") {
121 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
123 assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
128 on("ves hv message including header.vesEventListenerVersion with major part != 7") {
129 val commonHeader = commonHeader(vesEventListenerVersion = "1.2.3")
130 val rawMessageBytes = vesEventBytes(commonHeader)
132 it("should not accept message header") {
133 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
136 assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
141 on("ves hv message including header.vesEventListenerVersion with minor part not starting with a digit") {
142 val commonHeader = commonHeader(vesEventListenerVersion = "7.test")
143 val rawMessageBytes = vesEventBytes(commonHeader)
145 it("should not accept message header") {
146 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
149 assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
154 describe("validating messages and converting to Either of string for validation result") {
155 given("WireFrameMessage") {
156 on("valid message as input") {
157 val wireFrameMessage = WireFrameMessage("lets pretend it's valid".toByteArray())
158 val mockedWireFrameMessage = mock<WireFrameMessage> {
159 on { validate() } doReturn right(wireFrameMessage)
162 it("should be right") {
163 assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isRight())
167 on("invalid message as input") {
168 val mockedWireFrameMessage = mock<WireFrameMessage> {
169 on { validate() } doReturn left(InvalidMajorVersion(99))
172 it("should be left") {
173 assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isLeft())
180 on("valid message as input") {
181 val commonHeader = commonHeader()
182 val rawMessageBytes = vesEventBytes(commonHeader)
183 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
185 it("should be right") {
186 assertTrue(validateProtobufMessage(vesMessage).isRight())
190 on("invalid message as input") {
191 val commonHeader = newBuilder().build()
192 val rawMessageBytes = vesEventBytes(commonHeader)
193 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
195 it("should be left") {
196 assertTrue(cut.validateProtobufMessage(vesMessage).isLeft())