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(
43 classes = [TestDatabaseConfiguration::class,
44 ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::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, ErrorMessage::class.java)
153 "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
161 fun getAllFromWrongInputTest() {
165 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
167 .expectStatus().is4xxClientError
170 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
172 "Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
180 fun getOneFromResolutionKeyTest() {
181 val resolutionKey = "3"
182 val ra = createRA("joe")
184 store(ra, resKey = resolutionKey)
188 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
193 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
200 fun getOneFromResolutionKey404Test() {
201 val resolutionKey = "3"
204 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
206 .expectStatus().is4xxClientError
211 private suspend fun store(
212 resourceAssignment: ResourceAssignment,
217 resourceResolutionDBService.write(
228 private fun createRA(prefix: String): ResourceAssignment {
229 val property = PropertyDefinition()
230 property.value = "value$prefix".asJsonPrimitive()
232 val resourceAssignment = ResourceAssignment()
233 resourceAssignment.name = prefix
234 resourceAssignment.dictionaryName = "dd$prefix"
235 resourceAssignment.dictionarySource = "source$prefix"
236 resourceAssignment.version = 2
237 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
238 resourceAssignment.property = property
239 return resourceAssignment
242 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
244 JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
245 resourceResolution.value
247 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
248 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
249 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
250 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
251 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
252 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
253 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)