diff options
author | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-07-04 13:16:21 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-02 12:01:26 +0200 |
commit | e31d59f63ebe5536d2d2d868703eb8896924b63d (patch) | |
tree | 0ed280beb287c59fbd7a3494684884f5738e377d /hv-collector-dcae-app-simulator | |
parent | e5ce5ac06cf1ce95a65f18ad05be9e8432be6d49 (diff) |
Use IO monad when starting servers
Change-Id: I3e97161535fc721dda6109c4cb5f23a1db0afde3
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-dcae-app-simulator')
2 files changed, 35 insertions, 30 deletions
diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt index f7d44ede..a8a4cf5a 100644 --- a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt +++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt @@ -19,30 +19,38 @@ */ package org.onap.dcae.collectors.veshv.simulators.dcaeapp -import arrow.core.Failure -import arrow.core.Success import org.onap.dcae.collectors.veshv.simulators.dcaeapp.config.ArgBasedDcaeAppSimConfiguration import org.onap.dcae.collectors.veshv.simulators.dcaeapp.config.DcaeAppSimConfiguration import org.onap.dcae.collectors.veshv.simulators.dcaeapp.kafka.KafkaSource import org.onap.dcae.collectors.veshv.simulators.dcaeapp.remote.ApiServer -import org.onap.dcae.collectors.veshv.utils.commandline.handleErrorsInMain +import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure +import org.onap.dcae.collectors.veshv.utils.arrow.unsafeRunEitherSync +import org.onap.dcae.collectors.veshv.utils.arrow.void +import org.onap.dcae.collectors.veshv.utils.commandline.handleWrongArgumentErrorCurried import org.onap.dcae.collectors.veshv.utils.logging.Logger import org.slf4j.LoggerFactory -private val logger = Logger(LoggerFactory.getLogger("DCAE simulator :: main")) +private const val PACKAGE_NAME = "org.onap.dcae.collectors.veshv.simulators.dcaeapp" +private val logger = Logger(PACKAGE_NAME) +const val PROGRAM_NAME = "java $PACKAGE_NAME.MainKt" -fun main(args: Array<String>) { - logger.info("Starting DCAE APP simulator") +fun main(args: Array<String>) = + ArgBasedDcaeAppSimConfiguration().parse(args) + .mapLeft(handleWrongArgumentErrorCurried(PROGRAM_NAME)) + .map(::startApp) + .unsafeRunEitherSync( + { ex -> + logger.error("Failed to start a server", ex) + ExitFailure(1) + }, + { + logger.info("Started DCAE-APP Simulator API server") + } + ) - val config = ArgBasedDcaeAppSimConfiguration().parse(args) - when (config) { - is Success -> startApp(config.value).unsafeRunSync() - is Failure -> config.handleErrorsInMain("", logger) - } -} private fun startApp(config: DcaeAppSimConfiguration) = KafkaSource.create(config.kafkaBootstrapServers, config.kafkaTopics) .start() .map(::ApiServer) - .flatMap { it.start(config.apiPort) } + .flatMap { it.start(config.apiPort).void() } diff --git a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt index d99de17b..b73a788e 100644 --- a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt +++ b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt @@ -21,12 +21,13 @@ package org.onap.dcae.collectors.veshv.simulators.dcaeapp.config import arrow.core.Failure import arrow.core.Success +import arrow.core.identity import org.assertj.core.api.Assertions.assertThat 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.utils.commandline.WrongArgumentException +import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError internal class ArgBasedDcaeAppSimConfigurationTest : Spek({ @@ -39,21 +40,17 @@ internal class ArgBasedDcaeAppSimConfigurationTest : Spek({ cut = ArgBasedDcaeAppSimConfiguration() } - fun parseExpectingSuccess(vararg cmdLine: String): DcaeAppSimConfiguration { - val result = cut.parse(cmdLine) - return when (result) { - is Success -> result.value - is Failure -> throw AssertionError("Parsing result should be present") - } - } + fun parseExpectingSuccess(vararg cmdLine: String): DcaeAppSimConfiguration = + cut.parse(cmdLine).fold( + { throw AssertionError("Parsing result should be present") }, + ::identity + ) - fun parseExpectingFailure(vararg cmdLine: String): Throwable { - val result = cut.parse(cmdLine) - return when (result) { - is Success -> throw AssertionError("parsing should have failed") - is Failure -> result.exception - } - } + fun parseExpectingFailure(vararg cmdLine: String) = + cut.parse(cmdLine).fold( + ::identity, + { throw AssertionError("parsing should have failed") } + ) describe("parsing arguments") { lateinit var result: DcaeAppSimConfiguration @@ -121,14 +118,14 @@ internal class ArgBasedDcaeAppSimConfigurationTest : Spek({ given("kafka topics are missing") { it("should throw exception") { assertThat(parseExpectingFailure("-s", kafkaBootstrapServers)) - .isInstanceOf(WrongArgumentException::class.java) + .isInstanceOf(WrongArgumentError::class.java) } } given("kafka bootstrap servers are missing") { it("should throw exception") { assertThat(parseExpectingFailure("-f", kafkaTopics)) - .isInstanceOf(WrongArgumentException::class.java) + .isInstanceOf(WrongArgumentError::class.java) } } } |