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.Assert
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.slf4j.LoggerFactory
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.security.SecurityProperties
32 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
33 import org.springframework.context.annotation.ComponentScan
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.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
41 @RunWith(SpringRunner::class)
43 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
44 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
45 @TestPropertySource(locations = ["classpath:application-test.properties"])
46 class ResourceControllerTest {
48 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
51 lateinit var resourceResolutionDBService: ResourceResolutionDBService
53 lateinit var webTestClient: WebTestClient
55 val blueprintName = "baseconfiguration"
56 val blueprintVersion = "1.0.0"
57 val templatePrefix = "activate"
60 fun `ping return Success`() {
62 webTestClient.get().uri("/api/v1/resources/health-check")
71 fun getAllFromResolutionKeyTest() {
73 val resolutionKey = "1"
74 val ra1 = createRA("bob")
75 val ra2 = createRA("dylan")
79 store(ra1, resKey = resolutionKey)
80 store(ra2, resKey = resolutionKey)
84 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
89 val json = String(it.responseBody!!)
90 val typeFactory = JacksonUtils.objectMapper.typeFactory
91 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
92 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
93 Assert.assertEquals(2, list.size)
94 assertEqual(ra1, list[0])
95 assertEqual(ra1, list[0])
101 fun getAllFromFromResourceTypeAndIdTest() {
104 val resourceType = "ServiceInstance"
105 val ra1 = createRA("bob")
106 val ra2 = createRA("dylan")
110 store(ra1, resId = resourceId, resType = resourceType)
111 store(ra2, resId = resourceId, resType = resourceType)
115 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
120 val json = String(it.responseBody!!)
121 val typeFactory = JacksonUtils.objectMapper.typeFactory
122 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
123 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
124 Assert.assertEquals(2, list.size)
125 assertEqual(ra1, list[0])
126 assertEqual(ra1, list[0])
133 fun getAllFromMissingParamTest() {
137 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
139 .expectStatus().is4xxClientError
142 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
143 Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
150 fun getAllFromWrongInputTest() {
154 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
156 .expectStatus().is4xxClientError
159 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
160 Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
167 fun getOneFromResolutionKeyTest() {
168 val resolutionKey = "3"
169 val ra = createRA("joe")
171 store(ra, resKey = resolutionKey)
175 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
180 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
187 fun getOneFromResolutionKey404Test() {
188 val resolutionKey = "3"
191 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
193 .expectStatus().is4xxClientError
198 private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
199 resType: String = "") {
200 resourceResolutionDBService.write(blueprintName,
209 private fun createRA(prefix: String): ResourceAssignment {
210 val property = PropertyDefinition()
211 property.value = "value$prefix".asJsonPrimitive()
213 val resourceAssignment = ResourceAssignment()
214 resourceAssignment.name = prefix
215 resourceAssignment.dictionaryName = "dd$prefix"
216 resourceAssignment.dictionarySource = "source$prefix"
217 resourceAssignment.version = 2
218 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
219 resourceAssignment.property = property
220 return resourceAssignment
223 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
224 Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
225 resourceResolution.value)
226 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
227 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
228 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
229 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
230 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
231 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
232 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)