From 4281a12d8e892f46f5f2226ee0f8aee8b862b177 Mon Sep 17 00:00:00 2001 From: kjaniak Date: Thu, 28 Mar 2019 10:43:59 +0100 Subject: Read HV-VES healthcheck api port from cmd line Change-Id: I6b4680a62512ef6ba15a0454e109b19619a997a6 Issue-ID: DCAEGEN2-1364 Signed-off-by: kjaniak --- .../veshv/config/api/ConfigurationModule.kt | 8 +- .../veshv/config/impl/ArgHvVesConfiguration.kt | 36 ------- .../veshv/config/impl/HvVesCommandLineParser.kt | 83 ++++++++++++++++ .../veshv/config/impl/ArgHvVesConfigurationTest.kt | 73 -------------- .../config/impl/HvVesCommandLineParserTest.kt | 107 +++++++++++++++++++++ 5 files changed, 195 insertions(+), 112 deletions(-) delete mode 100644 sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt create mode 100644 sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt delete mode 100644 sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt create mode 100644 sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt (limited to 'sources/hv-collector-configuration/src') diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt index efe0aa88..dd1b171e 100644 --- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt +++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt @@ -22,7 +22,7 @@ package org.onap.dcae.collectors.veshv.config.api import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration import org.onap.dcae.collectors.veshv.config.api.model.MissingArgumentException import org.onap.dcae.collectors.veshv.config.api.model.ValidationException -import org.onap.dcae.collectors.veshv.config.impl.ArgHvVesConfiguration +import org.onap.dcae.collectors.veshv.config.impl.HvVesCommandLineParser import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator import org.onap.dcae.collectors.veshv.config.impl.FileConfigurationReader import org.onap.dcae.collectors.veshv.utils.arrow.throwOnLeft @@ -30,14 +30,16 @@ import reactor.core.publisher.Flux class ConfigurationModule { - private val cmd = ArgHvVesConfiguration() + private val cmd = HvVesCommandLineParser() private val configReader = FileConfigurationReader() private val configValidator = ConfigurationValidator() private lateinit var initialConfig: HvVesConfiguration + fun healthCheckPort(args: Array): Int = cmd.getHealthcheckPort(args) + fun hvVesConfigurationUpdates(args: Array): Flux = - Flux.just(cmd.parse(args)) + Flux.just(cmd.getConfigurationFile(args)) .throwOnLeft { MissingArgumentException(it.message, it.cause) } .map { it.reader().use(configReader::loadConfig) } .map { configValidator.validate(it) } diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt deleted file mode 100644 index 9587d5b0..00000000 --- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2019 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.config.impl - -import arrow.core.Option -import org.apache.commons.cli.CommandLine -import org.apache.commons.cli.DefaultParser -import org.onap.dcae.collectors.veshv.commandline.ArgBasedConfiguration -import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE -import org.onap.dcae.collectors.veshv.commandline.stringValue -import java.io.File - -internal class ArgHvVesConfiguration : ArgBasedConfiguration(DefaultParser()) { - override val cmdLineOptionsList = listOf(CONFIGURATION_FILE) - - override fun getConfiguration(cmdLine: CommandLine): Option = - cmdLine.stringValue(CONFIGURATION_FILE).map(::File) - -} diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt new file mode 100644 index 00000000..3e93a400 --- /dev/null +++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2019 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.config.impl + +import arrow.core.* +import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.CommandLineParser +import org.apache.commons.cli.DefaultParser +import org.apache.commons.cli.Options +import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE +import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT +import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError +import org.onap.dcae.collectors.veshv.commandline.intValue +import org.onap.dcae.collectors.veshv.commandline.stringValue +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import java.io.File + +internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) { + private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT) + + private lateinit var parsedCommandLine: CommandLine + + fun getConfigurationFile(args: Array): Either = + parse(args) { + it.stringValue(CONFIGURATION_FILE).map(::File) + }.toEither { + WrongArgumentError( + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) + } + + fun getHealthcheckPort(args: Array): Int = + parse(args) { + it.intValue(HEALTH_CHECK_API_PORT) + }.getOrElse { + logger.info { "Healthcheck port missing on command line," + + " using default: $DEFAULT_HEALTHCHECK_PORT" } + DEFAULT_HEALTHCHECK_PORT + } + + private fun parse(args: Array, cmdLineMapper: (CommandLine) -> Option) = + Try { parseIfNotInitialized(args) } + .toOption() + .flatMap(cmdLineMapper) + + private fun parseIfNotInitialized(args: Array): CommandLine { + if (!this::parsedCommandLine.isInitialized) { + parsedCommandLine = parseArgumentsArray(args) + } + return parsedCommandLine + } + + private fun parseArgumentsArray(args: Array) = + cmdLineOptionsList + .map { it.option } + .fold(Options(), Options::addOption) + .let { parser.parse(it, args) } + + companion object { + private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060 + private val logger = Logger(HvVesCommandLineParser::class) + } + +} + + diff --git a/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt b/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt deleted file mode 100644 index dbe757c4..00000000 --- a/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt +++ /dev/null @@ -1,73 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018-2019 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.config.impl - -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.jetbrains.spek.api.dsl.on -import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError -import org.onap.dcae.collectors.veshv.tests.utils.absoluteResourcePath -import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure -import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess -import java.io.File - -/** - * @author Piotr Jaszczyk - * @since May 2018 - */ -object ArgVesHvConfigurationTest : Spek({ - lateinit var cut: ArgHvVesConfiguration - val configFilePath = javaClass.absoluteResourcePath("sampleConfig.json") - - beforeEachTest { - cut = ArgHvVesConfiguration() - } - - describe("parsing arguments") { - given("all parameters are present in the long form") { - lateinit var result: File - - beforeEachTest { - result = cut.parseExpectingSuccess( - "--configuration-file", configFilePath - ) - } - - it("should read proper configuration file") { - assertThat(result.exists()).isTrue() - } - } - - describe("required parameter is absent") { - on("missing configuration file path") { - it("should throw exception") { - assertThat( - cut.parseExpectingFailure( - "--non-existing-option", "" - ) - ).isInstanceOf(WrongArgumentError::class.java) - } - } - } - } -}) \ No newline at end of file diff --git a/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt b/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt new file mode 100644 index 00000000..0fdd41c9 --- /dev/null +++ b/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt @@ -0,0 +1,107 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018-2019 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.config.impl + +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.jetbrains.spek.api.dsl.on +import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError +import org.onap.dcae.collectors.veshv.tests.utils.absoluteResourcePath +import java.io.File + +/** + * @author Piotr Jaszczyk + * @since May 2018 + */ +object HvVesCommandLineParserTest : Spek({ + lateinit var cut: HvVesCommandLineParser + val DEFAULT_HEALTHCHECK_PORT = 6060 + val emptyConfig = "" + val configFilePath = javaClass.absoluteResourcePath("sampleConfig.json") + + beforeEachTest { + cut = HvVesCommandLineParser() + } + + describe("parsing arguments") { + given("all parameters are present in the long form") { + lateinit var result: File + + beforeEachTest { + result = cut.parseFileExpectingSuccess( + "--configuration-file", configFilePath + ) + } + + it("should read proper configuration file") { + assertThat(result.exists()).isTrue() + } + } + + given("required parameter is absent") { + on("missing configuration file path") { + it("should throw exception") { + assertThat( + cut.parseFileExpectingFailure( + "--non-existing-option", emptyConfig + ) + ).isInstanceOf(WrongArgumentError::class.java) + } + } + } + + given("healthcheck port defined via cmd") { + val healthCheckPort = 888 + val configWithHealthcheckPort = "--health-check-api-port $healthCheckPort" + on("parsing command") { + it("should assign proper port") { + assertThat( + cut.getHealthcheckPort(arrayOf(configWithHealthcheckPort)) + ).isEqualTo(healthCheckPort) + } + } + } + + given("no healthcheck port defined via cmd") { + on("parsing command") { + it("should return default port") { + assertThat( + cut.getHealthcheckPort(arrayOf(emptyConfig)) + ).isEqualTo(DEFAULT_HEALTHCHECK_PORT) + } + } + } + } +}) + +private fun HvVesCommandLineParser.parseFileExpectingSuccess(vararg cmdLine: String): File = + getConfigurationFile(cmdLine).fold( + { throw AssertionError("Parsing result should be present") }, + ::identity + ) + +private fun HvVesCommandLineParser.parseFileExpectingFailure(vararg cmdLine: String): WrongArgumentError = + getConfigurationFile(cmdLine).fold( + ::identity + ) { throw AssertionError("parsing should have failed") } \ No newline at end of file -- cgit 1.2.3-korg