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(
43 classes = [TestDatabaseConfiguration::class, ErrorCatalogTestConfiguration::class,
44 ResourceController::class, ResourceResolutionDBService::class]
47 basePackages = ["org.onap.ccsdk.cds.controllerblueprints.core.service",
48 "org.onap.ccsdk.cds.blueprintsprocessor.resource.api",
49 "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution"]
51 @TestPropertySource(locations = ["classpath:application-test.properties"])
52 class ResourceControllerTest {
54 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
57 lateinit var resourceResolutionDBService: ResourceResolutionDBService
59 lateinit var webTestClient: WebTestClient
61 val blueprintName = "baseconfiguration"
62 val blueprintVersion = "1.0.0"
63 val templatePrefix = "activate"
66 fun `ping return Success`() {
68 webTestClient.get().uri("/api/v1/resources/health-check")
77 fun getAllFromResolutionKeyTest() {
79 val resolutionKey = "1"
80 val ra1 = createRA("bob")
81 val ra2 = createRA("dylan")
85 store(ra1, resKey = resolutionKey)
86 store(ra2, resKey = resolutionKey)
90 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
95 val json = String(it.responseBody!!)
96 val typeFactory = JacksonUtils.objectMapper.typeFactory
97 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
99 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
101 Assert.assertEquals(2, list.size)
102 assertEqual(ra1, list[0])
103 assertEqual(ra1, list[0])
109 fun getAllFromFromResourceTypeAndIdTest() {
112 val resourceType = "ServiceInstance"
113 val ra1 = createRA("bob")
114 val ra2 = createRA("dylan")
118 store(ra1, resId = resourceId, resType = resourceType)
119 store(ra2, resId = resourceId, resType = resourceType)
123 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
128 val json = String(it.responseBody!!)
129 val typeFactory = JacksonUtils.objectMapper.typeFactory
130 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
132 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
134 Assert.assertEquals(2, list.size)
135 assertEqual(ra1, list[0])
136 assertEqual(ra1, list[0])
142 fun getAllFromMissingParamTest() {
146 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
148 .expectStatus().is4xxClientError
151 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorPayload::class.java)
153 "Cause: Missing param. Either retrieve resolved value using artifact name and " +
154 "resolution-key OR using resource-id and resource-type. \n" +
155 " Action : Please verify your request.",
163 fun getAllFromWrongInputTest() {
167 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
169 .expectStatus().is4xxClientError
172 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorPayload::class.java)
174 "Cause: Either retrieve resolved value using artifact name and resolution-key OR using " +
175 "resource-id and resource-type. \n Action : Please verify your request.",
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(
215 resourceAssignment: ResourceAssignment,
220 resourceResolutionDBService.write(
231 private fun createRA(prefix: String): ResourceAssignment {
232 val property = PropertyDefinition()
233 property.value = "value$prefix".asJsonPrimitive()
235 val resourceAssignment = ResourceAssignment()
236 resourceAssignment.name = prefix
237 resourceAssignment.dictionaryName = "dd$prefix"
238 resourceAssignment.dictionarySource = "source$prefix"
239 resourceAssignment.version = 2
240 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
241 resourceAssignment.property = property
242 return resourceAssignment
245 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
247 JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
248 resourceResolution.value
250 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
251 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
252 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
253 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
254 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
255 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
256 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)