Fixed restconf Python scripts bugs caused by BPP refactor
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / messaginglib / MessagingControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada
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 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.messaginglib
17
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.Test
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
33 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.MessagingController
34 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
40 import org.springframework.boot.autoconfigure.security.SecurityProperties
41 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
42 import org.springframework.context.annotation.Bean
43 import org.springframework.context.annotation.ComponentScan
44 import org.springframework.context.annotation.Configuration
45 import org.springframework.core.io.ByteArrayResource
46 import org.springframework.http.client.MultipartBodyBuilder
47 import org.springframework.kafka.annotation.EnableKafka
48 import org.springframework.kafka.annotation.KafkaListener
49 import org.springframework.kafka.annotation.PartitionOffset
50 import org.springframework.kafka.annotation.TopicPartition
51 import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
52 import org.springframework.kafka.core.ConsumerFactory
53 import org.springframework.kafka.core.DefaultKafkaConsumerFactory
54 import org.springframework.kafka.core.KafkaTemplate
55 import org.springframework.kafka.support.serializer.JsonDeserializer
56 import org.springframework.kafka.test.context.EmbeddedKafka
57 import org.springframework.test.annotation.DirtiesContext
58 import org.springframework.test.context.ContextConfiguration
59 import org.springframework.test.context.TestPropertySource
60 import org.springframework.test.context.junit4.SpringRunner
61 import org.springframework.test.web.reactive.server.WebTestClient
62 import org.springframework.test.web.reactive.server.returnResult
63 import org.springframework.web.reactive.function.BodyInserters
64 import java.io.File
65 import java.nio.file.Files
66 import java.nio.file.Paths
67 import kotlin.test.assertNotNull
68
69 @RunWith(SpringRunner::class)
70 @EnableAutoConfiguration
71 @ContextConfiguration(classes = [MessagingControllerTest::class, SecurityProperties::class])
72 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
73 @TestPropertySource(locations = ["classpath:application-test.properties"])
74 @DirtiesContext
75 @EmbeddedKafka(ports = [9092])
76 @WebFluxTest
77 class MessagingControllerTest {
78
79     private val log = LoggerFactory.getLogger(MessagingControllerTest::class.java)!!
80
81     @Autowired
82     lateinit var controller: MessagingController
83
84     @Value("\${blueprintsprocessor.messageclient.self-service-api.consumerTopic}")
85     lateinit var topicUsedForConsumer: String
86
87     @Autowired
88     lateinit var kt: KafkaTemplate<String, ExecutionServiceInput>
89
90     @Autowired
91     lateinit var webTestClient: WebTestClient
92
93     var event: ExecutionServiceInput? = null
94
95     @Before
96     fun setup() {
97         deleteDir("target", "blueprints")
98         uploadBluePrint()
99     }
100
101     @After
102     fun clean() {
103         deleteDir("target", "blueprints")
104     }
105
106     @Test
107     fun testReceive() {
108         val samplePayload = "{\n" +
109                 "    \"resource-assignment-request\": {\n" +
110                 "      \"artifact-name\": [\"hostname\"],\n" +
111                 "      \"store-result\": true,\n" +
112                 "      \"resource-assignment-properties\" : {\n" +
113                 "        \"hostname\": \"demo123\"\n" +
114                 "      }\n" +
115                 "    }\n" +
116                 "  }"
117
118         kt.defaultTopic = topicUsedForConsumer
119
120         val input = ExecutionServiceInput().apply {
121             commonHeader = CommonHeader().apply {
122                 originatorId = "1"
123                 requestId = "1234"
124                 subRequestId = "1234-1234"
125             }
126
127             actionIdentifiers = ActionIdentifiers().apply {
128                 blueprintName = "golden"
129                 blueprintVersion = "1.0.0"
130                 actionName = "resource-assignment"
131                 mode = "sync"
132             }
133
134             stepData = StepData().apply {
135                 name = "resource-assignment"
136             }
137
138             payload = JacksonUtils.jsonNode(samplePayload) as ObjectNode
139         }
140
141         kt.sendDefault(input)
142         log.info("test-sender sent message='{}'", ToStringBuilder.reflectionToString(input))
143
144         Thread.sleep(1000)
145
146         assertNotNull(event)
147     }
148
149     @KafkaListener(topicPartitions = [TopicPartition(topic = "\${blueprintsprocessor.messageclient.self-service-api.topic}", partitionOffsets = [PartitionOffset(partition = "0", initialOffset = "0")])])
150     fun receivedEventFromBluePrintProducer(receivedEvent: ExecutionServiceInput) {
151         event = receivedEvent
152     }
153
154     private fun uploadBluePrint() {
155         runBlocking {
156             val body = MultipartBodyBuilder().apply {
157                 part("file", object : ByteArrayResource(Files.readAllBytes(loadCbaArchive().toPath())) {
158                     override fun getFilename(): String {
159                         return "test-cba.zip"
160                     }
161                 })
162             }.build()
163
164             webTestClient
165                     .post()
166                     .uri("/api/v1/execution-service/upload")
167                     .body(BodyInserters.fromMultipartData(body))
168                     .exchange()
169                     .expectStatus().isOk
170                     .returnResult<String>()
171                     .responseBody
172                     .awaitSingle()
173         }
174     }
175
176     private fun loadCbaArchive():File {
177         return Paths.get("./src/test/resources/cba-for-kafka-integration_enriched.zip").toFile()
178     }
179
180     @Configuration
181     @EnableKafka
182     open class ConsumerConfiguration {
183
184         @Value("\${blueprintsprocessor.messageclient.self-service-api.bootstrapServers}")
185         lateinit var bootstrapServers: String
186
187         @Value("\${blueprintsprocessor.messageclient.self-service-api.groupId}")
188         lateinit var groupId:String
189
190         @Bean
191         open fun consumerFactory2(): ConsumerFactory<String, ExecutionServiceInput>? {
192             val configProperties = hashMapOf<String, Any>()
193             configProperties[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServers
194             configProperties[ConsumerConfig.GROUP_ID_CONFIG] = groupId
195             configProperties[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
196             configProperties[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = JsonDeserializer::class.java.name
197             configProperties[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
198             configProperties[ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG] = 1000
199
200             return DefaultKafkaConsumerFactory(configProperties, StringDeserializer(),
201                     JsonDeserializer(ExecutionServiceInput::class.java))
202         }
203
204         @Bean
205         open fun listenerFactory(): ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput> {
206             val factory = ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput>()
207             factory.consumerFactory = consumerFactory2()
208             return factory
209         }
210     }
211 }
212
213