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