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.annotation.JsonAlias
23 import com.fasterxml.jackson.annotation.JsonInclude
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import com.fasterxml.jackson.databind.annotation.JsonDeserialize
27 import com.fasterxml.jackson.module.kotlin.convertValue
28 import org.yaml.snakeyaml.DumperOptions
29 import org.yaml.snakeyaml.Yaml
30 import org.yaml.snakeyaml.nodes.Tag
32 @JsonInclude(JsonInclude.Include.NON_EMPTY)
33 data class ProcessDefinition(
35 val request: JsonNode,
36 val expectedResponse: JsonNode? = null,
37 val responseNormalizerSpec: JsonNode? = null
40 @JsonInclude(JsonInclude.Include.NON_EMPTY)
41 data class RequestDefinition(
43 @JsonDeserialize(using = PathDeserializer::class)
45 val headers: Map<String, String> = emptyMap(),
46 val body: JsonNode? = null
49 @JsonInclude(JsonInclude.Include.NON_EMPTY)
50 data class ResponseDefinition(val status: Int = 200, val body: JsonNode? = null) {
53 val DEFAULT_RESPONSE = ResponseDefinition()
57 @JsonInclude(JsonInclude.Include.NON_EMPTY)
58 data class ExpectationDefinition(
59 val request: RequestDefinition,
60 val response: ResponseDefinition = ResponseDefinition.DEFAULT_RESPONSE
63 @JsonInclude(JsonInclude.Include.NON_EMPTY)
64 data class ServiceDefinition(val selector: String, val expectations: List<ExpectationDefinition>)
66 @JsonInclude(JsonInclude.Include.NON_EMPTY)
67 data class UatDefinition(
68 val processes: List<ProcessDefinition>,
69 @JsonAlias("external-services")
70 val externalServices: List<ServiceDefinition> = emptyList()
73 fun dump(mapper: ObjectMapper, excludedProperties: List<String> = emptyList()): String {
74 val uatAsMap: Map<String, Any> = mapper.convertValue(this)
75 if (excludedProperties.isNotEmpty()) {
76 pruneTree(uatAsMap, excludedProperties)
78 return Yaml().dumpAs(uatAsMap, Tag.MAP, DumperOptions.FlowStyle.BLOCK)
81 fun toBare(): UatDefinition {
82 val newProcesses = processes.map { p ->
83 ProcessDefinition(p.name, p.request, null, p.responseNormalizerSpec)
85 return UatDefinition(newProcesses)
88 private fun pruneTree(node: Any?, excludedProperties: List<String>) {
90 is MutableMap<*, *> -> {
91 excludedProperties.forEach { key -> node.remove(key) }
92 node.forEach { (_, value) -> pruneTree(value, excludedProperties) }
94 is List<*> -> node.forEach { value -> pruneTree(value, excludedProperties) }
99 fun load(mapper: ObjectMapper, spec: String): UatDefinition =
100 mapper.convertValue(Yaml().load(spec), UatDefinition::class.java)