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