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 com.google.gson.Gson
21 import org.json.JSONException
22 import org.json.JSONObject
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.utils.BluePrintArchiveUtils
31 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.beans.factory.annotation.Value
34 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.context.annotation.ComponentScan
37 import org.springframework.core.io.ByteArrayResource
38 import org.springframework.http.HttpMethod
39 import org.springframework.http.HttpStatus
40 import org.springframework.test.context.ContextConfiguration
41 import org.springframework.test.context.junit4.SpringRunner
42 import org.springframework.test.web.reactive.server.WebTestClient
43 import org.springframework.util.Base64Utils
44 import org.springframework.web.reactive.function.BodyInserters
46 import java.io.IOException
47 import java.nio.charset.StandardCharsets.UTF_8
48 import java.nio.file.Files
49 import java.nio.file.Paths
52 * BlueprintModelControllerTest Purpose: Integration test at API level
58 @RunWith(SpringRunner::class)
59 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ContextConfiguration(classes = [TestApplication::class])
61 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.controllerblueprints"])
62 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
63 @EnableAutoConfiguration
64 class BlueprintModelControllerTest {
68 private var id: String? = null
69 private var name: String? = null
70 private var version: String? = null
71 private var tag: String? = null
72 private var result: String? = null
75 @Value("\${controllerblueprints.loadBluePrintPaths}")
76 private val loadBluePrintPaths: String? = null
79 private val webTestClient: WebTestClient? = null
81 @Value("\${controllerblueprints.loadBlueprintsExamplesPath}")
82 private val blueprintArchivePath: String? = null
84 private val filename = "test.zip"
85 private var blueprintFile: File? = null
86 private var zipBlueprintFile: File? = null
89 @Throws(Exception::class)
91 blueprintFile = File(loadBluePrintPaths+"/baseconfiguration")
92 if (blueprintFile!!.isDirectory) {
93 zipBlueprintFile = File(Paths.get(blueprintArchivePath).resolve(filename).toString())
94 BluePrintArchiveUtils.compress(blueprintFile!!, zipBlueprintFile!!, true)
99 @Throws(Exception::class)
101 zipBlueprintFile!!.delete()
105 @Throws(IOException::class, JSONException::class)
106 fun test1_saveBluePrint() {
107 webTestClient(HttpMethod.POST,
108 BodyInserters.fromMultipartData("file", object : ByteArrayResource(Files.readAllBytes(zipBlueprintFile!!.toPath())) {
109 override fun getFilename(): String? {
113 "/api/v1/blueprint-model",
118 @Throws(JSONException::class)
119 fun test2_getBluePrintByNameAndVersion() {
120 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/by-name/$name/version/$version", HttpStatus.OK, false)
125 @Throws(JSONException::class)
126 fun test3_getBlueprintModel() {
127 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/$id", HttpStatus.OK, false)
131 @Throws(JSONException::class)
132 fun test4_getAllBlueprintModel() {
133 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
137 @Throws(JSONException::class)
138 fun test5_downloadBluePrint() {
139 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/download/$id", HttpStatus.OK, false)
143 fun test6_publishBlueprintModel() {
147 @Throws(JSONException::class)
148 fun test7_searchBlueprintModels() {
149 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/search/$name", HttpStatus.OK, false)
153 @Throws(JSONException::class)
154 fun test8_downloadBlueprintByNameAndVersion() {
155 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/download/by-name/$name/version/$version", HttpStatus.OK, false)
159 fun test9_deleteBluePrint() {
160 //TODO: Use webTestClient function
161 //webTestClient(HttpMethod.DELETE, null, "/api/v1/blueprint-model/" + id, HttpStatus.OK, false);
162 webTestClient!!.delete().uri("/api/v1/blueprint-model/$id")
163 .header("Authorization", "Basic " + Base64Utils
164 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
166 .expectStatus().is2xxSuccessful
169 @Throws(JSONException::class)
170 private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String, expectedResponceStatus: HttpStatus, setParam: Boolean) {
172 result = String(webTestClient!!.method(requestMethod).uri(uri)
173 .header("Authorization", "Basic " + Base64Utils
174 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
177 .expectStatus().isEqualTo(expectedResponceStatus)
179 .returnResult().responseBody!!)
182 val jsonResponse = JSONObject(result)
183 val blueprintModelSearchJSON = jsonResponse.getJSONObject("blueprintModel")
185 val blueprintModelSearch = gson.fromJson(blueprintModelSearchJSON.toString(), BlueprintModelSearch::class.java)
186 id = blueprintModelSearch.id
187 name = blueprintModelSearch.artifactName
188 version = blueprintModelSearch.artifactVersion
189 tag = blueprintModelSearch.tags