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