1657d70b4065e3b5b383f28a4bf55bef17a6c82c
[ccsdk/cds.git] /
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 @RunWith(SpringRunner::class)
42 @DirtiesContext
43 @ContextConfiguration(
44     classes = [BluePrintMessageLibConfiguration::class,
45         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
46 )
47 @TestPropertySource(
48     properties =
49     [
50         "blueprintsprocessor.messageproducer.sample.type=kafka-basic-auth",
51         "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
52         "blueprintsprocessor.messageproducer.sample.topic=default-stream-topic",
53         "blueprintsprocessor.messageproducer.sample.clientId=default-client-id",
54
55         "blueprintsprocessor.messageconsumer.stream-consumer.type=kafka-streams-basic-auth",
56         "blueprintsprocessor.messageconsumer.stream-consumer.bootstrapServers=127.0.0.1:9092",
57         "blueprintsprocessor.messageconsumer.stream-consumer.applicationId=test-streams-application",
58         "blueprintsprocessor.messageconsumer.stream-consumer.topic=default-stream-topic"
59
60     ]
61 )
62 class KafkaStreamsBasicAuthConsumerServiceTest {
63
64     @Autowired
65     lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
66
67     @Test
68     fun testProperties() {
69         val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
70         assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
71     }
72
73     /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
74     // @Test
75     fun testKafkaStreamingMessageConsumer() {
76         runBlocking {
77             val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
78
79             // Dynamic Consumer Function to create Topology
80             val consumerFunction = object : KafkaStreamConsumerFunction {
81                 override suspend fun createTopology(
82                     messageConsumerProperties: MessageConsumerProperties,
83                     additionalConfig: Map<String, Any>?
84                 ): Topology {
85                     val topology = Topology()
86                     val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
87                             as KafkaStreamsBasicAuthConsumerProperties
88
89                     val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
90                     topology.addSource("Source", *topics.toTypedArray())
91                     // Processor Supplier
92                     val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
93                         override fun get(): Processor<ByteArray, ByteArray> {
94                             return FirstProcessor()
95                         }
96                     }
97                     val changelogConfig: MutableMap<String, String> = hashMapOf()
98                     changelogConfig.put("min.insync.replicas", "1")
99
100                     // Store Buolder
101                     val countStoreSupplier = Stores.keyValueStoreBuilder(
102                         Stores.persistentKeyValueStore("PriorityMessageState"),
103                         Serdes.String(),
104                         PriorityMessageSerde()
105                     )
106                         .withLoggingEnabled(changelogConfig)
107
108                     topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
109                     topology.addStateStore(countStoreSupplier, "FirstProcessor")
110                     topology.addSink(
111                         "SINK", "default-stream-topic-out", Serdes.String().serializer(),
112                         PriorityMessageSerde().serializer(), "FirstProcessor"
113                     )
114                     return topology
115                 }
116             }
117
118             /** Send message with every 1 sec */
119             val blueprintMessageProducerService = bluePrintMessageLibPropertyService
120                 .blueprintMessageProducerService("sample") as KafkaBasicAuthMessageProducerService
121             launch {
122                 repeat(5) {
123                     delay(1000)
124                     val headers: MutableMap<String, String> = hashMapOf()
125                     headers["id"] = it.toString()
126                     blueprintMessageProducerService.sendMessageNB(
127                         message = "this is my message($it)",
128                         headers = headers
129                     )
130                 }
131             }
132             streamingConsumerService.consume(null, consumerFunction)
133             delay(10000)
134             streamingConsumerService.shutDown()
135         }
136     }
137 }