9cd9746226b209998dcb31f8d29c2b3d232dcc3e
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / KafkaStreamsBasicAuthConsumerServiceTest.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
18
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
27 import org.junit.Test
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
40
41
42 @RunWith(SpringRunner::class)
43 @DirtiesContext
44 @ContextConfiguration(classes = [BluePrintMessageLibConfiguration::class,
45     BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class])
46 @TestPropertySource(properties =
47 [
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",
52
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"
57
58 ])
59 class KafkaStreamsBasicAuthConsumerServiceTest {
60     @Autowired
61     lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
62
63     @Test
64     fun testProperties() {
65         val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
66         assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
67     }
68
69     /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
70     //@Test
71     fun testKafkaStreamingMessageConsumer() {
72         runBlocking {
73             val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
74
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
82
83                     val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
84                     topology.addSource("Source", *topics.toTypedArray())
85                     // Processor Supplier
86                     val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
87                         override fun get(): Processor<ByteArray, ByteArray> {
88                             return FirstProcessor()
89                         }
90                     }
91                     val changelogConfig: MutableMap<String, String> = hashMapOf()
92                     changelogConfig.put("min.insync.replicas", "1")
93
94                     // Store Buolder
95                     val countStoreSupplier = Stores.keyValueStoreBuilder(
96                             Stores.persistentKeyValueStore("PriorityMessageState"),
97                             Serdes.String(),
98                             PriorityMessageSerde())
99                             .withLoggingEnabled(changelogConfig)
100
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")
105                     return topology
106                 }
107             }
108
109             /** Send message with every 1 sec */
110             val blueprintMessageProducerService = bluePrintMessageLibPropertyService
111                     .blueprintMessageProducerService("sample") as KafkaBasicAuthMessageProducerService
112             launch {
113                 repeat(5) {
114                     delay(1000)
115                     val headers: MutableMap<String, String> = hashMapOf()
116                     headers["id"] = it.toString()
117                     blueprintMessageProducerService.sendMessageNB(message = "this is my message($it)",
118                             headers = headers)
119                 }
120             }
121             streamingConsumerService.consume(null, consumerFunction)
122             delay(10000)
123             streamingConsumerService.shutDown()
124         }
125     }
126 }