Kafka (De-)Serialization Test
[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.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 java.util.concurrent.TimeUnit
35
36 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
37 @DirtiesContext
38 @Testcontainers
39 @EnableAutoConfiguration
40 class FilterStrategiesIntegrationSpec extends ConsumerBaseSpec {
41
42     @SpringBean
43     EventsPublisher mockEventsPublisher = Mock()
44
45     @SpringBean
46     NcmpAsyncRequestResponseEventMapper mapper = Stub()
47
48     @Value('${app.ncmp.async-m2m.topic}')
49     def topic
50
51     def 'Legacy event consumer with cloud event.'() {
52         given: 'a cloud event of type: #eventType'
53             def cloudEvent = CloudEventBuilder.v1().withId('some id')
54                 .withType('DataOperationEvent')
55                 .withSource(URI.create('some-source'))
56                 .build()
57         when: 'send the cloud event'
58             cloudEventKafkaTemplate.send(topic, cloudEvent)
59         and: 'wait a little for async processing of message'
60             TimeUnit.MILLISECONDS.sleep(300)
61         then: 'event is not consumed'
62             0 * mockEventsPublisher.publishEvent(*_)
63     }
64
65     def 'Legacy event consumer with valid legacy event.'() {
66         given: 'a cloud event of type: #eventType'
67             DmiAsyncRequestResponseEvent legacyEvent = new DmiAsyncRequestResponseEvent(eventId:'legacyEventId', eventTarget:'legacyEventTarget')
68         when: 'send the cloud event'
69             legacyEventKafkaTemplate.send(topic, legacyEvent)
70         and: 'wait a little for async processing of message'
71             TimeUnit.MILLISECONDS.sleep(300)
72         then: 'the event is consumed by the (legacy) AsynRestRequest consumer'
73             1 * mockEventsPublisher.publishEvent(*_)
74     }
75
76     def 'Filtering Cloud Events on Type.'() {
77         given: 'a cloud event of type: #eventType'
78             def cloudEvent = CloudEventBuilder.v1().withId('some id')
79                 .withType(eventType)
80                 .withSource(URI.create('some-source'))
81                 .build()
82         when: 'send the cloud event'
83             cloudEventKafkaTemplate.send(topic, cloudEvent)
84         and: 'wait a little for async processing of message'
85             TimeUnit.MILLISECONDS.sleep(300)
86         then: 'the event has only been forwarded for the correct type'
87             expectedNUmberOfCallsToPublishForwardedEvent * mockEventsPublisher.publishCloudEvent(*_)
88         where: 'the following event types are used'
89             eventType                                        || expectedNUmberOfCallsToPublishForwardedEvent
90             'DataOperationEvent'                             || 1
91             'other type'                                     || 0
92             'any type contain the word "DataOperationEvent"' || 1
93     }
94
95     //TODO Toine, add positive test with data to prove event is converted correctly (using correct factory)
96
97     def 'Non cloud events on same Topic.'() {
98         when: 'sending a non-cloud event on the same topic'
99             legacyEventKafkaTemplate.send(topic, 'simple string event')
100         and: 'wait a little for async processing of message'
101             TimeUnit.MILLISECONDS.sleep(300)
102         then: 'the event is not processed by this consumer'
103             0 * mockEventsPublisher.publishCloudEvent(*_)
104     }
105
106 }