diff options
author | 2018-07-17 07:54:58 +0200 | |
---|---|---|
committer | 2018-08-03 07:18:32 +0200 | |
commit | 40c5abeac588ca6c13477675960c94a97dcdeb15 (patch) | |
tree | a4e0b6cb8b843b2eae99349ac7b76d2d9dd9e6fc /hv-collector-core/src/test | |
parent | 8e31525e1003bede54c23a061cee2ca7e59a4be5 (diff) |
Use Try/Option monad when decoding protobuf
Closes ONAP-143
Change-Id: I33cb2d24cd5962318a6f405096db298bbdbab963
Signed-off-by: fkrzywka <filip.krzywka@nokia.com>
Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-core/src/test')
-rw-r--r-- | hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt | 39 |
1 files changed, 30 insertions, 9 deletions
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 90b34b1c..3f1f610e 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 @@ -19,10 +19,8 @@ */ package org.onap.dcae.collectors.veshv.impl +import arrow.core.Option import com.google.protobuf.ByteString -import com.google.protobuf.InvalidProtocolBufferException -import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.it @@ -33,6 +31,8 @@ import org.onap.dcae.collectors.veshv.model.VesMessage import org.onap.ves.VesEventV5.VesEvent import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader import java.nio.charset.Charset +import kotlin.test.assertTrue +import kotlin.test.fail internal object VesDecoderTest : Spek({ @@ -41,22 +41,24 @@ internal object VesDecoderTest : Spek({ val cut = VesDecoder() on("ves hv message bytes") { - val commonHeader = CommonEventHeader.getDefaultInstance() + val commonHeader = commonEventHeader() val msg = VesEvent.newBuilder() .setCommonEventHeader(commonHeader) .setHvRanMeasFields(ByteString.copyFromUtf8("highvolume measurements")) .build() val rawMessageBytes = msg.toByteData() - it("should decode only header and pass it on along with raw message") { val expectedMessage = VesMessage( commonHeader, rawMessageBytes ) - assertThat(cut.decode(rawMessageBytes)).isEqualTo(expectedMessage) - + assertTrue { + cut.decode(rawMessageBytes).exists { + it == expectedMessage + } + } } } @@ -64,9 +66,28 @@ internal object VesDecoderTest : Spek({ val rawMessageBytes = ByteData("ala ma kota".toByteArray(Charset.defaultCharset())) it("should throw error") { - assertThatExceptionOfType(InvalidProtocolBufferException::class.java) - .isThrownBy { cut.decode(rawMessageBytes) } + assertFailedWithError(cut.decode(rawMessageBytes)) } } } }) + +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() |