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.BluePrintProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
32 import org.onap.ccsdk.cds.controllerblueprints.core.*
33 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
34 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.core.io.ByteArrayResource
39 import org.springframework.http.HttpMethod
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.client.MultipartBodyBuilder
42 import org.springframework.test.context.ContextConfiguration
43 import org.springframework.test.context.TestPropertySource
44 import org.springframework.test.context.junit4.SpringRunner
45 import org.springframework.test.web.reactive.server.WebTestClient
46 import org.springframework.test.web.reactive.server.returnResult
47 import org.springframework.util.Base64Utils
48 import org.springframework.web.reactive.function.BodyInserters
50 import java.nio.charset.StandardCharsets.UTF_8
51 import kotlin.test.assertEquals
52 import kotlin.test.assertNotNull
53 import kotlin.test.assertTrue
56 * BlueprintModelControllerTest Purpose: Integration test at API level
62 @RunWith(SpringRunner::class)
63 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
64 @ContextConfiguration(classes = [DesignerApiTestConfiguration::class,
65 BlueprintPropertyConfiguration::class, BluePrintProperties::class, BluePrintDBLibConfiguration::class])
66 @TestPropertySource(locations = ["classpath:application-test.properties"])
67 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 class BlueprintModelControllerTest {
70 private val log = LoggerFactory.getLogger(BlueprintModelControllerTest::class.java)!!
73 private var bp: BlueprintModelSearch? = null
77 lateinit var webTestClient: WebTestClient
79 private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
81 private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
82 private var zipBlueprintFileName: String? = null
84 private var testZipFile: File? = null
89 assertNotNull(webTestClient, " Failed to create WebTestClient")
91 bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
92 blueprintArchivePath = "./target/blueprints/archive"
93 blueprintWorkingPath = "./target/blueprints/work"
94 blueprintDeployPath = "./target/blueprints/deploy"
96 zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
98 val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
99 assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
101 val blueprintFile = normalizedFile(blueprintDir)
102 testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
103 assertNotNull(testZipFile, "test zip is null")
104 assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
109 deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
110 deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
114 fun test01_saveBluePrint() {
116 val body = MultipartBodyBuilder().apply {
117 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
118 override fun getFilename(): String {
124 val saveBP = webTestClient
126 .uri("/api/v1/blueprint-model")
127 .body(BodyInserters.fromMultipartData(body))
130 .returnResult<BlueprintModelSearch>()
134 assertNotNull(saveBP, "failed to get response")
135 assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
136 assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
137 assertEquals("N", saveBP.published, "mismatch publish")
143 @Throws(JSONException::class)
144 fun test02_getBluePrintByNameAndVersion() {
145 webTestClient(HttpMethod.GET, null,
146 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
147 HttpStatus.OK, false)
152 @Throws(JSONException::class)
153 fun test03_getBlueprintModel() {
154 webTestClient(HttpMethod.GET, null,
155 "/api/v1/blueprint-model/${bp!!.id}",
156 HttpStatus.OK, false)
160 @Throws(JSONException::class)
161 fun test04_getAllBlueprintModel() {
162 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
166 @Throws(JSONException::class)
167 fun test05_downloadBluePrint() {
168 webTestClient(HttpMethod.GET, null,
169 "/api/v1/blueprint-model/download/${bp!!.id}",
170 HttpStatus.OK, false)
174 fun test06_enrichBlueprintModel() {
178 fun test07_publishBlueprintModel() {
182 @Throws(JSONException::class)
183 fun test08_searchBlueprintModels() {
184 webTestClient(HttpMethod.GET, null,
185 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
186 HttpStatus.OK, false)
190 @Throws(JSONException::class)
191 fun test09_downloadBlueprintByNameAndVersion() {
192 webTestClient(HttpMethod.GET, null,
193 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
194 HttpStatus.OK, false)
198 fun test10_deleteBluePrint() {
199 webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
200 .header("Authorization", "Basic " + Base64Utils
201 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
203 .expectStatus().is2xxSuccessful
206 @Throws(JSONException::class)
207 private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
208 expectedResponceStatus: HttpStatus, setParam: Boolean) {
210 log.info("Requesting($uri): Method(${requestMethod.name})")
212 webTestClient.method(requestMethod).uri(uri)
213 .header("Authorization", "Basic " + Base64Utils
214 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
217 .expectStatus().isEqualTo(expectedResponceStatus)
219 .returnResult().responseBody!!