Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / hv-collector-dcae-app-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / dcaeapp / impl / MessageStreamValidation.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl
21
22 import arrow.effects.IO
23 import arrow.effects.fix
24 import arrow.effects.instances.io.monadError.monadError
25 import arrow.typeclasses.bindingCatch
26 import org.onap.dcae.collectors.veshv.domain.ByteData
27 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
28 import org.onap.dcae.collectors.veshv.utils.arrow.asIo
29 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType
33 import org.onap.ves.VesEventOuterClass
34 import java.io.InputStream
35 import javax.json.Json
36
37 class MessageStreamValidation(
38         private val messageGenerator: MessageGenerator,
39         private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) {
40
41     fun validate(jsonDescription: InputStream, consumedMessages: List<ByteArray>): IO<Boolean> =
42             IO.monadError().bindingCatch {
43                 val messageParams = parseMessageParams(jsonDescription)
44                 val expectedEvents = generateEvents(messageParams).bind()
45                 val actualEvents = decodeConsumedEvents(consumedMessages)
46                 if (shouldValidatePayloads(messageParams)) {
47                     expectedEvents == actualEvents
48                 } else {
49                     validateHeaders(actualEvents, expectedEvents)
50                 }
51             }.fix()
52
53     private fun parseMessageParams(input: InputStream): List<MessageParameters> {
54         val expectations = Json.createReader(input).readArray()
55         val messageParams = messageParametersParser.parse(expectations)
56
57         return messageParams.fold(
58                 { throw IllegalArgumentException("Parsing error: " + it.message) },
59                 {
60                     if (it.isEmpty())
61                         throw IllegalArgumentException("Message param list cannot be empty")
62                     it
63                 }
64         )
65     }
66
67     private fun shouldValidatePayloads(parameters: List<MessageParameters>) =
68             parameters.all { it.messageType == MessageType.FIXED_PAYLOAD }
69
70     private fun validateHeaders(actual: List<VesEventOuterClass.VesEvent>,
71                                 expected: List<VesEventOuterClass.VesEvent>): Boolean {
72         val consumedHeaders = actual.map { it.commonEventHeader }
73         val generatedHeaders = expected.map { it.commonEventHeader }
74         return generatedHeaders == consumedHeaders
75     }
76
77     private fun generateEvents(parameters: List<MessageParameters>): IO<List<VesEventOuterClass.VesEvent>> =
78             messageGenerator.createMessageFlux(parameters)
79                     .map(WireFrameMessage::payload)
80                     .map(ByteData::unsafeAsArray)
81                     .map(VesEventOuterClass.VesEvent::parseFrom)
82                     .collectList()
83                     .asIo()
84
85     private fun decodeConsumedEvents(consumedMessages: List<ByteArray>) =
86             consumedMessages.map(VesEventOuterClass.VesEvent::parseFrom)
87
88 }