3db8520c299dad61106058225981968e9f7bd1fe
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / NcmpAsyncDataOperationEventConsumerIntegrationSpec.groovy
1 package org.onap.cps.ncmp.api.impl.async
2
3 import io.cloudevents.CloudEvent
4 import io.cloudevents.core.builder.CloudEventBuilder
5 import io.cloudevents.kafka.CloudEventSerializer
6 import org.apache.kafka.clients.producer.KafkaProducer
7 import org.apache.kafka.clients.producer.ProducerRecord
8 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
9 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
10 import org.spockframework.spring.SpringBean
11 import org.springframework.beans.factory.annotation.Autowired
12 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
13 import org.springframework.boot.test.context.SpringBootTest
14 import org.springframework.kafka.config.KafkaListenerEndpointRegistry
15 import org.springframework.kafka.test.utils.ContainerTestUtils
16 import org.springframework.test.annotation.DirtiesContext
17 import org.testcontainers.spock.Testcontainers
18 import java.util.concurrent.TimeUnit
19
20 @SpringBootTest(classes =[NcmpAsyncDataOperationEventConsumer, DataOperationRecordFilterStrategy])
21 @DirtiesContext
22 @Testcontainers
23 @EnableAutoConfiguration
24 class NcmpAsyncDataOperationEventConsumerIntegrationSpec extends MessagingBaseSpec {
25
26     @Autowired
27     private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry
28
29     @SpringBean
30     EventsPublisher mockEventsPublisher = Mock()
31
32     def activateListeners() {
33         kafkaListenerEndpointRegistry.getListenerContainers().forEach(
34                 messageListenerContainer -> { ContainerTestUtils.waitForAssignment(messageListenerContainer, 1) }
35         )
36     }
37
38     def 'Filtering Events.'() {
39         given: 'a cloud event of type: #eventType'
40             def cloudEvent = CloudEventBuilder.v1().withId("some-uuid")
41                     .withType(eventType)
42                     .withSource(URI.create("sample-test-source"))
43                     .build();
44         and: 'activate message listener container'
45             activateListeners()
46         when: 'send the cloud event'
47             ProducerRecord<String, CloudEvent> record = new ProducerRecord<>('ncmp-async-m2m', cloudEvent)
48             KafkaProducer<String, CloudEvent> producer = new KafkaProducer<>(eventProducerConfigProperties(CloudEventSerializer))
49             producer.send(record);
50         and: 'wait a little for async processing of message'
51             TimeUnit.MILLISECONDS.sleep(100)
52         then: 'the event has only been forwarded for the correct type'
53             expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(_, _, _)
54         where:
55             eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
56             'DataOperationEvent'                             || 1
57             'other type'                                     || 0
58             'any type contain the word "DataOperationEvent"' || 1
59     }
60 }
61