2 * Copyright © 2018-2019 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
19 import kotlinx.coroutines.delay
20 import kotlinx.coroutines.launch
21 import kotlinx.coroutines.runBlocking
22 import org.apache.kafka.common.serialization.Serdes
23 import org.apache.kafka.streams.Topology
24 import org.apache.kafka.streams.processor.Processor
25 import org.apache.kafka.streams.processor.ProcessorSupplier
26 import org.apache.kafka.streams.state.Stores
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.message.BluePrintMessageLibConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
33 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.annotation.DirtiesContext
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import kotlin.test.assertNotNull
42 @RunWith(SpringRunner::class)
44 @ContextConfiguration(classes = [BluePrintMessageLibConfiguration::class,
45 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class])
46 @TestPropertySource(properties =
48 "blueprintsprocessor.messageproducer.sample.type=kafka-basic-auth",
49 "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
50 "blueprintsprocessor.messageproducer.sample.topic=default-stream-topic",
51 "blueprintsprocessor.messageproducer.sample.clientId=default-client-id",
53 "blueprintsprocessor.messageconsumer.stream-consumer.type=kafka-streams-basic-auth",
54 "blueprintsprocessor.messageconsumer.stream-consumer.bootstrapServers=127.0.0.1:9092",
55 "blueprintsprocessor.messageconsumer.stream-consumer.applicationId=test-streams-application",
56 "blueprintsprocessor.messageconsumer.stream-consumer.topic=default-stream-topic"
59 class KafkaStreamsBasicAuthConsumerServiceTest {
61 lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
64 fun testProperties() {
65 val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
66 assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
69 /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
71 fun testKafkaStreamingMessageConsumer() {
73 val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
75 // Dynamic Consumer Function to create Topology
76 val consumerFunction = object : KafkaStreamConsumerFunction {
77 override suspend fun createTopology(messageConsumerProperties: MessageConsumerProperties,
78 additionalConfig: Map<String, Any>?): Topology {
79 val topology = Topology()
80 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
81 as KafkaStreamsBasicAuthConsumerProperties
83 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
84 topology.addSource("Source", *topics.toTypedArray())
86 val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
87 override fun get(): Processor<ByteArray, ByteArray> {
88 return FirstProcessor()
91 val changelogConfig: MutableMap<String, String> = hashMapOf()
92 changelogConfig.put("min.insync.replicas", "1")
95 val countStoreSupplier = Stores.keyValueStoreBuilder(
96 Stores.persistentKeyValueStore("PriorityMessageState"),
98 PriorityMessageSerde())
99 .withLoggingEnabled(changelogConfig)
101 topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
102 topology.addStateStore(countStoreSupplier, "FirstProcessor")
103 topology.addSink("SINK", "default-stream-topic-out", Serdes.String().serializer(),
104 PriorityMessageSerde().serializer(), "FirstProcessor")
109 /** Send message with every 1 sec */
110 val blueprintMessageProducerService = bluePrintMessageLibPropertyService
111 .blueprintMessageProducerService("sample") as KafkaBasicAuthMessageProducerService
115 val headers: MutableMap<String, String> = hashMapOf()
116 headers["id"] = it.toString()
117 blueprintMessageProducerService.sendMessageNB(message = "this is my message($it)",
121 streamingConsumerService.consume(null, consumerFunction)
123 streamingConsumerService.shutDown()