From 63f1796ec022705504742a5bd1700f9e20c33540 Mon Sep 17 00:00:00 2001 From: fkrzywka Date: Tue, 31 Jul 2018 14:22:59 +0200 Subject: Move MessageParametersParser To avoid dependency of utilities module on modules other than domain Change-Id: I90ef8640a86501315d84848118d3e748aafd095c Signed-off-by: fkrzywka Issue-ID: DCAEGEN2-601 --- .../veshv/simulators/dcaeapp/remote/ApiServer.kt | 4 +- hv-collector-utils/pom.xml | 6 +- .../utils/messages/CommonEventHeaderParser.kt | 52 ------------ .../utils/messages/MessageParametersParser.kt | 51 ----------- .../utils/messages/MessageParametersParserTest.kt | 63 -------------- .../collectors/veshv/utils/messages/parameters.kt | 98 ---------------------- hv-collector-ves-message-generator/pom.xml | 4 + .../generator/api/MessageParametersParser.kt | 33 ++++++++ .../generator/impl/CommonEventHeaderParser.kt | 52 ++++++++++++ .../generator/impl/MessageParametersParserImpl.kt | 53 ++++++++++++ .../impl/impl/MessageParametersParserTest.kt | 64 ++++++++++++++ .../ves/message/generator/impl/impl/parameters.kt | 98 ++++++++++++++++++++++ .../veshv/simulators/xnf/impl/HttpServer.kt | 4 +- 13 files changed, 309 insertions(+), 273 deletions(-) delete mode 100644 hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/CommonEventHeaderParser.kt delete mode 100644 hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParser.kt delete mode 100644 hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParserTest.kt delete mode 100644 hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/parameters.kt create mode 100644 hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt create mode 100644 hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt create mode 100644 hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt create mode 100644 hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt create mode 100644 hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/parameters.kt diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/remote/ApiServer.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/remote/ApiServer.kt index 79f143ee..cd258134 100644 --- a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/remote/ApiServer.kt +++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/remote/ApiServer.kt @@ -24,7 +24,7 @@ import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage import org.onap.dcae.collectors.veshv.simulators.dcaeapp.kafka.ConsumerFactory import org.onap.dcae.collectors.veshv.simulators.dcaeapp.kafka.ConsumerStateProvider import org.onap.dcae.collectors.veshv.utils.logging.Logger -import org.onap.dcae.collectors.veshv.utils.messages.MessageParametersParser +import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.FIXED_PAYLOAD @@ -41,7 +41,7 @@ import javax.json.Json * @since May 2018 */ class ApiServer(private val consumerFactory: ConsumerFactory, - private val messageParametersParser: MessageParametersParser = MessageParametersParser()) { + private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) { private lateinit var consumerState: ConsumerStateProvider diff --git a/hv-collector-utils/pom.xml b/hv-collector-utils/pom.xml index 39097c10..9b21d751 100644 --- a/hv-collector-utils/pom.xml +++ b/hv-collector-utils/pom.xml @@ -61,7 +61,7 @@ ${project.parent.groupId} - hv-collector-ves-message-generator + hv-collector-domain ${project.parent.version} @@ -88,10 +88,6 @@ org.slf4j slf4j-api - - org.glassfish - javax.json - com.nhaarman mockito-kotlin diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/CommonEventHeaderParser.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/CommonEventHeaderParser.kt deleted file mode 100644 index d115675d..00000000 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/CommonEventHeaderParser.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018 NOKIA - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.collectors.veshv.utils.messages - -import com.google.protobuf.ByteString -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Priority -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.newBuilder -import javax.json.JsonObject - -/** - * @author Jakub Dudycz - * @since July 2018 - */ -class CommonEventHeaderParser { - fun parse(json: JsonObject): CommonEventHeader = newBuilder() - .setVersion(json.getString("version")) - .setDomain(Domain.valueOf(json.getString("domain"))) - .setSequence(json.getInt("sequence")) - .setPriority(Priority.forNumber(json.getInt("priority"))) - .setEventId(json.getString("version")) - .setEventName(json.getString("version")) - .setEventType(json.getString("version")) - .setStartEpochMicrosec(json.getJsonNumber("startEpochMicrosec").longValue()) - .setLastEpochMicrosec(json.getJsonNumber("lastEpochMicrosec").longValue()) - .setNfNamingCode(json.getString("nfNamingCode")) - .setNfcNamingCode(json.getString("nfcNamingCode")) - .setReportingEntityId(json.getString("reportingEntityId")) - .setReportingEntityName(ByteString.copyFromUtf8(json.getString("reportingEntityName"))) - .setSourceId(ByteString.copyFromUtf8(json.getString("sourceId"))) - .setSourceName(json.getString("sourceName")) - .build() - -} diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParser.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParser.kt deleted file mode 100644 index 1621ba59..00000000 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParser.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018 NOKIA - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.collectors.veshv.utils.messages - -import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters -import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType -import javax.json.JsonArray - -/** - * @author Jakub Dudycz - * @since July 2018 - */ -class MessageParametersParser( - private val commonEventHeaderParser: CommonEventHeaderParser = CommonEventHeaderParser()) { - - fun parse(request: JsonArray): List = - try { - request - .map { it.asJsonObject() } - .map { - val commonEventHeader = commonEventHeaderParser - .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()) - MessageParameters(commonEventHeader, messageType, messagesAmount) - } - } catch (e: Exception) { - throw ParsingException("Parsing request body failed", e) - } - - internal class ParsingException(message: String, cause: Exception) : Exception(message, cause) -} diff --git a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParserTest.kt b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParserTest.kt deleted file mode 100644 index ec628a2a..00000000 --- a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/MessageParametersParserTest.kt +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018 NOKIA - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.collectors.veshv.utils.messages - -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.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.utils.messages.MessageParametersParser.ParsingException -import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType - -private const val EXPECTED_MESSAGES_AMOUNT = 25000L - -/** - * @author Jakub Dudycz - * @since July 2018 - */ -object MessageParametersParserTest : Spek({ - describe("Messages parameters parser") { - val messageParametersParser = MessageParametersParser() - - given("parameters json array") { - on("valid parameters json") { - 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) - } - } - on("invalid parameters json") { - it("should throw exception") { - assertThatExceptionOfType(ParsingException::class.java).isThrownBy { - messageParametersParser.parse(invalidMessagesParametesJson()) - } - } - } - } - } -}) diff --git a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/parameters.kt b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/parameters.kt deleted file mode 100644 index f6a3a15b..00000000 --- a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/messages/parameters.kt +++ /dev/null @@ -1,98 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018 NOKIA - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.collectors.veshv.utils.messages - -import javax.json.Json - -private const val validMessageParameters = "[\n" + - " {\n" + - " \"commonEventHeader\": {\n" + - " \"version\": \"sample-version\",\n" + - " \"domain\": \"HVRANMEAS\",\n" + - " \"sequence\": 1,\n" + - " \"priority\": 1,\n" + - " \"eventId\": \"sample-event-id\",\n" + - " \"eventName\": \"sample-event-name\",\n" + - " \"eventType\": \"sample-event-type\",\n" + - " \"startEpochMicrosec\": 120034455,\n" + - " \"lastEpochMicrosec\": 120034455,\n" + - " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + - " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + - " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + - " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + - " \"sourceId\": \"sample-source-id\",\n" + - " \"sourceName\": \"sample-source-name\"\n" + - " },\n" + - " \"messageType\": \"VALID\",\n" + - " \"messagesAmount\": 25000\n" + - " },\n" + - " {\n" + - " \"commonEventHeader\": {\n" + - " \"version\": \"sample-version\",\n" + - " \"domain\": \"HVRANMEAS\",\n" + - " \"sequence\": 1,\n" + - " \"priority\": 1,\n" + - " \"eventId\": \"sample-event-id\",\n" + - " \"eventName\": \"sample-event-name\",\n" + - " \"eventType\": \"sample-event-type\",\n" + - " \"startEpochMicrosec\": 120034455,\n" + - " \"lastEpochMicrosec\": 120034455,\n" + - " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + - " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + - " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + - " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + - " \"sourceId\": \"sample-source-id\",\n" + - " \"sourceName\": \"sample-source-name\"\n" + - " },\n" + - " \"messageType\": \"TOO_BIG_PAYLOAD\",\n" + - " \"messagesAmount\": 100\n" + - " }\n" + - "]" - -private const val invalidMessageParameters = "[\n" + - " {\n" + - " \"commonEventHeader\": {\n" + - " \"version\": \"sample-version\",\n" + - " \"domain\": \"HVRANMEAS\",\n" + - " \"sequence\": 1,\n" + - " \"priority\": 1,\n" + - " \"eventId\": \"sample-event-id\",\n" + - " \"eventName\": \"sample-event-name\",\n" + - " \"eventType\": \"sample-event-type\",\n" + - " \"startEpochMicrosec\": 120034455,\n" + - " \"lastEpochMicrosec\": 120034455,\n" + - " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + - " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + - " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + - " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + - " \"sourceId\": \"sample-source-id\",\n" + - " \"sourceName\": \"sample-source-name\"\n" + - " },\n" + - " \"messagesAmount\": 3\n" + - " }\n" + - "]" - -fun validMessagesParametesJson() = Json - .createReader(validMessageParameters.reader()) - .readArray() - -fun invalidMessagesParametesJson() = Json - .createReader(invalidMessageParameters.reader()) - .readArray() diff --git a/hv-collector-ves-message-generator/pom.xml b/hv-collector-ves-message-generator/pom.xml index a7dad24b..f049d78f 100644 --- a/hv-collector-ves-message-generator/pom.xml +++ b/hv-collector-ves-message-generator/pom.xml @@ -95,6 +95,10 @@ logback-classic runtime + + org.glassfish + javax.json + 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 new file mode 100644 index 00000000..060f28a2 --- /dev/null +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/api/MessageParametersParser.kt @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.ves.message.generator.api + +import org.onap.dcae.collectors.veshv.ves.message.generator.impl.MessageParametersParserImpl +import javax.json.JsonArray + +interface MessageParametersParser { + fun parse(request: JsonArray): List + + companion object { + val INSTANCE: MessageParametersParser by lazy { + MessageParametersParserImpl() + } + } +} diff --git a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt new file mode 100644 index 00000000..e5977fb7 --- /dev/null +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.ves.message.generator.impl + +import com.google.protobuf.ByteString +import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader +import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain +import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Priority +import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.newBuilder +import javax.json.JsonObject + +/** + * @author Jakub Dudycz + * @since July 2018 + */ +class CommonEventHeaderParser { + fun parse(json: JsonObject): CommonEventHeader = newBuilder() + .setVersion(json.getString("version")) + .setDomain(Domain.valueOf(json.getString("domain"))) + .setSequence(json.getInt("sequence")) + .setPriority(Priority.forNumber(json.getInt("priority"))) + .setEventId(json.getString("version")) + .setEventName(json.getString("version")) + .setEventType(json.getString("version")) + .setStartEpochMicrosec(json.getJsonNumber("startEpochMicrosec").longValue()) + .setLastEpochMicrosec(json.getJsonNumber("lastEpochMicrosec").longValue()) + .setNfNamingCode(json.getString("nfNamingCode")) + .setNfcNamingCode(json.getString("nfcNamingCode")) + .setReportingEntityId(json.getString("reportingEntityId")) + .setReportingEntityName(ByteString.copyFromUtf8(json.getString("reportingEntityName"))) + .setSourceId(ByteString.copyFromUtf8(json.getString("sourceId"))) + .setSourceName(json.getString("sourceName")) + .build() + +} 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 new file mode 100644 index 00000000..5b328f1c --- /dev/null +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt @@ -0,0 +1,53 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.ves.message.generator.impl + +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 javax.json.JsonArray + +/** + * @author Jakub Dudycz + * @since July 2018 + */ +internal class MessageParametersParserImpl( + private val commonEventHeaderParser: CommonEventHeaderParser = CommonEventHeaderParser() +) : MessageParametersParser { + + override fun parse(request: JsonArray): List = + try { + request + .map { it.asJsonObject() } + .map { + val commonEventHeader = commonEventHeaderParser + .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()) + MessageParameters(commonEventHeader, messageType, messagesAmount) + } + } catch (e: Exception) { + throw ParsingException("Parsing request body failed", e) + } + + 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 new file mode 100644 index 00000000..92561995 --- /dev/null +++ b/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/MessageParametersParserTest.kt @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +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.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 + +private const val EXPECTED_MESSAGES_AMOUNT = 25000L + +/** + * @author Jakub Dudycz + * @since July 2018 + */ +object MessageParametersParserTest : Spek({ + describe("Messages parameters parser") { + val messageParametersParser = MessageParametersParserImpl() + + given("parameters json array") { + on("valid parameters json") { + 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) + } + } + on("invalid parameters json") { + it("should throw exception") { + assertThatExceptionOfType(ParsingException::class.java).isThrownBy { + messageParametersParser.parse(invalidMessagesParametesJson()) + } + } + } + } + } +}) diff --git a/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/parameters.kt b/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/parameters.kt new file mode 100644 index 00000000..88550603 --- /dev/null +++ b/hv-collector-ves-message-generator/src/test/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/impl/parameters.kt @@ -0,0 +1,98 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.ves.message.generator.impl.impl + +import javax.json.Json + +private const val validMessageParameters = "[\n" + + " {\n" + + " \"commonEventHeader\": {\n" + + " \"version\": \"sample-version\",\n" + + " \"domain\": \"HVRANMEAS\",\n" + + " \"sequence\": 1,\n" + + " \"priority\": 1,\n" + + " \"eventId\": \"sample-event-id\",\n" + + " \"eventName\": \"sample-event-name\",\n" + + " \"eventType\": \"sample-event-type\",\n" + + " \"startEpochMicrosec\": 120034455,\n" + + " \"lastEpochMicrosec\": 120034455,\n" + + " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + + " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + + " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + + " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + + " \"sourceId\": \"sample-source-id\",\n" + + " \"sourceName\": \"sample-source-name\"\n" + + " },\n" + + " \"messageType\": \"VALID\",\n" + + " \"messagesAmount\": 25000\n" + + " },\n" + + " {\n" + + " \"commonEventHeader\": {\n" + + " \"version\": \"sample-version\",\n" + + " \"domain\": \"HVRANMEAS\",\n" + + " \"sequence\": 1,\n" + + " \"priority\": 1,\n" + + " \"eventId\": \"sample-event-id\",\n" + + " \"eventName\": \"sample-event-name\",\n" + + " \"eventType\": \"sample-event-type\",\n" + + " \"startEpochMicrosec\": 120034455,\n" + + " \"lastEpochMicrosec\": 120034455,\n" + + " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + + " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + + " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + + " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + + " \"sourceId\": \"sample-source-id\",\n" + + " \"sourceName\": \"sample-source-name\"\n" + + " },\n" + + " \"messageType\": \"TOO_BIG_PAYLOAD\",\n" + + " \"messagesAmount\": 100\n" + + " }\n" + + "]" + +private const val invalidMessageParameters = "[\n" + + " {\n" + + " \"commonEventHeader\": {\n" + + " \"version\": \"sample-version\",\n" + + " \"domain\": \"HVRANMEAS\",\n" + + " \"sequence\": 1,\n" + + " \"priority\": 1,\n" + + " \"eventId\": \"sample-event-id\",\n" + + " \"eventName\": \"sample-event-name\",\n" + + " \"eventType\": \"sample-event-type\",\n" + + " \"startEpochMicrosec\": 120034455,\n" + + " \"lastEpochMicrosec\": 120034455,\n" + + " \"nfNamingCode\": \"sample-nf-naming-code\",\n" + + " \"nfcNamingCode\": \"sample-nfc-naming-code\",\n" + + " \"reportingEntityId\": \"sample-reporting-entity-id\",\n" + + " \"reportingEntityName\": \"sample-reporting-entity-name\",\n" + + " \"sourceId\": \"sample-source-id\",\n" + + " \"sourceName\": \"sample-source-name\"\n" + + " },\n" + + " \"messagesAmount\": 3\n" + + " }\n" + + "]" + +fun validMessagesParametesJson() = Json + .createReader(validMessageParameters.reader()) + .readArray() + +fun invalidMessagesParametesJson() = Json + .createReader(invalidMessageParameters.reader()) + .readArray() diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt index e6025734..3fb4be99 100644 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt @@ -21,8 +21,8 @@ package org.onap.dcae.collectors.veshv.simulators.xnf.impl import arrow.effects.IO import org.onap.dcae.collectors.veshv.utils.logging.Logger -import org.onap.dcae.collectors.veshv.utils.messages.MessageParametersParser import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator +import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser import ratpack.handling.Chain import ratpack.handling.Context import ratpack.server.RatpackServer @@ -35,7 +35,7 @@ import javax.json.Json * @since June 2018 */ internal class HttpServer(private val vesClient: XnfSimulator, - private val messageParametersParser: MessageParametersParser = MessageParametersParser()) { + private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) { fun start(port: Int): IO = IO { RatpackServer.start { server -> -- cgit 1.2.3-korg