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