/*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils import com.fasterxml.jackson.annotation.JsonAlias import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.convertValue import org.yaml.snakeyaml.DumperOptions import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.nodes.Tag @JsonInclude(JsonInclude.Include.NON_EMPTY) data class ProcessDefinition( val name: String, val request: JsonNode, val expectedResponse: JsonNode? = null, val responseNormalizerSpec: JsonNode? = null ) @JsonInclude(JsonInclude.Include.NON_EMPTY) data class RequestDefinition( val method: String, @JsonDeserialize(using = PathDeserializer::class) val path: String, val headers: Map = emptyMap(), val body: JsonNode? = null ) @JsonInclude(JsonInclude.Include.NON_EMPTY) data class ResponseDefinition(val status: Int = 200, val body: JsonNode? = null) { companion object { val DEFAULT_RESPONSE = ResponseDefinition() } } @JsonInclude(JsonInclude.Include.NON_EMPTY) data class ExpectationDefinition( val request: RequestDefinition, val response: ResponseDefinition = ResponseDefinition.DEFAULT_RESPONSE ) @JsonInclude(JsonInclude.Include.NON_EMPTY) data class ServiceDefinition(val selector: String, val expectations: List) @JsonInclude(JsonInclude.Include.NON_EMPTY) data class UatDefinition( val processes: List, @JsonAlias("external-services") val externalServices: List = emptyList() ) { fun dump(mapper: ObjectMapper, excludedProperties: List = emptyList()): String { val uatAsMap: Map = mapper.convertValue(this) if (excludedProperties.isNotEmpty()) { pruneTree(uatAsMap, excludedProperties) } return Yaml().dumpAs(uatAsMap, Tag.MAP, DumperOptions.FlowStyle.BLOCK) } fun toBare(): UatDefinition { val newProcesses = processes.map { p -> ProcessDefinition(p.name, p.request, null, p.responseNormalizerSpec) } return UatDefinition(newProcesses) } private fun pruneTree(node: Any?, excludedProperties: List) { when (node) { is MutableMap<*, *> -> { excludedProperties.forEach { key -> node.remove(key) } node.forEach { (_, value) -> pruneTree(value, excludedProperties) } } is List<*> -> node.forEach { value -> pruneTree(value, excludedProperties) } } } companion object { fun load(mapper: ObjectMapper, spec: String): UatDefinition = mapper.convertValue(Yaml().load(spec), UatDefinition::class.java) } }