Copyright notice correction
[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 com.google.protobuf.ByteString
23 import io.netty.buffer.ByteBuf
24 import io.netty.buffer.Unpooled
25 import io.netty.buffer.Unpooled.wrappedBuffer
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.given
28 import org.jetbrains.spek.api.dsl.it
29 import org.jetbrains.spek.api.dsl.on
30 import org.onap.dcae.collectors.veshv.domain.VesMessage
31 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader
32 import org.onap.ves.VesEventV5.VesEvent
33 import org.assertj.core.api.Assertions.assertThat
34 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.*
35
36 internal object MessageValidatorTest : Spek({
37
38     fun vesMessageBytes(commonHeader: CommonEventHeader): ByteBuf {
39         val msg = VesEvent.newBuilder()
40                 .setCommonEventHeader(commonHeader)
41                 .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data"))
42                 .build()
43         return wrappedBuffer(msg.toByteArray())
44     }
45
46     given("Message validator") {
47         val cut = MessageValidator()
48
49         on("ves hv message including header with fully initialized fields") {
50             val commonHeader = newBuilder()
51                     .setVersion("1.9")
52                     .setEventName("Sample event name")
53                     .setDomain(Domain.HVRANMEAS)
54                     .setEventId("Sample event Id")
55                     .setSourceName("Sample Source")
56                     .setReportingEntityName(ByteString.copyFromUtf8("Sample byte String"))
57                     .setPriority(Priority.MEDIUM)
58                     .setStartEpochMicrosec(120034455)
59                     .setLastEpochMicrosec(120034459)
60                     .setSequence(2)
61                     .build()
62
63             it("should accept message with fully initialized message header") {
64                 val vesMessage = VesMessage(commonHeader, vesMessageBytes(commonHeader))
65                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isTrue()
66             }
67
68             it("should reject message with domain other than HVRANMEAS") {
69                 Domain.values()
70                         .filter { it != Domain.HVRANMEAS && it != Domain.UNRECOGNIZED }
71                         .forEach { domain ->
72                             val header = newBuilder(commonHeader).setDomain(domain).build()
73                             val vesMessage = VesMessage(header, vesMessageBytes(header))
74                             assertThat(cut.isValid(vesMessage))
75                                     .describedAs("message with $domain domain")
76                                     .isFalse()
77                         }
78             }
79         }
80
81         on("ves hv message bytes") {
82             val vesMessage = VesMessage(getDefaultInstance(), Unpooled.EMPTY_BUFFER)
83             it("should not accept message with default header") {
84                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
85             }
86         }
87
88
89         on("ves hv message including header with not initialized fields") {
90             val commonHeader = newBuilder()
91                     .setVersion("1.9")
92                     .setEventName("Sample event name")
93                     .setEventId("Sample event Id")
94                     .setSourceName("Sample Source")
95                     .build()
96             val msg = VesEvent.newBuilder()
97                     .setCommonEventHeader(commonHeader)
98                     .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data !!!"))
99                     .build()
100             val rawMessageBytes = wrappedBuffer(msg.toByteArray())
101
102             it("should not accept not fully initialized message header ") {
103                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
104                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
105             }
106         }
107     }
108 })