149acb03c962e84759f610e34ba5310762a1c72a
[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.*
34 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.test.context.SpringBootTest
37 import org.springframework.core.io.ByteArrayResource
38 import org.springframework.http.HttpMethod
39 import org.springframework.http.HttpStatus
40 import org.springframework.http.client.MultipartBodyBuilder
41 import org.springframework.test.context.ContextConfiguration
42 import org.springframework.test.context.TestPropertySource
43 import org.springframework.test.context.junit4.SpringRunner
44 import org.springframework.test.web.reactive.server.WebTestClient
45 import org.springframework.test.web.reactive.server.returnResult
46 import org.springframework.util.Base64Utils
47 import org.springframework.web.reactive.function.BodyInserters
48 import java.io.File
49 import java.nio.charset.StandardCharsets.UTF_8
50 import kotlin.test.assertEquals
51 import kotlin.test.assertNotNull
52 import kotlin.test.assertTrue
53
54 /**
55  * BlueprintModelControllerTest Purpose: Integration test at API level
56  *
57  * @author Vinal Patel
58  * @version 1.0
59  */
60
61 @RunWith(SpringRunner::class)
62 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
63 @ContextConfiguration(classes = [DesignerApiTestConfiguration::class,
64     BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDBLibConfiguration::class])
65 @TestPropertySource(locations = ["classpath:application-test.properties"])
66 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
67 class BlueprintModelControllerTest {
68
69     private val log = logger(BlueprintModelControllerTest::class)
70
71     companion object {
72         private var bp: BlueprintModelSearch? = null
73     }
74
75     @Autowired
76     lateinit var webTestClient: WebTestClient
77
78     private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
79
80     private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
81     private var zipBlueprintFileName: String? = null
82
83     private var testZipFile: File? = null
84
85
86     @Before
87     fun setUp() {
88         assertNotNull(webTestClient, " Failed to create WebTestClient")
89
90         bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
91             blueprintArchivePath = "./target/blueprints/archive"
92             blueprintWorkingPath = "./target/blueprints/work"
93             blueprintDeployPath = "./target/blueprints/deploy"
94         }
95         zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
96
97         val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
98         assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
99
100         val blueprintFile = normalizedFile(blueprintDir)
101         testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
102         assertNotNull(testZipFile, "test zip is null")
103         assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
104     }
105
106     @After
107     fun tearDown() {
108         deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
109         deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
110     }
111
112     @Test
113     fun test01_saveBluePrint() {
114         bp = runBlocking {
115             val body = MultipartBodyBuilder().apply {
116                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
117                     override fun getFilename(): String {
118                         return "test.zip"
119                     }
120                 })
121             }.build()
122
123             val saveBP = webTestClient
124                     .post()
125                     .uri("/api/v1/blueprint-model")
126                     .body(BodyInserters.fromMultipartData(body))
127                     .exchange()
128                     .expectStatus().isOk
129                     .returnResult<BlueprintModelSearch>()
130                     .responseBody
131                     .awaitSingle()
132
133             assertNotNull(saveBP, "failed to get response")
134             assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
135             assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
136             assertEquals("N", saveBP.published, "mismatch publish")
137             saveBP
138         }
139     }
140
141     @Test
142     @Throws(JSONException::class)
143     fun test02_getBluePrintByNameAndVersion() {
144         webTestClient(HttpMethod.GET, null,
145                 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
146                 HttpStatus.OK, false)
147     }
148
149
150     @Test
151     @Throws(JSONException::class)
152     fun test03_getBlueprintModel() {
153         webTestClient(HttpMethod.GET, null,
154                 "/api/v1/blueprint-model/${bp!!.id}",
155                 HttpStatus.OK, false)
156     }
157
158     @Test
159     @Throws(JSONException::class)
160     fun test04_getAllBlueprintModel() {
161         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
162     }
163
164     @Test
165     @Throws(JSONException::class)
166     fun test05_downloadBluePrint() {
167         webTestClient(HttpMethod.GET, null,
168                 "/api/v1/blueprint-model/download/${bp!!.id}",
169                 HttpStatus.OK, false)
170     }
171
172     @Test
173     fun test06_enrichBlueprintModel() {
174     }
175
176     @Test
177     fun test07_publishBlueprintModel() {
178         bp = runBlocking {
179             val body = MultipartBodyBuilder().apply {
180                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
181                     override fun getFilename(): String {
182                         return "test.zip"
183                     }
184                 })
185             }.build()
186
187             val publishBP = webTestClient
188                     .post()
189                     .uri("/api/v1/blueprint-model/publish")
190                     .body(BodyInserters.fromMultipartData(body))
191                     .exchange()
192                     .expectStatus().isOk
193                     .returnResult<BlueprintModelSearch>()
194                     .responseBody
195                     .awaitSingle()
196
197             assertNotNull(publishBP, "failed to get response")
198             assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
199             assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
200             assertEquals("Y", publishBP.published, "mismatch publish")
201             publishBP
202         }
203     }
204
205     @Test
206     @Throws(JSONException::class)
207     fun test08_searchBlueprintModels() {
208         webTestClient(HttpMethod.GET, null,
209                 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
210                 HttpStatus.OK, false)
211     }
212
213     @Test
214     @Throws(JSONException::class)
215     fun test09_downloadBlueprintByNameAndVersion() {
216         webTestClient(HttpMethod.GET, null,
217                 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
218                 HttpStatus.OK, false)
219     }
220
221     @Test
222     fun test10_deleteBluePrint() {
223 //        webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
224 //                .header("Authorization", "Basic " + Base64Utils
225 //                        .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
226 //                .exchange()
227 //                .expectStatus().is2xxSuccessful
228
229         webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
230                 .header("Authorization", "Basic " + Base64Utils
231                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
232                 .exchange()
233                 .expectStatus().is2xxSuccessful
234     }
235
236     @Throws(JSONException::class)
237     private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
238                               expectedResponceStatus: HttpStatus, setParam: Boolean) {
239
240         log.info("Requesting($uri): Method(${requestMethod.name})")
241
242         webTestClient.method(requestMethod).uri(uri)
243                 .header("Authorization", "Basic " + Base64Utils
244                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
245                 .body(body)
246                 .exchange()
247                 .expectStatus().isEqualTo(expectedResponceStatus)
248                 .expectBody()
249                 .returnResult().responseBody!!
250
251     }
252
253 }