summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/async/NcmpAsyncDataOperationEventConsumerIntegrationSpec.groovy
blob: 3db8520c299dad61106058225981968e9f7bd1fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.onap.cps.ncmp.api.impl.async

import io.cloudevents.CloudEvent
import io.cloudevents.core.builder.CloudEventBuilder
import io.cloudevents.kafka.CloudEventSerializer
import org.apache.kafka.clients.producer.KafkaProducer
import org.apache.kafka.clients.producer.ProducerRecord
import org.onap.cps.ncmp.api.impl.events.EventsPublisher
import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.kafka.config.KafkaListenerEndpointRegistry
import org.springframework.kafka.test.utils.ContainerTestUtils
import org.springframework.test.annotation.DirtiesContext
import org.testcontainers.spock.Testcontainers
import java.util.concurrent.TimeUnit

@SpringBootTest(classes =[NcmpAsyncDataOperationEventConsumer, DataOperationRecordFilterStrategy])
@DirtiesContext
@Testcontainers
@EnableAutoConfiguration
class NcmpAsyncDataOperationEventConsumerIntegrationSpec extends MessagingBaseSpec {

    @Autowired
    private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry

    @SpringBean
    EventsPublisher mockEventsPublisher = Mock()

    def activateListeners() {
        kafkaListenerEndpointRegistry.getListenerContainers().forEach(
                messageListenerContainer -> { ContainerTestUtils.waitForAssignment(messageListenerContainer, 1) }
        )
    }

    def 'Filtering Events.'() {
        given: 'a cloud event of type: #eventType'
            def cloudEvent = CloudEventBuilder.v1().withId("some-uuid")
                    .withType(eventType)
                    .withSource(URI.create("sample-test-source"))
                    .build();
        and: 'activate message listener container'
            activateListeners()
        when: 'send the cloud event'
            ProducerRecord<String, CloudEvent> record = new ProducerRecord<>('ncmp-async-m2m', cloudEvent)
            KafkaProducer<String, CloudEvent> producer = new KafkaProducer<>(eventProducerConfigProperties(CloudEventSerializer))
            producer.send(record);
        and: 'wait a little for async processing of message'
            TimeUnit.MILLISECONDS.sleep(100)
        then: 'the event has only been forwarded for the correct type'
            expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(_, _, _)
        where:
            eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
            'DataOperationEvent'                             || 1
            'other type'                                     || 0
            'any type contain the word "DataOperationEvent"' || 1
    }
}