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