aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt
blob: f784daa472e01725f2362870613bfbc649ad50e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * ============LICENSE_START=======================================================
 * dcaegen2-collectors-veshv
 * ================================================================================
 * Copyright (C) 2018 NOKIA
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.collectors.veshv.impl

import arrow.core.Either.Companion.left
import arrow.core.Either.Companion.right
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.*
import org.onap.dcae.collectors.veshv.domain.*
import org.onap.dcae.collectors.veshv.model.VesMessage
import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes
import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
import kotlin.test.assertTrue
import kotlin.test.fail

internal object MessageValidatorTest : Spek({

    describe("Message validator") {
        val cut = MessageValidator

        on("ves hv message including header with fully initialized fields") {
            val commonHeader = commonHeader()

            it("should accept message with fully initialized message header") {
                val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
                with(cut) {
                    assertThat(validateProtobufMessage(vesMessage).isRight())
                        .describedAs("message validation result").isTrue()
                }
            }

            VesEventDomain.values().forEach { domain ->
                it("should accept message with $domain domain") {
                    val header = commonHeader(domain)
                    val vesMessage = VesMessage(header, vesEventBytes(header))
                    with(cut) {
                        assertThat(validateProtobufMessage(vesMessage).isRight())
                            .describedAs("message validation result").isTrue()
                    }
                }
            }
        }

        on("ves hv message bytes") {
            val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
            it("should not accept message with default header") {

                with(cut) {
                    validateProtobufMessage(vesMessage).fold({
                        val failMessages = it.invoke()

                        val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
                                && failMessages.contains("missing domain field")
                                && failMessages.contains("missing version field")
                                && failMessages.contains("missing priority field")
                                && failMessages.contains("missing eventId field")
                                && failMessages.contains("missing eventName field")
                                && failMessages.contains("missing lastEpochMicrosec field")
                                && failMessages.contains("missing startEpochMicrosec field")
                                && failMessages.contains("missing reportingEntityName field")
                                && failMessages.contains("missing sourceName field")
                                && failMessages.contains("missing vesEventListenerVersion field")

                        assertThat(containsAllErrorMessages)
                            .describedAs("message validation result").isTrue()
                    }, {
                        fail()
                    })
                }
            }
        }

        given("priority test cases") {
            mapOf(
                Priority.PRIORITY_NOT_PROVIDED to false,
                Priority.LOW to true,
                Priority.MEDIUM to true,
                Priority.HIGH to true
            ).forEach { value, expectedResult ->
                on("ves hv message including header with priority $value") {
                    val commonEventHeader = commonHeader(priority = value)
                    val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))

                    it("should resolve validation result") {
                        with(cut) {
                            assertThat(validateProtobufMessage(vesMessage).isRight())
                                .describedAs("message validation results")
                                .isEqualTo(expectedResult)
                        }
                    }
                }
            }
        }


        on("ves hv message including header with not initialized fields") {
            val commonHeader = newBuilder()
                .setVersion("1.9")
                .setEventName("Sample event name")
                .setEventId("Sample event Id")
                .setSourceName("Sample Source")
                .build()
            val rawMessageBytes = vesEventBytes(commonHeader)

            it("should not accept not fully initialized message header") {
                val vesMessage = VesMessage(commonHeader, rawMessageBytes)
                with(cut) {
                    validateProtobufMessage(vesMessage).fold({
                        val failMessages = it.invoke()

                        val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
                                && failMessages.contains("missing domain field")
                                && failMessages.contains("missing priority field")
                                && failMessages.contains("missing lastEpochMicrosec field")
                                && failMessages.contains("missing startEpochMicrosec field")
                                && failMessages.contains("missing reportingEntityName field")
                                && failMessages.contains("missing vesEventListenerVersion field")

                        assertThat(containsAllErrorMessages).describedAs("message validation result")
                            .isTrue()
                    }, {
                        fail()
                    })
                }
            }
        }

        on("ves hv message including header.vesEventListenerVersion with non-string major part") {
            val commonHeader = commonHeader(vesEventListenerVersion = "sample-version")
            val rawMessageBytes = vesEventBytes(commonHeader)


            it("should not accept message header") {
                val vesMessage = VesMessage(commonHeader, rawMessageBytes)
                with(cut) {
                    validateProtobufMessage(vesMessage).fold({
                        val failMessages = it.invoke()

                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
                            .describedAs("message validation result")
                            .isTrue()
                    }, {
                        fail()
                    })
                }
            }
        }

        on("ves hv message including header.vesEventListenerVersion with major part != 7") {
            val commonHeader = commonHeader(vesEventListenerVersion = "1.2.3")
            val rawMessageBytes = vesEventBytes(commonHeader)

            it("should not accept message header") {
                val vesMessage = VesMessage(commonHeader, rawMessageBytes)

                with(cut) {
                    validateProtobufMessage(vesMessage).fold({
                        val failMessages = it.invoke()

                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
                            .describedAs("message validation result")
                            .isTrue()
                    }, {
                        fail()
                    })
                }
            }
        }

        on("ves hv message including header.vesEventListenerVersion with minor part not starting with a digit") {
            val commonHeader = commonHeader(vesEventListenerVersion = "7.test")
            val rawMessageBytes = vesEventBytes(commonHeader)

            it("should not accept message header") {
                val vesMessage = VesMessage(commonHeader, rawMessageBytes)

                with(cut) {
                    validateProtobufMessage(vesMessage).fold({
                        val failMessages = it.invoke()

                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
                            .describedAs("message validation result")
                            .isTrue()
                    }, {
                        fail()
                    })
                }
            }
        }

        describe("validating messages and converting to Either of string for validation result") {
            given("WireFrameMessage") {
                on("valid message as input") {
                    val wireFrameMessage = WireFrameMessage("lets pretend it's valid".toByteArray())
                    val mockedWireFrameMessage = mock<WireFrameMessage> {
                        on { validate() } doReturn right(wireFrameMessage)
                    }

                    it("should be right") {
                        assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isRight())
                    }
                }

                on("invalid message as input") {
                    val mockedWireFrameMessage = mock<WireFrameMessage> {
                        on { validate() } doReturn left(InvalidMajorVersion(99))
                    }

                    it("should be left") {
                        assertTrue(cut.validateFrameMessage(mockedWireFrameMessage).isLeft())
                    }
                }
            }

            given("VesEvent") {
                with(cut) {
                    on("valid message as input") {
                        val commonHeader = commonHeader()
                        val rawMessageBytes = vesEventBytes(commonHeader)
                        val vesMessage = VesMessage(commonHeader, rawMessageBytes)

                        it("should be right") {
                            assertTrue(validateProtobufMessage(vesMessage).isRight())
                        }
                    }
                }
                on("invalid message as input") {
                    val commonHeader = newBuilder().build()
                    val rawMessageBytes = vesEventBytes(commonHeader)
                    val vesMessage = VesMessage(commonHeader, rawMessageBytes)

                    it("should be left") {
                        assertTrue(cut.validateProtobufMessage(vesMessage).isLeft())
                    }
                }
            }

        }
    }
})