813c900d7ecaca82561c76d92d6ecd0d9cdede47
[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.resolutionresults.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.HttpStatus
31 import org.springframework.http.MediaType
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
35 import org.springframework.test.web.reactive.server.WebTestClient
36 import org.springframework.web.reactive.function.BodyInserters
37 import java.io.File
38 import java.nio.file.Paths
39 import java.util.*
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.assertTrue
43
44 @RunWith(SpringRunner::class)
45 @WebFluxTest
46 @ContextConfiguration(classes = [ResolutionResultsServiceHandler::class, BluePrintCoreConfiguration::class,
47     BluePrintCatalogService::class, SecurityProperties::class])
48 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
49 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 class ResolutionResultsServiceHandlerTest {
51
52     private val log = LoggerFactory.getLogger(ResolutionResultsServiceHandlerTest::class.toString())
53
54     @Autowired
55     lateinit var blueprintCatalog: BluePrintCatalogService
56     @Autowired
57     lateinit var webTestClient: WebTestClient
58
59     var resolutionKey = "7cafa9f3-bbc8-49ec-8f25-fcaa6ac3ff08"
60     val blueprintName =  "baseconfiguration"
61     val blueprintVersion = "1.0.0"
62     val templatePrefix = "activate"
63     val payloadDummyTemplateData = "PAYLOAD DATA"
64
65     @BeforeTest
66     fun init() {
67         runBlocking {
68             deleteDir("target", "blueprints")
69             blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
70         }
71     }
72
73     @AfterTest
74     fun cleanDir() {
75         deleteDir("target", "blueprints")
76     }
77
78     @Test
79     fun `ping return Success`() {
80         runBlocking {
81
82             webTestClient.get().uri("/api/v1/resolution-results/ping")
83                     .exchange()
84                         .expectStatus().isOk
85                         .expectBody().equals("Success")
86         }
87     }
88
89     @Test
90     fun `store-retrieve-delete result by path or UUID`() {
91         runBlocking {
92             createRetrieveDelete()
93         }
94     }
95
96     @Test
97     fun `get returns requested JSON content-type`() {
98         runBlocking {
99             createRetrieveDelete("json")
100         }
101     }
102
103     @Test
104     fun `get returns requested XML content-type`() {
105         runBlocking {
106             createRetrieveDelete("xml")
107         }
108     }
109
110     private fun createRetrieveDelete(expectedType : String? = null): WebTestClient.ResponseSpec {
111         var uuid = "MISSING"
112
113         // Store new result for blueprint/artifact/resolutionkey
114         webTestClient
115                 .post()
116                 .uri("/api/v1/resolution-results/$blueprintName/$blueprintVersion/$templatePrefix/$resolutionKey/")
117                 .body(BodyInserters.fromObject(payloadDummyTemplateData))
118                 .exchange()
119                 .expectStatus().is2xxSuccessful
120                 .expectBody()
121                 .consumeWith {
122                     uuid = String(it.responseBody)
123                     log.info("Stored result under UUID $uuid")
124                 }
125         // Retrieve same payload
126         var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
127                 "&artifactName=$templatePrefix&resolutionKey=$resolutionKey"
128         if (expectedType != null) {
129             requestArguments = "$requestArguments&format=$expectedType"
130             webTestClient
131                     .get()
132                     .uri("/api/v1/resolution-results/?$requestArguments")
133                     .exchange()
134                     .expectStatus().is2xxSuccessful
135                     .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
136                     .expectBody().equals(payloadDummyTemplateData)
137         } else {
138             webTestClient
139                     .get()
140                     .uri("/api/v1/resolution-results/?$requestArguments")
141                     .exchange()
142                     .expectStatus().is2xxSuccessful
143                     .expectHeader().contentType(MediaType.TEXT_PLAIN)
144                     .expectBody().equals(payloadDummyTemplateData)
145         }
146         // And delete by UUID
147         return webTestClient
148                 .delete()
149                 .uri("/api/v1/resolution-results/$uuid/")
150                 .exchange()
151                 .expectStatus().is2xxSuccessful
152     }
153
154     /*
155      * Error cases
156      */
157     @Test
158     fun `get returns 400 error if missing arg`() {
159         runBlocking {
160             val arguments = "bpBADName=$blueprintName" +
161                     "&bpBADVersion=$blueprintVersion" +
162                     "&artifactName=$templatePrefix" +
163                     "&resolutionKey=$resolutionKey"
164
165             webTestClient.get().uri("/api/v1/resolution-results/?$arguments")
166                     .exchange()
167                         .expectStatus().isBadRequest
168         }
169     }
170
171     @Test
172     fun `get returns 503 error if Blueprint not found`() {
173         runBlocking {
174             val arguments = "bpName=BAD_BP_NAME" +
175                     "&bpVersion=BAD_BP_VERSION" +
176                     "&artifactName=$templatePrefix" +
177                     "&resolutionKey=$resolutionKey"
178
179             webTestClient.get().uri("/api/v1/resolution-results/?$arguments")
180                     .exchange()
181                     .expectStatus().isEqualTo(HttpStatus.SERVICE_UNAVAILABLE)
182         }
183     }
184
185     @Test
186     fun `get returns 404 if entry not found`() {
187         runBlocking {
188
189             webTestClient
190                     .get()
191                     .uri("/api/v1/resolution-results/?bpName=$blueprintName&bpVersion=$blueprintVersion" +
192                             "&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
193                     .exchange()
194                     .expectStatus().isNotFound
195         }
196     }
197
198     @Test
199     fun `get returns 404 if UUID not found`() {
200         runBlocking {
201
202             webTestClient
203                     .get()
204                     .uri("/api/v1/resolution-results/234234234234/")
205                     .exchange()
206                     .expectStatus().isNotFound
207         }
208     }
209
210     private fun loadTestCbaFile(): File {
211         val testCbaFile = Paths.get("./src/test/resources/test-cba.zip").toFile()
212         assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}")
213         return testCbaFile
214     }
215 }