50eafd12ccf256a9e1d74e742e229fa37752fafe
[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.core.BluePrintPropertiesService
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
33 import org.onap.ccsdk.cds.controllerblueprints.core.compress
34 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
35 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
36 import org.onap.ccsdk.cds.controllerblueprints.core.logger
37 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
38 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
39 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateDirs
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.test.context.SpringBootTest
42 import org.springframework.core.io.ByteArrayResource
43 import org.springframework.http.HttpMethod
44 import org.springframework.http.HttpStatus
45 import org.springframework.http.client.MultipartBodyBuilder
46 import org.springframework.test.context.ContextConfiguration
47 import org.springframework.test.context.TestPropertySource
48 import org.springframework.test.context.junit4.SpringRunner
49 import org.springframework.test.web.reactive.server.WebTestClient
50 import org.springframework.test.web.reactive.server.returnResult
51 import org.springframework.util.Base64Utils
52 import org.springframework.web.reactive.function.BodyInserters
53 import java.io.File
54 import java.nio.charset.StandardCharsets.UTF_8
55 import kotlin.test.assertEquals
56 import kotlin.test.assertNotNull
57 import kotlin.test.assertTrue
58
59 /**
60  * BlueprintModelControllerTest Purpose: Integration test at API level
61  *
62  * @author Vinal Patel
63  * @version 1.0
64  */
65
66 @RunWith(SpringRunner::class)
67 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
68 @ContextConfiguration(
69     classes = [DesignerApiTestConfiguration::class,
70         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDBLibConfiguration::class]
71 )
72 @TestPropertySource(locations = ["classpath:application-test.properties"])
73 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
74 class BlueprintModelControllerTest {
75
76     private val log = logger(BlueprintModelControllerTest::class)
77
78     companion object {
79         private var bp: BlueprintModelSearch? = null
80     }
81
82     @Autowired
83     lateinit var webTestClient: WebTestClient
84
85     private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
86
87     private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
88     private var zipBlueprintFileName: String? = null
89
90     private var testZipFile: File? = null
91
92     @Before
93     fun setUp() {
94         assertNotNull(webTestClient, " Failed to create WebTestClient")
95
96         bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
97             blueprintArchivePath = "./target/blueprints/archive"
98             blueprintWorkingPath = "./target/blueprints/work"
99             blueprintDeployPath = "./target/blueprints/deploy"
100         }
101         zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
102
103         val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
104         assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
105
106         val blueprintFile = normalizedFile(blueprintDir)
107         testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
108         assertNotNull(testZipFile, "test zip is null")
109         assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
110     }
111
112     @After
113     fun tearDown() {
114         deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
115         deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
116     }
117
118     @Test
119     fun test01_saveBluePrint() {
120         bp = runBlocking {
121             val body = MultipartBodyBuilder().apply {
122                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
123                     override fun getFilename(): String {
124                         return "test.zip"
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("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
192                     override fun getFilename(): String {
193                         return "test.zip"
194                     }
195                 })
196             }.build()
197
198             val publishBP = webTestClient
199                 .post()
200                 .uri("/api/v1/blueprint-model/publish")
201                 .body(BodyInserters.fromMultipartData(body))
202                 .exchange()
203                 .expectStatus().isOk
204                 .returnResult<BlueprintModelSearch>()
205                 .responseBody
206                 .awaitSingle()
207
208             assertNotNull(publishBP, "failed to get response")
209             assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
210             assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
211             assertEquals("Y", publishBP.published, "mismatch publish")
212             publishBP
213         }
214     }
215
216     @Test
217     @Throws(JSONException::class)
218     fun test08_searchBlueprintModels() {
219         webTestClient(
220             HttpMethod.GET, null,
221             "/api/v1/blueprint-model/search/${bp!!.artifactName}",
222             HttpStatus.OK, false
223         )
224     }
225
226     @Test
227     @Throws(JSONException::class)
228     fun test09_downloadBlueprintByNameAndVersion() {
229         webTestClient(
230             HttpMethod.GET, null,
231             "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
232             HttpStatus.OK, false
233         )
234     }
235
236     @Test
237     fun test10_deleteBluePrint() {
238         //        webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
239         //                .header("Authorization", "Basic " + Base64Utils
240         //                        .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
241         //                .exchange()
242         //                .expectStatus().is2xxSuccessful
243
244         webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
245             .header(
246                 "Authorization", "Basic " + Base64Utils
247                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
248             )
249             .exchange()
250             .expectStatus().is2xxSuccessful
251     }
252
253     @Throws(JSONException::class)
254     private fun webTestClient(
255         requestMethod: HttpMethod,
256         body: BodyInserters.MultipartInserter?,
257         uri: String,
258         expectedResponceStatus: HttpStatus,
259         setParam: Boolean
260     ) {
261
262         log.info("Requesting($uri): Method(${requestMethod.name})")
263
264         webTestClient.method(requestMethod).uri(uri)
265             .header(
266                 "Authorization", "Basic " + Base64Utils
267                     .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
268             )
269             .body(body)
270             .exchange()
271             .expectStatus().isEqualTo(expectedResponceStatus)
272             .expectBody()
273             .returnResult().responseBody!!
274     }
275 }