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
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.cps.ncmp.impl.data.async
 
  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
 
  36 import java.util.concurrent.TimeUnit
 
  38 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
 
  41 @EnableAutoConfiguration
 
  42 class FilterStrategiesIntegrationSpec extends ConsumerBaseSpec {
 
  45     EventsPublisher mockEventsPublisher = Mock()
 
  48     NcmpAsyncRequestResponseEventMapper mapper = Stub()
 
  50     @Value('${app.ncmp.async-m2m.topic}')
 
  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'))
 
  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(*_)
 
  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
 
  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
 
  84     def 'Filtering Cloud Events on Type.'() {
 
  85         given: 'a cloud event of type: #eventType'
 
  86             def cloudEvent = CloudEventBuilder.v1().withId('some id')
 
  88                 .withSource(URI.create('some-source'))
 
  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
 
  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
 
 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
 
 109     //TODO Toine, add positive test with data to prove event is converted correctly (using correct factory)
 
 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(*_)