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.resolutionresults.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.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.slf4j.LoggerFactory
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.security.SecurityProperties
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
38 import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ErrorMessage
39 import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ResourceController
40 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
43 @RunWith(SpringRunner::class)
45 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
46 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
47 @TestPropertySource(locations = ["classpath:application-test.properties"])
48 class ResourceControllerTest {
50 private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
53 lateinit var resourceResolutionDBService: ResourceResolutionDBService
55 lateinit var webTestClient: WebTestClient
57 val blueprintName = "baseconfiguration"
58 val blueprintVersion = "1.0.0"
59 val templatePrefix = "activate"
62 fun `ping return Success`() {
64 webTestClient.get().uri("/api/v1/resources/health-check")
73 fun getAllFromResolutionKeyTest() {
75 val resolutionKey = "1"
76 val ra1 = createRA("bob")
77 val ra2 = createRA("dylan")
81 store(ra1, resKey = resolutionKey)
82 store(ra2, resKey = resolutionKey)
86 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
91 val json = String(it.responseBody!!)
92 val typeFactory = JacksonUtils.objectMapper.typeFactory
93 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
94 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
95 Assert.assertEquals(2, list.size)
96 assertEqual(ra1, list[0])
97 assertEqual(ra1, list[0])
103 fun getAllFromFromResourceTypeAndIdTest() {
106 val resourceType = "ServiceInstance"
107 val ra1 = createRA("bob")
108 val ra2 = createRA("dylan")
112 store(ra1, resId = resourceId, resType = resourceType)
113 store(ra2, resId = resourceId, resType = resourceType)
117 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
122 val json = String(it.responseBody!!)
123 val typeFactory = JacksonUtils.objectMapper.typeFactory
124 val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
125 typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
126 Assert.assertEquals(2, list.size)
127 assertEqual(ra1, list[0])
128 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)
145 Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
152 fun getAllFromWrongInputTest() {
156 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
158 .expectStatus().is4xxClientError
161 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
162 Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
169 fun getOneFromResolutionKeyTest() {
170 val resolutionKey = "3"
171 val ra = createRA("joe")
173 store(ra, resKey = resolutionKey)
177 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
182 val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
189 fun getOneFromResolutionKey404Test() {
190 val resolutionKey = "3"
193 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
195 .expectStatus().is4xxClientError
200 private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
201 resType: String = "") {
202 resourceResolutionDBService.write(blueprintName,
211 private fun createRA(prefix: String): ResourceAssignment {
212 val property = PropertyDefinition()
213 property.value = "value$prefix".asJsonPrimitive()
215 val resourceAssignment = ResourceAssignment()
216 resourceAssignment.name = prefix
217 resourceAssignment.dictionaryName = "dd$prefix"
218 resourceAssignment.dictionarySource = "source$prefix"
219 resourceAssignment.version = 2
220 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
221 resourceAssignment.property = property
222 return resourceAssignment
225 private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
226 Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
227 resourceResolution.value)
228 Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
229 Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
230 Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
231 Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
232 Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
233 Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
234 Assert.assertEquals(blueprintName, resourceResolution.blueprintName)