a6ebe9c3eff6c6591c2384ffcfc9a343e35e71dc
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / ExecutionServiceControllerTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
20 import kotlinx.coroutines.runBlocking
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
25 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
27 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
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.test.context.ContextConfiguration
34 import org.springframework.test.context.TestPropertySource
35 import org.springframework.test.context.junit4.SpringRunner
36 import org.springframework.test.web.reactive.server.WebTestClient
37 import org.springframework.web.reactive.function.BodyInserters
38 import java.io.File
39 import java.util.UUID
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.assertTrue
43
44 @RunWith(SpringRunner::class)
45 @WebFluxTest
46 @ContextConfiguration(
47     classes = [ExecutionServiceHandler::class, BluePrintCoreConfiguration::class,
48         BluePrintCatalogService::class, SecurityProperties::class]
49 )
50 @ComponentScan(
51     basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
52         "org.onap.ccsdk.cds.controllerblueprints"]
53 )
54 @TestPropertySource(locations = ["classpath:application-test.properties"])
55 class ExecutionServiceControllerTest {
56
57     @Autowired
58     lateinit var blueprintsProcessorCatalogService: BluePrintCatalogService
59     @Autowired
60     lateinit var webTestClient: WebTestClient
61
62     @BeforeTest
63     fun init() {
64         deleteDir("target", "blueprints")
65     }
66
67     @AfterTest
68     fun cleanDir() {
69         deleteDir("target", "blueprints")
70     }
71
72     @Test
73     fun `test rest process`() {
74         runBlocking {
75             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
76
77             val executionServiceInput = JacksonUtils
78                 .readValueFromClassPathFile(
79                     "execution-input/default-input.json",
80                     ExecutionServiceInput::class.java
81                 )!!
82
83             webTestClient
84                 .post()
85                 .uri("/api/v1/execution-service/process")
86                 .body(BodyInserters.fromObject(executionServiceInput))
87                 .exchange()
88                 .expectStatus().isOk
89         }
90     }
91
92     @Test
93     fun `rest resource process should return status code 500 in case of server-side exception`() {
94         runBlocking {
95             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
96
97             val executionServiceInput = JacksonUtils
98                 .readValueFromClassPathFile(
99                     "execution-input/faulty-input.json",
100                     ExecutionServiceInput::class.java
101                 )!!
102
103             webTestClient
104                 .post()
105                 .uri("/api/v1/execution-service/process")
106                 .body(BodyInserters.fromObject(executionServiceInput))
107                 .exchange()
108                 .expectStatus().is5xxServerError
109         }
110     }
111
112     private fun loadTestCbaFile(): File {
113         val testCbaFile = normalizedFile("./src/test/resources/test-cba.zip")
114         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
115         return testCbaFile
116     }
117 }