5f54bbe3dd0aea531a592e1aa23768ba6e40355c
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avc / AvcEventConsumerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (c) 2023 Nordix Foundation.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.avc
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.ConsumerRecord
25 import org.apache.kafka.clients.consumer.KafkaConsumer
26 import org.apache.kafka.common.header.internals.RecordHeader
27 import org.mapstruct.factory.Mappers
28 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.avc.v1.AvcEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.test.annotation.DirtiesContext
37 import org.springframework.util.SerializationUtils
38 import org.testcontainers.spock.Testcontainers
39
40 import java.time.Duration
41
42 @SpringBootTest(classes = [EventsPublisher, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
43 @Testcontainers
44 @DirtiesContext
45 class AvcEventConsumerSpec extends MessagingBaseSpec {
46
47     @SpringBean
48     AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
49
50     @SpringBean
51     EventsPublisher eventsPublisher = new EventsPublisher<AvcEvent>(kafkaTemplate)
52
53     @SpringBean
54     AvcEventConsumer acvEventConsumer = new AvcEventConsumer(eventsPublisher, avcEventMapper)
55
56     @Autowired
57     JsonObjectMapper jsonObjectMapper
58
59     def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
60
61     def 'Consume and forward valid message'() {
62         given: 'consumer has a subscription on a topic'
63             def cmEventsTopicName = 'cm-events'
64             acvEventConsumer.cmEventsTopicName = cmEventsTopicName
65             kafkaConsumer.subscribe([cmEventsTopicName] as List<String>)
66         and: 'an event is sent'
67             def jsonData = TestUtils.getResourceFileContent('sampleAvcInputEvent.json')
68             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, AvcEvent.class)
69         and: 'event has header information'
70             def consumerRecord = new ConsumerRecord<String,AvcEvent>(cmEventsTopicName,0, 0, 'sample-eventid', testEventSent)
71             consumerRecord.headers().add(new RecordHeader('eventId', SerializationUtils.serialize('sample-eventid')))
72             consumerRecord.headers().add(new RecordHeader('eventCorrelationId', SerializationUtils.serialize('cmhandle1')))
73         when: 'the event is consumed'
74             acvEventConsumer.consumeAndForward(consumerRecord)
75         and: 'the topic is polled'
76             def records = kafkaConsumer.poll(Duration.ofMillis(1500))
77         then: 'poll returns one record'
78             assert records.size() == 1
79         and: 'record can be converted to AVC event'
80             def record = records.iterator().next()
81             def convertedAvcEvent = jsonObjectMapper.convertJsonString(record.value(), AvcEvent.class)
82         and: 'we have correct headers forwarded where correlation id matches'
83             record.headers().forEach(header -> {
84                 if (header.key().equals('eventCorrelationId')) {
85                     assert SerializationUtils.deserialize(header.value()) == 'cmhandle1'
86                 }
87             })
88         and: 'event id differs(as per requirement) between consumed and forwarded'
89             record.headers().forEach(header -> {
90                 if (header.key().equals('eventId')) {
91                     assert SerializationUtils.deserialize(header.value()) != 'sample-eventid'
92                 }
93             })
94         and: 'the event payload still matches'
95             assert testEventSent == convertedAvcEvent
96     }
97
98 }