2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.event.lcm
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsPublisher
26 import org.onap.cps.ncmp.api.utils.MessagingSpec
27 import org.onap.cps.ncmp.utils.TestUtils
28 import org.onap.cps.utils.JsonObjectMapper
29 import org.onap.ncmp.cmhandle.event.lcm.Event
30 import org.onap.ncmp.cmhandle.event.lcm.LcmEvent
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.test.annotation.DirtiesContext
35 import org.testcontainers.spock.Testcontainers
37 import java.time.Duration
39 @SpringBootTest(classes = [LcmEventsPublisher, ObjectMapper, JsonObjectMapper])
42 class LcmEventsPublisherSpec extends MessagingSpec {
44 def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
46 def testTopic = 'ncmp-events-test'
49 LcmEventsPublisher lcmEventsPublisher = new LcmEventsPublisher(kafkaTemplate)
52 JsonObjectMapper jsonObjectMapper
55 def 'Produce and Consume Lcm Event'() {
56 given: 'event key and event data'
58 def eventData = new LcmEvent(
60 eventCorrelationId: 'cmhandle-as-correlationid',
61 eventSource: 'org.onap.ncmp',
62 eventTime: '2022-12-31T20:30:40.000+0000',
63 eventType: 'org.onap.ncmp.cmhandle.lcm.event',
64 eventSchema: 'org.onap.ncmp.cmhandle.lcm.event',
65 eventSchemaVersion: 'v1',
66 event: new Event(cmHandleId: 'cmhandle-test'))
67 and: 'consumer has a subscription'
68 kafkaConsumer.subscribe([testTopic] as List<String>)
69 when: 'an event is published'
70 lcmEventsPublisher.publishEvent(testTopic, eventKey, eventData)
71 and: 'topic is polled'
72 def records = kafkaConsumer.poll(Duration.ofMillis(1500))
73 then: 'poll returns one record'
74 assert records.size() == 1
75 and: 'record key matches the expected event key'
76 def record = records.iterator().next()
77 assert eventKey == record.key
78 and: 'record matches the expected event'
79 def expectedJsonString = TestUtils.getResourceFileContent('expectedLcmEvent.json')
80 def expectedLcmEvent = jsonObjectMapper.convertJsonString(expectedJsonString, LcmEvent.class)
81 assert expectedLcmEvent == jsonObjectMapper.convertJsonString(record.value, LcmEvent.class)