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