Refactoring BP Code with ErrorCatalog
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / BlueprintModelControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
19
20 import kotlinx.coroutines.reactive.awaitSingle
21 import kotlinx.coroutines.runBlocking
22 import org.json.JSONException
23 import org.junit.After
24 import org.junit.Before
25 import org.junit.FixMethodOrder
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.MethodSorters
29 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
30 import org.onap.ccsdk.cds.controllerblueprints.core.compress
31 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
32 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
33 import org.onap.ccsdk.cds.controllerblueprints.core.logger
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
35 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
36 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateDirs
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.test.context.SpringBootTest
39 import org.springframework.core.io.ByteArrayResource
40 import org.springframework.http.HttpMethod
41 import org.springframework.http.HttpStatus
42 import org.springframework.http.client.MultipartBodyBuilder
43 import org.springframework.test.context.ContextConfiguration
44 import org.springframework.test.context.TestPropertySource
45 import org.springframework.test.context.junit4.SpringRunner
46 import org.springframework.test.web.reactive.server.WebTestClient
47 import org.springframework.test.web.reactive.server.returnResult
48 import org.springframework.util.Base64Utils
49 import org.springframework.web.reactive.function.BodyInserters
50 import java.io.File
51 import java.nio.charset.StandardCharsets.UTF_8
52 import kotlin.test.assertEquals
53 import kotlin.test.assertNotNull
54 import kotlin.test.assertTrue
55
56 /**
57  * BlueprintModelControllerTest Purpose: Integration test at API level
58  *
59  * @author Vinal Patel
60  * @version 1.0
61  */
62
63 @RunWith(SpringRunner::class)
64 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
65 @ContextConfiguration(
66     classes = [DesignerApiTestConfiguration::class, ErrorCatalogTestConfiguration::class]
67 )
68 @TestPropertySource(locations = ["classpath:application-test.properties"])
69 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
70 class BlueprintModelControllerTest {
71
72     private val log = logger(BlueprintModelControllerTest::class)
73
74     companion object {
75         private var bp: BlueprintModelSearch? = null
76     }
77
78     @Autowired
79     lateinit var webTestClient: WebTestClient
80
81     private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
82
83     private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
84     private var zipBlueprintFileName: String? = null
85
86     private var testZipFile: File? = null
87
88     @Before
89     fun setUp() {
90         assertNotNull(webTestClient, " Failed to create WebTestClient")
91
92         bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
93             blueprintArchivePath = "./target/blueprints/archive"
94             blueprintWorkingPath = "./target/blueprints/work"
95             blueprintDeployPath = "./target/blueprints/deploy"
96         }
97         zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
98
99         val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
100         assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
101
102         val blueprintFile = normalizedFile(blueprintDir)
103         testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
104         assertNotNull(testZipFile, "test zip is null")
105         assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
106     }
107
108     @After
109     fun tearDown() {
110         deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
111         deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
112     }
113
114     @Test
115     fun test01_saveBluePrint() {
116         bp = runBlocking {
117             val body = MultipartBodyBuilder().apply {
118                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
119                     override fun getFilename(): String {
120                         return "test.zip"
121                     }
122                 })
123             }.build()
124
125             val saveBP = webTestClient
126                 .post()
127                 .uri("/api/v1/blueprint-model")
128                 .body(BodyInserters.fromMultipartData(body))
129                 .exchange()
130                 .expectStatus().isOk
131                 .returnResult<BlueprintModelSearch>()
132                 .responseBody
133                 .awaitSingle()
134
135             assertNotNull(saveBP, "failed to get response")
136             assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
137             assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
138             assertEquals("N", saveBP.published, "mismatch publish")
139             saveBP
140         }
141     }
142
143     @Test
144     @Throws(JSONException::class)
145     fun test02_getBluePrintByNameAndVersion() {
146         webTestClient(
147             HttpMethod.GET, null,
148             "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
149             HttpStatus.OK, false
150         )
151     }
152
153     @Test
154     @Throws(JSONException::class)
155     fun test03_getBlueprintModel() {
156         webTestClient(
157             HttpMethod.GET, null,
158             "/api/v1/blueprint-model/${bp!!.id}",
159             HttpStatus.OK, false
160         )
161     }
162
163     @Test
164     @Throws(JSONException::class)
165     fun test04_getAllBlueprintModel() {
166         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
167     }
168
169     @Test
170     @Throws(JSONException::class)
171     fun test05_downloadBluePrint() {
172         webTestClient(
173             HttpMethod.GET, null,
174             "/api/v1/blueprint-model/download/${bp!!.id}",
175             HttpStatus.OK, false
176         )
177     }
178
179     @Test
180     fun test06_enrichBlueprintModel() {
181     }
182
183     @Test
184     fun test07_publishBlueprintModel() {
185         bp = runBlocking {
186             val body = MultipartBodyBuilder().apply {
187                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
188                     override fun getFilename(): String {
189                         return "test.zip"
190                     }
191                 })
192             }.build()
193
194             val publishBP = webTestClient
195                 .post()
196                 .uri("/api/v1/blueprint-model/publish")
197                 .body(BodyInserters.fromMultipartData(body))
198                 .exchange()
199                 .expectStatus().isOk
200                 .returnResult<BlueprintModelSearch>()
201                 .responseBody
202                 .awaitSingle()
203
204             assertNotNull(publishBP, "failed to get response")
205             assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
206             assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
207             assertEquals("Y", publishBP.published, "mismatch publish")
208             publishBP
209         }
210     }
211
212     @Test
213     @Throws(JSONException::class)
214     fun test08_searchBlueprintModels() {
215         webTestClient(
216             HttpMethod.GET, null,
217             "/api/v1/blueprint-model/search/${bp!!.artifactName}",
218             HttpStatus.OK, false
219         )
220     }
221
222     @Test
223     @Throws(JSONException::class)
224     fun test09_downloadBlueprintByNameAndVersion() {
225         webTestClient(
226             HttpMethod.GET, null,
227             "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
228             HttpStatus.OK, false
229         )
230     }
231
232     @Test
233     fun test10_deleteBluePrint() {
234         //        webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
235         //                .header("Authorization", "Basic " + Base64Utils
236         //                        .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
237         //                .exchange()
238         //                .expectStatus().is2xxSuccessful
239
240         webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
241             .header(
242                 "Authorization", "Basic " + Base64Utils
243                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
244             )
245             .exchange()
246             .expectStatus().is2xxSuccessful
247     }
248
249     @Throws(JSONException::class)
250     private fun webTestClient(
251         requestMethod: HttpMethod,
252         body: BodyInserters.MultipartInserter?,
253         uri: String,
254         expectedResponceStatus: HttpStatus,
255         setParam: Boolean
256     ) {
257
258         log.info("Requesting($uri): Method(${requestMethod.name})")
259
260         webTestClient.method(requestMethod).uri(uri)
261             .header(
262                 "Authorization", "Basic " + Base64Utils
263                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
264             )
265             .body(body)
266             .exchange()
267             .expectStatus().isEqualTo(expectedResponceStatus)
268             .expectBody()
269             .returnResult().responseBody!!
270     }
271 }