Refactoring BP Code with ErrorCatalog
[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.test.autoconfigure.web.reactive.WebFluxTest
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
35 import org.springframework.test.web.reactive.server.WebTestClient
36 import org.springframework.web.reactive.function.BodyInserters
37 import java.io.File
38 import java.util.UUID
39 import kotlin.test.AfterTest
40 import kotlin.test.BeforeTest
41 import kotlin.test.assertTrue
42
43 @RunWith(SpringRunner::class)
44 @WebFluxTest
45 @ContextConfiguration(
46     classes = [ExecutionServiceHandler::class, BluePrintCoreConfiguration::class,
47         BluePrintCatalogService::class, SelfServiceApiTestConfiguration::class, ErrorCatalogTestConfiguration::class]
48 )
49 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 class ExecutionServiceControllerTest {
51
52     @Autowired
53     lateinit var blueprintsProcessorCatalogService: BluePrintCatalogService
54     @Autowired
55     lateinit var webTestClient: WebTestClient
56
57     @BeforeTest
58     fun init() {
59         deleteDir("target", "blueprints")
60
61         // Create sample CBA zip
62         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
63             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
64     }
65
66     @AfterTest
67     fun cleanDir() {
68         deleteDir("target", "blueprints")
69     }
70
71     @Test
72     fun `test rest process`() {
73         runBlocking {
74             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
75
76             val executionServiceInput = JacksonUtils
77                 .readValueFromClassPathFile(
78                     "execution-input/default-input.json",
79                     ExecutionServiceInput::class.java
80                 )!!
81
82             webTestClient
83                 .post()
84                 .uri("/api/v1/execution-service/process")
85                 .body(BodyInserters.fromObject(executionServiceInput))
86                 .exchange()
87                 .expectStatus().isOk
88         }
89     }
90
91     @Test
92     fun `rest resource process should return status code 500 in case of server-side exception`() {
93         runBlocking {
94             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
95
96             val executionServiceInput = JacksonUtils
97                 .readValueFromClassPathFile(
98                     "execution-input/faulty-input.json",
99                     ExecutionServiceInput::class.java
100                 )!!
101
102             webTestClient
103                 .post()
104                 .uri("/api/v1/execution-service/process")
105                 .body(BodyInserters.fromObject(executionServiceInput))
106                 .exchange()
107                 .expectStatus().is5xxServerError
108         }
109     }
110
111     private fun loadTestCbaFile(): File {
112         val testCbaFile = normalizedFile("./target/blueprints/generated-cba.zip")
113         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
114         return testCbaFile
115     }
116 }