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.resource.api
19 import kotlinx.coroutines.runBlocking
20 import org.junit.Before
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
25 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
27 import org.slf4j.LoggerFactory
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.autoconfigure.security.SecurityProperties
30 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
31 import org.springframework.context.annotation.ComponentScan
32 import org.springframework.http.HttpStatus
33 import org.springframework.http.MediaType
34 import org.springframework.test.context.ContextConfiguration
35 import org.springframework.test.context.TestPropertySource
36 import org.springframework.test.context.junit4.SpringRunner
37 import org.springframework.test.web.reactive.server.WebTestClient
38 import org.springframework.web.reactive.function.BodyInserters
40 import java.nio.file.Paths
41 import kotlin.test.AfterTest
42 import kotlin.test.BeforeTest
43 import kotlin.test.assertTrue
45 @RunWith(SpringRunner::class)
47 @ContextConfiguration(classes = [BluePrintCoreConfiguration::class,
48 BluePrintCatalogService::class, SecurityProperties::class])
49 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
50 @TestPropertySource(locations = ["classpath:application-test.properties"])
51 class TemplateControllerTest {
53 private val log = LoggerFactory.getLogger(TemplateControllerTest::class.toString())
56 lateinit var webTestClient: WebTestClient
58 var resolutionKey = "7cafa9f3-bbc8-49ec-8f25-fcaa6ac3ff08"
59 val blueprintName = "baseconfiguration"
60 val blueprintVersion = "1.0.0"
61 val templatePrefix = "activate"
62 val payloadDummyTemplateData = "PAYLOAD DATA"
64 var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
65 "&artifactName=$templatePrefix&resolutionKey=$resolutionKey"
69 deleteDir("target", "blueprints")
73 fun `ping return Success`() {
75 webTestClient.get().uri("/api/v1/template/health-check")
84 fun `store same value and tries to retrieve - duplicate entry execption`() {
94 .uri("/api/v1/template?$requestArguments")
96 .expectStatus().is4xxClientError
97 .expectBody().equals(payloadDummyTemplateData)
102 fun `get returns requested JSON content-type`() {
106 get("json", resolutionKey)
111 fun `get returns requested XML content-type`() {
115 get("xml", resolutionKey)
120 fun `get returns 400 error if missing arg`() {
122 val arguments = "bpBADName=$blueprintName" +
123 "&bpBADVersion=$blueprintVersion" +
124 "&artifactName=$templatePrefix" +
125 "&resolutionKey=$resolutionKey"
127 webTestClient.get().uri("/api/v1/template?$arguments")
129 .expectStatus().isBadRequest
134 fun `get returns 404 if entry not found`() {
139 .uri("/api/v1/template?bpName=$blueprintName&bpVersion=$blueprintVersion" +
140 "&artifactName=$templatePrefix&resolutionKey=bob")
142 .expectStatus().isNotFound
146 private fun post(resKey: String) {
149 .uri("/api/v1/template/$blueprintName/$blueprintVersion/$templatePrefix/$resKey")
150 .body(BodyInserters.fromObject(payloadDummyTemplateData))
152 .expectStatus().is2xxSuccessful
155 log.info("Stored result under UUID ${it.responseBody}")
159 private fun get(expectedType: String, resKey: String) {
160 var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
161 "&artifactName=$templatePrefix&resolutionKey=$resKey"
163 if (expectedType.isNotEmpty()) {
164 requestArguments = "$requestArguments&format=$expectedType"
167 .uri("/api/v1/template?$requestArguments")
169 .expectStatus().is2xxSuccessful
170 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
171 .expectBody().equals(payloadDummyTemplateData)
175 .uri("/api/v1/template?$requestArguments")
177 .expectStatus().is2xxSuccessful
178 .expectHeader().contentType(MediaType.TEXT_PLAIN)
179 .expectBody().equals(payloadDummyTemplateData)