2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2022 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 org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.mapstruct.factory.Mappers
26 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
27 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
28 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
29 import org.onap.cps.ncmp.utils.TestUtils
30 import org.onap.cps.utils.JsonObjectMapper
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.test.annotation.DirtiesContext
35 import org.testcontainers.spock.Testcontainers
37 import java.time.Duration
39 @SpringBootTest(classes = [NcmpAsyncRequestResponseEventProducer, NcmpAsyncRequestResponseEventConsumer, ObjectMapper, JsonObjectMapper])
42 class NcmpAsyncRequestResponseEventProducerIntegrationSpec extends MessagingBaseSpec {
45 NcmpAsyncRequestResponseEventProducer cpsAsyncRequestResponseEventProducerService =
46 new NcmpAsyncRequestResponseEventProducer(kafkaTemplate);
49 NcmpAsyncRequestResponseEventMapper ncmpAsyncRequestResponseEventMapper =
50 Mappers.getMapper(NcmpAsyncRequestResponseEventMapper.class)
53 NcmpAsyncRequestResponseEventConsumer ncmpAsyncRequestResponseEventConsumer =
54 new NcmpAsyncRequestResponseEventConsumer(cpsAsyncRequestResponseEventProducerService,
55 ncmpAsyncRequestResponseEventMapper)
58 JsonObjectMapper jsonObjectMapper
60 def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('test'))
62 def 'Consume and forward valid message'() {
63 given: 'consumer has a subscription'
64 kafkaConsumer.subscribe(['test-topic'] as List<String>)
65 and: 'an event is sent'
66 def jsonData = TestUtils.getResourceFileContent('dmiAsyncRequestResponseEvent.json')
67 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, DmiAsyncRequestResponseEvent.class)
68 when: 'the event is consumed'
69 ncmpAsyncRequestResponseEventConsumer.consumeAndForward(testEventSent)
70 and: 'the topic is polled'
71 def records = kafkaConsumer.poll(Duration.ofMillis(1500))
72 then: 'poll returns one record'
73 assert records.size() == 1
74 and: 'consumed forwarded event id is the same as sent event id'
75 def record = records.iterator().next()
76 assert testEventSent.eventId.equalsIgnoreCase(jsonObjectMapper.convertJsonString(record.value(),
77 NcmpAsyncRequestResponseEvent).getForwardedEvent().getEventId())