Harmonize logging and add new logs
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / test / kotlin / org / onap / dcae / collectors / veshv / impl / MessageValidatorTest.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.impl
21
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
41
42 internal object MessageValidatorTest : Spek({
43
44     describe("Message validator") {
45         val cut = MessageValidator
46
47         on("ves hv message including header with fully initialized fields") {
48             val commonHeader = commonHeader()
49
50             it("should accept message with fully initialized message header") {
51                 val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
52                 with(cut) {
53                     assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
54                 }
55             }
56
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))
61                     with(cut) {
62                         assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
63                     }
64                 }
65             }
66         }
67
68         on("ves hv message bytes") {
69             val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
70             it("should not accept message with default header") {
71                 with(cut) {
72                     assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
73                 }
74             }
75         }
76
77         val priorityTestCases = mapOf(
78                 Priority.PRIORITY_NOT_PROVIDED to false,
79                 Priority.LOW to true,
80                 Priority.MEDIUM to true,
81                 Priority.HIGH to true
82         )
83
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))
88
89                 it("should resolve validation result") {
90                     with(cut) {
91                         assertThat(vesMessage.isValid()).describedAs("message validation results")
92                                 .isEqualTo(expectedResult)
93                     }
94                 }
95             }
96         }
97
98         on("ves hv message including header with not initialized fields") {
99             val commonHeader = newBuilder()
100                     .setVersion("1.9")
101                     .setEventName("Sample event name")
102                     .setEventId("Sample event Id")
103                     .setSourceName("Sample Source")
104                     .build()
105             val rawMessageBytes = vesEventBytes(commonHeader)
106
107             it("should not accept not fully initialized message header") {
108                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
109                 with(cut) {
110                     assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
111                 }
112             }
113         }
114
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)
118
119
120             it("should not accept message header") {
121                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
122                 with(cut) {
123                     assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
124                 }
125             }
126         }
127
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)
131
132             it("should not accept message header") {
133                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
134
135                 with(cut) {
136                     assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
137                 }
138             }
139         }
140
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)
144
145             it("should not accept message header") {
146                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
147
148                 with(cut) {
149                     assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
150                 }
151             }
152         }
153
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)
160                     }
161
162                     it("should be right") {
163                         assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isRight())
164                     }
165                 }
166
167                 on("invalid message as input") {
168                     val mockedWireFrameMessage = mock<WireFrameMessage> {
169                         on { validate() } doReturn left(InvalidMajorVersion(99))
170                     }
171
172                     it("should be left") {
173                         assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isLeft())
174                     }
175                 }
176             }
177
178             given("VesEvent") {
179                 with(cut) {
180                     on("valid message as input") {
181                         val commonHeader = commonHeader()
182                         val rawMessageBytes = vesEventBytes(commonHeader)
183                         val vesMessage = VesMessage(commonHeader, rawMessageBytes)
184
185                         it("should be right") {
186                             assertTrue(validateProtobufMessage(vesMessage).isRight())
187                         }
188                     }
189                 }
190                 on("invalid message as input") {
191                     val commonHeader = newBuilder().build()
192                     val rawMessageBytes = vesEventBytes(commonHeader)
193                     val vesMessage = VesMessage(commonHeader, rawMessageBytes)
194
195                     it("should be left") {
196                         assertTrue(cut.validateProtobufMessage(vesMessage).isLeft())
197                     }
198                 }
199             }
200
201         }
202     }
203 })