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 com.fasterxml.jackson.databind.ObjectMapper
 
  24 import io.cloudevents.core.builder.CloudEventBuilder
 
  25 import org.onap.cps.events.EventsPublisher
 
  26 import org.onap.cps.ncmp.api.impl.config.kafka.KafkaConfig
 
  27 import org.onap.cps.ncmp.api.kafka.ConsumerBaseSpec
 
  28 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
 
  29 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
 
  30 import org.onap.cps.ncmp.events.async1_0_0.Data
 
  31 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
 
  32 import org.onap.cps.ncmp.events.async1_0_0.Response
 
  33 import org.spockframework.spring.SpringBean
 
  34 import org.springframework.beans.factory.annotation.Autowired
 
  35 import org.springframework.beans.factory.annotation.Value
 
  36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
 
  37 import org.springframework.boot.test.context.SpringBootTest
 
  38 import org.springframework.test.annotation.DirtiesContext
 
  39 import org.testcontainers.spock.Testcontainers
 
  40 import spock.util.concurrent.PollingConditions
 
  42 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
 
  45 @EnableAutoConfiguration
 
  46 class SerializationIntegrationSpec extends ConsumerBaseSpec {
 
  49     EventsPublisher mockEventsPublisher = Mock()
 
  52     NcmpAsyncRequestResponseEventMapper mapper = Stub() { toNcmpAsyncEvent(_) >> new NcmpAsyncRequestResponseEvent(eventId: 'my-event-id', eventTarget: 'some client topic')}
 
  55     private ObjectMapper objectMapper
 
  57     @Value('${app.ncmp.async-m2m.topic}')
 
  60     def 'Forwarding DataOperation Event Data.'() {
 
  61         given: 'a data operation cloud event'
 
  62             def cloudEvent = createCloudEvent()
 
  63         and: 'a flag to track the publish cloud event call'
 
  64             def publishCloudEventMethodCalled = false
 
  65         and: 'the (mocked) events publisher will use the flag to indicate if it is called and will capture the cloud event'
 
  66             mockEventsPublisher.publishCloudEvent('some client topic', 'some-correlation-id', cloudEvent) >> {
 
  67                 publishCloudEventMethodCalled = true
 
  69         when: 'send the event'
 
  70             cloudEventKafkaTemplate.send(topic, cloudEvent)
 
  71         then: 'the event has been forwarded'
 
  72             new PollingConditions().within(1) {
 
  73                 assert publishCloudEventMethodCalled == true
 
  77     def 'Forwarding AsyncRestRequestResponse Event Data.'() {
 
  78         given: 'async request response legacy event'
 
  79             def dmiAsyncRequestResponseEvent = new DmiAsyncRequestResponseEvent(eventId: 'my-event-id',eventTarget: 'some client topic')
 
  80         and: 'a flag to track the publish event call'
 
  81             def publishEventMethodCalled = false
 
  82         and: 'the (mocked) events publisher will use the flag to indicate if it is called and will capture the event'
 
  83             mockEventsPublisher.publishEvent(*_) >> {
 
  84                 publishEventMethodCalled = true
 
  86         when: 'send the event'
 
  87             legacyEventKafkaTemplate.send(topic, dmiAsyncRequestResponseEvent)
 
  88         then: 'the event has been forwarded'
 
  89             new PollingConditions().within(1) {
 
  90                 assert publishEventMethodCalled == true
 
  94     def createCloudEvent() {
 
  95         def dataOperationEvent = new DataOperationEvent(data: new Data(responses: [new Response()]))
 
  96         return CloudEventBuilder.v1()
 
  97             .withId('my-event-id')
 
  98             .withType('DataOperationEvent')
 
  99             .withSource(URI.create('some-source'))
 
 100             .withExtension('destination','some client topic')
 
 101             .withExtension('correlationid','some-correlation-id')
 
 102             .withData(objectMapper.writeValueAsBytes(dataOperationEvent))