cd49870ae5752a43fc61d409e06665f9ce9d4338
[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.controllerblueprints.core.*
33 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
34 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
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     }
180
181     @Test
182     @Throws(JSONException::class)
183     fun test08_searchBlueprintModels() {
184         webTestClient(HttpMethod.GET, null,
185                 "/api/v1/blueprint-model/search/${bp!!.artifactName}",
186                 HttpStatus.OK, false)
187     }
188
189     @Test
190     @Throws(JSONException::class)
191     fun test09_downloadBlueprintByNameAndVersion() {
192         webTestClient(HttpMethod.GET, null,
193                 "/api/v1/blueprint-model/download/by-name/${bp!!.artifactName}/version/${bp!!.artifactVersion}",
194                 HttpStatus.OK, false)
195     }
196
197     @Test
198     fun test10_deleteBluePrint() {
199         webTestClient.delete().uri("/api/v1/blueprint-model/${bp!!.id}")
200                 .header("Authorization", "Basic " + Base64Utils
201                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
202                 .exchange()
203                 .expectStatus().is2xxSuccessful
204     }
205
206     @Throws(JSONException::class)
207     private fun webTestClient(requestMethod: HttpMethod, body: BodyInserters.MultipartInserter?, uri: String,
208                               expectedResponceStatus: HttpStatus, setParam: Boolean) {
209
210         log.info("Requesting($uri): Method(${requestMethod.name})")
211
212         webTestClient.method(requestMethod).uri(uri)
213                 .header("Authorization", "Basic " + Base64Utils
214                         .encodeToString(("ccsdkapps" + ":" + "ccsdkapps").toByteArray(UTF_8)))
215                 .body(body)
216                 .exchange()
217                 .expectStatus().isEqualTo(expectedResponceStatus)
218                 .expectBody()
219                 .returnResult().responseBody!!
220
221     }
222
223 }