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]
68 @TestPropertySource(locations = ["classpath:application-test.properties"])
69 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
70 class BlueprintModelControllerTest {
72 private val log = logger(BlueprintModelControllerTest::class)
75 private var bp: BlueprintModelSearch? = null
79 lateinit var webTestClient: WebTestClient
81 private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
83 private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
84 private var zipBlueprintFileName: String? = null
86 private var testZipFile: File? = null
90 assertNotNull(webTestClient, " Failed to create WebTestClient")
92 bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
93 blueprintArchivePath = "./target/blueprints/archive"
94 blueprintWorkingPath = "./target/blueprints/work"
95 blueprintDeployPath = "./target/blueprints/deploy"
97 zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
99 val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
100 assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
102 val blueprintFile = normalizedFile(blueprintDir)
103 testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
104 assertNotNull(testZipFile, "test zip is null")
105 assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
110 deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
111 deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
115 fun test01_saveBluePrint() {
117 val body = MultipartBodyBuilder().apply {
118 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
119 override fun getFilename(): String {
125 val saveBP = webTestClient
127 .uri("/api/v1/blueprint-model")
128 .body(BodyInserters.fromMultipartData(body))
131 .returnResult<BlueprintModelSearch>()
135 assertNotNull(saveBP, "failed to get response")
136 assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
137 assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
138 assertEquals("N", saveBP.published, "mismatch publish")
144 @Throws(JSONException::class)
145 fun test02_getBluePrintByNameAndVersion() {
147 HttpMethod.GET, null,
148 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
154 @Throws(JSONException::class)
155 fun test03_getBlueprintModel() {
157 HttpMethod.GET, null,
158 "/api/v1/blueprint-model/${bp!!.id}",
164 @Throws(JSONException::class)
165 fun test04_getAllBlueprintModel() {
166 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
170 @Throws(JSONException::class)
171 fun test05_downloadBluePrint() {
173 HttpMethod.GET, null,
174 "/api/v1/blueprint-model/download/${bp!!.id}",
180 fun test06_enrichBlueprintModel() {
184 fun test07_publishBlueprintModel() {
186 val body = MultipartBodyBuilder().apply {
187 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
188 override fun getFilename(): String {
194 val publishBP = webTestClient
196 .uri("/api/v1/blueprint-model/publish")
197 .body(BodyInserters.fromMultipartData(body))
200 .returnResult<BlueprintModelSearch>()
204 assertNotNull(publishBP, "failed to get response")
205 assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
206 assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
207 assertEquals("Y", publishBP.published, "mismatch publish")
213 @Throws(JSONException::class)
214 fun test08_searchBlueprintModels() {
216 HttpMethod.GET, null,
217 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
223 @Throws(JSONException::class)
224 fun test09_downloadBlueprintByNameAndVersion() {
226 HttpMethod.GET, null,
227 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
233 fun test10_deleteBluePrint() {
234 // webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
235 // .header("Authorization", "Basic " + Base64Utils
236 // .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
238 // .expectStatus().is2xxSuccessful
240 webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
242 "Authorization", "Basic " + Base64Utils
243 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
246 .expectStatus().is2xxSuccessful
249 @Throws(JSONException::class)
250 private fun webTestClient(
251 requestMethod: HttpMethod,
252 body: BodyInserters.MultipartInserter?,
254 expectedResponceStatus: HttpStatus,
258 log.info("Requesting($uri): Method(${requestMethod.name})")
260 webTestClient.method(requestMethod).uri(uri)
262 "Authorization", "Basic " + Base64Utils
263 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8))
267 .expectStatus().isEqualTo(expectedResponceStatus)
269 .returnResult().responseBody!!