Optimize spring data JPA UT.
[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(
43     classes = [TestDatabaseConfiguration::class,
44         ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::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, ErrorMessage::class.java)
152                     Assert.assertEquals(
153                         "Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
154                         r.message
155                     )
156                 }
157         }
158     }
159
160     @Test
161     fun getAllFromWrongInputTest() {
162         runBlocking {
163             webTestClient
164                 .get()
165                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
166                 .exchange()
167                 .expectStatus().is4xxClientError
168                 .expectBody()
169                 .consumeWith {
170                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
171                     Assert.assertEquals(
172                         "Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
173                         r.message
174                     )
175                 }
176         }
177     }
178
179     @Test
180     fun getOneFromResolutionKeyTest() {
181         val resolutionKey = "3"
182         val ra = createRA("joe")
183         runBlocking {
184             store(ra, resKey = resolutionKey)
185         }
186         runBlocking {
187             webTestClient.get()
188                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
189                 .exchange()
190                 .expectStatus().isOk
191                 .expectBody()
192                 .consumeWith {
193                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
194                     assertEqual(ra, r)
195                 }
196         }
197     }
198
199     @Test
200     fun getOneFromResolutionKey404Test() {
201         val resolutionKey = "3"
202         runBlocking {
203             webTestClient.get()
204                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
205                 .exchange()
206                 .expectStatus().is4xxClientError
207                 .expectBody()
208         }
209     }
210
211     private suspend fun store(
212         resourceAssignment: ResourceAssignment,
213         resKey: String = "",
214         resId: String = "",
215         resType: String = ""
216     ) {
217         resourceResolutionDBService.write(
218             blueprintName,
219             blueprintVersion,
220             resKey,
221             resId,
222             resType,
223             templatePrefix,
224             resourceAssignment
225         )
226     }
227
228     private fun createRA(prefix: String): ResourceAssignment {
229         val property = PropertyDefinition()
230         property.value = "value$prefix".asJsonPrimitive()
231
232         val resourceAssignment = ResourceAssignment()
233         resourceAssignment.name = prefix
234         resourceAssignment.dictionaryName = "dd$prefix"
235         resourceAssignment.dictionarySource = "source$prefix"
236         resourceAssignment.version = 2
237         resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
238         resourceAssignment.property = property
239         return resourceAssignment
240     }
241
242     private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
243         Assert.assertEquals(
244             JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
245             resourceResolution.value
246         )
247         Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
248         Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
249         Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
250         Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
251         Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
252         Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
253         Assert.assertEquals(blueprintName, resourceResolution.blueprintName)
254     }
255 }