Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / configs-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / configs / api / ResourceConfigSnapshotControllerTest.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.configs.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.interfaces.BlueprintCatalogService
24 import org.slf4j.LoggerFactory
25 import org.springframework.beans.factory.annotation.Autowired
26 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
27 import org.springframework.context.annotation.ComponentScan
28 import org.springframework.http.MediaType
29 import org.springframework.test.context.ContextConfiguration
30 import org.springframework.test.context.TestPropertySource
31 import org.springframework.test.context.junit4.SpringRunner
32 import org.springframework.test.web.reactive.server.WebTestClient
33 import org.springframework.web.reactive.function.BodyInserters
34
35 @RunWith(SpringRunner::class)
36 @WebFluxTest
37 @ContextConfiguration(
38     classes = [BlueprintCoreConfiguration::class, BlueprintCatalogService::class, ErrorCatalogTestConfiguration::class]
39 )
40 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
41 @TestPropertySource(locations = ["classpath:application-test.properties"])
42 class ResourceConfigSnapshotControllerTest {
43
44     private val log = LoggerFactory.getLogger(ResourceConfigSnapshotControllerTest::class.toString())
45
46     @Autowired
47     lateinit var webTestClient: WebTestClient
48
49     val resourceId = "fcaa6ac3ff08"
50     val resourceType = "PNF"
51     val snapshotData = "PAYLOAD DATA"
52
53     var requestArguments = "resourceId=$resourceId&resourceType=$resourceType"
54
55     @Test
56     fun `ping return Success`() {
57         runBlocking {
58             webTestClient.get().uri("/api/v1/configs/health-check")
59                 .exchange()
60                 .expectStatus().isOk
61                 .expectBody()
62                 .equals("Success")
63         }
64     }
65
66     @Test
67     fun `update configuration is allowed and updates timestamp`() {
68         runBlocking {
69
70             webTestClient
71                 .post()
72                 .uri("/api/v1/configs/$resourceType/$resourceId/running")
73                 .body(BodyInserters.fromObject(snapshotData))
74                 .exchange()
75                 .expectStatus().is2xxSuccessful
76                 .expectBody()
77                 .jsonPath("$.createdDate")
78                 .value<String> { println(it) }
79
80             webTestClient
81                 .post()
82                 .uri("/api/v1/configs/$resourceType/$resourceId/running")
83                 .body(BodyInserters.fromObject(snapshotData))
84                 .exchange()
85                 .expectStatus().is2xxSuccessful
86                 .expectBody()
87                 .jsonPath("$.createdDate")
88                 .value<String> { println(it) }
89         }
90     }
91
92     @Test
93     fun `get returns requested JSON content-type`() {
94         runBlocking {
95             post(resourceType, "22", "RUNNING")
96             get("json", resourceType, "22", "RUNNING")
97         }
98     }
99
100     @Test
101     fun `get returns requested XML content-type`() {
102         runBlocking {
103             post(resourceType, "3", "CANDIDATE")
104             get("xml", resourceType, "3", "CANDIDATE")
105         }
106     }
107
108     @Test
109     fun `get returns 400 error if missing arg`() {
110         runBlocking {
111             val arguments = "artifactName=WRONGARG1&resolutionKey=WRONGARG1"
112
113             webTestClient.get().uri("/api/v1/configs?$arguments")
114                 .exchange()
115                 .expectStatus().is4xxClientError
116         }
117     }
118
119     @Test
120     fun `get returns 400 error if wrong Status arg`() {
121         runBlocking {
122             val arguments = "resourceId=MISSING&resourceType=PNF&status=TOTALLY_WRONG"
123
124             webTestClient.get().uri("/api/v1/configs?$arguments")
125                 .exchange()
126                 .expectStatus().isBadRequest
127         }
128     }
129
130     @Test
131     fun `get returns 200 if entry not found`() {
132         runBlocking {
133
134             webTestClient
135                 .get()
136                 .uri("/api/v1/configs?resourceId=MISSING&resourceType=PNF")
137                 .exchange()
138                 .expectStatus().is2xxSuccessful
139                 .expectBody()
140         }
141     }
142
143     @Test
144     fun `getAllByID returns 200 if entries found`() {
145         runBlocking {
146             post(resourceType, "3", "RUNNING")
147             post(resourceType, "2", "RUNNING")
148             post(resourceType, resourceId, "RUNNING")
149
150             webTestClient
151                 .get()
152                 .uri("/api/v1/configs/allByID?resourceId=$resourceId")
153                 .exchange()
154                 .expectStatus().is2xxSuccessful
155                 .expectBody()
156                 .jsonPath("$.length()")
157                 .isEqualTo(1)
158         }
159     }
160
161     @Test
162     fun `getAllByID with CANDIDATE status returns 200 if entries found`() {
163         runBlocking {
164             post(resourceType, "3", "RUNNING")
165             post(resourceType, "2", "RUNNING")
166             post(resourceType, resourceId, "CANDIDATE")
167
168             webTestClient
169                 .get()
170                 .uri("/api/v1/configs/allByID?resourceId=$resourceId&status=CANDIDATE")
171                 .exchange()
172                 .expectStatus().is2xxSuccessful
173                 .expectBody()
174                 .jsonPath("$.length()")
175                 .isEqualTo(1)
176         }
177     }
178
179     @Test
180     fun `getAllByID returns 400 error if missing parameter`() {
181         runBlocking {
182
183             webTestClient
184                 .get()
185                 .uri("/api/v1/configs/allByID")
186                 .exchange()
187                 .expectStatus().is4xxClientError
188                 .expectBody()
189         }
190     }
191
192     @Test
193     fun `getAllByID returns 400 error if wrong status parameter`() {
194         runBlocking {
195
196             webTestClient
197                 .get()
198                 .uri("/api/v1/configs/allByID?resourceId=$resourceId&status=NOTGOOD")
199                 .exchange()
200                 .expectStatus().is4xxClientError
201                 .expectBody()
202         }
203     }
204
205     @Test
206     fun `getAllByType returns 200 if entries found`() {
207         runBlocking {
208             post(resourceType, "3", "RUNNING")
209             post(resourceType + "DIFF", "2", "RUNNING")
210             post(resourceType, "1", "RUNNING")
211
212             webTestClient
213                 .get()
214                 .uri("/api/v1/configs/allByType?resourceType=$resourceType")
215                 .exchange()
216                 .expectStatus().is2xxSuccessful
217                 .expectBody()
218                 .jsonPath("$.length()")
219                 .isEqualTo(3)
220         }
221     }
222
223     @Test
224     fun `getAllByType returns 400 error if missing parameter`() {
225         runBlocking {
226
227             webTestClient
228                 .get()
229                 .uri("/api/v1/configs/allByType")
230                 .exchange()
231                 .expectStatus().is4xxClientError
232                 .expectBody()
233         }
234     }
235
236     @Test
237     fun `getAllByType returns 400 error if wrong status parameter`() {
238         runBlocking {
239
240             webTestClient
241                 .get()
242                 .uri("/api/v1/configs/allByType?resourceType=$resourceType&status=NOTGOOD")
243                 .exchange()
244                 .expectStatus().is4xxClientError
245                 .expectBody()
246         }
247     }
248
249     private fun post(resourceType: String, resourceId: String, status: String) {
250         webTestClient
251             .post()
252             .uri("/api/v1/configs/$resourceType/$resourceId/$status")
253             .body(BodyInserters.fromObject(snapshotData))
254             .exchange()
255             .expectStatus().is2xxSuccessful
256             .expectBody()
257     }
258
259     private fun get(expectedType: String, resourceType: String, resourceId: String, status: String) {
260         var requestArguments = "resourceId=$resourceId&resourceType=$resourceType&status=$status"
261
262         if (expectedType.isNotEmpty()) {
263             requestArguments = "$requestArguments&format=$expectedType"
264             webTestClient
265                 .get()
266                 .uri("/api/v1/configs?$requestArguments")
267                 .exchange()
268                 .expectStatus().is2xxSuccessful
269                 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
270                 .expectBody().equals(snapshotData)
271         } else {
272             webTestClient
273                 .get()
274                 .uri("/api/v1/configs?$requestArguments")
275                 .exchange()
276                 .expectStatus().is2xxSuccessful
277                 .expectHeader().contentType(MediaType.TEXT_PLAIN)
278                 .expectBody().equals(snapshotData)
279         }
280     }
281 }