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 org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ErrorMessage
55 import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ResourceController
56 import java.lang.reflect.Array
59 @RunWith(SpringRunner::class)
61 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
62 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
63 @TestPropertySource(locations = ["classpath:application-test.properties"])
64 class ResourceControllerTest {
66 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
69 lateinit var resourceResolutionDBService: ResourceResolutionDBService
71 lateinit var webTestClient: WebTestClient
73 val blueprintName = "baseconfiguration"
74 val blueprintVersion = "1.0.0"
75 val templatePrefix = "activate"
78 fun `ping return Success`() {
80 webTestClient.get().uri("/api/v1/resources/ping")
89 fun getAllFromResolutionKeyTest() {
91 val resolutionKey = "1"
92 val ra1 = createRA("bob")
93 val ra2 = createRA("dylan")
97 store(ra1, resKey = resolutionKey)
98 store(ra2, resKey = resolutionKey)
102 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
107 val json = String(it.responseBody!!)
108 val typeFactory = JacksonUtils.objectMapper.typeFactory
109 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
110 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
111 Assert.assertEquals(2, list.size)
112 assertEqual(ra1, list[0])
113 assertEqual(ra1, list[0])
119 fun getAllFromFromResourceTypeAndIdTest() {
122 val resourceType = "ServiceInstance"
123 val ra1 = createRA("bob")
124 val ra2 = createRA("dylan")
128 store(ra1, resId = resourceId, resType = resourceType)
129 store(ra2, resId = resourceId, resType = resourceType)
133 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
138 val json = String(it.responseBody!!)
139 val typeFactory = JacksonUtils.objectMapper.typeFactory
140 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
141 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
142 Assert.assertEquals(2, list.size)
143 assertEqual(ra1, list[0])
144 assertEqual(ra1, list[0])
151 fun getAllFromMissingParamTest() {
155 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
157 .expectStatus().is4xxClientError
160 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
161 Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
168 fun getAllFromWrongInputTest() {
172 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
174 .expectStatus().is4xxClientError
177 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
178 Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
185 fun getOneFromResolutionKeyTest() {
186 val resolutionKey = "3"
187 val ra = createRA("joe")
189 store(ra, resKey = resolutionKey)
193 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
198 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
205 fun getOneFromResolutionKey404Test() {
206 val resolutionKey = "3"
209 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
211 .expectStatus().is4xxClientError
216 private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
217 resType: String = "") {
218 resourceResolutionDBService.write(blueprintName,
227 private fun createRA(prefix: String): ResourceAssignment {
228 val property = PropertyDefinition()
229 property.value = "value$prefix".asJsonPrimitive()
231 val resourceAssignment = ResourceAssignment()
232 resourceAssignment.name = prefix
233 resourceAssignment.dictionaryName = "dd$prefix"
234 resourceAssignment.dictionarySource = "source$prefix"
235 resourceAssignment.version = 2
236 resourceAssignment.status = "SUCCESS"
237 resourceAssignment.property = property
238 return resourceAssignment
241 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
242 Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
243 resourceResolution.value)
244 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
245 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
246 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
247 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
248 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
249 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
250 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)