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