aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/commons/message-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/message/service/KafkaStreamsConsumerServiceTest.kt
blob: 657cf64ff732391bd892ed8ee2fad39cc2faf871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright © 2018-2019 AT&T Intellectual Property.
 * Modifications Copyright © 2021 Bell Canada.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.message.service

import io.micrometer.core.instrument.MeterRegistry
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.apache.kafka.common.serialization.Serdes
import org.apache.kafka.streams.Topology
import org.apache.kafka.streams.processor.Processor
import org.apache.kafka.streams.processor.ProcessorSupplier
import org.apache.kafka.streams.state.Stores
import org.junit.Test
import org.junit.runner.RunWith
import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
import org.onap.ccsdk.cds.blueprintsprocessor.message.BluePrintMessageLibConfiguration
import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit4.SpringRunner
import kotlin.test.assertNotNull

@RunWith(SpringRunner::class)
@DirtiesContext
@ContextConfiguration(
    classes = [
        BluePrintMessageLibConfiguration::class,
        BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class
    ]
)
@TestPropertySource(
    properties =
        [
            "blueprintsprocessor.messageproducer.sample.type=kafka-scram-ssl-auth",
            "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
            "blueprintsprocessor.messageproducer.sample.topic=default-stream-topic",
            "blueprintsprocessor.messageproducer.sample.clientId=default-client-id",
            "blueprintsprocessor.messageproducer.sample.truststore=/path/to/truststore.jks",
            "blueprintsprocessor.messageproducer.sample.truststorePassword=secretpassword",
            "blueprintsprocessor.messageproducer.sample.scramUsername=sample-user",
            "blueprintsprocessor.messageproducer.sample.scramPassword=secretpassword",

            "blueprintsprocessor.messageconsumer.stream-consumer.type=kafka-streams-scram-ssl-auth",
            "blueprintsprocessor.messageconsumer.stream-consumer.bootstrapServers=127.0.0.1:9092",
            "blueprintsprocessor.messageconsumer.stream-consumer.applicationId=test-streams-application",
            "blueprintsprocessor.messageconsumer.stream-consumer.topic=default-stream-topic",
            "blueprintsprocessor.messageproducer.stream-consumer.truststore=/path/to/truststore.jks",
            "blueprintsprocessor.messageproducer.stream-consumer.truststorePassword=secretpassword",
            "blueprintsprocessor.messageproducer.stream-consumer.scramUsername=sample-user",
            "blueprintsprocessor.messageproducer.stream-consumer.scramPassword=secretpassword"

        ]
)
class KafkaStreamsConsumerServiceTest {

    @Autowired
    lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService

    @MockBean
    lateinit var meterRegistry: MeterRegistry

    @Test
    fun testProperties() {
        val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
        assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
    }

    /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
    // @Test
    fun testKafkaStreamingMessageConsumer() {
        runBlocking {
            val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")

            // Dynamic Consumer Function to create Topology
            val consumerFunction = object : KafkaStreamConsumerFunction {
                override suspend fun createTopology(
                    messageConsumerProperties: MessageConsumerProperties,
                    additionalConfig: Map<String, Any>?
                ): Topology {
                    val topology = Topology()
                    val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
                        as KafkaStreamsBasicAuthConsumerProperties

                    val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
                    topology.addSource("Source", *topics.toTypedArray())
                    // Processor Supplier
                    val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
                        override fun get(): Processor<ByteArray, ByteArray> {
                            return FirstProcessor()
                        }
                    }
                    val changelogConfig: MutableMap<String, String> = hashMapOf()
                    changelogConfig.put("min.insync.replicas", "1")

                    // Store Buolder
                    val countStoreSupplier = Stores.keyValueStoreBuilder(
                        Stores.persistentKeyValueStore("PriorityMessageState"),
                        Serdes.String(),
                        PriorityMessageSerde()
                    )
                        .withLoggingEnabled(changelogConfig)

                    topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
                    topology.addStateStore(countStoreSupplier, "FirstProcessor")
                    topology.addSink(
                        "SINK", "default-stream-topic-out", Serdes.String().serializer(),
                        PriorityMessageSerde().serializer(), "FirstProcessor"
                    )
                    return topology
                }
            }

            /** Send message with every 1 sec */
            val blueprintMessageProducerService = bluePrintMessageLibPropertyService
                .blueprintMessageProducerService("sample") as KafkaMessageProducerService
            launch {
                repeat(5) {
                    delay(1000)
                    val headers: MutableMap<String, String> = hashMapOf()
                    headers["id"] = it.toString()
                    blueprintMessageProducerService.sendMessageNB(
                        key = "mykey",
                        message = "this is my message($it)",
                        headers = headers
                    )
                }
            }
            streamingConsumerService.consume(null, consumerFunction)
            delay(10000)
            streamingConsumerService.shutDown()
        }
    }
}