Modify dmi plugin stub mapping of data operation
[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.events.async1_0_0.Data
29 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
30 import org.onap.cps.ncmp.events.async1_0_0.Response
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.beans.factory.annotation.Value
34 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.test.annotation.DirtiesContext
37 import org.testcontainers.spock.Testcontainers
38
39 import java.util.concurrent.TimeUnit
40
41 @SpringBootTest(classes =[DataOperationEventConsumer, RecordFilterStrategies, KafkaConfig])
42 @DirtiesContext
43 @Testcontainers
44 @EnableAutoConfiguration
45 class SerializationIntegrationSpec extends ConsumerBaseSpec {
46
47     @SpringBean
48     EventsPublisher mockEventsPublisher = Mock()
49
50     @Autowired
51     private ObjectMapper objectMapper
52
53     @Value('${app.ncmp.async-m2m.topic}')
54     def topic
55
56     def capturedForwardedEvent
57
58     def 'Forwarding DataOperation Event Data.'() {
59         given: 'a data operation cloud event'
60             def cloudEventSent = createCloudEvent()
61         when: 'send the event'
62             cloudEventKafkaTemplate.send(topic, cloudEventSent)
63         and: 'wait a little for async processing of message'
64             TimeUnit.MILLISECONDS.sleep(300)
65         then: 'the event has been forwarded'
66             1 * mockEventsPublisher.publishCloudEvent('some client topic', 'my-event-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
67         and: 'the forwarded event is identical to the event that was sent'
68             assert capturedForwardedEvent == cloudEventSent
69     }
70
71     def createCloudEvent() {
72         def dataOperationEvent = new DataOperationEvent(data: new Data(responses: [new Response()]))
73         return CloudEventBuilder.v1()
74             .withId('my-event-id')
75             .withType('DataOperationEvent')
76             .withSource(URI.create('some-source'))
77             .withExtension('destination','some client topic')
78             .withData(objectMapper.writeValueAsBytes(dataOperationEvent))
79             .build()
80     }
81
82 }