Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / resource-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / resource / api / TemplateControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.resource.api
18
19 import kotlinx.coroutines.runBlocking
20 import org.junit.Test
21 import org.junit.runner.RunWith
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
23 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.slf4j.LoggerFactory
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
28 import org.springframework.context.annotation.ComponentScan
29 import org.springframework.http.MediaType
30 import org.springframework.test.context.ContextConfiguration
31 import org.springframework.test.context.TestPropertySource
32 import org.springframework.test.context.junit4.SpringRunner
33 import org.springframework.test.web.reactive.server.WebTestClient
34 import org.springframework.web.reactive.function.BodyInserters
35 import kotlin.test.AfterTest
36
37 @RunWith(SpringRunner::class)
38 @WebFluxTest
39 @ContextConfiguration(
40     classes = [
41         TestDatabaseConfiguration::class, BluePrintCoreConfiguration::class,
42         BluePrintCatalogService::class, ErrorCatalogTestConfiguration::class
43     ]
44 )
45 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
46 @TestPropertySource(locations = ["classpath:application-test.properties"])
47 class TemplateControllerTest {
48
49     private val log = LoggerFactory.getLogger(TemplateControllerTest::class.toString())
50
51     @Autowired
52     lateinit var webTestClient: WebTestClient
53
54     var resolutionKey = "7cafa9f3-bbc8-49ec-8f25-fcaa6ac3ff08"
55     val blueprintName = "baseconfiguration"
56     val blueprintVersion = "1.0.0"
57     val templatePrefix = "activate"
58     val payloadDummyTemplateData = "PAYLOAD DATA"
59
60     var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
61         "&artifactName=$templatePrefix&resolutionKey=$resolutionKey"
62
63     @AfterTest
64     fun cleanDir() {
65         deleteDir("target", "blueprints")
66     }
67
68     @Test
69     fun `ping return Success`() {
70         runBlocking {
71             webTestClient.get().uri("/api/v1/template/health-check")
72                 .exchange()
73                 .expectStatus().isOk
74                 .expectBody()
75                 .equals("Success")
76         }
77     }
78
79     @Test
80     fun `store same value and tries to retrieve - duplicate entry execption`() {
81         runBlocking {
82
83             resolutionKey = "1"
84
85             post(resolutionKey)
86             post(resolutionKey)
87
88             webTestClient
89                 .get()
90                 .uri("/api/v1/template?$requestArguments")
91                 .exchange()
92                 .expectStatus().is4xxClientError
93                 .expectBody().equals(payloadDummyTemplateData)
94         }
95     }
96
97     @Test
98     fun `get returns requested JSON content-type`() {
99         runBlocking {
100             resolutionKey = "2"
101             post(resolutionKey)
102             get("json", resolutionKey)
103         }
104     }
105
106     @Test
107     fun `get returns requested XML content-type`() {
108         runBlocking {
109             resolutionKey = "3"
110             post(resolutionKey)
111             get("xml", resolutionKey)
112         }
113     }
114
115     @Test
116     fun `get returns 400 error if missing arg`() {
117         runBlocking {
118             val arguments = "bpBADName=$blueprintName" +
119                 "&bpBADVersion=$blueprintVersion" +
120                 "&artifactName=$templatePrefix" +
121                 "&resolutionKey=$resolutionKey"
122
123             webTestClient.get().uri("/api/v1/template?$arguments")
124                 .exchange()
125                 .expectStatus().isNotFound
126         }
127     }
128
129     @Test
130     fun `get returns 404 if entry not found`() {
131         runBlocking {
132
133             webTestClient
134                 .get()
135                 .uri(
136                     "/api/v1/template?bpName=$blueprintName&bpVersion=$blueprintVersion" +
137                         "&artifactName=$templatePrefix&resolutionKey=notFound"
138                 )
139                 .exchange()
140                 .expectStatus().isNotFound
141         }
142     }
143
144     private fun post(resKey: String) {
145         webTestClient
146             .post()
147             .uri("/api/v1/template/$blueprintName/$blueprintVersion/$templatePrefix/$resKey")
148             .body(BodyInserters.fromObject(payloadDummyTemplateData))
149             .exchange()
150             .expectStatus().is2xxSuccessful
151             .expectBody()
152             .consumeWith {
153                 log.info("Stored result under UUID ${it.responseBody}")
154             }
155     }
156
157     private fun get(expectedType: String, resKey: String) {
158         var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
159             "&artifactName=$templatePrefix&resolutionKey=$resKey"
160
161         if (expectedType.isNotEmpty()) {
162             requestArguments = "$requestArguments&format=$expectedType"
163             webTestClient
164                 .get()
165                 .uri("/api/v1/template?$requestArguments")
166                 .exchange()
167                 .expectStatus().is2xxSuccessful
168                 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
169                 .expectBody().equals(payloadDummyTemplateData)
170         } else {
171             webTestClient
172                 .get()
173                 .uri("/api/v1/template?$requestArguments")
174                 .exchange()
175                 .expectStatus().is2xxSuccessful
176                 .expectHeader().contentType(MediaType.TEXT_PLAIN)
177                 .expectBody().equals(payloadDummyTemplateData)
178         }
179     }
180 }