Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / MockKafkaTopologyComponents.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 org.apache.kafka.common.serialization.Deserializer
20 import org.apache.kafka.common.serialization.Serde
21 import org.apache.kafka.common.serialization.Serializer
22 import org.apache.kafka.streams.processor.Processor
23 import org.apache.kafka.streams.processor.ProcessorContext
24 import org.apache.kafka.streams.state.KeyValueStore
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import java.io.Serializable
30 import java.nio.charset.Charset
31 import java.util.UUID
32
33 class PriorityMessage : Serializable {
34
35     lateinit var id: String
36     lateinit var requestMessage: String
37 }
38
39 open class PriorityMessageSerde : Serde<PriorityMessage> {
40
41     override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
42     }
43
44     override fun close() {
45     }
46
47     override fun deserializer(): Deserializer<PriorityMessage> {
48         return object : Deserializer<PriorityMessage> {
49             override fun deserialize(topic: String, data: ByteArray): PriorityMessage {
50                 return JacksonUtils.readValue(String(data), PriorityMessage::class.java)
51                     ?: throw BluePrintProcessorException("failed to convert")
52             }
53
54             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
55             }
56
57             override fun close() {
58             }
59         }
60     }
61
62     override fun serializer(): Serializer<PriorityMessage> {
63         return object : Serializer<PriorityMessage> {
64             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
65             }
66
67             override fun serialize(topic: String?, data: PriorityMessage): ByteArray {
68                 return data.asJsonString().toByteArray(Charset.defaultCharset())
69             }
70
71             override fun close() {
72             }
73         }
74     }
75 }
76
77 class FirstProcessor : Processor<ByteArray, ByteArray> {
78
79     private val log = logger(FirstProcessor::class)
80
81     private lateinit var context: ProcessorContext
82     private lateinit var kvStore: KeyValueStore<String, PriorityMessage>
83
84     override fun process(key: ByteArray, value: ByteArray) {
85         log.info("First Processor key(${String(key)} : value(${String(value)})")
86         val newMessage = PriorityMessage().apply {
87             id = UUID.randomUUID().toString()
88             requestMessage = String(value)
89         }
90         kvStore.put(newMessage.id, newMessage)
91         this.context.forward(newMessage.id, newMessage)
92     }
93
94     override fun init(context: ProcessorContext) {
95         log.info("init... ${context.keySerde()}, ${context.valueSerde()}")
96         this.context = context
97         this.kvStore = context.getStateStore("PriorityMessageState") as KeyValueStore<String, PriorityMessage>
98     }
99
100     override fun close() {
101         log.info("Close...")
102     }
103 }