diff options
author | 2018-07-31 14:26:09 +0200 | |
---|---|---|
committer | 2018-08-03 11:02:26 +0200 | |
commit | 185bc70fa1c024e532649bea650183e05c2d3d87 (patch) | |
tree | dafcd1a21d71948d79f903380b860cca36bebb6c /hv-collector-core/src/test | |
parent | 8a0e9e5d4d7613793d2804d7e16a9352e3883874 (diff) |
Extract test-utils module
- removed duplicate code that was creating VesMessages
and similiar objects
- removed duplicate code in command line parsing tests
- made minor refactorings to avoid passing unnecessary params
and to be as verbose as possible in tests
Closes ONAP-699
Change-Id: I2607f1f775054ae1c5f275c231895f838b415371
Signed-off-by: fkrzywka <filip.krzywka@nokia.com>
Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-core/src/test')
3 files changed, 21 insertions, 77 deletions
diff --git a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt index eb8971c3..213f4544 100644 --- a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt +++ b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt @@ -19,17 +19,15 @@ */ package org.onap.dcae.collectors.veshv.impl -import com.google.protobuf.ByteString import org.assertj.core.api.Assertions.assertThat import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.it import org.jetbrains.spek.api.dsl.on import org.onap.dcae.collectors.veshv.domain.ByteData -import org.onap.dcae.collectors.veshv.domain.toByteData import org.onap.dcae.collectors.veshv.model.VesMessage -import org.onap.ves.VesEventV5.VesEvent -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader +import org.onap.dcae.collectors.veshv.tests.utils.commonHeader +import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Priority import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.getDefaultInstance @@ -37,22 +35,14 @@ import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.newBuilder internal object MessageValidatorTest : Spek({ - fun vesMessageBytes(commonHeader: CommonEventHeader): ByteData { - val msg = VesEvent.newBuilder() - .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data")) - .build() - return msg.toByteData() - } - given("Message validator") { val cut = MessageValidator on("ves hv message including header with fully initialized fields") { - val commonHeader = createInitializedHeaderBuilder().build() + val commonHeader = commonHeader() it("should accept message with fully initialized message header") { - val vesMessage = VesMessage(commonHeader, vesMessageBytes(commonHeader)) + val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader)) assertThat(cut.isValid(vesMessage)).describedAs("message validation result").isTrue() } @@ -60,8 +50,8 @@ internal object MessageValidatorTest : Spek({ .filter { (it != Domain.UNRECOGNIZED && it != Domain.DOMAIN_UNDEFINED) } .forEach { domain -> it("should accept message with $domain domain") { - val header = newBuilder(commonHeader).setDomain(domain).build() - val vesMessage = VesMessage(header, vesMessageBytes(header)) + val header = commonHeader(domain) + val vesMessage = VesMessage(header, vesEventBytes(header)) assertThat(cut.isValid(vesMessage)) .isTrue() } @@ -83,10 +73,8 @@ internal object MessageValidatorTest : Spek({ domainTestCases.forEach { value, expectedResult -> on("ves hv message including header with domain $value") { - val commonEventHeader = createInitializedHeaderBuilder() - .setDomain(value) - .build() - val vesMessage = VesMessage(commonEventHeader, vesMessageBytes(commonEventHeader)) + val commonEventHeader = commonHeader(value) + val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader)) it("should resolve validation result") { assertThat(cut.isValid(vesMessage)).describedAs("message validation results") @@ -102,10 +90,8 @@ internal object MessageValidatorTest : Spek({ priorityTestCases.forEach { value, expectedResult -> on("ves hv message including header with priority $value") { - val commonEventHeader = createInitializedHeaderBuilder() - .setPriority(value) - .build() - val vesMessage = VesMessage(commonEventHeader, vesMessageBytes(commonEventHeader)) + val commonEventHeader = commonHeader(priority = value) + val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader)) it("should resolve validation result") { assertThat(cut.isValid(vesMessage)).describedAs("message validation results") @@ -114,7 +100,6 @@ internal object MessageValidatorTest : Spek({ } } - on("ves hv message including header with not initialized fields") { val commonHeader = newBuilder() .setVersion("1.9") @@ -122,11 +107,7 @@ internal object MessageValidatorTest : Spek({ .setEventId("Sample event Id") .setSourceName("Sample Source") .build() - val msg = VesEvent.newBuilder() - .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data !!!")) - .build() - val rawMessageBytes = msg.toByteData() + val rawMessageBytes = vesEventBytes(commonHeader) it("should not accept not fully initialized message header ") { val vesMessage = VesMessage(commonHeader, rawMessageBytes) @@ -135,16 +116,3 @@ internal object MessageValidatorTest : Spek({ } } }) - -private fun createInitializedHeaderBuilder(): CommonEventHeader.Builder = - newBuilder() - .setVersion("1.9") - .setEventName("Sample event name") - .setDomain(Domain.HVRANMEAS) - .setEventId("Sample event Id") - .setSourceName("Sample Source") - .setReportingEntityName(ByteString.copyFromUtf8("Sample byte String")) - .setPriority(Priority.MEDIUM) - .setStartEpochMicrosec(120034455) - .setLastEpochMicrosec(120034459) - .setSequence(2)
\ No newline at end of file diff --git a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/RouterTest.kt b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/RouterTest.kt index 599a9d40..91fa7c19 100644 --- a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/RouterTest.kt +++ b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/RouterTest.kt @@ -30,7 +30,7 @@ import org.onap.dcae.collectors.veshv.domain.ByteData import org.onap.dcae.collectors.veshv.model.RoutedMessage import org.onap.dcae.collectors.veshv.model.VesMessage import org.onap.dcae.collectors.veshv.model.routing -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader +import org.onap.dcae.collectors.veshv.tests.utils.commonHeader import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain /** @@ -56,7 +56,7 @@ object RouterTest : Spek({ val cut = Router(config) on("message with existing route (rtpm)") { - val message = VesMessage(vesCommonHeaderWithDomain(Domain.HVRANMEAS), ByteData.EMPTY) + val message = VesMessage(commonHeader(Domain.HVRANMEAS), ByteData.EMPTY) val result = cut.findDestination(message) it("should have route available") { @@ -77,7 +77,7 @@ object RouterTest : Spek({ } on("message with existing route (trace)") { - val message = VesMessage(vesCommonHeaderWithDomain(Domain.SYSLOG), ByteData.EMPTY) + val message = VesMessage(commonHeader(Domain.SYSLOG), ByteData.EMPTY) val result = cut.findDestination(message) it("should have route available") { @@ -98,7 +98,7 @@ object RouterTest : Spek({ } on("message with unknown route") { - val message = VesMessage(vesCommonHeaderWithDomain(Domain.HEARTBEAT), ByteData.EMPTY) + val message = VesMessage(commonHeader(Domain.HEARTBEAT), ByteData.EMPTY) val result = cut.findDestination(message) it("should not have route available") { @@ -106,9 +106,4 @@ object RouterTest : Spek({ } } } -}) - -private fun vesCommonHeaderWithDomain(domain: Domain) = - CommonEventHeader.getDefaultInstance().toBuilder() - .setDomain(domain) - .build()
\ No newline at end of file +})
\ No newline at end of file diff --git a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt index 3f1f610e..a7d3971e 100644 --- a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt +++ b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt @@ -26,10 +26,10 @@ import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.it import org.jetbrains.spek.api.dsl.on import org.onap.dcae.collectors.veshv.domain.ByteData -import org.onap.dcae.collectors.veshv.domain.toByteData import org.onap.dcae.collectors.veshv.model.VesMessage -import org.onap.ves.VesEventV5.VesEvent -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader +import org.onap.dcae.collectors.veshv.tests.utils.commonHeader +import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes +import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain import java.nio.charset.Charset import kotlin.test.assertTrue import kotlin.test.fail @@ -41,12 +41,8 @@ internal object VesDecoderTest : Spek({ val cut = VesDecoder() on("ves hv message bytes") { - val commonHeader = commonEventHeader() - val msg = VesEvent.newBuilder() - .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(ByteString.copyFromUtf8("highvolume measurements")) - .build() - val rawMessageBytes = msg.toByteData() + val commonHeader = commonHeader(Domain.HEARTBEAT) + val rawMessageBytes = vesEventBytes(commonHeader, ByteString.copyFromUtf8("highvolume measurements")) it("should decode only header and pass it on along with raw message") { val expectedMessage = VesMessage( @@ -76,18 +72,3 @@ private fun <A> assertFailedWithError(option: Option<A>) = option.exists { fail("Error expected") } - - -private fun commonEventHeader() = - CommonEventHeader.getDefaultInstance().toBuilder() - .setDomain(CommonEventHeader.Domain.HEARTBEAT) - .setVersion("1.0") - .setEventName("xyz") - .setEventId("eventID") - .setEventName("Sample event name") - .setSourceName("Sample Source") - .setPriority(CommonEventHeader.Priority.MEDIUM) - .setStartEpochMicrosec(120034455) - .setLastEpochMicrosec(120034459) - .setSequence(1) - .build() |