Revert "Renaming Files having BluePrint to have Blueprint"
[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
76         private var bp: BlueprintModelSearch? = null
77     }
78
79     @Autowired
80     lateinit var webTestClient: WebTestClient
81
82     private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
83
84     private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
85     private var zipBlueprintFileName: String? = null
86
87     private var testZipFile: File? = null
88
89     @Before
90     fun setUp() {
91         assertNotNull(webTestClient, " Failed to create WebTestClient")
92
93         bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
94             blueprintArchivePath = "./target/blueprints/archive"
95             blueprintWorkingPath = "./target/blueprints/work"
96             blueprintDeployPath = "./target/blueprints/deploy"
97         }
98         zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
99
100         val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
101         assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
102
103         val blueprintFile = normalizedFile(blueprintDir)
104         testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
105         assertNotNull(testZipFile, "test zip is null")
106         assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
107     }
108
109     @After
110     fun tearDown() {
111         deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
112         deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
113     }
114
115     @Test
116     fun test01_saveBluePrint() {
117         bp = runBlocking {
118             val body = MultipartBodyBuilder().apply {
119                 part(
120                     "file",
121                     object : ByteArrayResource(testZipFile!!.readBytes()) {
122                         override fun getFilename(): String {
123                             return "test.zip"
124                         }
125                     }
126                 )
127             }.build()
128
129             val saveBP = webTestClient
130                 .post()
131                 .uri("/api/v1/blueprint-model")
132                 .body(BodyInserters.fromMultipartData(body))
133                 .exchange()
134                 .expectStatus().isOk
135                 .returnResult<BlueprintModelSearch>()
136                 .responseBody
137                 .awaitSingle()
138
139             assertNotNull(saveBP, "failed to get response")
140             assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
141             assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
142             assertEquals("N", saveBP.published, "mismatch publish")
143             saveBP
144         }
145     }
146
147     @Test
148     @Throws(JSONException::class)
149     fun test02_getBluePrintByNameAndVersion() {
150         webTestClient(
151             HttpMethod.GET, null,
152             "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
153             HttpStatus.OK, false
154         )
155     }
156
157     @Test
158     @Throws(JSONException::class)
159     fun test03_getBlueprintModel() {
160         webTestClient(
161             HttpMethod.GET, null,
162             "/api/v1/blueprint-model/${bp!!.id}",
163             HttpStatus.OK, false
164         )
165     }
166
167     @Test
168     @Throws(JSONException::class)
169     fun test04_getAllBlueprintModel() {
170         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
171     }
172
173     @Test
174     @Throws(JSONException::class)
175     fun test05_downloadBluePrint() {
176         webTestClient(
177             HttpMethod.GET, null,
178             "/api/v1/blueprint-model/download/${bp!!.id}",
179             HttpStatus.OK, false
180         )
181     }
182
183     @Test
184     fun test06_enrichBlueprintModel() {
185     }
186
187     @Test
188     fun test07_publishBlueprintModel() {
189         bp = runBlocking {
190             val body = MultipartBodyBuilder().apply {
191                 part(
192                     "file",
193                     object : ByteArrayResource(testZipFile!!.readBytes()) {
194                         override fun getFilename(): String {
195                             return "test.zip"
196                         }
197                     }
198                 )
199             }.build()
200
201             val publishBP = webTestClient
202                 .post()
203                 .uri("/api/v1/blueprint-model/publish")
204                 .body(BodyInserters.fromMultipartData(body))
205                 .exchange()
206                 .expectStatus().isOk
207                 .returnResult<BlueprintModelSearch>()
208                 .responseBody
209                 .awaitSingle()
210
211             assertNotNull(publishBP, "failed to get response")
212             assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
213             assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
214             assertEquals("Y", publishBP.published, "mismatch publish")
215             publishBP
216         }
217     }
218
219     @Test
220     @Throws(JSONException::class)
221     fun test08_searchBlueprintModels() {
222         webTestClient(
223             HttpMethod.GET, null,
224             "/api/v1/blueprint-model/search/${bp!!.artifactName}",
225             HttpStatus.OK, false
226         )
227     }
228
229     @Test
230     @Throws(JSONException::class)
231     fun test09_downloadBlueprintByNameAndVersion() {
232         webTestClient(
233             HttpMethod.GET, null,
234             "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
235             HttpStatus.OK, false
236         )
237     }
238
239     @Test
240     fun test10_deleteBluePrint() {
241         //        webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
242         //                .header("Authorization", "Basic " + Base64Utils
243         //                        .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
244         //                .exchange()
245         //                .expectStatus().is2xxSuccessful
246
247         webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
248             .header(
249                 "Authorization",
250                 "Basic " + Base64Utils
251                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
252             )
253             .exchange()
254             .expectStatus().is2xxSuccessful
255     }
256
257     @Throws(JSONException::class)
258     private fun webTestClient(
259         requestMethod: HttpMethod,
260         body: BodyInserters.MultipartInserter?,
261         uri: String,
262         expectedResponceStatus: HttpStatus,
263         setParam: Boolean
264     ) {
265
266         log.info("Requesting($uri): Method(${requestMethod.name})")
267
268         webTestClient.method(requestMethod).uri(uri)
269             .header(
270                 "Authorization",
271                 "Basic " + Base64Utils
272                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
273             )
274             .body(body)
275             .exchange()
276             .expectStatus().isEqualTo(expectedResponceStatus)
277             .expectBody()
278             .returnResult().responseBody!!
279     }
280 }