2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
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.inventory.sync.lcm
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.apache.kafka.common.serialization.StringDeserializer
26 import org.onap.cps.events.EventsProducer
27 import org.onap.cps.events.LegacyEvent
28 import org.onap.cps.ncmp.events.lcm.v1.Event
29 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent
30 import org.onap.cps.ncmp.utils.TestUtils
31 import org.onap.cps.ncmp.utils.events.MessagingBaseSpec
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 = [ObjectMapper, JsonObjectMapper])
45 class EventsProducerSpec extends MessagingBaseSpec {
47 def legacyEventKafkaConsumer = new KafkaConsumer<String, LegacyEvent>(eventConsumerConfigProperties('ncmp-group', StringDeserializer))
49 def testTopic = 'ncmp-events-test'
52 EventsProducer eventsProducer = new EventsProducer(legacyEventKafkaTemplate, cloudEventKafkaTemplate, cloudEventKafkaTemplateForEos)
55 JsonObjectMapper jsonObjectMapper
58 def 'Produce and Consume Event'() {
59 given: 'event key and event data'
61 def eventId = 'test-uuid'
62 def eventCorrelationId = 'cmhandle-test'
63 def eventSource = 'org.onap.ncmp'
64 def eventTime = '2022-12-31T20:30:40.000+0000'
65 def eventType = 'org.onap.ncmp.cmhandle.lcm.event'
66 def eventSchema = 'org.onap.ncmp.cmhandle.lcm.event'
67 def eventSchemaVersion = 'v1'
68 def eventData = new LcmEvent(
70 eventCorrelationId: eventCorrelationId,
71 eventSource: eventSource,
74 eventSchema: eventSchema,
75 eventSchemaVersion: eventSchemaVersion,
76 event: new Event(cmHandleId: 'cmhandle-test'))
77 and: 'we have a event header'
80 eventCorrelationId: eventCorrelationId,
81 eventSource : eventSource,
82 eventTime : eventTime,
83 eventType : eventType,
84 eventSchema : eventSchema,
85 eventSchemaVersion: eventSchemaVersion]
86 and: 'consumer has a subscription'
87 legacyEventKafkaConsumer.subscribe([testTopic] as List<String>)
88 when: 'an event is sent'
89 eventsProducer.sendLegacyEvent(testTopic, eventKey, eventHeader, eventData)
90 and: 'topic is polled'
91 def records = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))
92 then: 'poll returns one record'
93 assert records.size() == 1
94 and: 'record key matches the expected event key'
95 def record = records.iterator().next()
96 assert eventKey == record.key
97 and: 'record matches the expected event'
98 def expectedJsonString = TestUtils.getResourceFileContent('expectedLcmEvent.json')
99 def expectedLcmEvent = jsonObjectMapper.convertJsonString(expectedJsonString, LcmEvent.class)
100 assert expectedLcmEvent == jsonObjectMapper.convertJsonString(record.value, LcmEvent.class)
101 and: 'record header matches the expected parameters'
102 assert SerializationUtils.deserialize(record.headers().lastHeader('eventId').value()) == eventId
103 assert SerializationUtils.deserialize(record.headers().lastHeader('eventCorrelationId').value()) == eventCorrelationId