2 * Copyright © 2019 Bell Canada Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
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
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
51 import java.nio.charset.StandardCharsets.UTF_8
52 import kotlin.test.assertEquals
53 import kotlin.test.assertNotNull
54 import kotlin.test.assertTrue
57 * BlueprintModelControllerTest Purpose: Integration test at API level
63 @RunWith(SpringRunner::class)
64 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
65 @ContextConfiguration(
66 classes = [DesignerApiTestConfiguration::class, ErrorCatalogTestConfiguration::class]
68 @TestPropertySource(locations = ["classpath:application-test.properties"])
69 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
70 class BlueprintModelControllerTest {
72 private val log = logger(BlueprintModelControllerTest::class)
76 private var bp: BlueprintModelSearch? = null
80 lateinit var webTestClient: WebTestClient
82 private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
84 private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
85 private var zipBlueprintFileName: String? = null
87 private var testZipFile: File? = null
91 assertNotNull(webTestClient, " Failed to create WebTestClient")
93 bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
94 blueprintArchivePath = "./target/blueprints/archive"
95 blueprintWorkingPath = "./target/blueprints/work"
96 blueprintDeployPath = "./target/blueprints/deploy"
98 zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
100 val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
101 assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
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}")
111 deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
112 deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
116 fun test01_saveBluePrint() {
118 val body = MultipartBodyBuilder().apply {
121 object : ByteArrayResource(testZipFile!!.readBytes()) {
122 override fun getFilename(): String {
129 val saveBP = webTestClient
131 .uri("/api/v1/blueprint-model")
132 .body(BodyInserters.fromMultipartData(body))
135 .returnResult<BlueprintModelSearch>()
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")
148 @Throws(JSONException::class)
149 fun test02_getBluePrintByNameAndVersion() {
151 HttpMethod.GET, null,
152 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
158 @Throws(JSONException::class)
159 fun test03_getBlueprintModel() {
161 HttpMethod.GET, null,
162 "/api/v1/blueprint-model/${bp!!.id}",
168 @Throws(JSONException::class)
169 fun test04_getAllBlueprintModel() {
170 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
174 @Throws(JSONException::class)
175 fun test05_downloadBluePrint() {
177 HttpMethod.GET, null,
178 "/api/v1/blueprint-model/download/${bp!!.id}",
184 fun test06_enrichBlueprintModel() {
188 fun test07_publishBlueprintModel() {
190 val body = MultipartBodyBuilder().apply {
193 object : ByteArrayResource(testZipFile!!.readBytes()) {
194 override fun getFilename(): String {
201 val publishBP = webTestClient
203 .uri("/api/v1/blueprint-model/publish")
204 .body(BodyInserters.fromMultipartData(body))
207 .returnResult<BlueprintModelSearch>()
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")
220 @Throws(JSONException::class)
221 fun test08_searchBlueprintModels() {
223 HttpMethod.GET, null,
224 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
230 @Throws(JSONException::class)
231 fun test09_downloadBlueprintByNameAndVersion() {
233 HttpMethod.GET, null,
234 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
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)))
245 // .expectStatus().is2xxSuccessful
247 webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
250 "Basic " + Base64Utils
251 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
254 .expectStatus().is2xxSuccessful
257 @Throws(JSONException::class)
258 private fun webTestClient(
259 requestMethod: HttpMethod,
260 body: BodyInserters.MultipartInserter?,
262 expectedResponceStatus: HttpStatus,
266 log.info("Requesting($uri): Method(${requestMethod.name})")
268 webTestClient.method(requestMethod).uri(uri)
271 "Basic " + Base64Utils
272 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
276 .expectStatus().isEqualTo(expectedResponceStatus)
278 .returnResult().responseBody!!