Replace deprecated WebSecurityConfigurerAdapter
[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.onap.cps.ncmp.api.impl.events.EventsPublisher
26 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
27 import org.onap.cps.ncmp.events.lcm.v1.Event
28 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent
29 import org.onap.cps.ncmp.utils.TestUtils
30 import org.onap.cps.utils.JsonObjectMapper
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.springframework.util.SerializationUtils
36 import org.testcontainers.spock.Testcontainers
37
38 import java.time.Duration
39
40 @SpringBootTest(classes = [EventsPublisher, ObjectMapper, JsonObjectMapper])
41 @Testcontainers
42 @DirtiesContext
43 class LcmEventsPublisherSpec extends MessagingBaseSpec {
44
45     def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
46
47     def testTopic = 'ncmp-events-test'
48
49     @SpringBean
50     EventsPublisher<LcmEvent> lcmEventsPublisher = new EventsPublisher(kafkaTemplate)
51
52     @Autowired
53     JsonObjectMapper jsonObjectMapper
54
55
56     def 'Produce and Consume Lcm Event'() {
57         given: 'event key and event data'
58             def eventKey = 'lcm'
59             def eventId = 'test-uuid'
60             def eventCorrelationId = 'cmhandle-test'
61             def eventSource = 'org.onap.ncmp'
62             def eventTime = '2022-12-31T20:30:40.000+0000'
63             def eventType = 'org.onap.ncmp.cmhandle.lcm.event'
64             def eventSchema = 'org.onap.ncmp.cmhandle.lcm.event'
65             def eventSchemaVersion = 'v1'
66             def eventData = new LcmEvent(
67                 eventId: eventId,
68                 eventCorrelationId: eventCorrelationId,
69                 eventSource: eventSource,
70                 eventTime: eventTime,
71                 eventType: eventType,
72                 eventSchema: eventSchema,
73                 eventSchemaVersion: eventSchemaVersion,
74                 event: new Event(cmHandleId: 'cmhandle-test'))
75         and: 'we have a event header'
76             def eventHeader = [
77                 eventId           : eventId,
78                 eventCorrelationId: eventCorrelationId,
79                 eventSource       : eventSource,
80                 eventTime         : eventTime,
81                 eventType         : eventType,
82                 eventSchema       : eventSchema,
83                 eventSchemaVersion: eventSchemaVersion]
84         and: 'consumer has a subscription'
85             kafkaConsumer.subscribe([testTopic] as List<String>)
86         when: 'an event is published'
87             lcmEventsPublisher.publishEvent(testTopic, eventKey, eventHeader, eventData)
88         and: 'topic is polled'
89             def records = kafkaConsumer.poll(Duration.ofMillis(1500))
90         then: 'poll returns one record'
91             assert records.size() == 1
92         and: 'record key matches the expected event key'
93             def record = records.iterator().next()
94             assert eventKey == record.key
95         and: 'record matches the expected event'
96             def expectedJsonString = TestUtils.getResourceFileContent('expectedLcmEvent.json')
97             def expectedLcmEvent = jsonObjectMapper.convertJsonString(expectedJsonString, LcmEvent.class)
98             assert expectedLcmEvent == jsonObjectMapper.convertJsonString(record.value, LcmEvent.class)
99         and: 'record header matches the expected parameters'
100             assert SerializationUtils.deserialize(record.headers().lastHeader('eventId').value()) == eventId
101             assert SerializationUtils.deserialize(record.headers().lastHeader('eventCorrelationId').value()) == eventCorrelationId
102     }
103 }