14ecd928246cdd21b2cfc4a43347bbd406e7b82f
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / SerializationIntegrationSpec.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 com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import org.onap.cps.ncmp.api.impl.config.kafka.KafkaConfig
26 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
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
41 import java.util.concurrent.TimeUnit
42
43 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
44 @DirtiesContext
45 @Testcontainers
46 @EnableAutoConfiguration
47 class SerializationIntegrationSpec extends ConsumerBaseSpec {
48
49     @SpringBean
50     EventsPublisher mockEventsPublisher = Mock()
51
52     @SpringBean
53     NcmpAsyncRequestResponseEventMapper mapper = Stub() { toNcmpAsyncEvent(_) >> new NcmpAsyncRequestResponseEvent(eventId: 'my-event-id', eventTarget: 'some client topic')}
54
55     @Autowired
56     private ObjectMapper objectMapper
57
58     @Value('${app.ncmp.async-m2m.topic}')
59     def topic
60
61     def capturedForwardedEvent
62
63     def 'Forwarding DataOperation Event Data.'() {
64         given: 'a data operation cloud event'
65             def cloudEvent = createCloudEvent()
66         when: 'send the event'
67             cloudEventKafkaTemplate.send(topic, cloudEvent)
68         and: 'wait a little for async processing of message'
69             TimeUnit.MILLISECONDS.sleep(300)
70         then: 'the event has been forwarded'
71             1 * mockEventsPublisher.publishCloudEvent('some client topic', 'my-event-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
72         and: 'the forwarded event is identical to the event that was sent'
73             assert capturedForwardedEvent == cloudEvent
74     }
75
76     def 'Forwarding AsyncRestRequestResponse Event Data.'() {
77         given: 'async request response legacy event'
78             def dmiAsyncRequestResponseEvent = new DmiAsyncRequestResponseEvent(eventId: 'my-event-id',eventTarget: 'some client topic')
79         when: 'send the event'
80             legacyEventKafkaTemplate.send(topic, dmiAsyncRequestResponseEvent)
81         and: 'wait a little for async processing of message'
82             TimeUnit.MILLISECONDS.sleep(300)
83         then: 'the event has been forwarded'
84             1 * mockEventsPublisher.publishEvent('some client topic', 'my-event-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
85         and: 'the captured id and target of the forwarded event is same as the one that was sent'
86             assert capturedForwardedEvent.eventId == dmiAsyncRequestResponseEvent.eventId
87             assert capturedForwardedEvent.eventTarget == dmiAsyncRequestResponseEvent.eventTarget
88     }
89
90     def createCloudEvent() {
91         def dataOperationEvent = new DataOperationEvent(data: new Data(responses: [new Response()]))
92         return CloudEventBuilder.v1()
93             .withId('my-event-id')
94             .withType('DataOperationEvent')
95             .withSource(URI.create('some-source'))
96             .withExtension('destination','some client topic')
97             .withData(objectMapper.writeValueAsBytes(dataOperationEvent))
98             .build()
99     }
100
101 }