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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.events.avc
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
41 import java.time.Duration
43 @SpringBootTest(classes = [EventsPublisher, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
46 class AvcEventConsumerSpec extends MessagingBaseSpec {
49 AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
52 EventsPublisher eventsPublisher = new EventsPublisher<AvcEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
55 AvcEventConsumer acvEventConsumer = new AvcEventConsumer(eventsPublisher, avcEventMapper)
58 JsonObjectMapper jsonObjectMapper
60 def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('ncmp-group', StringDeserializer))
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'
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'
95 and: 'the event payload still matches'
96 assert testEventSent == convertedAvcEvent