14910808708534d9370e1561637711a381ce249b
[ccsdk/cds.git] /
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.cds.blueprintsprocessor.designer.api
19
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
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.MethodSorters
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
33 import org.onap.ccsdk.cds.controllerblueprints.core.*
34 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.core.io.ByteArrayResource
39 import org.springframework.http.HttpMethod
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.client.MultipartBodyBuilder
42 import org.springframework.test.context.ContextConfiguration
43 import org.springframework.test.context.TestPropertySource
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
49 import java.io.File
50 import java.nio.charset.StandardCharsets.UTF_8
51 import kotlin.test.assertEquals
52 import kotlin.test.assertNotNull
53 import kotlin.test.assertTrue
54
55 /**
56  * BlueprintModelControllerTest Purpose: Integration test at API level
57  *
58  * @author Vinal Patel
59  * @version 1.0
60  */
61
62 @RunWith(SpringRunner::class)
63 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
64 @ContextConfiguration(classes = [DesignerApiTestConfiguration::class,
65     BlueprintPropertyConfiguration::class, BluePrintProperties::class, BluePrintDBLibConfiguration::class])
66 @TestPropertySource(locations = ["classpath:application-test.properties"])
67 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 class BlueprintModelControllerTest {
69
70     private val log = LoggerFactory.getLogger(BlueprintModelControllerTest::class.java)!!
71
72     companion object {
73         private var bp: BlueprintModelSearch? = null
74     }
75
76     @Autowired
77     lateinit var webTestClient: WebTestClient
78
79     private var bluePrintLoadConfiguration: BluePrintLoadConfiguration? = null
80
81     private val blueprintDir = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
82     private var zipBlueprintFileName: String? = null
83
84     private var testZipFile: File? = null
85
86
87     @Before
88     fun setUp() {
89         assertNotNull(webTestClient, " Failed to create WebTestClient")
90
91         bluePrintLoadConfiguration = BluePrintLoadConfiguration().apply {
92             blueprintArchivePath = "./target/blueprints/archive"
93             blueprintWorkingPath = "./target/blueprints/work"
94             blueprintDeployPath = "./target/blueprints/deploy"
95         }
96         zipBlueprintFileName = normalizedPathName(bluePrintLoadConfiguration!!.blueprintArchivePath, "test.zip")
97
98         val archiveDir = normalizedFile(bluePrintLoadConfiguration!!.blueprintArchivePath).reCreateDirs()
99         assertTrue(archiveDir.exists(), "failed to create archiveDir(${archiveDir.absolutePath}")
100
101         val blueprintFile = normalizedFile(blueprintDir)
102         testZipFile = blueprintFile.compress(zipBlueprintFileName!!)
103         assertNotNull(testZipFile, "test zip is null")
104         assertTrue(testZipFile!!.exists(), "Failed to create blueprint test zip(${testZipFile!!.absolutePath}")
105     }
106
107     @After
108     fun tearDown() {
109         deleteDir(bluePrintLoadConfiguration!!.blueprintArchivePath)
110         deleteDir(bluePrintLoadConfiguration!!.blueprintWorkingPath)
111     }
112
113     @Test
114     fun test01_saveBluePrint() {
115         bp = runBlocking {
116             val body = MultipartBodyBuilder().apply {
117                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
118                     override fun getFilename(): String {
119                         return "test.zip"
120                     }
121                 })
122             }.build()
123
124             val saveBP = webTestClient
125                     .post()
126                     .uri("/api/v1/blueprint-model")
127                     .body(BodyInserters.fromMultipartData(body))
128                     .exchange()
129                     .expectStatus().isOk
130                     .returnResult<BlueprintModelSearch>()
131                     .responseBody
132                     .awaitSingle()
133
134             assertNotNull(saveBP, "failed to get response")
135             assertEquals("baseconfiguration", saveBP.artifactName, "mismatch artifact name")
136             assertEquals("1.0.0", saveBP.artifactVersion, "mismatch artifact version")
137             assertEquals("N", saveBP.published, "mismatch publish")
138             saveBP
139         }
140     }
141
142     @Test
143     @Throws(JSONException::class)
144     fun test02_getBluePrintByNameAndVersion() {
145         webTestClient(HttpMethod.GET, null,
146                 "/api/v1/blueprint-model/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
147                 HttpStatus.OK, false)
148     }
149
150
151     @Test
152     @Throws(JSONException::class)
153     fun test03_getBlueprintModel() {
154         webTestClient(HttpMethod.GET, null,
155                 "/api/v1/blueprint-model/${bp!!.id}",
156                 HttpStatus.OK, false)
157     }
158
159     @Test
160     @Throws(JSONException::class)
161     fun test04_getAllBlueprintModel() {
162         webTestClient(HttpMethod.GET, null, "/api/v1/blueprint-model", HttpStatus.OK, false)
163     }
164
165     @Test
166     @Throws(JSONException::class)
167     fun test05_downloadBluePrint() {
168         webTestClient(HttpMethod.GET, null,
169                 "/api/v1/blueprint-model/download/${bp!!.id}",
170                 HttpStatus.OK, false)
171     }
172
173     @Test
174     fun test06_enrichBlueprintModel() {
175     }
176
177     @Test
178     fun test07_publishBlueprintModel() {
179         bp = runBlocking {
180             val body = MultipartBodyBuilder().apply {
181                 part("file", object : ByteArrayResource(testZipFile!!.readBytes()) {
182                     override fun getFilename(): String {
183                         return "test.zip"
184                     }
185                 })
186             }.build()
187
188             val publishBP = webTestClient
189                     .post()
190                     .uri("/api/v1/blueprint-model/publish")
191                     .body(BodyInserters.fromMultipartData(body))
192                     .exchange()
193                     .expectStatus().isOk
194                     .returnResult<BlueprintModelSearch>()
195                     .responseBody
196                     .awaitSingle()
197
198             assertNotNull(publishBP, "failed to get response")
199             assertEquals("baseconfiguration", publishBP.artifactName, "mismatch artifact name")
200             assertEquals("1.0.0", publishBP.artifactVersion, "mismatch artifact version")
201             assertEquals("Y", publishBP.published, "mismatch publish")
202             publishBP
203         }
204     }
205
206     @Test
207     @Throws(JSONException::class)
208     fun test08_searchBlueprintModels() {
209         webTestClient(HttpMethod.GET, null,
210                 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
211                 HttpStatus.OK, false)
212     }
213
214     @Test
215     @Throws(JSONException::class)
216     fun test09_downloadBlueprintByNameAndVersion() {
217         webTestClient(HttpMethod.GET, null,
218                 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
219                 HttpStatus.OK, false)
220     }
221
222     @Test
223     fun test10_deleteBluePrint() {
224 //        webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
225 //                .header("Authorization", "Basic " + Base64Utils
226 //                        .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
227 //                .exchange()
228 //                .expectStatus().is2xxSuccessful
229
230         webTestClient.delete().uri("/api/v1/blueprint-model/name/${bp!!.artifactName}/version/${bp!!.artifactVersion}")
231                 .header("Authorization", "Basic " + Base64Utils
232                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
233                 .exchange()
234                 .expectStatus().is2xxSuccessful
235     }
236
237     @Throws(JSONException::class)
238     private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
239                               expectedResponceStatus: HttpStatus, setParam: Boolean) {
240
241         log.info("Requesting($uri): Method(${requestMethod.name})")
242
243         webTestClient.method(requestMethod).uri(uri)
244                 .header("Authorization", "Basic " + Base64Utils
245                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
246                 .body(body)
247                 .exchange()
248                 .expectStatus().isEqualTo(expectedResponceStatus)
249                 .expectBody()
250                 .returnResult().responseBody!!
251
252     }
253
254 }