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.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
40 import java.time.Duration
42 @SpringBootTest(classes = [EventsPublisher, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
45 class AvcEventConsumerSpec extends MessagingBaseSpec {
48 AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
51 EventsPublisher eventsPublisher = new EventsPublisher<AvcEvent>(kafkaTemplate)
54 AvcEventConsumer acvEventConsumer = new AvcEventConsumer(eventsPublisher, avcEventMapper)
57 JsonObjectMapper jsonObjectMapper
59 def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
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'
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'
94 and: 'the event payload still matches'
95 assert testEventSent == convertedAvcEvent