Merge "Add CSIT test: Delete CM Handle"
[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-2024 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.events.EventsPublisher
25 import org.onap.cps.ncmp.api.impl.config.kafka.KafkaConfig
26 import org.onap.cps.ncmp.api.kafka.ConsumerBaseSpec
27 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Value
30 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
31 import org.springframework.boot.test.context.SpringBootTest
32 import org.springframework.test.annotation.DirtiesContext
33 import org.testcontainers.spock.Testcontainers
34 import spock.util.concurrent.PollingConditions
35 import java.util.concurrent.TimeUnit
36
37 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
38 @DirtiesContext
39 @Testcontainers
40 @EnableAutoConfiguration
41 class FilterStrategiesIntegrationSpec extends ConsumerBaseSpec {
42
43     @SpringBean
44     EventsPublisher mockEventsPublisher = Mock()
45
46     @SpringBean
47     NcmpAsyncRequestResponseEventMapper mapper = Stub()
48
49     @Value('${app.ncmp.async-m2m.topic}')
50     def topic
51
52     def 'Legacy event consumer with cloud event.'() {
53         given: 'a data operation cloud event type'
54             def cloudEvent = CloudEventBuilder.v1().withId('some id')
55                 .withType('DataOperationEvent')
56                 .withSource(URI.create('some-source'))
57                 .build()
58         when: 'send the cloud event'
59             cloudEventKafkaTemplate.send(topic, cloudEvent)
60         then: 'wait a little for async processing of message (must wait to try to avoid false positives)'
61             TimeUnit.MILLISECONDS.sleep(300)
62         and: 'event is not consumed'
63             0 * mockEventsPublisher.publishEvent(*_)
64     }
65
66     def 'Legacy event consumer with valid legacy event.'() {
67         given: 'a legacy event'
68             DmiAsyncRequestResponseEvent legacyEvent = new DmiAsyncRequestResponseEvent(eventId:'legacyEventId', eventTarget:'legacyEventTarget')
69         and: 'a flag to track the publish event call'
70             def publishEventMethodCalled = false
71         and: 'the (mocked) events publisher will use the flag to indicate if it is called'
72             mockEventsPublisher.publishEvent(*_) >> {
73                 publishEventMethodCalled = true
74             }
75         when: 'send the cloud event'
76             legacyEventKafkaTemplate.send(topic, legacyEvent)
77         then: 'the event is consumed by the (legacy) AsynRestRequest consumer'
78             new PollingConditions().within(1) {
79                 assert publishEventMethodCalled == true
80             }
81     }
82
83     def 'Filtering Cloud Events on Type.'() {
84         given: 'a cloud event of type: #eventType'
85             def cloudEvent = CloudEventBuilder.v1().withId('some id')
86                 .withType(eventType)
87                 .withSource(URI.create('some-source'))
88                 .build()
89         and: 'a flag to track the publish event call'
90             def publishEventMethodCalled = false
91         and: 'the (mocked) events publisher will use the flag to indicate if it is called'
92             mockEventsPublisher.publishCloudEvent(*_) >> {
93                 publishEventMethodCalled = true
94             }
95         when: 'send the cloud event'
96             cloudEventKafkaTemplate.send(topic, cloudEvent)
97         then: 'the event has only been forwarded for the correct type'
98             new PollingConditions(initialDelay: 0.3).within(1) {
99                 assert publishEventMethodCalled == expectCallToPublishEventMethod
100             }
101         where: 'the following event types are used'
102             eventType                                        || expectCallToPublishEventMethod
103             'DataOperationEvent'                             || true
104             'other type'                                     || false
105             'any type contain the word "DataOperationEvent"' || true
106     }
107
108     //TODO Toine, add positive test with data to prove event is converted correctly (using correct factory)
109
110     def 'Non cloud events on same Topic.'() {
111         when: 'sending a non-cloud event on the same topic'
112             legacyEventKafkaTemplate.send(topic, 'simple string event')
113         then: 'wait a little for async processing of message (must wait to try to avoid false positives)'
114             TimeUnit.MILLISECONDS.sleep(300)
115         and: 'the event is not processed by this consumer'
116             0 * mockEventsPublisher.publishCloudEvent(*_)
117     }
118
119 }