2 * Copyright © 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api
19 import kotlinx.coroutines.runBlocking
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
38 import java.nio.file.Paths
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.assertTrue
44 @RunWith(SpringRunner::class)
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 {
52 private val log = LoggerFactory.getLogger(ResolutionResultsServiceHandlerTest::class.toString())
55 lateinit var blueprintCatalog: BluePrintCatalogService
57 lateinit var webTestClient: WebTestClient
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"
68 deleteDir("target", "blueprints")
69 blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile())
75 deleteDir("target", "blueprints")
79 fun `ping return Success`() {
82 webTestClient.get().uri("/api/v1/resolution-results/ping")
85 .expectBody().equals("Success")
90 fun `store-retrieve-delete result by path or UUID`() {
92 createRetrieveDelete()
97 fun `get returns requested JSON content-type`() {
99 createRetrieveDelete("json")
104 fun `get returns requested XML content-type`() {
106 createRetrieveDelete("xml")
110 private fun createRetrieveDelete(expectedType : String? = null): WebTestClient.ResponseSpec {
113 // Store new result for blueprint/artifact/resolutionkey
116 .uri("/api/v1/resolution-results/$blueprintName/$blueprintVersion/$templatePrefix/$resolutionKey/")
117 .body(BodyInserters.fromObject(payloadDummyTemplateData))
119 .expectStatus().is2xxSuccessful
122 uuid = String(it.responseBody)
123 log.info("Stored result under UUID $uuid")
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"
132 .uri("/api/v1/resolution-results/?$requestArguments")
134 .expectStatus().is2xxSuccessful
135 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
136 .expectBody().equals(payloadDummyTemplateData)
140 .uri("/api/v1/resolution-results/?$requestArguments")
142 .expectStatus().is2xxSuccessful
143 .expectHeader().contentType(MediaType.TEXT_PLAIN)
144 .expectBody().equals(payloadDummyTemplateData)
146 // And delete by UUID
149 .uri("/api/v1/resolution-results/$uuid/")
151 .expectStatus().is2xxSuccessful
158 fun `get returns 400 error if missing arg`() {
160 val arguments = "bpBADName=$blueprintName" +
161 "&bpBADVersion=$blueprintVersion" +
162 "&artifactName=$templatePrefix" +
163 "&resolutionKey=$resolutionKey"
165 webTestClient.get().uri("/api/v1/resolution-results/?$arguments")
167 .expectStatus().isBadRequest
172 fun `get returns 503 error if Blueprint not found`() {
174 val arguments = "bpName=BAD_BP_NAME" +
175 "&bpVersion=BAD_BP_VERSION" +
176 "&artifactName=$templatePrefix" +
177 "&resolutionKey=$resolutionKey"
179 webTestClient.get().uri("/api/v1/resolution-results/?$arguments")
181 .expectStatus().isEqualTo(HttpStatus.SERVICE_UNAVAILABLE)
186 fun `get returns 404 if entry not found`() {
191 .uri("/api/v1/resolution-results/?bpName=$blueprintName&bpVersion=$blueprintVersion" +
192 "&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
194 .expectStatus().isNotFound
199 fun `get returns 404 if UUID not found`() {
204 .uri("/api/v1/resolution-results/234234234234/")
206 .expectStatus().isNotFound
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}")