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.*
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
49 import java.nio.charset.StandardCharsets.UTF_8
50 import kotlin.test.assertEquals
51 import kotlin.test.assertNotNull
52 import kotlin.test.assertTrue
55 * BlueprintModelControllerTest Purpose: Integration test at API level
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 {
69 private val log = logger(BlueprintModelControllerTest::class)
72 private var bp: BlueprintModelSearch? = null
76 lateinit var webTestClient: WebTestClient
78 private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
80 private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
81 private var zipBlueprintFileName: String? = null
83 private var testZipFile: File? = null
88 assertNotNull(webTestClient, " Failed to create WebTestClient")
90 bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
91 blueprintArchivePath = "./target/blueprints/archive"
92 blueprintWorkingPath = "./target/blueprints/work"
93 blueprintDeployPath = "./target/blueprints/deploy"
95 zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
97 val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
98 assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
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}")
108 deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
109 deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
113 fun test01_saveBluePrint() {
115 val body = MultipartBodyBuilder().apply {
116 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
117 override fun getFilename(): String {
123 val saveBP = webTestClient
125 .uri("/api/v1/blueprint-model")
126 .body(BodyInserters.fromMultipartData(body))
129 .returnResult<BlueprintModelSearch>()
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")
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)
151 @Throws(JSONException::class)
152 fun test03_getBlueprintModel() {
153 webTestClient(HttpMethod.GET, null,
154 "/api/v1/blueprint-model/${bp!!.id}",
155 HttpStatus.OK, false)
159 @Throws(JSONException::class)
160 fun test04_getAllBlueprintModel() {
161 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
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)
173 fun test06_enrichBlueprintModel() {
177 fun test07_publishBlueprintModel() {
179 val body = MultipartBodyBuilder().apply {
180 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
181 override fun getFilename(): String {
187 val publishBP = webTestClient
189 .uri("/api/v1/blueprint-model/publish")
190 .body(BodyInserters.fromMultipartData(body))
193 .returnResult<BlueprintModelSearch>()
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")
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)
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)
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)))
227 // .expectStatus().is2xxSuccessful
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)))
233 .expectStatus().is2xxSuccessful
236 @Throws(JSONException::class)
237 private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
238 expectedResponceStatus: HttpStatus, setParam: Boolean) {
240 log.info("Requesting($uri): Method(${requestMethod.name})")
242 webTestClient.method(requestMethod).uri(uri)
243 .header("Authorization", "Basic " + Base64Utils
244 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
247 .expectStatus().isEqualTo(expectedResponceStatus)
249 .returnResult().responseBody!!