2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2023-2024 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.impl.cmnotificationsubscription.cmavc
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import io.cloudevents.kafka.CloudEventDeserializer
27 import io.cloudevents.kafka.impl.KafkaHeaders
28 import org.apache.kafka.clients.consumer.ConsumerRecord
29 import org.apache.kafka.clients.consumer.KafkaConsumer
30 import org.onap.cps.events.EventsPublisher
31 import org.onap.cps.ncmp.events.avc1_0_0.AvcEvent
32 import org.onap.cps.ncmp.utils.TestUtils
33 import org.onap.cps.ncmp.utils.events.MessagingBaseSpec
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.spockframework.spring.SpringBean
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.test.annotation.DirtiesContext
39 import org.testcontainers.spock.Testcontainers
40 import java.time.Duration
42 import static org.onap.cps.ncmp.utils.events.CloudEventMapper.toTargetEvent
44 @SpringBootTest(classes = [EventsPublisher, CmAvcEventConsumer, ObjectMapper, JsonObjectMapper])
47 class CmAvcEventConsumerSpec extends MessagingBaseSpec {
50 EventsPublisher eventsPublisher = new EventsPublisher<CloudEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
53 CmAvcEventConsumer acvEventConsumer = new CmAvcEventConsumer(eventsPublisher)
56 JsonObjectMapper jsonObjectMapper
58 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('ncmp-group', CloudEventDeserializer))
60 def 'Consume and forward valid message'() {
61 given: 'consumer has a subscription on a topic'
62 def cmEventsTopicName = 'cm-events'
63 acvEventConsumer.cmEventsTopicName = cmEventsTopicName
64 cloudEventKafkaConsumer.subscribe([cmEventsTopicName] as List<String>)
65 and: 'an event is sent'
66 def jsonData = TestUtils.getResourceFileContent('sampleAvcInputEvent.json')
67 def testEventKey = 'sample-eventid-key'
68 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, AvcEvent.class)
69 def testCloudEventSent = CloudEventBuilder.v1()
70 .withData(jsonObjectMapper.asJsonBytes(testEventSent))
71 .withId('sample-eventid')
72 .withType('sample-test-type')
73 .withSource(URI.create('sample-test-source'))
74 .withExtension('correlationid', 'test-cmhandle1').build()
75 and: 'event has header information'
76 def consumerRecord = new ConsumerRecord<String, CloudEvent>(cmEventsTopicName, 0, 0, testEventKey, testCloudEventSent)
77 when: 'the event is consumed and forwarded to target topic'
78 acvEventConsumer.consumeAndForward(consumerRecord)
79 and: 'the target topic is polled'
80 def records = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500))
81 then: 'poll returns one record'
82 assert records.size() == 1
83 and: 'target record can be converted to AVC event'
84 def record = records.iterator().next()
85 def cloudEvent = record.value() as CloudEvent
86 def convertedAvcEvent = toTargetEvent(cloudEvent, AvcEvent.class)
87 and: 'the target event has the same key as the source event to maintain the ordering in a partition'
88 assert record.key() == consumerRecord.key()
89 and: 'we have correct headers forwarded where correlation id matches'
90 assert KafkaHeaders.getParsedKafkaHeader(record.headers(), 'ce_correlationid') == 'test-cmhandle1'
91 and: 'event id is same between consumed and forwarded'
92 assert KafkaHeaders.getParsedKafkaHeader(record.headers(), 'ce_id') == 'sample-eventid'
93 and: 'the event payload still matches'
94 assert testEventSent == convertedAvcEvent