diff options
Diffstat (limited to 'hv-collector-xnf-simulator/src')
-rw-r--r-- | hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt (renamed from hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt) | 33 | ||||
-rw-r--r-- | hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt | 4 | ||||
-rw-r--r-- | hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt (renamed from hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt) | 77 |
3 files changed, 85 insertions, 29 deletions
diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt index 8954fd22..999d0327 100644 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt @@ -19,6 +19,11 @@ */ package org.onap.dcae.collectors.veshv.simulators.xnf.config +import arrow.core.ForOption +import arrow.core.Option +import arrow.core.fix +import arrow.instances.extensions +import arrow.typeclasses.binding import org.apache.commons.cli.CommandLine import org.apache.commons.cli.DefaultParser import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration @@ -30,7 +35,7 @@ import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.* * @author Jakub Dudycz <jakub.dudycz@nokia.com> * @since June 2018 */ -internal class ArgConfigurationProvider : ArgBasedConfiguration<SimulatorConfiguration>(DefaultParser()) { +internal class ArgXnfSimulatorConfiguration : ArgBasedConfiguration<SimulatorConfiguration>(DefaultParser()) { override val cmdLineOptionsList = listOf( VES_HV_PORT, VES_HV_HOST, @@ -41,17 +46,20 @@ internal class ArgConfigurationProvider : ArgBasedConfiguration<SimulatorConfigu TRUST_CERT_FILE ) - override fun getConfiguration(cmdLine: CommandLine): SimulatorConfiguration { - val vesHost = cmdLine.stringValue(VES_HV_HOST, DefaultValues.VES_HV_HOST) - val vesPort = cmdLine.intValue(VES_HV_PORT, DefaultValues.VES_HV_PORT) - val listenPort = cmdLine.intValue(LISTEN_PORT, DefaultValues.LISTEN_PORT) + override fun getConfiguration(cmdLine: CommandLine): Option<SimulatorConfiguration> = + ForOption extensions { + binding { + val listenPort = cmdLine.intValue(LISTEN_PORT).bind() + val vesHost = cmdLine.stringValue(VES_HV_HOST).bind() + val vesPort = cmdLine.intValue(VES_HV_PORT).bind() - return SimulatorConfiguration( - listenPort, - vesHost, - vesPort, - parseSecurityConfig(cmdLine)) - } + SimulatorConfiguration( + listenPort, + vesHost, + vesPort, + parseSecurityConfig(cmdLine)) + }.fix() + } private fun parseSecurityConfig(cmdLine: CommandLine): SecurityConfiguration { val sslDisable = cmdLine.hasOption(SSL_DISABLE) @@ -70,8 +78,5 @@ internal class ArgConfigurationProvider : ArgBasedConfiguration<SimulatorConfigu const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key" const val CERT_FILE = "/etc/ves-hv/client.crt" const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" - const val VES_HV_PORT = 6061 - const val VES_HV_HOST = "veshvcollector" - const val LISTEN_PORT = 6062 } } diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt index a47cef58..19c52efa 100644 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt @@ -19,7 +19,7 @@ */ package org.onap.dcae.collectors.veshv.simulators.xnf -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration import org.onap.dcae.collectors.veshv.simulators.xnf.impl.HttpServer import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure @@ -36,7 +36,7 @@ const val PROGRAM_NAME = "java $PACKAGE_NAME.MainKt" * @author Jakub Dudycz <jakub.dudycz@nokia.com> * @since June 2018 */ -fun main(args: Array<String>) = ArgConfigurationProvider().parse(args) +fun main(args: Array<String>) = ArgXnfSimulatorConfiguration().parse(args) .mapLeft(handleWrongArgumentErrorCurried(PROGRAM_NAME)) .map { XnfSimulator(it) diff --git a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt index 04d81c74..e3a20c70 100644 --- a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt +++ b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt @@ -20,6 +20,7 @@ package org.onap.dcae.collectors.veshv.main.config import arrow.core.identity +import org.assertj.core.api.Assertions import org.assertj.core.api.Assertions.assertThat import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe @@ -27,22 +28,25 @@ 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.domain.SecurityConfiguration -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider.DefaultValues +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration.DefaultValues import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError import java.nio.file.Paths import kotlin.test.assertTrue -object ArgConfigurationProviderTest : Spek({ - lateinit var cut: ArgConfigurationProvider - val vesHost = "localhosting" +object ArgXnfSimulatorConfiurationTest : Spek({ + lateinit var cut: ArgXnfSimulatorConfiguration + val listenPort = "4321" + val vesHost = "localhost" + val vesPort = "1234" val pk = Paths.get("/", "etc", "ves", "pk.pem") val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt") val trustCert = Paths.get("/", "etc", "ves", "trusted.crt") beforeEachTest { - cut = ArgConfigurationProvider() + cut = ArgXnfSimulatorConfiguration() } fun parse(vararg cmdLine: String): SimulatorConfiguration = @@ -51,6 +55,12 @@ object ArgConfigurationProviderTest : Spek({ ::identity ) + fun parseExpectingFailure(vararg cmdLine: String) = + cut.parse(cmdLine).fold( + ::identity, + { throw AssertionError("parsing should have failed") } + ) + describe("parsing arguments") { lateinit var result: SimulatorConfiguration @@ -58,15 +68,24 @@ object ArgConfigurationProviderTest : Spek({ beforeEachTest { result = parse("--ssl-disable", - "--ves-port", "6969", + "--listen-port", listenPort, "--ves-host", vesHost, + "--ves-port", vesPort, "--private-key-file", pk.toFile().absolutePath, "--cert-file", cert.toFile().absolutePath, "--trust-cert-file", trustCert.toFile().absolutePath) } - it("should set proper port") { - assertThat(result.vesPort).isEqualTo(6969) + it("should set proper listen port") { + assertThat(result.listenPort).isEqualTo(listenPort.toInt()) + } + + it("should set proper ves host") { + assertThat(result.vesHost).isEqualTo(vesHost) + } + + it("should set proper ves port") { + assertThat(result.vesPort).isEqualTo(vesPort.toInt()) } it("should set proper security configuration") { @@ -79,18 +98,26 @@ object ArgConfigurationProviderTest : Spek({ given("some parameters are present in the short form") { beforeEachTest { - result = parse("-h", "ves-hv", "--ves-port", "666") + result = parse("-p", listenPort, "-h", vesHost, "--ves-port", vesPort) + } + + it("should set proper listen port") { + assertThat(result.listenPort).isEqualTo(listenPort.toInt()) + } + + it("should set proper ves host") { + assertThat(result.vesHost).isEqualTo(vesHost) } - it("should set proper port") { - assertThat(result.vesPort).isEqualTo(666) + it("should set proper ves port") { + assertThat(result.vesPort).isEqualTo(vesPort.toInt()) } } given("all optional parameters are absent") { beforeEachTest { - result = parse("-h", "ves-hv", "-v", "666") + result = parse("-p", listenPort, "-h", vesHost, "-v", vesPort) } on("security config") { @@ -113,6 +140,7 @@ object ArgConfigurationProviderTest : Spek({ given("disabled ssl certs together with all other parameters") { beforeEachTest { result = parse("--ssl-disable", + "--listen-port", listenPort, "--ves-port", "888", "--ves-host", vesHost, "--private-key-file", pk.toFile().absolutePath, @@ -138,5 +166,28 @@ object ArgConfigurationProviderTest : Spek({ } } } + + describe("required parameter is absent") { + given("ves port is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-p", listenPort, "-h", vesHost)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + + given("ves host is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-p", listenPort, "-v", vesPort)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + + given("listen port is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-h", vesHost, "-v", vesPort)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + } } }) |