3090042d63820f45ae26097a982280faf2260f53
[dcaegen2/collectors/hv-ves.git] / 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 org.assertj.core.api.Assertions.assertThat
23 import org.jetbrains.spek.api.Spek
24 import org.jetbrains.spek.api.dsl.given
25 import org.jetbrains.spek.api.dsl.it
26 import org.jetbrains.spek.api.dsl.on
27 import org.onap.dcae.collectors.veshv.domain.ByteData
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
29 import org.onap.dcae.collectors.veshv.model.VesMessage
30 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
31 import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes
32 import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
33
34 internal object MessageValidatorTest : Spek({
35
36     given("Message validator") {
37         val cut = MessageValidator
38
39         on("ves hv message including header with fully initialized fields") {
40             val commonHeader = commonHeader()
41
42             it("should accept message with fully initialized message header") {
43                 val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
44                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isTrue()
45             }
46
47             VesEventDomain.values()
48                     .forEach { domain ->
49                         it("should accept message with $domain domain") {
50                             val header = commonHeader(domain)
51                             val vesMessage = VesMessage(header, vesEventBytes(header))
52                             assertThat(cut.isValid(vesMessage))
53                                     .isTrue()
54                         }
55                     }
56         }
57
58         on("ves hv message bytes") {
59             val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
60             it("should not accept message with default header") {
61                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
62             }
63         }
64
65         val priorityTestCases = mapOf(
66                 Priority.PRIORITY_NOT_PROVIDED to false,
67                 Priority.HIGH to true
68         )
69
70         priorityTestCases.forEach { value, expectedResult ->
71             on("ves hv message including header with priority $value") {
72                 val commonEventHeader = commonHeader(priority = value)
73                 val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))
74
75                 it("should resolve validation result") {
76                     assertThat(cut.isValid(vesMessage)).describedAs("message validation results")
77                             .isEqualTo(expectedResult)
78                 }
79             }
80         }
81
82         on("ves hv message including header with not initialized fields") {
83             val commonHeader = newBuilder()
84                     .setVersion("1.9")
85                     .setEventName("Sample event name")
86                     .setEventId("Sample event Id")
87                     .setSourceName("Sample Source")
88                     .build()
89             val rawMessageBytes = vesEventBytes(commonHeader)
90
91             it("should not accept not fully initialized message header") {
92                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
93                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
94             }
95         }
96
97         on("ves hv message including header.vesEventListenerVersion with non-string major part") {
98             val commonHeader = commonHeader(vesEventListenerVersion = "sample-version")
99             val rawMessageBytes = vesEventBytes(commonHeader)
100
101
102             it("should not accept message header") {
103                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
104                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
105             }
106         }
107
108         on("ves hv message including header.vesEventListenerVersion with major part != 7") {
109             val commonHeader = commonHeader(vesEventListenerVersion = "1.2.3")
110             val rawMessageBytes = vesEventBytes(commonHeader)
111
112             it("should not accept message header") {
113                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
114                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
115             }
116         }
117
118         on("ves hv message including header.vesEventListenerVersion with minor part not starting with a digit") {
119             val commonHeader = commonHeader(vesEventListenerVersion = "7.test")
120             val rawMessageBytes = vesEventBytes(commonHeader)
121
122             it("should not accept message header") {
123                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
124                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
125             }
126         }
127     }
128 })