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