Use released Oslo/Potassium parent poms
[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 io.micrometer.core.instrument.simple.SimpleMeterRegistry
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.blueprintsprocessor.functions.workflow.audit.DatabaseStoreAuditService
27 import org.onap.ccsdk.cds.controllerblueprints.core.compress
28 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
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 = [
49         ExecutionServiceHandler::class, BluePrintCoreConfiguration::class,
50         BluePrintCatalogService::class, DatabaseStoreAuditService::class,
51         SelfServiceApiTestConfiguration::class,
52         ErrorCatalogTestConfiguration::class, SimpleMeterRegistry::class
53     ]
54 )
55 @TestPropertySource(locations = ["classpath:application-test.properties"])
56 class ExecutionServiceControllerTest {
57
58     @Autowired
59     lateinit var blueprintsProcessorCatalogService: BluePrintCatalogService
60
61     @Autowired
62     lateinit var webTestClient: WebTestClient
63
64     @BeforeTest
65     fun init() {
66         deleteDir("target", "blueprints")
67
68         // Create sample CBA zip
69         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
70             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
71     }
72
73     @AfterTest
74     fun cleanDir() {
75         deleteDir("target", "blueprints")
76     }
77
78     @Test
79     fun `test rest process`() {
80         runBlocking {
81             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
82
83             val executionServiceInput = JacksonUtils
84                 .readValueFromClassPathFile(
85                     "execution-input/default-input.json",
86                     ExecutionServiceInput::class.java
87                 )!!
88
89             webTestClient
90                 .post()
91                 .uri("/api/v1/execution-service/process")
92                 .body(BodyInserters.fromObject(executionServiceInput))
93                 .exchange()
94                 .expectStatus().isOk
95         }
96     }
97
98     @Test
99     fun `rest resource process should return status code 500 in case of server-side exception`() {
100         runBlocking {
101             blueprintsProcessorCatalogService.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
102
103             val executionServiceInput = JacksonUtils
104                 .readValueFromClassPathFile(
105                     "execution-input/faulty-input.json",
106                     ExecutionServiceInput::class.java
107                 )!!
108
109             webTestClient
110                 .post()
111                 .uri("/api/v1/execution-service/process")
112                 .body(BodyInserters.fromObject(executionServiceInput))
113                 .exchange()
114                 .expectStatus().is5xxServerError
115         }
116     }
117
118     private fun loadTestCbaFile(): File {
119         val testCbaFile = normalizedFile("./target/blueprints/generated-cba.zip")
120         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
121         return testCbaFile
122     }
123 }