d14761cc914474f2a667908cffb311fb0417a6d4
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
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
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
20 import kotlinx.coroutines.reactive.awaitSingle
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
26 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.autoconfigure.security.SecurityProperties
31 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
32 import org.springframework.context.annotation.ComponentScan
33 import org.springframework.core.io.ByteArrayResource
34 import org.springframework.http.client.MultipartBodyBuilder
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.nio.file.Files
42 import java.nio.file.Paths
43 import java.util.*
44 import kotlin.test.AfterTest
45 import kotlin.test.BeforeTest
46 import kotlin.test.assertTrue
47
48 @RunWith(SpringRunner::class)
49 @WebFluxTest
50 @ContextConfiguration(classes = [ExecutionServiceHandler::class, BluePrintCoreConfiguration::class, BluePrintCatalogService::class, SecurityProperties::class])
51 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
52 @TestPropertySource(locations = ["classpath:application-test.properties"])
53 class ExecutionServiceHandlerTest {
54
55     @Autowired
56     lateinit var blueprintCatalog: BluePrintCatalogService
57     @Autowired
58     lateinit var webTestClient: WebTestClient
59
60     @BeforeTest
61     fun init() {
62         deleteDir("target", "blueprints")
63     }
64
65     @AfterTest
66     fun cleanDir() {
67         deleteDir("target", "blueprints")
68     }
69
70
71     @Test
72     fun `test rest upload blueprint`() {
73         runBlocking {
74             val file = Paths.get("./src/test/resources/test-cba.zip").toFile()
75             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
76
77             val body = MultipartBodyBuilder().apply {
78                 part("file", object : ByteArrayResource(Files.readAllBytes(Paths.get("./src/test/resources/test-cba.zip"))) {
79                     override fun getFilename(): String {
80                         return "test-cba.zip"
81                     }
82                 })
83             }.build()
84
85             webTestClient
86                     .post()
87                     .uri("/api/v1/execution-service/upload")
88                     .body(BodyInserters.fromMultipartData(body))
89                     .exchange()
90                     .expectStatus().isOk
91                     .returnResult<String>()
92                     .responseBody
93                     .awaitSingle()
94         }
95
96     }
97
98     @Test
99     fun `test rest process`() {
100         runBlocking {
101             val file = Paths.get("./src/test/resources/test-cba.zip").toFile()
102             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
103             blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), file)
104
105             val executionServiceInput = JacksonUtils
106                     .readValueFromClassPathFile("execution-input/default-input.json",
107                             ExecutionServiceInput::class.java)!!
108
109             webTestClient
110                     .post()
111                     .uri("/api/v1/execution-service/process")
112                     .body(BodyInserters.fromObject(executionServiceInput))
113                     .exchange()
114                     .expectStatus().isOk
115         }
116     }
117 }