cde71d86c7855ccb96ced5a4b656f3b88b83d462
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / resource-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / resolutionresults / 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.resolutionresults.api
18
19 import com.fasterxml.jackson.core.type.TypeReference
20 import com.fasterxml.jackson.module.kotlin.readValue
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Assert
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
33 import org.python.jline.console.internal.ConsoleRunner.property
34 import org.slf4j.LoggerFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.autoconfigure.security.SecurityProperties
37 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.http.HttpHeaders
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.MediaType
42 import org.springframework.test.context.ContextConfiguration
43 import org.springframework.test.context.TestPropertySource
44 import org.springframework.test.context.junit4.SpringRunner
45 import org.springframework.test.web.reactive.server.WebTestClient
46 import org.springframework.web.reactive.function.BodyInserters
47 import java.util.function.Consumer
48 import kotlin.test.BeforeTest
49 import org.h2.value.DataType.readValue
50 import java.util.*
51 import org.h2.value.DataType.readValue
52 import org.python.bouncycastle.asn1.x500.style.RFC4519Style.l
53 import org.h2.value.DataType.readValue
54 import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ErrorMessage
55 import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ResourceController
56 import java.lang.reflect.Array
57
58
59 @RunWith(SpringRunner::class)
60 @WebFluxTest
61 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
62 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
63 @TestPropertySource(locations = ["classpath:application-test.properties"])
64 class ResourceControllerTest {
65
66     private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
67
68     @Autowired
69     lateinit var resourceResolutionDBService: ResourceResolutionDBService
70     @Autowired
71     lateinit var webTestClient: WebTestClient
72
73     val blueprintName = "baseconfiguration"
74     val blueprintVersion = "1.0.0"
75     val templatePrefix = "activate"
76
77     @Test
78     fun `ping return Success`() {
79         runBlocking {
80             webTestClient.get().uri("/api/v1/resources/ping")
81                 .exchange()
82                 .expectStatus().isOk
83                 .expectBody()
84                 .equals("Success")
85         }
86     }
87
88     @Test
89     fun getAllFromResolutionKeyTest() {
90
91         val resolutionKey = "1"
92         val ra1 = createRA("bob")
93         val ra2 = createRA("dylan")
94
95         runBlocking {
96
97             store(ra1, resKey = resolutionKey)
98             store(ra2, resKey = resolutionKey)
99
100             webTestClient
101                 .get()
102                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
103                 .exchange()
104                 .expectStatus().isOk
105                 .expectBody()
106                 .consumeWith {
107                     val json = String(it.responseBody!!)
108                     val typeFactory = JacksonUtils.objectMapper.typeFactory
109                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
110                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
111                     Assert.assertEquals(2, list.size)
112                     assertEqual(ra1, list[0])
113                     assertEqual(ra1, list[0])
114                 }
115         }
116     }
117
118     @Test
119     fun getAllFromFromResourceTypeAndIdTest() {
120
121         val resourceId = "1"
122         val resourceType = "ServiceInstance"
123         val ra1 = createRA("bob")
124         val ra2 = createRA("dylan")
125
126         runBlocking {
127
128             store(ra1, resId = resourceId, resType = resourceType)
129             store(ra2, resId = resourceId, resType = resourceType)
130
131             webTestClient
132                 .get()
133                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
134                 .exchange()
135                 .expectStatus().isOk
136                 .expectBody()
137                 .consumeWith {
138                     val json = String(it.responseBody!!)
139                     val typeFactory = JacksonUtils.objectMapper.typeFactory
140                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
141                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
142                     Assert.assertEquals(2, list.size)
143                     assertEqual(ra1, list[0])
144                     assertEqual(ra1, list[0])
145                 }
146         }
147     }
148
149
150     @Test
151     fun getAllFromMissingParamTest() {
152         runBlocking {
153             webTestClient
154                 .get()
155                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
156                 .exchange()
157                 .expectStatus().is4xxClientError
158                 .expectBody()
159                 .consumeWith {
160                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
161                     Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
162                         r.message)
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, ErrorMessage::class.java)
178                     Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
179                         r.message)
180                 }
181         }
182     }
183
184     @Test
185     fun getOneFromResolutionKeyTest() {
186         val resolutionKey = "3"
187         val ra = createRA("joe")
188         runBlocking {
189             store(ra, resKey = resolutionKey)
190         }
191         runBlocking {
192             webTestClient.get()
193                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe")
194                 .exchange()
195                 .expectStatus().isOk
196                 .expectBody()
197                 .consumeWith {
198                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java)
199                     assertEqual(ra, r)
200                 }
201         }
202     }
203
204     @Test
205     fun getOneFromResolutionKey404Test() {
206         val resolutionKey = "3"
207         runBlocking {
208             webTestClient.get()
209                 .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist")
210                 .exchange()
211                 .expectStatus().is4xxClientError
212                 .expectBody()
213         }
214     }
215
216     private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
217                               resType: String = "") {
218         resourceResolutionDBService.write(blueprintName,
219             blueprintVersion,
220             resKey,
221             resId,
222             resType,
223             templatePrefix,
224             resourceAssignment)
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 = "SUCCESS"
237         resourceAssignment.property = property
238         return resourceAssignment
239     }
240
241     private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
242         Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
243             resourceResolution.value)
244         Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
245         Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
246         Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
247         Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
248         Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
249         Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
250         Assert.assertEquals(blueprintName, resourceResolution.blueprintName)
251
252     }
253 }