85ac7bddd97671ef13817ab4205f07c83632b465
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / resource-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / resource / api / ResourceControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.resource.api
18
19 import kotlinx.coroutines.runBlocking
20 import org.junit.Assert
21 import org.junit.Test
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.controllerblueprints.core.BluePrintConstants
39
40
41 @RunWith(SpringRunner::class)
42 @WebFluxTest
43 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
44 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
45 @TestPropertySource(locations = ["classpath:application-test.properties"])
46 class ResourceControllerTest {
47
48     private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
49
50     @Autowired
51     lateinit var resourceResolutionDBService: ResourceResolutionDBService
52     @Autowired
53     lateinit var webTestClient: WebTestClient
54
55     val blueprintName = "baseconfiguration"
56     val blueprintVersion = "1.0.0"
57     val templatePrefix = "activate"
58
59     @Test
60     fun `ping return Success`() {
61         runBlocking {
62             webTestClient.get().uri("/api/v1/resources/health-check")
63                 .exchange()
64                 .expectStatus().isOk
65                 .expectBody()
66                 .equals("Success")
67         }
68     }
69
70     @Test
71     fun getAllFromResolutionKeyTest() {
72
73         val resolutionKey = "1"
74         val ra1 = createRA("bob")
75         val ra2 = createRA("dylan")
76
77         runBlocking {
78
79             store(ra1, resKey = resolutionKey)
80             store(ra2, resKey = resolutionKey)
81
82             webTestClient
83                 .get()
84                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
85                 .exchange()
86                 .expectStatus().isOk
87                 .expectBody()
88                 .consumeWith {
89                     val json = String(it.responseBody!!)
90                     val typeFactory = JacksonUtils.objectMapper.typeFactory
91                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
92                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
93                     Assert.assertEquals(2, list.size)
94                     assertEqual(ra1, list[0])
95                     assertEqual(ra1, list[0])
96                 }
97         }
98     }
99
100     @Test
101     fun getAllFromFromResourceTypeAndIdTest() {
102
103         val resourceId = "1"
104         val resourceType = "ServiceInstance"
105         val ra1 = createRA("bob")
106         val ra2 = createRA("dylan")
107
108         runBlocking {
109
110             store(ra1, resId = resourceId, resType = resourceType)
111             store(ra2, resId = resourceId, resType = resourceType)
112
113             webTestClient
114                 .get()
115                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
116                 .exchange()
117                 .expectStatus().isOk
118                 .expectBody()
119                 .consumeWith {
120                     val json = String(it.responseBody!!)
121                     val typeFactory = JacksonUtils.objectMapper.typeFactory
122                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
123                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
124                     Assert.assertEquals(2, list.size)
125                     assertEqual(ra1, list[0])
126                     assertEqual(ra1, list[0])
127                 }
128         }
129     }
130
131
132     @Test
133     fun getAllFromMissingParamTest() {
134         runBlocking {
135             webTestClient
136                 .get()
137                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
138                 .exchange()
139                 .expectStatus().is4xxClientError
140                 .expectBody()
141                 .consumeWith {
142                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
143                     Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
144                         r.message)
145                 }
146         }
147     }
148
149     @Test
150     fun getAllFromWrongInputTest() {
151         runBlocking {
152             webTestClient
153                 .get()
154                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
155                 .exchange()
156                 .expectStatus().is4xxClientError
157                 .expectBody()
158                 .consumeWith {
159                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
160                     Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
161                         r.message)
162                 }
163         }
164     }
165
166     @Test
167     fun getOneFromResolutionKeyTest() {
168         val resolutionKey = "3"
169         val ra = createRA("joe")
170         runBlocking {
171             store(ra, resKey = resolutionKey)
172         }
173         runBlocking {
174             webTestClient.get()
175                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
176                 .exchange()
177                 .expectStatus().isOk
178                 .expectBody()
179                 .consumeWith {
180                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
181                     assertEqual(ra, r)
182                 }
183         }
184     }
185
186     @Test
187     fun getOneFromResolutionKey404Test() {
188         val resolutionKey = "3"
189         runBlocking {
190             webTestClient.get()
191                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
192                 .exchange()
193                 .expectStatus().is4xxClientError
194                 .expectBody()
195         }
196     }
197
198     private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
199                               resType: String = "") {
200         resourceResolutionDBService.write(blueprintName,
201             blueprintVersion,
202             resKey,
203             resId,
204             resType,
205             templatePrefix,
206             resourceAssignment)
207     }
208
209     private fun createRA(prefix: String): ResourceAssignment {
210         val property = PropertyDefinition()
211         property.value = "value$prefix".asJsonPrimitive()
212
213         val resourceAssignment = ResourceAssignment()
214         resourceAssignment.name = prefix
215         resourceAssignment.dictionaryName = "dd$prefix"
216         resourceAssignment.dictionarySource = "source$prefix"
217         resourceAssignment.version = 2
218         resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
219         resourceAssignment.property = property
220         return resourceAssignment
221     }
222
223     private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
224         Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
225             resourceResolution.value)
226         Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
227         Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
228         Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
229         Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
230         Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
231         Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
232         Assert.assertEquals(blueprintName, resourceResolution.blueprintName)
233
234     }
235 }