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
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 spock.lang.Shared
35 import spock.lang.Specification
37 @SpringBootTest(classes = [KafkaProperties, KafkaConfig])
38 @EnableSharedInjection
39 @EnableConfigurationProperties
40 class KafkaConfigSpec extends Specification {
44 KafkaTemplate<String, String> legacyEventKafkaTemplate
48 KafkaTemplate<String, CloudEvent> cloudEventKafkaTemplate
50 def 'Verify kafka template serializer and deserializer configuration for #eventType.'() {
51 expect: 'kafka template is instantiated'
52 assert kafkaTemplateInstance.properties['beanName'] == beanName
53 and: 'verify event key and value serializer'
54 assert kafkaTemplateInstance.properties['producerFactory'].configs['value.serializer'].asType(String.class).contains(valueSerializer.getCanonicalName())
55 and: 'verify event key and value deserializer'
56 assert kafkaTemplateInstance.properties['consumerFactory'].configs['spring.deserializer.value.delegate.class'].asType(String.class).contains(delegateDeserializer.getCanonicalName())
57 where: 'the following event type is used'
58 eventType | kafkaTemplateInstance || beanName | valueSerializer | delegateDeserializer
59 'legacy event' | legacyEventKafkaTemplate || 'legacyEventKafkaTemplate' | JsonSerializer | JsonDeserializer
60 'cloud event' | cloudEventKafkaTemplate || 'cloudEventKafkaTemplate' | CloudEventSerializer | CloudEventDeserializer