Merge "Improve performance of updateDataLeaves"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / NcmpAsyncDataOperationEventConsumerIntegrationSpec.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.async
22
23 import io.cloudevents.CloudEvent
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import io.cloudevents.kafka.CloudEventSerializer
26 import org.apache.kafka.clients.producer.KafkaProducer
27 import org.apache.kafka.clients.producer.ProducerRecord
28 import org.apache.kafka.common.serialization.StringSerializer
29 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
30 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.beans.factory.annotation.Value
34 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.kafka.config.KafkaListenerEndpointRegistry
37 import org.springframework.kafka.test.utils.ContainerTestUtils
38 import org.springframework.test.annotation.DirtiesContext
39 import org.testcontainers.spock.Testcontainers
40 import java.util.concurrent.TimeUnit
41
42 @SpringBootTest(classes =[NcmpAsyncDataOperationEventConsumer, DataOperationRecordFilterStrategy])
43 @DirtiesContext
44 @Testcontainers
45 @EnableAutoConfiguration
46 class NcmpAsyncDataOperationEventConsumerIntegrationSpec extends MessagingBaseSpec {
47
48     @SpringBean
49     EventsPublisher mockEventsPublisher = Mock()
50
51     @Autowired
52     private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry
53
54     @Value('${app.ncmp.async-m2m.topic}')
55     def topic
56
57     def setup() {
58         activateListeners()
59     }
60
61     def 'Filtering Cloud Events on Type.'() {
62         given: 'a cloud event of type: #eventType'
63             def cloudEvent = CloudEventBuilder.v1().withId('some id')
64                     .withType(eventType)
65                     .withSource(URI.create('some-source'))
66                     .build()
67         when: 'send the cloud event'
68             ProducerRecord<String, CloudEvent> record = new ProducerRecord<>(topic, cloudEvent)
69             KafkaProducer<String, CloudEvent> producer = new KafkaProducer<>(eventProducerConfigProperties(CloudEventSerializer))
70             producer.send(record)
71         and: 'wait a little for async processing of message'
72             TimeUnit.MILLISECONDS.sleep(100)
73         then: 'the event has only been forwarded for the correct type'
74             expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(*_)
75         where: 'the following event types are used'
76             eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
77             'DataOperationEvent'                             || 1
78             'other type'                                     || 0
79             'any type contain the word "DataOperationEvent"' || 1
80     }
81
82     def 'Non cloud events on same Topic.'() {
83         when: 'sending a non-cloud event on the same topic'
84             ProducerRecord<String, String> record = new ProducerRecord<>(topic, 'simple string event')
85             KafkaProducer<String, String> producer = new KafkaProducer<>(eventProducerConfigProperties(StringSerializer))
86             producer.send(record)
87         and: 'wait a little for async processing of message'
88             TimeUnit.MILLISECONDS.sleep(100)
89         then: 'the event is not processed by this consumer'
90             0 * mockEventsPublisher.publishCloudEvent(*_)
91     }
92
93     def activateListeners() {
94         kafkaListenerEndpointRegistry.getListenerContainers().forEach(
95             messageListenerContainer -> { ContainerTestUtils.waitForAssignment(messageListenerContainer, 1) }
96         )
97     }
98
99 }