NCMP : NCMP : Handle non responding DMI-Plugin
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / lcm / LcmEventsPublisherSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.lcm
22
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.ncmp.api.impl.events.EventsPublisher
27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
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.utils.JsonObjectMapper
32 import org.spockframework.spring.SpringBean
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.test.context.SpringBootTest
35 import org.springframework.test.annotation.DirtiesContext
36 import org.springframework.util.SerializationUtils
37 import org.testcontainers.spock.Testcontainers
38
39 import java.time.Duration
40
41 @SpringBootTest(classes = [EventsPublisher, ObjectMapper, JsonObjectMapper])
42 @Testcontainers
43 @DirtiesContext
44 class LcmEventsPublisherSpec extends MessagingBaseSpec {
45
46     def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('ncmp-group', StringDeserializer))
47
48     def testTopic = 'ncmp-events-test'
49
50     @SpringBean
51     EventsPublisher<LcmEvent> lcmEventsPublisher = new EventsPublisher(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
52
53     @Autowired
54     JsonObjectMapper jsonObjectMapper
55
56
57     def 'Produce and Consume Lcm Event'() {
58         given: 'event key and event data'
59             def eventKey = 'lcm'
60             def eventId = 'test-uuid'
61             def eventCorrelationId = 'cmhandle-test'
62             def eventSource = 'org.onap.ncmp'
63             def eventTime = '2022-12-31T20:30:40.000+0000'
64             def eventType = 'org.onap.ncmp.cmhandle.lcm.event'
65             def eventSchema = 'org.onap.ncmp.cmhandle.lcm.event'
66             def eventSchemaVersion = 'v1'
67             def eventData = new LcmEvent(
68                 eventId: eventId,
69                 eventCorrelationId: eventCorrelationId,
70                 eventSource: eventSource,
71                 eventTime: eventTime,
72                 eventType: eventType,
73                 eventSchema: eventSchema,
74                 eventSchemaVersion: eventSchemaVersion,
75                 event: new Event(cmHandleId: 'cmhandle-test'))
76         and: 'we have a event header'
77             def eventHeader = [
78                 eventId           : eventId,
79                 eventCorrelationId: eventCorrelationId,
80                 eventSource       : eventSource,
81                 eventTime         : eventTime,
82                 eventType         : eventType,
83                 eventSchema       : eventSchema,
84                 eventSchemaVersion: eventSchemaVersion]
85         and: 'consumer has a subscription'
86             legacyEventKafkaConsumer.subscribe([testTopic] as List<String>)
87         when: 'an event is published'
88             lcmEventsPublisher.publishEvent(testTopic, eventKey, eventHeader, eventData)
89         and: 'topic is polled'
90             def records = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))
91         then: 'poll returns one record'
92             assert records.size() == 1
93         and: 'record key matches the expected event key'
94             def record = records.iterator().next()
95             assert eventKey == record.key
96         and: 'record matches the expected event'
97             def expectedJsonString = TestUtils.getResourceFileContent('expectedLcmEvent.json')
98             def expectedLcmEvent = jsonObjectMapper.convertJsonString(expectedJsonString, LcmEvent.class)
99             assert expectedLcmEvent == jsonObjectMapper.convertJsonString(record.value, LcmEvent.class)
100         and: 'record header matches the expected parameters'
101             assert SerializationUtils.deserialize(record.headers().lastHeader('eventId').value()) == eventId
102             assert SerializationUtils.deserialize(record.headers().lastHeader('eventCorrelationId').value()) == eventCorrelationId
103     }
104 }