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.onap.ccsdk.cds.error.catalog.core.ErrorPayload
31 import org.slf4j.LoggerFactory
32 import org.springframework.beans.factory.annotation.Autowired
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(
44 TestDatabaseConfiguration::class, ErrorCatalogTestConfiguration::class,
45 ResourceController::class, ResourceResolutionDBService::class
50 "org.onap.ccsdk.cds.controllerblueprints.core.service",
51 "org.onap.ccsdk.cds.blueprintsprocessor.resource.api",
52 "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution"
55 @TestPropertySource(locations = ["classpath:application-test.properties"])
56 class ResourceControllerTest {
58 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
61 lateinit var resourceResolutionDBService: ResourceResolutionDBService
64 lateinit var webTestClient: WebTestClient
66 val blueprintName = "baseconfiguration"
67 val blueprintVersion = "1.0.0"
68 val templatePrefix = "activate"
71 fun `ping return Success`() {
73 webTestClient.get().uri("/api/v1/resources/health-check")
82 fun getAllFromResolutionKeyTest() {
84 val resolutionKey = "1"
85 val ra1 = createRA("bob")
86 val ra2 = createRA("dylan")
90 store(ra1, resKey = resolutionKey)
91 store(ra2, resKey = resolutionKey)
95 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
100 val json = String(it.responseBody!!)
101 val typeFactory = JacksonUtils.objectMapper.typeFactory
102 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
104 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
106 Assert.assertEquals(2, list.size)
107 assertEqual(ra1, list[0])
108 assertEqual(ra1, list[0])
114 fun getAllFromFromResourceTypeAndIdTest() {
117 val resourceType = "ServiceInstance"
118 val ra1 = createRA("bob")
119 val ra2 = createRA("dylan")
123 store(ra1, resId = resourceId, resType = resourceType)
124 store(ra2, resId = resourceId, resType = resourceType)
128 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
133 val json = String(it.responseBody!!)
134 val typeFactory = JacksonUtils.objectMapper.typeFactory
135 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
137 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
139 Assert.assertEquals(2, list.size)
140 assertEqual(ra1, list[0])
141 assertEqual(ra1, list[0])
147 fun getAllFromMissingParamTest() {
151 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
153 .expectStatus().is4xxClientError
156 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorPayload::class.java)
158 "Cause: Missing param. Either retrieve resolved value using artifact name and " +
159 "resolution-key OR using resource-id and resource-type. \n" +
160 " Action : Please verify your request.",
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, ErrorPayload::class.java)
179 "Cause: Either retrieve resolved value using artifact name and resolution-key OR using " +
180 "resource-id and resource-type. \n Action : Please verify your request.",
188 fun getOneFromResolutionKeyTest() {
189 val resolutionKey = "3"
190 val ra = createRA("joe")
192 store(ra, resKey = resolutionKey)
196 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
201 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
208 fun getOneFromResolutionKey404Test() {
209 val resolutionKey = "3"
212 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
214 .expectStatus().is4xxClientError
219 private suspend fun store(
220 resourceAssignment: ResourceAssignment,
225 resourceResolutionDBService.write(
236 private fun createRA(prefix: String): ResourceAssignment {
237 val property = PropertyDefinition()
238 property.value = "value$prefix".asJsonPrimitive()
240 val resourceAssignment = ResourceAssignment()
241 resourceAssignment.name = prefix
242 resourceAssignment.dictionaryName = "dd$prefix"
243 resourceAssignment.dictionarySource = "source$prefix"
244 resourceAssignment.version = 2
245 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
246 resourceAssignment.property = property
247 return resourceAssignment
250 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
252 JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
253 resourceResolution.value
255 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
256 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
257 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
258 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
259 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
260 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
261 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)