239f710269063458fff4ea57bc4db3012de12ef1
[dcaegen2/collectors/hv-ves.git] /
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.monadError
25 import arrow.typeclasses.bindingCatch
26 import org.onap.dcae.collectors.veshv.domain.ByteData
27 import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage
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.VesEventV5
34 import java.io.InputStream
35 import javax.json.Json
36
37 class MessageStreamValidation(
38         private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE,
39         private val messageGenerator: MessageGenerator = MessageGenerator.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         if (messageParams.isEmpty())
58             throw IllegalArgumentException("Message param list cannot be empty")
59
60         return messageParams
61     }
62
63     private fun shouldValidatePayloads(parameters: List<MessageParameters>) =
64             parameters.all { it.messageType == MessageType.FIXED_PAYLOAD }
65
66
67     private fun validateHeaders(actual: List<VesEventV5.VesEvent>, expected: List<VesEventV5.VesEvent>): Boolean {
68         val consumedHeaders = actual.map { it.commonEventHeader }
69         val generatedHeaders = expected.map { it.commonEventHeader }
70         return generatedHeaders == consumedHeaders
71     }
72
73
74     private fun generateEvents(parameters: List<MessageParameters>): IO<List<VesEventV5.VesEvent>> =
75             messageGenerator.createMessageFlux(parameters)
76                     .map(PayloadWireFrameMessage::payload)
77                     .map(ByteData::unsafeAsArray)
78                     .map(VesEventV5.VesEvent::parseFrom)
79                     .collectList()
80                     .asIo()
81
82     private fun decodeConsumedEvents(consumedMessages: List<ByteArray>) =
83             consumedMessages.map(VesEventV5.VesEvent::parseFrom)
84
85 }