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
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.config.kafka
23 import io.cloudevents.CloudEvent
24 import io.cloudevents.kafka.CloudEventDeserializer
25 import io.cloudevents.kafka.CloudEventSerializer
26 import org.spockframework.spring.EnableSharedInjection
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.autoconfigure.kafka.KafkaProperties
29 import org.springframework.boot.context.properties.EnableConfigurationProperties
30 import org.springframework.boot.test.context.SpringBootTest
31 import org.springframework.kafka.core.KafkaTemplate
32 import org.springframework.kafka.support.serializer.JsonDeserializer
33 import org.springframework.kafka.support.serializer.JsonSerializer
34 import org.springframework.test.context.TestPropertySource
35 import spock.lang.Shared
36 import spock.lang.Specification
38 @SpringBootTest(classes = [KafkaProperties, KafkaConfig])
39 @EnableSharedInjection
40 @EnableConfigurationProperties
41 @TestPropertySource(properties = ["cps.tracing.enabled=true"])
42 class KafkaConfigSpec extends Specification {
46 KafkaTemplate<String, String> legacyEventKafkaTemplate
50 KafkaTemplate<String, CloudEvent> cloudEventKafkaTemplate
52 def 'Verify kafka template serializer and deserializer configuration for #eventType.'() {
53 expect: 'kafka template is instantiated'
54 assert kafkaTemplateInstance.properties['beanName'] == beanName
55 and: 'verify event key and value serializer'
56 assert kafkaTemplateInstance.properties['producerFactory'].configs['value.serializer'].asType(String.class).contains(valueSerializer.getCanonicalName())
57 and: 'verify event key and value deserializer'
58 assert kafkaTemplateInstance.properties['consumerFactory'].configs['spring.deserializer.value.delegate.class'].asType(String.class).contains(delegateDeserializer.getCanonicalName())
59 where: 'the following event type is used'
60 eventType | kafkaTemplateInstance || beanName | valueSerializer | delegateDeserializer
61 'legacy event' | legacyEventKafkaTemplate || 'legacyEventKafkaTemplate' | JsonSerializer | JsonDeserializer
62 'cloud event' | cloudEventKafkaTemplate || 'cloudEventKafkaTemplate' | CloudEventSerializer | CloudEventDeserializer