diff options
author | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-29 13:24:59 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-09-06 07:43:30 +0200 |
commit | d6f5bfa934b9aa0571e853fc5432ab84eceb9db1 (patch) | |
tree | 2422451223385dba0a6b8f5714762a57cf6c002a /hv-collector-ves-message-generator/src | |
parent | 199edc49a418ab015ad3a54a5750f1a3f485b7e7 (diff) |
Improve coverage of xNF simulator
Also refactor to make it possible.
Change-Id: I6da6d3f33e57c524a7e353ecebd3e045d8ceed2a
Issue-ID: DCAEGEN2-739
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'hv-collector-ves-message-generator/src')
3 files changed, 26 insertions, 18 deletions
diff --git a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt index 060f28a2..754fa31f 100644 --- a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt @@ -19,11 +19,13 @@ */ package org.onap.dcae.collectors.veshv.ves.message.generator.api +import arrow.core.Either +import arrow.core.Option import org.onap.dcae.collectors.veshv.ves.message.generator.impl.MessageParametersParserImpl import javax.json.JsonArray interface MessageParametersParser { - fun parse(request: JsonArray): List<MessageParameters> + fun parse(request: JsonArray): Either<ParsingError, List<MessageParameters>> companion object { val INSTANCE: MessageParametersParser by lazy { @@ -31,3 +33,5 @@ interface MessageParametersParser { } } } + +data class ParsingError(val message: String, val cause: Option<Throwable>) diff --git a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt index 5b328f1c..f3095618 100644 --- a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt @@ -19,9 +19,12 @@ */ package org.onap.dcae.collectors.veshv.ves.message.generator.impl +import arrow.core.Option +import arrow.core.Try import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType +import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError import javax.json.JsonArray /** @@ -32,8 +35,8 @@ internal class MessageParametersParserImpl( private val commonEventHeaderParser: CommonEventHeaderParser = CommonEventHeaderParser() ) : MessageParametersParser { - override fun parse(request: JsonArray): List<MessageParameters> = - try { + override fun parse(request: JsonArray) = + Try { request .map { it.asJsonObject() } .map { @@ -41,13 +44,13 @@ internal class MessageParametersParserImpl( .parse(it.getJsonObject("commonEventHeader")) val messageType = MessageType.valueOf(it.getString("messageType")) val messagesAmount = it.getJsonNumber("messagesAmount")?.longValue() - ?: throw ParsingException("\"messagesAmount\" could not be parsed from message.", - NullPointerException()) + ?: throw NullPointerException("\"messagesAmount\" could not be parsed from message.") MessageParameters(commonEventHeader, messageType, messagesAmount) } - } catch (e: Exception) { - throw ParsingException("Parsing request body failed", e) + }.toEither().mapLeft { ex -> + ParsingError( + ex.message ?: "Unable to parse message parameters", + Option.fromNullable(ex)) } - internal class ParsingException(message: String, cause: Exception) : Exception(message, cause) } diff --git a/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt b/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt index 92561995..3b1a48b3 100644 --- a/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt +++ b/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt @@ -20,13 +20,12 @@ package org.onap.dcae.collectors.veshv.ves.message.generator.impl.impl import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.assertThatExceptionOfType +import org.assertj.core.api.Assertions.fail 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.jetbrains.spek.api.dsl.on -import org.onap.dcae.collectors.veshv.ves.message.generator.impl.MessageParametersParserImpl.ParsingException import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType import org.onap.dcae.collectors.veshv.ves.message.generator.impl.MessageParametersParserImpl @@ -45,18 +44,20 @@ object MessageParametersParserTest : Spek({ it("should parse MessagesParameters object successfully") { val result = messageParametersParser.parse(validMessagesParametesJson()) - assertThat(result).isNotNull - assertThat(result).hasSize(2) - val firstMessage = result.first() - assertThat(firstMessage.messageType).isEqualTo(MessageType.VALID) - assertThat(firstMessage.amount).isEqualTo(EXPECTED_MESSAGES_AMOUNT) + result.fold({ fail("should have succeeded") }) { rightResult -> + assertThat(rightResult).hasSize(2) + val firstMessage = rightResult.first() + assertThat(firstMessage.messageType).isEqualTo(MessageType.VALID) + assertThat(firstMessage.amount).isEqualTo(EXPECTED_MESSAGES_AMOUNT) + + } } } + on("invalid parameters json") { it("should throw exception") { - assertThatExceptionOfType(ParsingException::class.java).isThrownBy { - messageParametersParser.parse(invalidMessagesParametesJson()) - } + val result = messageParametersParser.parse(invalidMessagesParametesJson()) + assertThat(result.isLeft()).describedAs("is left").isTrue() } } } |