ad7d741ae2a259385145739a516188fbb617f573
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2022-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.impl.data.async
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.apache.kafka.common.serialization.StringDeserializer
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.events.EventsPublisher
28 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
29 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
30 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
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.time.Duration
40
41 @SpringBootTest(classes = [EventsPublisher, AsyncRestRequestResponseEventConsumer, ObjectMapper, JsonObjectMapper])
42 @Testcontainers
43 @DirtiesContext
44 class NcmpAsyncRequestResponseEventProducerIntegrationSpec extends MessagingBaseSpec {
45
46     @SpringBean
47     EventsPublisher cpsAsyncRequestResponseEventPublisher =
48         new EventsPublisher<NcmpAsyncRequestResponseEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate);
49
50
51     @SpringBean
52     NcmpAsyncRequestResponseEventMapper ncmpAsyncRequestResponseEventMapper =
53             Mappers.getMapper(NcmpAsyncRequestResponseEventMapper.class)
54
55     @SpringBean
56     AsyncRestRequestResponseEventConsumer ncmpAsyncRequestResponseEventConsumer =
57             new AsyncRestRequestResponseEventConsumer(cpsAsyncRequestResponseEventPublisher,
58                     ncmpAsyncRequestResponseEventMapper)
59
60     @Autowired
61     JsonObjectMapper jsonObjectMapper
62
63     def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test', StringDeserializer))
64
65     def 'Consume and forward valid message'() {
66         given: 'consumer has a subscription'
67             legacyEventKafkaConsumer.subscribe(['test-topic'] as List<String>)
68         and: 'an event is sent'
69             def jsonData = TestUtils.getResourceFileContent('dmiAsyncRequestResponseEvent.json')
70             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, DmiAsyncRequestResponseEvent.class)
71         when: 'the event is consumed'
72             ncmpAsyncRequestResponseEventConsumer.consumeAndForward(testEventSent)
73         and: 'the topic is polled'
74             def records = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))
75         then: 'poll returns one record'
76             assert records.size() == 1
77         and: 'consumed forwarded event id is the same as sent event id'
78             def record = records.iterator().next()
79             assert testEventSent.eventId.equalsIgnoreCase(jsonObjectMapper.convertJsonString(record.value(),
80                     NcmpAsyncRequestResponseEvent).getForwardedEvent().getEventId())
81     }
82
83 }