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