Metadata for name, version, tags and type
[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.compress
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.normalizedFile
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.security.SecurityProperties
32 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
33 import org.springframework.context.annotation.ComponentScan
34 import org.springframework.test.context.ContextConfiguration
35 import org.springframework.test.context.TestPropertySource
36 import org.springframework.test.context.junit4.SpringRunner
37 import org.springframework.test.web.reactive.server.WebTestClient
38 import org.springframework.web.reactive.function.BodyInserters
39 import java.io.File
40 import java.util.UUID
41 import kotlin.test.AfterTest
42 import kotlin.test.BeforeTest
43 import kotlin.test.assertTrue
44
45 @RunWith(SpringRunner::class)
46 @WebFluxTest
47 @ContextConfiguration(
48     classes = [ExecutionServiceHandler::class, BluePrintCoreConfiguration::class,
49         BluePrintCatalogService::class, SecurityProperties::class]
50 )
51 @ComponentScan(
52     basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
53         "org.onap.ccsdk.cds.controllerblueprints"]
54 )
55 @TestPropertySource(locations = ["classpath:application-test.properties"])
56 class ExecutionServiceControllerTest {
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         // Create sample CBA zip
68         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
69             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
70     }
71
72     @AfterTest
73     fun cleanDir() {
74         deleteDir("target", "blueprints")
75     }
76
77     @Test
78     fun `test rest process`() {
79         runBlocking {
80             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
81
82             val executionServiceInput = JacksonUtils
83                 .readValueFromClassPathFile(
84                     "execution-input/default-input.json",
85                     ExecutionServiceInput::class.java
86                 )!!
87
88             webTestClient
89                 .post()
90                 .uri("/api/v1/execution-service/process")
91                 .body(BodyInserters.fromObject(executionServiceInput))
92                 .exchange()
93                 .expectStatus().isOk
94         }
95     }
96
97     @Test
98     fun `rest resource process should return status code 500 in case of server-side exception`() {
99         runBlocking {
100             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
101
102             val executionServiceInput = JacksonUtils
103                 .readValueFromClassPathFile(
104                     "execution-input/faulty-input.json",
105                     ExecutionServiceInput::class.java
106                 )!!
107
108             webTestClient
109                 .post()
110                 .uri("/api/v1/execution-service/process")
111                 .body(BodyInserters.fromObject(executionServiceInput))
112                 .exchange()
113                 .expectStatus().is5xxServerError
114         }
115     }
116
117     private fun loadTestCbaFile(): File {
118         val testCbaFile = normalizedFile("./target/blueprints/generated-cba.zip")
119         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
120         return testCbaFile
121     }
122 }