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.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.autoconfigure.security.SecurityProperties
33 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
34 import org.springframework.context.annotation.ComponentScan
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import org.springframework.test.web.reactive.server.WebTestClient
40 @RunWith(SpringRunner::class)
42 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
43 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
44 @TestPropertySource(locations = ["classpath:application-test.properties"])
45 class ResourceControllerTest {
47 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
50 lateinit var resourceResolutionDBService: ResourceResolutionDBService
52 lateinit var webTestClient: WebTestClient
54 val blueprintName = "baseconfiguration"
55 val blueprintVersion = "1.0.0"
56 val templatePrefix = "activate"
59 fun `ping return Success`() {
61 webTestClient.get().uri("/api/v1/resources/health-check")
70 fun getAllFromResolutionKeyTest() {
72 val resolutionKey = "1"
73 val ra1 = createRA("bob")
74 val ra2 = createRA("dylan")
78 store(ra1, resKey = resolutionKey)
79 store(ra2, resKey = resolutionKey)
83 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
88 val json = String(it.responseBody!!)
89 val typeFactory = JacksonUtils.objectMapper.typeFactory
90 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
92 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
94 Assert.assertEquals(2, list.size)
95 assertEqual(ra1, list[0])
96 assertEqual(ra1, list[0])
102 fun getAllFromFromResourceTypeAndIdTest() {
105 val resourceType = "ServiceInstance"
106 val ra1 = createRA("bob")
107 val ra2 = createRA("dylan")
111 store(ra1, resId = resourceId, resType = resourceType)
112 store(ra2, resId = resourceId, resType = resourceType)
116 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
121 val json = String(it.responseBody!!)
122 val typeFactory = JacksonUtils.objectMapper.typeFactory
123 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
125 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
127 Assert.assertEquals(2, list.size)
128 assertEqual(ra1, list[0])
129 assertEqual(ra1, list[0])
135 fun getAllFromMissingParamTest() {
139 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
141 .expectStatus().is4xxClientError
144 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
146 "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
154 fun getAllFromWrongInputTest() {
158 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
160 .expectStatus().is4xxClientError
163 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
165 "Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
173 fun getOneFromResolutionKeyTest() {
174 val resolutionKey = "3"
175 val ra = createRA("joe")
177 store(ra, resKey = resolutionKey)
181 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
186 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
193 fun getOneFromResolutionKey404Test() {
194 val resolutionKey = "3"
197 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
199 .expectStatus().is4xxClientError
204 private suspend fun store(
205 resourceAssignment: ResourceAssignment,
210 resourceResolutionDBService.write(
221 private fun createRA(prefix: String): ResourceAssignment {
222 val property = PropertyDefinition()
223 property.value = "value$prefix".asJsonPrimitive()
225 val resourceAssignment = ResourceAssignment()
226 resourceAssignment.name = prefix
227 resourceAssignment.dictionaryName = "dd$prefix"
228 resourceAssignment.dictionarySource = "source$prefix"
229 resourceAssignment.version = 2
230 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
231 resourceAssignment.property = property
232 return resourceAssignment
235 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
237 JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
238 resourceResolution.value
240 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
241 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
242 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
243 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
244 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
245 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
246 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)