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.api.impl.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
28 import org.onap.cps.ncmp.api.kafka.ConsumerBaseSpec
29 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
30 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
31 import org.onap.cps.ncmp.events.async1_0_0.Data
32 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
33 import org.onap.cps.ncmp.events.async1_0_0.Response
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.beans.factory.annotation.Value
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
38 import org.springframework.boot.test.context.SpringBootTest
39 import org.springframework.test.annotation.DirtiesContext
40 import org.testcontainers.spock.Testcontainers
42 import java.util.concurrent.TimeUnit
44 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
47 @EnableAutoConfiguration
48 class SerializationIntegrationSpec extends ConsumerBaseSpec {
51 EventsPublisher mockEventsPublisher = Mock()
54 NcmpAsyncRequestResponseEventMapper mapper = Stub() { toNcmpAsyncEvent(_) >> new NcmpAsyncRequestResponseEvent(eventId: 'my-event-id', eventTarget: 'some client topic')}
57 private ObjectMapper objectMapper
59 @Value('${app.ncmp.async-m2m.topic}')
62 def capturedForwardedEvent
64 def 'Forwarding DataOperation Event Data.'() {
65 given: 'a data operation cloud event'
66 def cloudEvent = createCloudEvent()
67 when: 'send the event'
68 cloudEventKafkaTemplate.send(topic, cloudEvent)
69 and: 'wait a little for async processing of message'
70 TimeUnit.MILLISECONDS.sleep(300)
71 then: 'the event has been forwarded'
72 1 * mockEventsPublisher.publishCloudEvent('some client topic', 'some-correlation-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
73 and: 'the forwarded event is identical to the event that was sent'
74 assert capturedForwardedEvent == cloudEvent
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 when: 'send the event'
81 legacyEventKafkaTemplate.send(topic, dmiAsyncRequestResponseEvent)
82 and: 'wait a little for async processing of message'
83 TimeUnit.MILLISECONDS.sleep(300)
84 then: 'the event has been forwarded'
85 1 * mockEventsPublisher.publishEvent('some client topic', 'my-event-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
86 and: 'the captured id and target of the forwarded event is same as the one that was sent'
87 assert capturedForwardedEvent.eventId == dmiAsyncRequestResponseEvent.eventId
88 assert capturedForwardedEvent.eventTarget == dmiAsyncRequestResponseEvent.eventTarget
91 def createCloudEvent() {
92 def dataOperationEvent = new DataOperationEvent(data: new Data(responses: [new Response()]))
93 return CloudEventBuilder.v1()
94 .withId('my-event-id')
95 .withType('DataOperationEvent')
96 .withSource(URI.create('some-source'))
97 .withExtension('destination','some client topic')
98 .withExtension('correlationid','some-correlation-id')
99 .withData(objectMapper.writeValueAsBytes(dataOperationEvent))