Merge "Replace deprecated WebSecurityConfigurerAdapter"
[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.apache.kafka.common.serialization.StringDeserializer
28 import org.mapstruct.factory.Mappers
29 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
30 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
31 import org.onap.cps.ncmp.events.avc.v1.AvcEvent
32 import org.onap.cps.ncmp.utils.TestUtils
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.test.context.SpringBootTest
37 import org.springframework.test.annotation.DirtiesContext
38 import org.springframework.util.SerializationUtils
39 import org.testcontainers.spock.Testcontainers
40
41 import java.time.Duration
42
43 @SpringBootTest(classes = [EventsPublisher, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
44 @Testcontainers
45 @DirtiesContext
46 class AvcEventConsumerSpec extends MessagingBaseSpec {
47
48     @SpringBean
49     AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
50
51     @SpringBean
52     EventsPublisher eventsPublisher = new EventsPublisher<AvcEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
53
54     @SpringBean
55     AvcEventConsumer acvEventConsumer = new AvcEventConsumer(eventsPublisher, avcEventMapper)
56
57     @Autowired
58     JsonObjectMapper jsonObjectMapper
59
60     def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('ncmp-group', StringDeserializer))
61
62     def 'Consume and forward valid message'() {
63         given: 'consumer has a subscription on a topic'
64             def cmEventsTopicName = 'cm-events'
65             acvEventConsumer.cmEventsTopicName = cmEventsTopicName
66             legacyEventKafkaConsumer.subscribe([cmEventsTopicName] as List<String>)
67         and: 'an event is sent'
68             def jsonData = TestUtils.getResourceFileContent('sampleAvcInputEvent.json')
69             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, AvcEvent.class)
70         and: 'event has header information'
71             def consumerRecord = new ConsumerRecord<String,AvcEvent>(cmEventsTopicName,0, 0, 'sample-eventid', testEventSent)
72             consumerRecord.headers().add(new RecordHeader('eventId', SerializationUtils.serialize('sample-eventid')))
73             consumerRecord.headers().add(new RecordHeader('eventCorrelationId', SerializationUtils.serialize('cmhandle1')))
74         when: 'the event is consumed'
75             acvEventConsumer.consumeAndForward(consumerRecord)
76         and: 'the topic is polled'
77             def records = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))
78         then: 'poll returns one record'
79             assert records.size() == 1
80         and: 'record can be converted to AVC event'
81             def record = records.iterator().next()
82             def convertedAvcEvent = jsonObjectMapper.convertJsonString(record.value(), AvcEvent.class)
83         and: 'we have correct headers forwarded where correlation id matches'
84             record.headers().forEach(header -> {
85                 if (header.key().equals('eventCorrelationId')) {
86                     assert SerializationUtils.deserialize(header.value()) == 'cmhandle1'
87                 }
88             })
89         and: 'event id differs(as per requirement) between consumed and forwarded'
90             record.headers().forEach(header -> {
91                 if (header.key().equals('eventId')) {
92                     assert SerializationUtils.deserialize(header.value()) != 'sample-eventid'
93                 }
94             })
95         and: 'the event payload still matches'
96             assert testEventSent == convertedAvcEvent
97     }
98
99 }