9cbd898dcaca7c82af83202c849578fb37ff9c91
[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.io.File
42 import java.nio.file.Files
43 import java.nio.file.Paths
44 import java.util.*
45 import kotlin.test.AfterTest
46 import kotlin.test.BeforeTest
47 import kotlin.test.assertTrue
48
49 @RunWith(SpringRunner::class)
50 @WebFluxTest
51 @ContextConfiguration(classes = [ExecutionServiceHandler::class, BluePrintCoreConfiguration::class, BluePrintCatalogService::class, SecurityProperties::class])
52 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
53 @TestPropertySource(locations = ["classpath:application-test.properties"])
54 class ExecutionServiceHandlerTest {
55
56     @Autowired
57     lateinit var blueprintCatalog: BluePrintCatalogService
58     @Autowired
59     lateinit var webTestClient: WebTestClient
60
61     @BeforeTest
62     fun init() {
63         deleteDir("target", "blueprints")
64     }
65
66     @AfterTest
67     fun cleanDir() {
68         deleteDir("target", "blueprints")
69     }
70
71
72     @Test
73     fun `test rest upload blueprint`() {
74         runBlocking {
75             val body = MultipartBodyBuilder().apply {
76                 part("file", object : ByteArrayResource(Files.readAllBytes(loadTestCbaFile().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
96     @Test
97     fun `test rest process`() {
98         runBlocking {
99             blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
100
101             val executionServiceInput = JacksonUtils
102                     .readValueFromClassPathFile("execution-input/default-input.json",
103                             ExecutionServiceInput::class.java)!!
104
105             webTestClient
106                     .post()
107                     .uri("/api/v1/execution-service/process")
108                     .body(BodyInserters.fromObject(executionServiceInput))
109                     .exchange()
110                     .expectStatus().isOk
111         }
112     }
113
114     @Test
115     fun `rest resource process should return status code 500 in case of server-side exception`() {
116         runBlocking {
117             blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
118
119             val executionServiceInput = JacksonUtils
120                     .readValueFromClassPathFile("execution-input/faulty-input.json",
121                             ExecutionServiceInput::class.java)!!
122
123             webTestClient
124                     .post()
125                     .uri("/api/v1/execution-service/process")
126                     .body(BodyInserters.fromObject(executionServiceInput))
127                     .exchange()
128                     .expectStatus().is5xxServerError
129         }
130     }
131
132     private fun loadTestCbaFile(): File {
133         val testCbaFile = Paths.get("./src/test/resources/test-cba.zip").toFile()
134         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
135         return testCbaFile
136     }
137 }