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