Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / test / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / controller / BlueprintModelControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.service.controller
19
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
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.MethodSorters
29 import org.onap.ccsdk.apps.controllerblueprints.TestApplication
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
31 import org.onap.ccsdk.apps.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
45 import java.io.File
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
50
51 /**
52  * BlueprintModelControllerTest Purpose: Integration test at API level
53  *
54  * @author Vinal Patel
55  * @version 1.0
56  */
57
58 @RunWith(SpringRunner::class)
59 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ContextConfiguration(classes = [TestApplication::class])
61 @ComponentScan(basePackages = ["org.onap.ccsdk.apps.controllerblueprints"])
62 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
63 @EnableAutoConfiguration
64 class BlueprintModelControllerTest {
65
66     companion object {
67
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
73     }
74
75     @Value("\${controllerblueprints.loadBluePrintPaths}")
76     private val loadBluePrintPaths: String? = null
77
78     @Autowired
79     private val webTestClient: WebTestClient? = null
80
81     @Value("\${controllerblueprints.loadBlueprintsExamplesPath}")
82     private val blueprintArchivePath: String? = null
83
84     private val filename = "test.zip"
85     private var blueprintFile: File? = null
86     private var zipBlueprintFile: File? = null
87
88     @Before
89     @Throws(Exception::class)
90     fun setUp() {
91         blueprintFile = File(loadBluePrintPaths+"/baseconfiguration")
92         if (blueprintFile!!.isDirectory) {
93             zipBlueprintFile = File(Paths.get(blueprintArchivePath).resolve(filename).toString())
94             BluePrintArchiveUtils.compress(blueprintFile!!, zipBlueprintFile!!, true)
95         }
96     }
97
98     @After
99     @Throws(Exception::class)
100     fun tearDown() {
101         zipBlueprintFile!!.delete()
102     }
103
104     @Test
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? {
110                         return "test.zip"
111                     }
112                 }),
113                 "/api/v1/blueprint-model",
114                 HttpStatus.OK, true)
115     }
116
117     @Test
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)
121     }
122
123
124     @Test
125     @Throws(JSONException::class)
126     fun test3_getBlueprintModel() {
127         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/$id", HttpStatus.OK, false)
128     }
129
130     @Test
131     @Throws(JSONException::class)
132     fun test4_getAllBlueprintModel() {
133         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
134     }
135
136     @Test
137     @Throws(JSONException::class)
138     fun test5_downloadBluePrint() {
139         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/download/$id", HttpStatus.OK, false)
140     }
141
142     @Test
143     fun test6_publishBlueprintModel() {
144     }
145
146     @Test
147     @Throws(JSONException::class)
148     fun test7_searchBlueprintModels() {
149         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model/search/$name", HttpStatus.OK, false)
150     }
151
152     @Test
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)
156     }
157
158     @Test
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)))
165                 .exchange()
166                 .expectStatus().is2xxSuccessful
167     }
168
169     @Throws(JSONException::class)
170     private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String, expectedResponceStatus: HttpStatus, setParam: Boolean) {
171
172         result = String(webTestClient!!.method(requestMethod).uri(uri)
173                 .header("Authorization", "Basic " + Base64Utils
174                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
175                 .body(body)
176                 .exchange()
177                 .expectStatus().isEqualTo(expectedResponceStatus)
178                 .expectBody()
179                 .returnResult().responseBody!!)
180
181         if (setParam) {
182             val jsonResponse = JSONObject(result)
183             val blueprintModelSearchJSON = jsonResponse.getJSONObject("blueprintModel")
184             val gson = Gson()
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
190         }
191     }
192
193 }