Merge "Resource resolution fix + improvement."
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / ExecutionServiceControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
18
19 import kotlinx.coroutines.reactive.awaitSingle
20 import kotlinx.coroutines.runBlocking
21 import org.junit.After
22 import org.junit.Before
23 import org.junit.runner.RunWith
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
25 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
26 import org.slf4j.LoggerFactory
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
29 import org.springframework.boot.autoconfigure.security.SecurityProperties
30 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
31 import org.springframework.context.annotation.ComponentScan
32 import org.springframework.core.io.ByteArrayResource
33 import org.springframework.http.client.MultipartBodyBuilder
34 import org.springframework.test.annotation.DirtiesContext
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import org.springframework.test.web.reactive.server.WebTestClient
39 import org.springframework.test.web.reactive.server.returnResult
40 import org.springframework.web.reactive.function.BodyInserters
41 import java.io.File
42 import java.nio.file.Files
43 import java.nio.file.Paths
44 import kotlin.test.Test
45
46 @RunWith(SpringRunner::class)
47 @EnableAutoConfiguration
48 @ContextConfiguration(classes = [ExecutionServiceControllerTest::class, SecurityProperties::class])
49 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
50 @TestPropertySource(locations = ["classpath:application-test.properties"])
51 @DirtiesContext
52 @WebFluxTest
53 class ExecutionServiceControllerTest {
54
55     private val log = LoggerFactory.getLogger(ExecutionServiceControllerTest::class.java)!!
56
57     @Autowired
58     lateinit var webTestClient: WebTestClient
59
60     var event: ExecutionServiceInput? = null
61
62     @Before
63     fun setup() {
64         deleteDir("target", "blueprints")
65     }
66
67     @After
68     fun clean() {
69         deleteDir("target", "blueprints")
70     }
71
72     @Test
73     fun uploadBluePrint() {
74         runBlocking {
75             val body = MultipartBodyBuilder().apply {
76                 part("file", object : ByteArrayResource(Files.readAllBytes(loadCbaArchive().toPath())) {
77                     override fun getFilename(): String {
78                         return "test-cba.zip"
79                     }
80                 })
81             }.build()
82
83             webTestClient
84                     .post()
85                     .uri("/api/v1/execution-service/upload")
86                     .body(BodyInserters.fromMultipartData(body))
87                     .exchange()
88                     .expectStatus().isOk
89                     .returnResult<String>()
90                     .responseBody
91                     .awaitSingle()
92         }
93     }
94
95     private fun loadCbaArchive(): File {
96         return Paths.get("./src/test/resources/cba-for-kafka-integration_enriched.zip").toFile()
97     }
98 }
99
100