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 org.apache.kafka.clients.consumer.ConsumerConfig
24 import org.apache.kafka.clients.producer.ProducerConfig
25 import org.apache.kafka.common.serialization.StringSerializer
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
28 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
29 import org.springframework.kafka.core.DefaultKafkaProducerFactory
30 import org.springframework.kafka.core.KafkaTemplate
31 import org.springframework.kafka.support.serializer.JsonSerializer
32 import org.testcontainers.containers.KafkaContainer
33 import org.testcontainers.spock.Testcontainers
34 import org.testcontainers.utility.DockerImageName
36 import java.time.Duration
37 import com.fasterxml.jackson.databind.ObjectMapper
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.apache.kafka.clients.consumer.KafkaConsumer
40 import org.apache.kafka.common.serialization.StringDeserializer
41 import org.onap.cps.ncmp.utils.TestUtils;
42 import org.springframework.boot.test.context.SpringBootTest
43 import org.spockframework.spring.SpringBean
44 import org.springframework.test.annotation.DirtiesContext
45 import org.springframework.test.context.DynamicPropertyRegistry
46 import org.springframework.test.context.DynamicPropertySource
47 import spock.lang.Specification
49 @SpringBootTest(classes = [NcmpAsyncRequestResponseEventProducer, NcmpAsyncRequestResponseEventConsumer])
52 class NcmpAsyncRequestResponseEventProducerIntegrationSpec extends Specification {
54 static kafkaTestContainer = new KafkaContainer(
55 DockerImageName.parse('confluentinc/cp-kafka:6.2.1')
59 Runtime.getRuntime().addShutdownHook(new Thread(kafkaTestContainer::stop))
63 kafkaTestContainer.start()
66 def producerConfigProperties = [
67 (ProducerConfig.BOOTSTRAP_SERVERS_CONFIG) : kafkaTestContainer.getBootstrapServers().split(',')[0],
68 (ProducerConfig.RETRIES_CONFIG) : 0,
69 (ProducerConfig.BATCH_SIZE_CONFIG) : 16384,
70 (ProducerConfig.LINGER_MS_CONFIG) : 1,
71 (ProducerConfig.BUFFER_MEMORY_CONFIG) : 33554432,
72 (ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG) : StringSerializer,
73 (ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG) : JsonSerializer
76 def consumerConfigProperties = [
77 (ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG) : kafkaTestContainer.getBootstrapServers().split(',')[0],
78 (ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG) : StringDeserializer,
79 (ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG): StringDeserializer,
80 (ConsumerConfig.AUTO_OFFSET_RESET_CONFIG) : 'earliest',
81 (ConsumerConfig.GROUP_ID_CONFIG) : 'test'
84 def kafkaTemplate = new KafkaTemplate<>(new DefaultKafkaProducerFactory<Integer, String>(producerConfigProperties))
87 NcmpAsyncRequestResponseEventProducer cpsAsyncRequestResponseEventProducerService =
88 new NcmpAsyncRequestResponseEventProducer(kafkaTemplate);
91 NcmpAsyncRequestResponseEventMapper ncmpAsyncRequestResponseEventMapper =
92 Mappers.getMapper(NcmpAsyncRequestResponseEventMapper.class)
95 NcmpAsyncRequestResponseEventConsumer ncmpAsyncRequestResponseEventConsumer =
96 new NcmpAsyncRequestResponseEventConsumer(cpsAsyncRequestResponseEventProducerService,
97 ncmpAsyncRequestResponseEventMapper)
99 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
101 def kafkaConsumer = new KafkaConsumer<>(getConsumerConfigProperties())
103 def 'Consume and forward valid message'() {
104 given: 'consumer has a subscription'
105 kafkaConsumer.subscribe(['test-topic'] as List<String>)
106 and: 'an event is sent'
107 def jsonData = TestUtils.getResourceFileContent('dmiAsyncRequestResponseEvent.json')
108 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, DmiAsyncRequestResponseEvent.class)
109 when: 'the event is consumed'
110 ncmpAsyncRequestResponseEventConsumer.consumeAndForward(testEventSent)
111 and: 'the topic is polled'
112 def records = kafkaConsumer.poll(Duration.ofMillis(1500))
113 then: 'poll returns one record'
114 assert records.size() == 1
115 and: 'consumed forwarded event id is the same as sent event id'
116 def record = records.iterator().next()
117 assert testEventSent.eventId.equalsIgnoreCase(jsonObjectMapper.convertJsonString(record.value(),
118 NcmpAsyncRequestResponseEvent).getForwardedEvent().getEventId())
121 @DynamicPropertySource
122 static void registerKafkaProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
123 dynamicPropertyRegistry.add('spring.kafka.bootstrap-servers', kafkaTestContainer::getBootstrapServers)