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.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
39 @RunWith(SpringRunner::class)
41 @ContextConfiguration(
42 classes = [TestDatabaseConfiguration::class,
43 ResourceController::class, ResourceResolutionDBService::class]
46 basePackages = ["org.onap.ccsdk.cds.controllerblueprints.core.service",
47 "org.onap.ccsdk.cds.blueprintsprocessor.resource.api",
48 "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution"]
50 @TestPropertySource(locations = ["classpath:application-test.properties"])
51 class ResourceControllerTest {
53 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
56 lateinit var resourceResolutionDBService: ResourceResolutionDBService
58 lateinit var webTestClient: WebTestClient
60 val blueprintName = "baseconfiguration"
61 val blueprintVersion = "1.0.0"
62 val templatePrefix = "activate"
65 fun `ping return Success`() {
67 webTestClient.get().uri("/api/v1/resources/health-check")
76 fun getAllFromResolutionKeyTest() {
78 val resolutionKey = "1"
79 val ra1 = createRA("bob")
80 val ra2 = createRA("dylan")
84 store(ra1, resKey = resolutionKey)
85 store(ra2, resKey = resolutionKey)
89 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
94 val json = String(it.responseBody!!)
95 val typeFactory = JacksonUtils.objectMapper.typeFactory
96 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
98 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
100 Assert.assertEquals(2, list.size)
101 assertEqual(ra1, list[0])
102 assertEqual(ra1, list[0])
108 fun getAllFromFromResourceTypeAndIdTest() {
111 val resourceType = "ServiceInstance"
112 val ra1 = createRA("bob")
113 val ra2 = createRA("dylan")
117 store(ra1, resId = resourceId, resType = resourceType)
118 store(ra2, resId = resourceId, resType = resourceType)
122 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
127 val json = String(it.responseBody!!)
128 val typeFactory = JacksonUtils.objectMapper.typeFactory
129 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(
131 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)
133 Assert.assertEquals(2, list.size)
134 assertEqual(ra1, list[0])
135 assertEqual(ra1, list[0])
141 fun getAllFromMissingParamTest() {
145 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
147 .expectStatus().is4xxClientError
150 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
152 "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
160 fun getAllFromWrongInputTest() {
164 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
166 .expectStatus().is4xxClientError
169 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
171 "Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
179 fun getOneFromResolutionKeyTest() {
180 val resolutionKey = "3"
181 val ra = createRA("joe")
183 store(ra, resKey = resolutionKey)
187 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
192 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
199 fun getOneFromResolutionKey404Test() {
200 val resolutionKey = "3"
203 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
205 .expectStatus().is4xxClientError
210 private suspend fun store(
211 resourceAssignment: ResourceAssignment,
216 resourceResolutionDBService.write(
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 = BluePrintConstants.STATUS_SUCCESS
237 resourceAssignment.property = property
238 return resourceAssignment
241 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
243 JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
244 resourceResolution.value
246 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
247 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
248 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
249 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
250 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
251 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
252 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)