aboutsummaryrefslogtreecommitdiffstats
path: root/hv-collector-dcae-app-simulator/src/main
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-29 13:24:59 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-09-06 07:43:30 +0200
commitd6f5bfa934b9aa0571e853fc5432ab84eceb9db1 (patch)
tree2422451223385dba0a6b8f5714762a57cf6c002a /hv-collector-dcae-app-simulator/src/main
parent199edc49a418ab015ad3a54a5750f1a3f485b7e7 (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-dcae-app-simulator/src/main')
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/MessageStreamValidation.kt12
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/DcaeAppApiServer.kt (renamed from hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/ApiServer.kt)92
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt9
3 files changed, 44 insertions, 69 deletions
diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/MessageStreamValidation.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/MessageStreamValidation.kt
index 239f7102..354edaeb 100644
--- a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/MessageStreamValidation.kt
+++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/MessageStreamValidation.kt
@@ -54,10 +54,14 @@ class MessageStreamValidation(
val expectations = Json.createReader(input).readArray()
val messageParams = messageParametersParser.parse(expectations)
- if (messageParams.isEmpty())
- throw IllegalArgumentException("Message param list cannot be empty")
-
- return messageParams
+ return messageParams.fold(
+ { throw IllegalArgumentException("Parsing error: " + it.message) },
+ {
+ if (it.isEmpty())
+ throw IllegalArgumentException("Message param list cannot be empty")
+ it
+ }
+ )
}
private fun shouldValidatePayloads(parameters: List<MessageParameters>) =
diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/ApiServer.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/DcaeAppApiServer.kt
index 6c830b9d..1eca9317 100644
--- a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/ApiServer.kt
+++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/DcaeAppApiServer.kt
@@ -19,18 +19,14 @@
*/
package org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.adapters
-import arrow.core.Left
-import arrow.core.Right
import arrow.effects.IO
-import arrow.effects.fix
-import arrow.effects.monad
-import arrow.typeclasses.binding
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.DcaeAppSimulator
-import org.onap.dcae.collectors.veshv.utils.logging.Logger
-import ratpack.exec.Promise
+import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
+import org.onap.dcae.collectors.veshv.utils.http.HttpStatus
+import org.onap.dcae.collectors.veshv.utils.http.Responses
+import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
+import org.onap.dcae.collectors.veshv.utils.http.sendOrError
import ratpack.handling.Chain
-import ratpack.handling.Context
-import ratpack.http.Response
import ratpack.server.RatpackServer
import ratpack.server.ServerConfig
@@ -38,7 +34,21 @@ import ratpack.server.ServerConfig
* @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
* @since May 2018
*/
-class ApiServer(private val simulator: DcaeAppSimulator) {
+class DcaeAppApiServer(private val simulator: DcaeAppSimulator) {
+ private val responseValid by lazy {
+ Responses.statusResponse(
+ name = "valid",
+ message = "validation succeeded"
+ )
+ }
+
+ private val responseInvalid by lazy {
+ Responses.statusResponse(
+ name = "invalid",
+ message = "validation failed",
+ httpStatus = HttpStatus.BAD_REQUEST
+ )
+ }
fun start(port: Int, kafkaTopics: Set<String>): IO<RatpackServer> =
@@ -52,10 +62,10 @@ class ApiServer(private val simulator: DcaeAppSimulator) {
private fun setupHandlers(chain: Chain) {
chain
.put("configuration/topics") { ctx ->
- val operation = ctx.bodyIo().flatMap { body ->
- simulator.listenToTopics(body.text)
+ ctx.request.body.then { body ->
+ val operation = simulator.listenToTopics(body.text)
+ ctx.response.sendOrError(operation)
}
- ctx.response.sendOrError(operation)
}
.delete("messages") { ctx ->
@@ -64,7 +74,7 @@ class ApiServer(private val simulator: DcaeAppSimulator) {
}
.get("messages/all/count") { ctx ->
simulator.state().fold(
- { ctx.response.status(STATUS_NOT_FOUND) },
+ { ctx.response.status(HttpConstants.STATUS_NOT_FOUND) },
{
ctx.response
.contentType(CONTENT_TEXT)
@@ -72,58 +82,20 @@ class ApiServer(private val simulator: DcaeAppSimulator) {
})
}
.post("messages/all/validate") { ctx ->
- val responseStatus = IO.monad().binding {
- val body = ctx.bodyIo().bind()
- val isValid = simulator.validate(body.inputStream).bind()
- if (isValid)
- STATUS_OK
- else
- STATUS_BAD_REQUEST
- }.fix()
-
- ctx.response.sendStatusOrError(responseStatus)
+ ctx.request.body.then { body ->
+ val response = simulator.validate(body.inputStream)
+ .map { isValid ->
+ if (isValid) responseValid else responseInvalid
+ }
+ ctx.response.sendAndHandleErrors(response)
+ }
}
.get("healthcheck") { ctx ->
- ctx.response.status(STATUS_OK).send()
+ ctx.response.status(HttpConstants.STATUS_OK).send()
}
}
- private fun Context.bodyIo() = request.body.asIo()
-
- private fun <T> Promise<T>.asIo(): IO<T> = IO.async { emitResult ->
- onError {
- emitResult(Left(it))
- }.then { result ->
- emitResult(Right(result))
- }
- }
-
- private fun Response.sendOrError(responseStatus: IO<Unit>) {
- sendStatusOrError(responseStatus.map { STATUS_OK })
- }
-
- private fun Response.sendStatusOrError(responseStatus: IO<Int>) {
- responseStatus.unsafeRunAsync { cb ->
- cb.fold(
- { err ->
- logger.warn("Error occurred. Sending HTTP$STATUS_INTERNAL_SERVER_ERROR.", err)
- status(ApiServer.STATUS_INTERNAL_SERVER_ERROR)
- .send(CONTENT_TEXT, err.message)
- },
- {
- status(it).send()
- }
- )
- }
- }
-
companion object {
- private val logger = Logger(ApiServer::class)
private const val CONTENT_TEXT = "text/plain"
-
- private const val STATUS_OK = 200
- private const val STATUS_BAD_REQUEST = 400
- private const val STATUS_NOT_FOUND = 404
- private const val STATUS_INTERNAL_SERVER_ERROR = 500
}
}
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 a65a2686..c0f8b340 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
@@ -24,11 +24,10 @@ import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.config.ArgDcaeAppS
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.config.DcaeAppSimConfiguration
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.ConsumerFactory
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.DcaeAppSimulator
-import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.MessageStreamValidation
-import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.adapters.ApiServer
+import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.adapters.DcaeAppApiServer
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.arrow.unit
import org.onap.dcae.collectors.veshv.utils.commandline.handleWrongArgumentErrorCurried
import org.onap.dcae.collectors.veshv.utils.logging.Logger
@@ -52,7 +51,7 @@ fun main(args: Array<String>) =
private fun startApp(config: DcaeAppSimConfiguration): IO<Unit> {
- return ApiServer(DcaeAppSimulator(ConsumerFactory(config.kafkaBootstrapServers)))
+ return DcaeAppApiServer(DcaeAppSimulator(ConsumerFactory(config.kafkaBootstrapServers)))
.start(config.apiPort, config.kafkaTopics)
- .void()
+ .unit()
}