2 * Copyright © 2019 Bell Canada Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.service.controller
19 import com.google.gson.Gson
20 import org.json.JSONException
21 import org.json.JSONObject
22 import org.junit.After
23 import org.junit.Before
24 import org.junit.FixMethodOrder
26 import org.junit.runner.RunWith
27 import org.junit.runners.MethodSorters
28 import org.onap.ccsdk.apps.controllerblueprints.TestApplication
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
30 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.beans.factory.annotation.Value
33 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
34 import org.springframework.boot.test.context.SpringBootTest
35 import org.springframework.context.annotation.ComponentScan
36 import org.springframework.core.io.ByteArrayResource
37 import org.springframework.http.HttpMethod
38 import org.springframework.http.HttpStatus
39 import org.springframework.test.context.ContextConfiguration
40 import org.springframework.test.context.junit4.SpringRunner
41 import org.springframework.test.web.reactive.server.WebTestClient
42 import org.springframework.util.Base64Utils
43 import org.springframework.web.reactive.function.BodyInserters
45 import java.io.IOException
46 import java.nio.charset.StandardCharsets.UTF_8
47 import java.nio.file.Files
48 import java.nio.file.Paths
51 * BlueprintModelRestTest Purpose: Integration test at API level
57 @RunWith(SpringRunner::class)
58 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 @ContextConfiguration(classes = [TestApplication::class])
60 @ComponentScan(basePackages = ["org.onap.ccsdk.apps.controllerblueprints"])
61 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 @EnableAutoConfiguration
63 class BlueprintModelRestTest {
67 private var id: String? = null
68 private var name: String? = null
69 private var version: String? = null
70 private var tag: String? = null
71 private var result: String? = null
74 @Value("\${controllerblueprints.loadBluePrintPaths}")
75 private val loadBluePrintPaths: String? = null
78 private val webTestClient: WebTestClient? = null
80 @Value("\${controllerblueprints.loadBlueprintsExamplesPath}")
81 private val blueprintArchivePath: String? = null
83 private val filename = "test.zip"
84 private var blueprintFile: File? = null
85 private var zipBlueprintFile: File? = null
88 @Throws(Exception::class)
90 blueprintFile = File(loadBluePrintPaths+"/baseconfiguration")
91 if (blueprintFile!!.isDirectory) {
92 zipBlueprintFile = File(Paths.get(blueprintArchivePath).resolve(filename).toString())
93 BluePrintArchiveUtils.compress(blueprintFile!!, zipBlueprintFile!!, true)
98 @Throws(Exception::class)
100 zipBlueprintFile!!.delete()
104 @Throws(IOException::class, JSONException::class)
105 fun test1_saveBluePrint() {
106 webTestClient(HttpMethod.POST,
107 BodyInserters.fromMultipartData("file", object : ByteArrayResource(Files.readAllBytes(zipBlueprintFile!!.toPath())) {
108 override fun getFilename(): String? {
112 "/api/v1/blueprint-model",
117 @Throws(JSONException::class)
118 fun test2_getBluePrintByNameAndVersion() {
119 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/by-name/$name/version/$version", HttpStatus.OK, false)
124 @Throws(JSONException::class)
125 fun test3_getBlueprintModel() {
126 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/$id", HttpStatus.OK, false)
130 @Throws(JSONException::class)
131 fun test4_getAllBlueprintModel() {
132 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
136 @Throws(JSONException::class)
137 fun test5_downloadBluePrint() {
138 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/download/$id", HttpStatus.OK, false)
142 fun test6_publishBlueprintModel() {
146 @Throws(JSONException::class)
147 fun test7_searchBlueprintModels() {
148 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/search/$name", HttpStatus.OK, false)
152 @Throws(JSONException::class)
153 fun test8_downloadBlueprintByNameAndVersion() {
154 webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/download/by-name/$name/version/$version", HttpStatus.OK, false)
158 fun test9_deleteBluePrint() {
159 //TODO: Use webTestClient function
160 //webTestClient(HttpMethod.DELETE, null, "/api/v1/blueprint-model/" + id, HttpStatus.OK, false);
161 webTestClient!!.delete().uri("/api/v1/blueprint-model/$id")
162 .header("Authorization", "Basic " + Base64Utils
163 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
165 .expectStatus().is2xxSuccessful
168 @Throws(JSONException::class)
169 private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String, expectedResponceStatus: HttpStatus, setParam: Boolean) {
171 result = String(webTestClient!!.method(requestMethod).uri(uri)
172 .header("Authorization", "Basic " + Base64Utils
173 .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
176 .expectStatus().isEqualTo(expectedResponceStatus)
178 .returnResult().responseBody!!)
181 val jsonResponse = JSONObject(result)
182 val blueprintModelSearchJSON = jsonResponse.getJSONObject("blueprintModel")
184 val blueprintModelSearch = gson.fromJson(blueprintModelSearchJSON.toString(), BlueprintModelSearch::class.java)
185 id = blueprintModelSearch.id
186 name = blueprintModelSearch.artifactName
187 version = blueprintModelSearch.artifactVersion
188 tag = blueprintModelSearch.tags