fa8bf4459941fcd6cf866423cf0332bcfa499e11
[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.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 java.lang.reflect.Array
55
56
57 @RunWith(SpringRunner::class)
58 @WebFluxTest
59 @ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class])
60 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 class ResourceControllerTest {
63
64     private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString())
65
66     @Autowired
67     lateinit var resourceResolutionDBService: ResourceResolutionDBService
68     @Autowired
69     lateinit var webTestClient: WebTestClient
70
71     val blueprintName = "baseconfiguration"
72     val blueprintVersion = "1.0.0"
73     val templatePrefix = "activate"
74
75     @Test
76     fun `ping return Success`() {
77         runBlocking {
78             webTestClient.get().uri("/api/v1/resources/ping")
79                 .exchange()
80                 .expectStatus().isOk
81                 .expectBody()
82                 .equals("Success")
83         }
84     }
85
86     @Test
87     fun getAllFromResolutionKeyTest() {
88
89         val resolutionKey = "1"
90         val ra1 = createRA("bob")
91         val ra2 = createRA("dylan")
92
93         runBlocking {
94
95             store(ra1, resKey = resolutionKey)
96             store(ra2, resKey = resolutionKey)
97
98             webTestClient
99                 .get()
100                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey")
101                 .exchange()
102                 .expectStatus().isOk
103                 .expectBody()
104                 .consumeWith {
105                     val json = String(it.responseBody!!)
106                     val typeFactory = JacksonUtils.objectMapper.typeFactory
107                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
108                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
109                     Assert.assertEquals(2, list.size)
110                     assertEqual(ra1, list[0])
111                     assertEqual(ra1, list[0])
112                 }
113         }
114     }
115
116     @Test
117     fun getAllFromFromResourceTypeAndIdTest() {
118
119         val resourceId = "1"
120         val resourceType = "ServiceInstance"
121         val ra1 = createRA("bob")
122         val ra2 = createRA("dylan")
123
124         runBlocking {
125
126             store(ra1, resId = resourceId, resType = resourceType)
127             store(ra2, resId = resourceId, resType = resourceType)
128
129             webTestClient
130                 .get()
131                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId")
132                 .exchange()
133                 .expectStatus().isOk
134                 .expectBody()
135                 .consumeWith {
136                     val json = String(it.responseBody!!)
137                     val typeFactory = JacksonUtils.objectMapper.typeFactory
138                     val list: List<ResourceResolution> = JacksonUtils.objectMapper.readValue(json,
139                         typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java))
140                     Assert.assertEquals(2, list.size)
141                     assertEqual(ra1, list[0])
142                     assertEqual(ra1, list[0])
143                 }
144         }
145     }
146
147
148     @Test
149     fun getAllFromMissingParamTest() {
150         runBlocking {
151             webTestClient
152                 .get()
153                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion")
154                 .exchange()
155                 .expectStatus().is4xxClientError
156                 .expectBody()
157                 .consumeWith {
158                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
159                     Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
160                         r.message)
161                 }
162         }
163     }
164
165     @Test
166     fun getAllFromWrongInputTest() {
167         runBlocking {
168             webTestClient
169                 .get()
170                 .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1")
171                 .exchange()
172                 .expectStatus().is4xxClientError
173                 .expectBody()
174                 .consumeWith {
175                     val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java)
176                     Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.",
177                         r.message)
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(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "",
215                               resType: String = "") {
216         resourceResolutionDBService.write(blueprintName,
217             blueprintVersion,
218             resKey,
219             resId,
220             resType,
221             templatePrefix,
222             resourceAssignment)
223     }
224
225     private fun createRA(prefix: String): ResourceAssignment {
226         val property = PropertyDefinition()
227         property.value = "value$prefix".asJsonPrimitive()
228
229         val resourceAssignment = ResourceAssignment()
230         resourceAssignment.name = prefix
231         resourceAssignment.dictionaryName = "dd$prefix"
232         resourceAssignment.dictionarySource = "source$prefix"
233         resourceAssignment.version = 2
234         resourceAssignment.status = "SUCCESS"
235         resourceAssignment.property = property
236         return resourceAssignment
237     }
238
239     private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) {
240         Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(),
241             resourceResolution.value)
242         Assert.assertEquals(resourceAssignment.status, resourceResolution.status)
243         Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource)
244         Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName)
245         Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion)
246         Assert.assertEquals(resourceAssignment.name, resourceResolution.name)
247         Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion)
248         Assert.assertEquals(blueprintName, resourceResolution.blueprintName)
249
250     }
251 }