34c7a7c7ca75e39af83c53880d24371875af4a94
[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.autoconfigure.security.SecurityProperties
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
36 @RunWith(SpringRunner::class)
37 @WebFluxTest
38 @ContextConfiguration(
39     classes = [BluePrintCoreConfiguration::class,
40         BluePrintCatalogService::class, SecurityProperties::class]
41 )
42 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
43 @TestPropertySource(locations = ["classpath:application-test.properties"])
44 class ResourceConfigSnapshotControllerTest {
45
46     private val log = LoggerFactory.getLogger(ResourceConfigSnapshotControllerTest::class.toString())
47
48     @Autowired
49     lateinit var webTestClient: WebTestClient
50
51     val resourceId = "fcaa6ac3ff08"
52     val resourceType = "PNF"
53     val snapshotData = "PAYLOAD DATA"
54
55     var requestArguments = "resourceId=$resourceId&resourceType=$resourceType"
56
57     @Test
58     fun `ping return Success`() {
59         runBlocking {
60             webTestClient.get().uri("/api/v1/configs/health-check")
61                 .exchange()
62                 .expectStatus().isOk
63                 .expectBody()
64                 .equals("Success")
65         }
66     }
67
68     @Test
69     fun `update configuration is allowed and updates timestamp`() {
70         runBlocking {
71
72             webTestClient
73                 .post()
74                 .uri("/api/v1/configs/$resourceType/$resourceId/running")
75                 .body(BodyInserters.fromObject(snapshotData))
76                 .exchange()
77                 .expectStatus().is2xxSuccessful
78                 .expectBody()
79                 .jsonPath("$.createdDate")
80                 .value<String> { println(it) }
81
82             webTestClient
83                 .post()
84                 .uri("/api/v1/configs/$resourceType/$resourceId/running")
85                 .body(BodyInserters.fromObject(snapshotData))
86                 .exchange()
87                 .expectStatus().is2xxSuccessful
88                 .expectBody()
89                 .jsonPath("$.createdDate")
90                 .value<String> { println(it) }
91         }
92     }
93
94     @Test
95     fun `get returns requested JSON content-type`() {
96         runBlocking {
97             post(resourceType, "22", "RUNNING")
98             get("json", resourceType, "22", "RUNNING")
99         }
100     }
101
102     @Test
103     fun `get returns requested XML content-type`() {
104         runBlocking {
105             post(resourceType, "3", "CANDIDATE")
106             get("xml", resourceType, "3", "CANDIDATE")
107         }
108     }
109
110     @Test
111     fun `get returns 400 error if missing arg`() {
112         runBlocking {
113             val arguments = "artifactName=WRONGARG1&resolutionKey=WRONGARG1"
114
115             webTestClient.get().uri("/api/v1/configs?$arguments")
116                 .exchange()
117                 .expectStatus().isBadRequest
118         }
119     }
120
121     @Test
122     fun `get returns 400 error if wrong Status arg`() {
123         runBlocking {
124             val arguments = "resourceId=MISSING&resourceType=PNF&status=TOTALLY_WRONG"
125
126             webTestClient.get().uri("/api/v1/configs?$arguments")
127                 .exchange()
128                 .expectStatus().isBadRequest
129         }
130     }
131
132     @Test
133     fun `get returns 200 if entry not found`() {
134         runBlocking {
135
136             webTestClient
137                 .get()
138                 .uri("/api/v1/configs?resourceId=MISSING&resourceType=PNF")
139                 .exchange()
140                 .expectStatus().is2xxSuccessful
141                 .expectBody()
142         }
143     }
144
145     private fun post(resourceType: String, resourceId: String, status: String) {
146         webTestClient
147             .post()
148             .uri("/api/v1/configs/$resourceType/$resourceId/$status")
149             .body(BodyInserters.fromObject(snapshotData))
150             .exchange()
151             .expectStatus().is2xxSuccessful
152             .expectBody()
153     }
154
155     private fun get(expectedType: String, resourceType: String, resourceId: String, status: String) {
156         var requestArguments = "resourceId=$resourceId&resourceType=$resourceType&status=$status"
157
158         if (expectedType.isNotEmpty()) {
159             requestArguments = "$requestArguments&format=$expectedType"
160             webTestClient
161                 .get()
162                 .uri("/api/v1/configs?$requestArguments")
163                 .exchange()
164                 .expectStatus().is2xxSuccessful
165                 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
166                 .expectBody().equals(snapshotData)
167         } else {
168             webTestClient
169                 .get()
170                 .uri("/api/v1/configs?$requestArguments")
171                 .exchange()
172                 .expectStatus().is2xxSuccessful
173                 .expectHeader().contentType(MediaType.TEXT_PLAIN)
174                 .expectBody().equals(snapshotData)
175         }
176     }
177 }