Log details about header validation failure
[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.*
29 import org.onap.dcae.collectors.veshv.domain.*
30 import org.onap.dcae.collectors.veshv.model.VesMessage
31 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
32 import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes
33 import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
34 import kotlin.test.assertTrue
35 import kotlin.test.fail
36
37 internal object MessageValidatorTest : Spek({
38
39     describe("Message validator") {
40         val cut = MessageValidator
41
42         on("ves hv message including header with fully initialized fields") {
43             val commonHeader = commonHeader()
44
45             it("should accept message with fully initialized message header") {
46                 val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
47                 with(cut) {
48                     assertThat(validateProtobufMessage(vesMessage).isRight())
49                         .describedAs("message validation result").isTrue()
50                 }
51             }
52
53             VesEventDomain.values().forEach { domain ->
54                 it("should accept message with $domain domain") {
55                     val header = commonHeader(domain)
56                     val vesMessage = VesMessage(header, vesEventBytes(header))
57                     with(cut) {
58                         assertThat(validateProtobufMessage(vesMessage).isRight())
59                             .describedAs("message validation result").isTrue()
60                     }
61                 }
62             }
63         }
64
65         on("ves hv message bytes") {
66             val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
67             it("should not accept message with default header") {
68
69                 with(cut) {
70                     validateProtobufMessage(vesMessage).fold({
71                         val failMessages = it.invoke()
72
73                         val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
74                                 && failMessages.contains("missing domain field")
75                                 && failMessages.contains("missing version field")
76                                 && failMessages.contains("missing priority field")
77                                 && failMessages.contains("missing eventId field")
78                                 && failMessages.contains("missing eventName field")
79                                 && failMessages.contains("missing lastEpochMicrosec field")
80                                 && failMessages.contains("missing startEpochMicrosec field")
81                                 && failMessages.contains("missing reportingEntityName field")
82                                 && failMessages.contains("missing sourceName field")
83                                 && failMessages.contains("missing vesEventListenerVersion field")
84
85                         assertThat(containsAllErrorMessages)
86                             .describedAs("message validation result").isTrue()
87                     }, {
88                         fail()
89                     })
90                 }
91             }
92         }
93
94         given("priority test cases") {
95             mapOf(
96                 Priority.PRIORITY_NOT_PROVIDED to false,
97                 Priority.LOW to true,
98                 Priority.MEDIUM to true,
99                 Priority.HIGH to true
100             ).forEach { value, expectedResult ->
101                 on("ves hv message including header with priority $value") {
102                     val commonEventHeader = commonHeader(priority = value)
103                     val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))
104
105                     it("should resolve validation result") {
106                         with(cut) {
107                             assertThat(validateProtobufMessage(vesMessage).isRight())
108                                 .describedAs("message validation results")
109                                 .isEqualTo(expectedResult)
110                         }
111                     }
112                 }
113             }
114         }
115
116
117         on("ves hv message including header with not initialized fields") {
118             val commonHeader = newBuilder()
119                 .setVersion("1.9")
120                 .setEventName("Sample event name")
121                 .setEventId("Sample event Id")
122                 .setSourceName("Sample Source")
123                 .build()
124             val rawMessageBytes = vesEventBytes(commonHeader)
125
126             it("should not accept not fully initialized message header") {
127                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
128                 with(cut) {
129                     validateProtobufMessage(vesMessage).fold({
130                         val failMessages = it.invoke()
131
132                         val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
133                                 && failMessages.contains("missing domain field")
134                                 && failMessages.contains("missing priority field")
135                                 && failMessages.contains("missing lastEpochMicrosec field")
136                                 && failMessages.contains("missing startEpochMicrosec field")
137                                 && failMessages.contains("missing reportingEntityName field")
138                                 && failMessages.contains("missing vesEventListenerVersion field")
139
140                         assertThat(containsAllErrorMessages).describedAs("message validation result")
141                             .isTrue()
142                     }, {
143                         fail()
144                     })
145                 }
146             }
147         }
148
149         on("ves hv message including header.vesEventListenerVersion with non-string major part") {
150             val commonHeader = commonHeader(vesEventListenerVersion = "sample-version")
151             val rawMessageBytes = vesEventBytes(commonHeader)
152
153
154             it("should not accept message header") {
155                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
156                 with(cut) {
157                     validateProtobufMessage(vesMessage).fold({
158                         val failMessages = it.invoke()
159
160                         assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
161                             .describedAs("message validation result")
162                             .isTrue()
163                     }, {
164                         fail()
165                     })
166                 }
167             }
168         }
169
170         on("ves hv message including header.vesEventListenerVersion with major part != 7") {
171             val commonHeader = commonHeader(vesEventListenerVersion = "1.2.3")
172             val rawMessageBytes = vesEventBytes(commonHeader)
173
174             it("should not accept message header") {
175                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
176
177                 with(cut) {
178                     validateProtobufMessage(vesMessage).fold({
179                         val failMessages = it.invoke()
180
181                         assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
182                             .describedAs("message validation result")
183                             .isTrue()
184                     }, {
185                         fail()
186                     })
187                 }
188             }
189         }
190
191         on("ves hv message including header.vesEventListenerVersion with minor part not starting with a digit") {
192             val commonHeader = commonHeader(vesEventListenerVersion = "7.test")
193             val rawMessageBytes = vesEventBytes(commonHeader)
194
195             it("should not accept message header") {
196                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
197
198                 with(cut) {
199                     validateProtobufMessage(vesMessage).fold({
200                         val failMessages = it.invoke()
201
202                         assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
203                             .describedAs("message validation result")
204                             .isTrue()
205                     }, {
206                         fail()
207                     })
208                 }
209             }
210         }
211
212         describe("validating messages and converting to Either of string for validation result") {
213             given("WireFrameMessage") {
214                 on("valid message as input") {
215                     val wireFrameMessage = WireFrameMessage("lets pretend it's valid".toByteArray())
216                     val mockedWireFrameMessage = mock<WireFrameMessage> {
217                         on { validate() } doReturn right(wireFrameMessage)
218                     }
219
220                     it("should be right") {
221                         assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isRight())
222                     }
223                 }
224
225                 on("invalid message as input") {
226                     val mockedWireFrameMessage = mock<WireFrameMessage> {
227                         on { validate() } doReturn left(InvalidMajorVersion(99))
228                     }
229
230                     it("should be left") {
231                         assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isLeft())
232                     }
233                 }
234             }
235
236             given("VesEvent") {
237                 with(cut) {
238                     on("valid message as input") {
239                         val commonHeader = commonHeader()
240                         val rawMessageBytes = vesEventBytes(commonHeader)
241                         val vesMessage = VesMessage(commonHeader, rawMessageBytes)
242
243                         it("should be right") {
244                             assertTrue(validateProtobufMessage(vesMessage).isRight())
245                         }
246                     }
247                 }
248                 on("invalid message as input") {
249                     val commonHeader = newBuilder().build()
250                     val rawMessageBytes = vesEventBytes(commonHeader)
251                     val vesMessage = VesMessage(commonHeader, rawMessageBytes)
252
253                     it("should be left") {
254                         assertTrue(cut.validateProtobufMessage(vesMessage).isLeft())
255                     }
256                 }
257             }
258
259         }
260     }
261 })