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