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