Migrate query tests to integration-test module #6
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avc / AvcEventProducerIntegrationSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 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.avc
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.mapstruct.factory.Mappers
26 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
27 import org.onap.cps.ncmp.event.model.AvcEvent
28 import org.onap.cps.ncmp.utils.TestUtils
29 import org.onap.cps.utils.JsonObjectMapper
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 = [AvcEventProducer, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
39 @Testcontainers
40 @DirtiesContext
41 class AvcEventProducerIntegrationSpec extends MessagingBaseSpec {
42
43     @SpringBean
44     AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
45
46     @SpringBean
47     AvcEventProducer avcEventProducer = new AvcEventProducer(kafkaTemplate, avcEventMapper)
48
49     @SpringBean
50     AvcEventConsumer acvEventConsumer = new AvcEventConsumer(avcEventProducer)
51
52     @Autowired
53     JsonObjectMapper jsonObjectMapper
54
55     def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
56
57     def 'Consume and forward valid message'() {
58         given: 'consumer has a subscription on a topic'
59             def cmEventsTopic = 'cm-events'
60             avcEventProducer.cmEventsTopic = cmEventsTopic
61             kafkaConsumer.subscribe([cmEventsTopic] as List<String>)
62         and: 'an event is sent'
63             def jsonData = TestUtils.getResourceFileContent('sampleAvcInputEvent.json')
64             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, AvcEvent.class)
65         when: 'the event is consumed'
66             acvEventConsumer.consumeAndForward(testEventSent)
67         and: 'the topic is polled'
68             def records = kafkaConsumer.poll(Duration.ofMillis(1500))
69         then: 'poll returns one record'
70             assert records.size() == 1
71         and: 'record can be converted to AVC event'
72             def record = records.iterator().next()
73             def convertedAvcEvent = jsonObjectMapper.convertJsonString(record.value(), AvcEvent)
74         and: 'consumed forwarded NCMP event id differs from DMI event id'
75             assert testEventSent.eventId != convertedAvcEvent.getEventId()
76         and: 'correlation id matches'
77             assert testEventSent.eventCorrelationId == convertedAvcEvent.getEventCorrelationId()
78         and: 'timestamps match'
79             assert testEventSent.eventTime == convertedAvcEvent.getEventTime()
80         and: 'target matches'
81             assert testEventSent.eventSource == convertedAvcEvent.getEventSource()
82     }
83
84 }