CloudEvents support for cps-core
[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
27 import org.onap.cps.ncmp.api.kafka.ConsumerBaseSpec
28 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
29 import org.spockframework.spring.SpringBean
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.test.annotation.DirtiesContext
34 import org.testcontainers.spock.Testcontainers
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 cloud event of type: #eventType'
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         and: 'wait a little for async processing of message'
61             TimeUnit.MILLISECONDS.sleep(300)
62         then: 'event is not consumed'
63             0 * mockEventsPublisher.publishEvent(*_)
64     }
65
66     def 'Legacy event consumer with valid legacy event.'() {
67         given: 'a cloud event of type: #eventType'
68             DmiAsyncRequestResponseEvent legacyEvent = new DmiAsyncRequestResponseEvent(eventId:'legacyEventId', eventTarget:'legacyEventTarget')
69         when: 'send the cloud event'
70             legacyEventKafkaTemplate.send(topic, legacyEvent)
71         and: 'wait a little for async processing of message'
72             TimeUnit.MILLISECONDS.sleep(300)
73         then: 'the event is consumed by the (legacy) AsynRestRequest consumer'
74             1 * mockEventsPublisher.publishEvent(*_)
75     }
76
77     def 'Filtering Cloud Events on Type.'() {
78         given: 'a cloud event of type: #eventType'
79             def cloudEvent = CloudEventBuilder.v1().withId('some id')
80                 .withType(eventType)
81                 .withSource(URI.create('some-source'))
82                 .build()
83         when: 'send the cloud event'
84             cloudEventKafkaTemplate.send(topic, cloudEvent)
85         and: 'wait a little for async processing of message'
86             TimeUnit.MILLISECONDS.sleep(300)
87         then: 'the event has only been forwarded for the correct type'
88             expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(*_)
89         where: 'the following event types are used'
90             eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
91             'DataOperationEvent'                             || 1
92             'other type'                                     || 0
93             'any type contain the word "DataOperationEvent"' || 1
94     }
95
96     //TODO Toine, add positive test with data to prove event is converted correctly (using correct factory)
97
98     def 'Non cloud events on same Topic.'() {
99         when: 'sending a non-cloud event on the same topic'
100             legacyEventKafkaTemplate.send(topic, 'simple string event')
101         and: 'wait a little for async processing of message'
102             TimeUnit.MILLISECONDS.sleep(300)
103         then: 'the event is not processed by this consumer'
104             0 * mockEventsPublisher.publishCloudEvent(*_)
105     }
106
107 }