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 com.fasterxml.jackson.core.type.TypeReference
20 import com.fasterxml.jackson.module.kotlin.readValue
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Assert
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
33 import org.python.jline.console.internal.ConsoleRunner.property
34 import org.slf4j.LoggerFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.autoconfigure.security.SecurityProperties
37 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.http.HttpHeaders
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.MediaType
42 import org.springframework.test.context.ContextConfiguration
43 import org.springframework.test.context.TestPropertySource
44 import org.springframework.test.context.junit4.SpringRunner
45 import org.springframework.test.web.reactive.server.WebTestClient
46 import org.springframework.web.reactive.function.BodyInserters
47 import java.util.function.Consumer
48 import kotlin.test.BeforeTest
49 import org.h2.value.DataType.readValue
51 import org.h2.value.DataType.readValue
52 import org.python.bouncycastle.asn1.x500.style.RFC4519Style.l
53 import org.h2.value.DataType.readValue
54 import java.lang.reflect.Array
57 @RunWith(SpringRunner::class)
59 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
60 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 class ResourceControllerTest {
64 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
67 lateinit var resourceResolutionDBService: ResourceResolutionDBService
69 lateinit var webTestClient: WebTestClient
71 val blueprintName = "baseconfiguration"
72 val blueprintVersion = "1.0.0"
73 val templatePrefix = "activate"
76 fun `ping return Success`() {
78 webTestClient.get().uri("/api/v1/resources/ping")
87 fun getAllFromResolutionKeyTest() {
89 val resolutionKey = "1"
90 val ra1 = createRA("bob")
91 val ra2 = createRA("dylan")
95 store(ra1, resKey = resolutionKey)
96 store(ra2, resKey = resolutionKey)
100 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
105 val json = String(it.responseBody!!)
106 val typeFactory = JacksonUtils.objectMapper.typeFactory
107 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
108 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
109 Assert.assertEquals(2, list.size)
110 assertEqual(ra1, list[0])
111 assertEqual(ra1, list[0])
117 fun getAllFromFromResourceTypeAndIdTest() {
120 val resourceType = "ServiceInstance"
121 val ra1 = createRA("bob")
122 val ra2 = createRA("dylan")
126 store(ra1, resId = resourceId, resType = resourceType)
127 store(ra2, resId = resourceId, resType = resourceType)
131 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
136 val json = String(it.responseBody!!)
137 val typeFactory = JacksonUtils.objectMapper.typeFactory
138 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
139 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
140 Assert.assertEquals(2, list.size)
141 assertEqual(ra1, list[0])
142 assertEqual(ra1, list[0])
149 fun getAllFromMissingParamTest() {
153 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
155 .expectStatus().is4xxClientError
158 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
159 Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
166 fun getAllFromWrongInputTest() {
170 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
172 .expectStatus().is4xxClientError
175 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
176 Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
183 fun getOneFromResolutionKeyTest() {
184 val resolutionKey = "3"
185 val ra = createRA("joe")
187 store(ra, resKey = resolutionKey)
191 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
196 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
203 fun getOneFromResolutionKey404Test() {
204 val resolutionKey = "3"
207 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
209 .expectStatus().is4xxClientError
214 private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
215 resType: String = "") {
216 resourceResolutionDBService.write(blueprintName,
225 private fun createRA(prefix: String): ResourceAssignment {
226 val property = PropertyDefinition()
227 property.value = "value$prefix".asJsonPrimitive()
229 val resourceAssignment = ResourceAssignment()
230 resourceAssignment.name = prefix
231 resourceAssignment.dictionaryName = "dd$prefix"
232 resourceAssignment.dictionarySource = "source$prefix"
233 resourceAssignment.version = 2
234 resourceAssignment.status = "SUCCESS"
235 resourceAssignment.property = property
236 return resourceAssignment
239 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
240 Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
241 resourceResolution.value)
242 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
243 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
244 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
245 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
246 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
247 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
248 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)