Merge "CmHandle delete is failing with InternalServerError: Null key is not allowed...
[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-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
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.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
41
42 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
43 @DirtiesContext
44 @Testcontainers
45 @EnableAutoConfiguration
46 class SerializationIntegrationSpec extends ConsumerBaseSpec {
47
48     @SpringBean
49     EventsPublisher mockEventsPublisher = Mock()
50
51     @SpringBean
52     NcmpAsyncRequestResponseEventMapper mapper = Stub() { toNcmpAsyncEvent(_) >> new NcmpAsyncRequestResponseEvent(eventId: 'my-event-id', eventTarget: 'some client topic')}
53
54     @Autowired
55     private ObjectMapper objectMapper
56
57     @Value('${app.ncmp.async-m2m.topic}')
58     def topic
59
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
68             }
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
74             }
75     }
76
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
85             }
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
91             }
92     }
93
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))
103             .build()
104     }
105
106 }