Merge "Updating the Kafka listener compliance to could events and legacy"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / FilterStrategiesIntegrationSpec.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.core.builder.CloudEventBuilder
24 import org.onap.cps.ncmp.api.impl.config.kafka.KafkaConfig
25 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
26 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
27 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.beans.factory.annotation.Value
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.kafka.config.KafkaListenerEndpointRegistry
34 import org.springframework.kafka.test.utils.ContainerTestUtils
35 import org.springframework.test.annotation.DirtiesContext
36 import org.testcontainers.spock.Testcontainers
37 import java.util.concurrent.TimeUnit
38
39 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
40 @DirtiesContext
41 @Testcontainers
42 @EnableAutoConfiguration
43 class FilterStrategiesIntegrationSpec extends MessagingBaseSpec {
44
45     @SpringBean
46     EventsPublisher mockEventsPublisher = Mock()
47
48     @SpringBean
49     NcmpAsyncRequestResponseEventMapper mapper = Stub()
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 'Legacy event consumer with cloud event.'() {
62         given: 'a cloud event of type: #eventType'
63             def cloudEvent = CloudEventBuilder.v1().withId('some id')
64                 .withType('DataOperationEvent')
65                 .withSource(URI.create('some-source'))
66                 .build()
67         when: 'send the cloud event'
68             cloudEventKafkaTemplate.send(topic, cloudEvent)
69         and: 'wait a little for async processing of message'
70             TimeUnit.MILLISECONDS.sleep(300)
71         then: 'event is not consumed'
72             0 * mockEventsPublisher.publishEvent(*_)
73     }
74
75     def 'Legacy event consumer with valid legacy event.'() {
76         given: 'a cloud event of type: #eventType'
77             DmiAsyncRequestResponseEvent legacyEvent = new DmiAsyncRequestResponseEvent(eventId:'legacyEventId', eventTarget:'legacyEventTarget')
78         when: 'send the cloud event'
79             legacyEventKafkaTemplate.send(topic, legacyEvent)
80         and: 'wait a little for async processing of message'
81             TimeUnit.MILLISECONDS.sleep(300)
82         then: 'the event is consumed by the (legacy) AsynRestRequest consumer'
83             1 * mockEventsPublisher.publishEvent(*_)
84     }
85
86     def 'Filtering Cloud Events on Type.'() {
87         given: 'a cloud event of type: #eventType'
88             def cloudEvent = CloudEventBuilder.v1().withId('some id')
89                 .withType(eventType)
90                 .withSource(URI.create('some-source'))
91                 .build()
92         when: 'send the cloud event'
93             cloudEventKafkaTemplate.send(topic, cloudEvent)
94         and: 'wait a little for async processing of message'
95             TimeUnit.MILLISECONDS.sleep(300)
96         then: 'the event has only been forwarded for the correct type'
97             expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(*_)
98         where: 'the following event types are used'
99             eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
100             'DataOperationEvent'                             || 1
101             'other type'                                     || 0
102             'any type contain the word "DataOperationEvent"' || 1
103     }
104
105     //TODO Toine, add positive test with data to prove event is converted correctly (using correct factory)
106
107     def 'Non cloud events on same Topic.'() {
108         when: 'sending a non-cloud event on the same topic'
109             legacyEventKafkaTemplate.send(topic, 'simple string event')
110         and: 'wait a little for async processing of message'
111             TimeUnit.MILLISECONDS.sleep(300)
112         then: 'the event is not processed by this consumer'
113             0 * mockEventsPublisher.publishCloudEvent(*_)
114     }
115
116     def activateListeners() {
117         kafkaListenerEndpointRegistry.getListenerContainers().forEach(
118             messageListenerContainer -> { ContainerTestUtils.waitForAssignment(messageListenerContainer, 1) }
119         )
120     }
121 }