Fix Common Event Header fields validation 75/72775/6
authorJakub Dudycz <jakub.dudycz@nokia.com>
Thu, 15 Nov 2018 12:29:09 +0000 (13:29 +0100)
committerJakub Dudycz <jakub.dudycz@nokia.com>
Mon, 19 Nov 2018 08:29:35 +0000 (09:29 +0100)
- "sequence" is no longer a required parameter,
  since deafult value "0" is acceptable
- "vesEventListenerVersion" has to match the regular expression "7\.[0-9]+\.[0-9]+"

Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Issue-ID: DCAEGEN2-976
Change-Id: I2f9fd6f375ccca3255cc9e035918dc37cc97bd6a

hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt
hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt
hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/validation.kt
hv-collector-test-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/tests/utils/vesEvents.kt

index a4a4374..fb94907 100644 (file)
@@ -20,6 +20,7 @@
 package org.onap.dcae.collectors.veshv.impl
 
 import org.onap.dcae.collectors.veshv.domain.headerRequiredFieldDescriptors
+import org.onap.dcae.collectors.veshv.domain.vesEventListenerVersionRegex
 import org.onap.dcae.collectors.veshv.model.VesMessage
 import org.onap.ves.VesEventOuterClass.CommonEventHeader
 
@@ -32,4 +33,6 @@ internal object MessageValidator {
     private fun allMandatoryFieldsArePresent(header: CommonEventHeader) =
             headerRequiredFieldDescriptors
                     .all { fieldDescriptor -> header.hasField(fieldDescriptor) }
+                    .and(vesEventListenerVersionRegex.matches(header.vesEventListenerVersion))
+
 }
index 443dfa2..25bd4f6 100644 (file)
@@ -29,10 +29,7 @@ import org.onap.dcae.collectors.veshv.domain.VesEventDomain
 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.Priority
-import org.onap.ves.VesEventOuterClass.CommonEventHeader.getDefaultInstance
-import org.onap.ves.VesEventOuterClass.CommonEventHeader.newBuilder
+import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
 
 internal object MessageValidatorTest : Spek({
 
@@ -91,9 +88,25 @@ internal object MessageValidatorTest : Spek({
                     .build()
             val rawMessageBytes = vesEventBytes(commonHeader)
 
-            it("should not accept not fully initialized message header ") {
+            it("should not accept not fully initialized message header") {
+                val vesMessage = VesMessage(commonHeader, rawMessageBytes)
+                assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
+            }
+        }
+
+        on("ves hv message including header with vesEventListenerVersion field not matching required pattern") {
+            val commonHeader = commonHeader(vesEventListenerVersion = "1.2.3")
+            val commonHeader2 = commonHeader(vesEventListenerVersion = "sample-version")
+
+            val rawMessageBytes = vesEventBytes(commonHeader)
+            val rawMessageBytes2 = vesEventBytes(commonHeader2)
+
+            it("should not accept message header") {
                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
                 assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isFalse()
+
+                val vesMessage2 = VesMessage(commonHeader2, rawMessageBytes2)
+                assertThat(cut.isValid(vesMessage2)).describedAs("second message validation result").isFalse()
             }
         }
     }
index 339a652..e0615bb 100644 (file)
@@ -24,7 +24,7 @@ import org.onap.ves.VesEventOuterClass
 val headerRequiredFieldDescriptors = listOf(
         "version",
         "domain",
-        "sequence",
+        /* field "sequence" has been removed from validation, since default value "0" is acceptable */
         "priority",
         "eventId",
         "eventName",
@@ -34,3 +34,5 @@ val headerRequiredFieldDescriptors = listOf(
         "sourceName",
         "vesEventListenerVersion")
         .map { fieldName -> VesEventOuterClass.CommonEventHeader.getDescriptor().findFieldByName(fieldName) }
+
+val vesEventListenerVersionRegex = """7\.[0-9]+\.[0-9]+""".toRegex()
index 20d0c50..569f1a9 100644 (file)
@@ -44,7 +44,8 @@ fun vesEvent(commonEventHeader: CommonEventHeader,
 
 fun commonHeader(domain: VesEventDomain = PERF3GPP,
                  id: String = randomUUID().toString(),
-                 priority: Priority = Priority.NORMAL): CommonEventHeader =
+                 priority: Priority = Priority.NORMAL,
+                 vesEventListenerVersion: String = "7.0.2"): CommonEventHeader =
         CommonEventHeader.newBuilder()
                 .setVersion("sample-version")
                 .setDomain(domain.domainName)
@@ -63,7 +64,7 @@ fun commonHeader(domain: VesEventDomain = PERF3GPP,
                 .setSourceId(ByteString.copyFromUtf8("sample-source-id"))
                 .setSourceName("sample-source-name")
                 .setTimeZoneOffset("+1")
-                .setVesEventListenerVersion("another-version")
+                .setVesEventListenerVersion(vesEventListenerVersion)
                 .build()
 
 fun vesEventBytes(commonHeader: CommonEventHeader, byteString: ByteString = ByteString.EMPTY): ByteData =