baf0aa7c123d806117f42dbb24f5d0bb01d23a6c
[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.autoconfigure.security.SecurityProperties
28 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
29 import org.springframework.context.annotation.ComponentScan
30 import org.springframework.http.MediaType
31 import org.springframework.test.context.ContextConfiguration
32 import org.springframework.test.context.TestPropertySource
33 import org.springframework.test.context.junit4.SpringRunner
34 import org.springframework.test.web.reactive.server.WebTestClient
35 import org.springframework.web.reactive.function.BodyInserters
36 import kotlin.test.AfterTest
37
38 @RunWith(SpringRunner::class)
39 @WebFluxTest
40 @ContextConfiguration(
41     classes = [BluePrintCoreConfiguration::class,
42         BluePrintCatalogService::class, SecurityProperties::class]
43 )
44 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
45 @TestPropertySource(locations = ["classpath:application-test.properties"])
46 class TemplateControllerTest {
47
48     private val log = LoggerFactory.getLogger(TemplateControllerTest::class.toString())
49
50     @Autowired
51     lateinit var webTestClient: WebTestClient
52
53     var resolutionKey = "7cafa9f3-bbc8-49ec-8f25-fcaa6ac3ff08"
54     val blueprintName = "baseconfiguration"
55     val blueprintVersion = "1.0.0"
56     val templatePrefix = "activate"
57     val payloadDummyTemplateData = "PAYLOAD DATA"
58
59     var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
60             "&artifactName=$templatePrefix&resolutionKey=$resolutionKey"
61
62     @AfterTest
63     fun cleanDir() {
64         deleteDir("target", "blueprints")
65     }
66
67     @Test
68     fun `ping return Success`() {
69         runBlocking {
70             webTestClient.get().uri("/api/v1/template/health-check")
71                 .exchange()
72                 .expectStatus().isOk
73                 .expectBody()
74                 .equals("Success")
75         }
76     }
77
78     @Test
79     fun `store same value and tries to retrieve - duplicate entry execption`() {
80         runBlocking {
81
82             resolutionKey = "1"
83
84             post(resolutionKey)
85             post(resolutionKey)
86
87             webTestClient
88                 .get()
89                 .uri("/api/v1/template?$requestArguments")
90                 .exchange()
91                 .expectStatus().is4xxClientError
92                 .expectBody().equals(payloadDummyTemplateData)
93         }
94     }
95
96     @Test
97     fun `get returns requested JSON content-type`() {
98         runBlocking {
99             resolutionKey = "2"
100             post(resolutionKey)
101             get("json", resolutionKey)
102         }
103     }
104
105     @Test
106     fun `get returns requested XML content-type`() {
107         runBlocking {
108             resolutionKey = "3"
109             post(resolutionKey)
110             get("xml", resolutionKey)
111         }
112     }
113
114     @Test
115     fun `get returns 400 error if missing arg`() {
116         runBlocking {
117             val arguments = "bpBADName=$blueprintName" +
118                     "&bpBADVersion=$blueprintVersion" +
119                     "&artifactName=$templatePrefix" +
120                     "&resolutionKey=$resolutionKey"
121
122             webTestClient.get().uri("/api/v1/template?$arguments")
123                 .exchange()
124                 .expectStatus().isBadRequest
125         }
126     }
127
128     @Test
129     fun `get returns 404 if entry not found`() {
130         runBlocking {
131
132             webTestClient
133                 .get()
134                 .uri(
135                     "/api/v1/template?bpName=$blueprintName&bpVersion=$blueprintVersion" +
136                             "&artifactName=$templatePrefix&resolutionKey=notFound"
137                 )
138                 .exchange()
139                 .expectStatus().isNotFound
140         }
141     }
142
143     private fun post(resKey: String) {
144         webTestClient
145             .post()
146             .uri("/api/v1/template/$blueprintName/$blueprintVersion/$templatePrefix/$resKey")
147             .body(BodyInserters.fromObject(payloadDummyTemplateData))
148             .exchange()
149             .expectStatus().is2xxSuccessful
150             .expectBody()
151             .consumeWith {
152                 log.info("Stored result under UUID ${it.responseBody}")
153             }
154     }
155
156     private fun get(expectedType: String, resKey: String) {
157         var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
158                 "&artifactName=$templatePrefix&resolutionKey=$resKey"
159
160         if (expectedType.isNotEmpty()) {
161             requestArguments = "$requestArguments&format=$expectedType"
162             webTestClient
163                 .get()
164                 .uri("/api/v1/template?$requestArguments")
165                 .exchange()
166                 .expectStatus().is2xxSuccessful
167                 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
168                 .expectBody().equals(payloadDummyTemplateData)
169         } else {
170             webTestClient
171                 .get()
172                 .uri("/api/v1/template?$requestArguments")
173                 .exchange()
174                 .expectStatus().is2xxSuccessful
175                 .expectHeader().contentType(MediaType.TEXT_PLAIN)
176                 .expectBody().equals(payloadDummyTemplateData)
177         }
178     }
179 }