d57527a4546887c8af192259c6780ba2a7bcdf66
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avc / AvcEventConsumerSpec.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.impl.events.EventsPublisher
27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
28 import org.onap.cps.ncmp.event.model.AvcEvent
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.testcontainers.spock.Testcontainers
36
37 import java.time.Duration
38
39 @SpringBootTest(classes = [EventsPublisher, AvcEventConsumer, ObjectMapper, JsonObjectMapper])
40 @Testcontainers
41 @DirtiesContext
42 class AvcEventConsumerSpec extends MessagingBaseSpec {
43
44     @SpringBean
45     AvcEventMapper avcEventMapper = Mappers.getMapper(AvcEventMapper.class)
46
47     @SpringBean
48     EventsPublisher eventsPublisher = new EventsPublisher<AvcEvent>(kafkaTemplate)
49
50     @SpringBean
51     AvcEventConsumer acvEventConsumer = new AvcEventConsumer(eventsPublisher, avcEventMapper)
52
53     @Autowired
54     JsonObjectMapper jsonObjectMapper
55
56     def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group'))
57
58     def 'Consume and forward valid message'() {
59         given: 'consumer has a subscription on a topic'
60             def cmEventsTopicName = 'cm-events'
61             acvEventConsumer.cmEventsTopicName = cmEventsTopicName
62             kafkaConsumer.subscribe([cmEventsTopicName] as List<String>)
63         and: 'an event is sent'
64             def jsonData = TestUtils.getResourceFileContent('sampleAvcInputEvent.json')
65             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, AvcEvent.class)
66         when: 'the event is consumed'
67             acvEventConsumer.consumeAndForward(testEventSent)
68         and: 'the topic is polled'
69             def records = kafkaConsumer.poll(Duration.ofMillis(1500))
70         then: 'poll returns one record'
71             assert records.size() == 1
72         and: 'record can be converted to AVC event'
73             def record = records.iterator().next()
74             def convertedAvcEvent = jsonObjectMapper.convertJsonString(record.value(), AvcEvent)
75         and: 'consumed forwarded NCMP event id differs from DMI event id'
76             assert testEventSent.eventId != convertedAvcEvent.getEventId()
77         and: 'correlation id matches'
78             assert testEventSent.eventCorrelationId == convertedAvcEvent.getEventCorrelationId()
79         and: 'timestamps match'
80             assert testEventSent.eventTime == convertedAvcEvent.getEventTime()
81         and: 'target matches'
82             assert testEventSent.eventSource == convertedAvcEvent.getEventSource()
83     }
84
85 }