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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.async
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
42 @SpringBootTest(classes =[NcmpAsyncDataOperationEventConsumer, DataOperationRecordFilterStrategy])
45 @EnableAutoConfiguration
46 class NcmpAsyncDataOperationEventConsumerIntegrationSpec extends MessagingBaseSpec {
49 EventsPublisher mockEventsPublisher = Mock()
52 private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry
54 @Value('${app.ncmp.async-m2m.topic}')
61 def 'Filtering Cloud Events on Type.'() {
62 given: 'a cloud event of type: #eventType'
63 def cloudEvent = CloudEventBuilder.v1().withId('some id')
65 .withSource(URI.create('some-source'))
67 when: 'send the cloud event'
68 ProducerRecord<String, CloudEvent> record = new ProducerRecord<>(topic, cloudEvent)
69 KafkaProducer<String, CloudEvent> producer = new KafkaProducer<>(eventProducerConfigProperties(CloudEventSerializer))
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
79 'any type contain the word "DataOperationEvent"' || 1
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))
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(*_)
93 def activateListeners() {
94 kafkaListenerEndpointRegistry.getListenerContainers().forEach(
95 messageListenerContainer -> { ContainerTestUtils.waitForAssignment(messageListenerContainer, 1) }