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.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
54 import java.nio.charset.StandardCharsets.UTF_8
55 import kotlin.test.assertEquals
56 import kotlin.test.assertNotNull
57 import kotlin.test.assertTrue
60 * BlueprintModelControllerTest Purpose: Integration test at API level
66 @RunWith(SpringRunner::class)
67 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
68 @ContextConfiguration(
69 classes = [DesignerApiTestConfiguration::class,
70 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDBLibConfiguration::class]
72 @TestPropertySource(locations = ["classpath:application-test.properties"])
73 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
74 class BlueprintModelControllerTest {
76 private val log = logger(BlueprintModelControllerTest::class)
79 private var bp: BlueprintModelSearch? = null
83 lateinit var webTestClient: WebTestClient
85 private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
87 private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
88 private var zipBlueprintFileName: String? = null
90 private var testZipFile: File? = null
94 assertNotNull(webTestClient, " Failed to create WebTestClient")
96 bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
97 blueprintArchivePath = "./target/blueprints/archive"
98 blueprintWorkingPath = "./target/blueprints/work"
99 blueprintDeployPath = "./target/blueprints/deploy"
101 zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
103 val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
104 assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
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}")
114 deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
115 deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
119 fun test01_saveBluePrint() {
121 val body = MultipartBodyBuilder().apply {
122 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
123 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 {
191 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
192 override fun getFilename(): String {
198 val publishBP = webTestClient
200 .uri("/api/v1/blueprint-model/publish")
201 .body(BodyInserters.fromMultipartData(body))
204 .returnResult<BlueprintModelSearch>()
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")
217 @Throws(JSONException::class)
218 fun test08_searchBlueprintModels() {
220 HttpMethod.GET, null,
221 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
227 @Throws(JSONException::class)
228 fun test09_downloadBlueprintByNameAndVersion() {
230 HttpMethod.GET, null,
231 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
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)))
242 // .expectStatus().is2xxSuccessful
244 webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
246 "Authorization", "Basic " + Base64Utils
247 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
250 .expectStatus().is2xxSuccessful
253 @Throws(JSONException::class)
254 private fun webTestClient(
255 requestMethod: HttpMethod,
256 body: BodyInserters.MultipartInserter?,
258 expectedResponceStatus: HttpStatus,
262 log.info("Requesting($uri): Method(${requestMethod.name})")
264 webTestClient.method(requestMethod).uri(uri)
266 "Authorization", "Basic " + Base64Utils
267 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
271 .expectStatus().isEqualTo(expectedResponceStatus)
273 .returnResult().responseBody!!