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