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