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
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")
75 open fun spy(@RequestPart("cba") cbaFile: FilePart,
76 @RequestPart("uat", required = false) uatFile: FilePart?): String = runBlocking {
77 val tempFile = createTempFile()
78 setContextColor(COLOR_SERVICES)
80 cbaFile.transferTo(tempFile)
82 uatFile != null -> uatFile.readText()
83 else -> readZipEntryAsText(tempFile, UAT_SPECIFICATION_FILE)
85 val uat = UatDefinition.load(mapper, uatSpec)
86 val cbaBytes = tempFile.readBytes()
87 val updatedUat = uatExecutor.execute(uat, cbaBytes)
88 return@runBlocking updatedUat.dump(mapper, FIELDS_TO_EXCLUDE)
89 } catch (t: Throwable) {
90 throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, t.message, t)
97 private fun FilePart.readText(): String {
98 val tempFile = createTempFile()
100 transferTo(tempFile).block()
101 return tempFile.readText()
107 @Suppress("SameParameterValue")
108 private fun readZipEntryAsText(file: File, entryName: String): String {
109 return ZipFile(file).use { zipFile -> zipFile.readEntryAsText(entryName) }
112 private fun ZipFile.readEntryAsText(entryName: String): String {
113 val zipEntry = getEntry(entryName)
114 return getInputStream(zipEntry).readBytes().toString(Charsets.UTF_8)
118 // Fields that can be safely ignored from BPP response, and can be omitted on the UAT specification.
119 private val FIELDS_TO_EXCLUDE = listOf("timestamp")