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.controllerblueprints.service.controller
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.controllerblueprints.TestApplication
30 import org.onap.ccsdk.cds.controllerblueprints.core.*
31 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
32 import org.onap.ccsdk.cds.controllerblueprints.service.ControllerBluePrintCoreConfiguration
33 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
34 import org.slf4j.LoggerFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.context.annotation.ComponentScan
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.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 java.nio.file.Paths
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(classes = [TestApplication::class, ControllerBluePrintCoreConfiguration::class])
66 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.controllerblueprints"])
67 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 @EnableAutoConfiguration
69 class BlueprintModelControllerTest {
71 private val log = LoggerFactory.getLogger(BlueprintModelControllerTest::class.java)!!
74 private var bp: BlueprintModelSearch? = null
78 lateinit var webTestClient: WebTestClient
80 private var bluePrintLoadConfiguration: BluePrintPathConfiguration? = null
82 private val blueprintDir = "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
83 private var zipBlueprintFileName: String? = null
85 private var testZipFile: File? = null
90 assertNotNull(webTestClient, " Failed to create WebTestClient")
92 bluePrintLoadConfiguration = BluePrintPathConfiguration().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 = Paths.get(blueprintDir).toFile().normalize()
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() {
146 webTestClient(HttpMethod.GET, null,
147 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
148 HttpStatus.OK, false)
153 @Throws(JSONException::class)
154 fun test03_getBlueprintModel() {
155 webTestClient(HttpMethod.GET, null,
156 "/api/v1/blueprint-model/${bp!!.id}",
157 HttpStatus.OK, false)
161 @Throws(JSONException::class)
162 fun test04_getAllBlueprintModel() {
163 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
167 @Throws(JSONException::class)
168 fun test05_downloadBluePrint() {
169 webTestClient(HttpMethod.GET, null,
170 "/api/v1/blueprint-model/download/${bp!!.id}",
171 HttpStatus.OK, false)
175 fun test06_enrichBlueprintModel() {
179 fun test07_publishBlueprintModel() {
183 @Throws(JSONException::class)
184 fun test08_searchBlueprintModels() {
185 webTestClient(HttpMethod.GET, null,
186 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
187 HttpStatus.OK, false)
191 @Throws(JSONException::class)
192 fun test09_downloadBlueprintByNameAndVersion() {
193 webTestClient(HttpMethod.GET, null,
194 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
195 HttpStatus.OK, false)
199 fun test10_deleteBluePrint() {
200 webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
201 .header("Authorization", "Basic " + Base64Utils
202 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
204 .expectStatus().is2xxSuccessful
207 @Throws(JSONException::class)
208 private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
209 expectedResponceStatus: HttpStatus, setParam: Boolean) {
211 log.info("Requesting($uri): Method(${requestMethod.name})")
213 webTestClient.method(requestMethod).uri(uri)
214 .header("Authorization", "Basic " + Base64Utils
215 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
218 .expectStatus().isEqualTo(expectedResponceStatus)
220 .returnResult().responseBody!!