2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.COLOR_SERVICES
25 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.resetContextColor
26 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.setContextColor
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.UAT_SPECIFICATION_FILE
28 import org.springframework.context.annotation.Profile
29 import org.springframework.http.HttpStatus
30 import org.springframework.http.MediaType
31 import org.springframework.http.codec.multipart.FilePart
32 import org.springframework.security.access.prepost.PreAuthorize
33 import org.springframework.web.bind.annotation.PostMapping
34 import org.springframework.web.bind.annotation.RequestMapping
35 import org.springframework.web.bind.annotation.RequestPart
36 import org.springframework.web.bind.annotation.RestController
37 import org.springframework.web.server.ResponseStatusException
39 import java.util.zip.ZipFile
42 * Supporting services to help creating UAT specifications.
44 * @author Eliezio Oliveira
47 @RequestMapping("/api/v1/uat")
49 open class UatServices(private val uatExecutor: UatExecutor, private val mapper: ObjectMapper) {
51 @PostMapping("/verify", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
52 @PreAuthorize("hasRole('USER')")
53 @Suppress("BlockingMethodInNonBlockingContext")
54 open fun verify(@RequestPart("cba") cbaFile: FilePart) = runBlocking {
55 setContextColor(COLOR_SERVICES)
56 val tempFile = createTempFile()
58 cbaFile.transferTo(tempFile)
59 val uatSpec = readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
60 val cbaBytes = tempFile.readBytes()
61 uatExecutor.execute(uatSpec, cbaBytes)
62 } catch (e: AssertionError) {
63 throw ResponseStatusException(HttpStatus.BAD_REQUEST, e.message)
64 } catch (t: Throwable) {
65 throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
72 @PostMapping("/spy", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE], produces = ["text/vnd.yaml"])
73 @PreAuthorize("hasRole('USER')")
74 @Suppress("BlockingMethodInNonBlockingContext")
76 @RequestPart("cba") cbaFile: FilePart,
77 @RequestPart("uat", required = false) uatFile: FilePart?
78 ): String = runBlocking {
79 val tempFile = createTempFile()
80 setContextColor(COLOR_SERVICES)
82 cbaFile.transferTo(tempFile)
84 uatFile != null -> uatFile.readText()
85 else -> readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
87 val uat = UatDefinition.load(mapper, uatSpec)
88 val cbaBytes = tempFile.readBytes()
89 val updatedUat = uatExecutor.execute(uat, cbaBytes)
90 return@runBlocking updatedUat.dump(mapper,
93 } catch (t: Throwable) {
94 throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
101 private fun FilePart.readText(): String {
102 val tempFile = createTempFile()
104 transferTo(tempFile).block()
105 return tempFile.readText()
111 @Suppress("SameParameterValue")
112 private fun readZipEntryAsText(file: File, entryName: String): String {
113 return ZipFile(file).use { zipFile -> zipFile.readEntryAsText(entryName) }
116 private fun ZipFile.readEntryAsText(entryName: String): String {
117 val zipEntry = getEntry(entryName)
118 return getInputStream(zipEntry).readBytes().toString(Charsets.UTF_8)
122 // Fields that can be safely ignored from BPP response, and can be omitted on the UAT specification.
123 private val FIELDS_TO_EXCLUDE = listOf("timestamp")