2 * Copyright © 2019 Bell Canada
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.
16 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.messaginglib
18 import com.fasterxml.jackson.databind.node.ObjectNode
19 import kotlinx.coroutines.reactive.awaitSingle
20 import kotlinx.coroutines.runBlocking
21 import org.apache.commons.lang.builder.ToStringBuilder
22 import org.apache.kafka.clients.CommonClientConfigs
23 import org.apache.kafka.clients.consumer.ConsumerConfig
24 import org.apache.kafka.common.serialization.StringDeserializer
25 import org.junit.After
26 import org.junit.Before
27 import org.junit.Ignore
29 import org.junit.runner.RunWith
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
34 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.MessagingController
35 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
37 import org.slf4j.LoggerFactory
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.beans.factory.annotation.Value
40 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
41 import org.springframework.boot.autoconfigure.security.SecurityProperties
42 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
43 import org.springframework.context.annotation.Bean
44 import org.springframework.context.annotation.ComponentScan
45 import org.springframework.context.annotation.Configuration
46 import org.springframework.core.io.ByteArrayResource
47 import org.springframework.http.client.MultipartBodyBuilder
48 import org.springframework.kafka.annotation.EnableKafka
49 import org.springframework.kafka.annotation.KafkaListener
50 import org.springframework.kafka.annotation.PartitionOffset
51 import org.springframework.kafka.annotation.TopicPartition
52 import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
53 import org.springframework.kafka.core.ConsumerFactory
54 import org.springframework.kafka.core.DefaultKafkaConsumerFactory
55 import org.springframework.kafka.core.KafkaTemplate
56 import org.springframework.kafka.support.serializer.JsonDeserializer
57 import org.springframework.kafka.test.context.EmbeddedKafka
58 import org.springframework.test.annotation.DirtiesContext
59 import org.springframework.test.context.ContextConfiguration
60 import org.springframework.test.context.TestPropertySource
61 import org.springframework.test.context.junit4.SpringRunner
62 import org.springframework.test.web.reactive.server.WebTestClient
63 import org.springframework.test.web.reactive.server.returnResult
64 import org.springframework.web.reactive.function.BodyInserters
66 import java.nio.file.Files
67 import java.nio.file.Paths
68 import kotlin.test.assertNotNull
69 //FIXME("testReceive method is failing in server build, It is not stable, may be timing issue.")
71 @RunWith(SpringRunner::class)
72 @EnableAutoConfiguration
73 @ContextConfiguration(classes = [MessagingControllerTest::class, SecurityProperties::class])
74 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
75 @TestPropertySource(locations = ["classpath:application-test.properties"])
77 @EmbeddedKafka(ports = [9092])
79 class MessagingControllerTest {
81 private val log = LoggerFactory.getLogger(MessagingControllerTest::class.java)!!
84 lateinit var controller: MessagingController
86 @Value("\${blueprintsprocessor.messageclient.self-service-api.consumerTopic}")
87 lateinit var topicUsedForConsumer: String
90 lateinit var kt: KafkaTemplate<String, ExecutionServiceInput>
93 lateinit var webTestClient: WebTestClient
95 var event: ExecutionServiceInput? = null
99 deleteDir("target", "blueprints")
105 deleteDir("target", "blueprints")
110 val samplePayload = "{\n" +
111 " \"resource-assignment-request\": {\n" +
112 " \"artifact-name\": [\"hostname\"],\n" +
113 " \"store-result\": true,\n" +
114 " \"resource-assignment-properties\" : {\n" +
115 " \"hostname\": \"demo123\"\n" +
120 kt.defaultTopic = topicUsedForConsumer
122 val input = ExecutionServiceInput().apply {
123 commonHeader = CommonHeader().apply {
126 subRequestId = "1234-1234"
129 actionIdentifiers = ActionIdentifiers().apply {
130 blueprintName = "golden"
131 blueprintVersion = "1.0.0"
132 actionName = "resource-assignment"
136 stepData = StepData().apply {
137 name = "resource-assignment"
140 payload = JacksonUtils.jsonNode(samplePayload) as ObjectNode
143 kt.sendDefault(input)
144 log.info("test-sender sent message='{}'", ToStringBuilder.reflectionToString(input))
151 @KafkaListener(topicPartitions = [TopicPartition(topic = "\${blueprintsprocessor.messageclient.self-service-api.topic}", partitionOffsets = [PartitionOffset(partition = "0", initialOffset = "0")])])
152 fun receivedEventFromBluePrintProducer(receivedEvent: ExecutionServiceInput) {
153 event = receivedEvent
156 private fun uploadBluePrint() {
158 val body = MultipartBodyBuilder().apply {
159 part("file", object : ByteArrayResource(Files.readAllBytes(loadCbaArchive().toPath())) {
160 override fun getFilename(): String {
161 return "test-cba.zip"
168 .uri("/api/v1/execution-service/upload")
169 .body(BodyInserters.fromMultipartData(body))
172 .returnResult<String>()
178 private fun loadCbaArchive():File {
179 return Paths.get("./src/test/resources/cba-for-kafka-integration_enriched.zip").toFile()
184 open class ConsumerConfiguration {
186 @Value("\${blueprintsprocessor.messageclient.self-service-api.bootstrapServers}")
187 lateinit var bootstrapServers: String
189 @Value("\${blueprintsprocessor.messageclient.self-service-api.groupId}")
190 lateinit var groupId:String
193 open fun consumerFactory2(): ConsumerFactory<String, ExecutionServiceInput>? {
194 val configProperties = hashMapOf<String, Any>()
195 configProperties[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServers
196 configProperties[ConsumerConfig.GROUP_ID_CONFIG] = groupId
197 configProperties[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
198 configProperties[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = JsonDeserializer::class.java.name
199 configProperties[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
200 configProperties[ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG] = 1000
202 return DefaultKafkaConsumerFactory(configProperties, StringDeserializer(),
203 JsonDeserializer(ExecutionServiceInput::class.java))
207 open fun listenerFactory(): ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput> {
208 val factory = ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput>()
209 factory.consumerFactory = consumerFactory2()