summaryrefslogtreecommitdiffstats
path: root/hv-collector-domain/src/test
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-06-28 14:42:05 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 11:04:01 +0200
commit03702b48989174dc8afa855e663a28e34b4da67b (patch)
treee89a4930aab1a53014b81e76be493dda41d9e007 /hv-collector-domain/src/test
parentcf1465f37d20391a921df449d5dd01454f64910c (diff)
Use Either instead of exceptions in frame decoder
Goals: * Make code cleaner (in a FP way) * Avoid costly exception throw each time we wait for the rest of the frame (collecting stack traces is costly and we do not need them anyway) Closes ONAP-437 Change-Id: I40341d3c2cb85f3ff581d89167245cb009dbb070 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com> Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-domain/src/test')
-rw-r--r--hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt33
1 files changed, 19 insertions, 14 deletions
diff --git a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt
index 9694caf7..a97d889c 100644
--- a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt
+++ b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt
@@ -19,16 +19,17 @@
*/
package org.onap.dcae.collectors.veshv.domain
+import arrow.core.Either
+import arrow.core.identity
import io.netty.buffer.Unpooled
import io.netty.buffer.UnpooledByteBufAllocator
import org.assertj.core.api.Assertions.assertThat
-import org.assertj.core.api.Assertions.assertThatExceptionOfType
+import org.assertj.core.api.Assertions.fail
+import org.assertj.core.api.ObjectAssert
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
-import org.onap.dcae.collectors.veshv.domain.exceptions.InvalidWireFrameMarkerException
-import org.onap.dcae.collectors.veshv.domain.exceptions.MissingWireFrameBytesException
import java.nio.charset.Charset
/**
@@ -119,7 +120,7 @@ object WireFrameCodecsTest : Spek({
describe("encode-decode methods' compatibility") {
val frame = createSampleFrame()
val encoded = encodeSampleFrame()
- val decoded = decoder.decodeFirst(encoded)
+ val decoded = decoder.decodeFirst(encoded).getOrFail()
it("should decode version") {
assertThat(decoded.version).isEqualTo(frame.version)
@@ -146,7 +147,7 @@ object WireFrameCodecsTest : Spek({
val buff = Unpooled.buffer()
.writeBytes(encodeSampleFrame())
.writeByte(0xAA)
- val decoded = decoder.decodeFirst(buff)
+ val decoded = decoder.decodeFirst(buff).getOrFail()
assertThat(decoded.isValid()).describedAs("should be valid").isTrue()
assertThat(buff.readableBytes()).isEqualTo(1)
@@ -156,8 +157,8 @@ object WireFrameCodecsTest : Spek({
val buff = Unpooled.buffer()
.writeByte(0xFF)
- assertThatExceptionOfType(MissingWireFrameBytesException::class.java)
- .isThrownBy { decoder.decodeFirst(buff) }
+ decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFrameHeaderBytes::class.java) }
+
}
it("should throw exception when first byte is not 0xFF but length looks ok") {
@@ -165,16 +166,14 @@ object WireFrameCodecsTest : Spek({
.writeByte(0xAA)
.writeBytes("some garbage".toByteArray())
- assertThatExceptionOfType(InvalidWireFrameMarkerException::class.java)
- .isThrownBy { decoder.decodeFirst(buff) }
+ decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(InvalidWireFrameMarker::class.java) }
}
it("should throw exception when first byte is not 0xFF and length is to short") {
val buff = Unpooled.buffer()
.writeByte(0xAA)
- assertThatExceptionOfType(InvalidWireFrameMarkerException::class.java)
- .isThrownBy { decoder.decodeFirst(buff) }
+ decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFrameHeaderBytes::class.java) }
}
it("should throw exception when payload doesn't fit") {
@@ -182,11 +181,17 @@ object WireFrameCodecsTest : Spek({
.writeBytes(encodeSampleFrame())
buff.writerIndex(buff.writerIndex() - 2)
- assertThatExceptionOfType(MissingWireFrameBytesException::class.java)
- .isThrownBy { decoder.decodeFirst(buff) }
+ decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFramePayloadBytes::class.java) }
}
}
}
-}) \ No newline at end of file
+})
+
+private fun <A, B> Either<A, B>.assertFailedWithError(assertj: (ObjectAssert<A>) -> Unit) {
+ fold({ assertj(assertThat(it)) }, { fail("Error expected") })
+}
+
+private fun Either<WireFrameDecodingError, WireFrame>.getOrFail(): WireFrame =
+ fold({ fail(it.message) }, ::identity) as WireFrame