3046f1041eb4d5374186b8bb690c07346e28bb16
[ccsdk/cds.git] /
1 /*-
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
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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.ccsdk.cds.blueprintsprocessor.uat
21
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
31
32 @JsonInclude(JsonInclude.Include.NON_EMPTY)
33 data class ProcessDefinition(val name: String, val request: JsonNode, val expectedResponse: JsonNode? = null,
34                              val responseNormalizerSpec: JsonNode? = null)
35
36 @JsonInclude(JsonInclude.Include.NON_EMPTY)
37 data class RequestDefinition(val method: String,
38                              @JsonDeserialize(using = PathDeserializer::class)
39                              val path: String,
40                              val headers: Map<String, String> = emptyMap(),
41                              val body: JsonNode? = null)
42
43 @JsonInclude(JsonInclude.Include.NON_EMPTY)
44 data class ResponseDefinition(val status: Int = 200, val body: JsonNode? = null) {
45     companion object {
46         val DEFAULT_RESPONSE = ResponseDefinition()
47     }
48 }
49
50 @JsonInclude(JsonInclude.Include.NON_EMPTY)
51 data class ExpectationDefinition(val request: RequestDefinition,
52                                  val response: ResponseDefinition = ResponseDefinition.DEFAULT_RESPONSE)
53
54 @JsonInclude(JsonInclude.Include.NON_EMPTY)
55 data class ServiceDefinition(val selector: String, val expectations: List<ExpectationDefinition>)
56
57 @JsonInclude(JsonInclude.Include.NON_EMPTY)
58 data class UatDefinition(val processes: List<ProcessDefinition>,
59                          @JsonAlias("external-services")
60                          val externalServices: List<ServiceDefinition> = emptyList()) {
61
62     fun dump(mapper: ObjectMapper, excludedProperties: List<String> = emptyList()): String {
63         val uatAsMap: Map<String, Any> = mapper.convertValue(this)
64         if (excludedProperties.isNotEmpty()) {
65             pruneTree(uatAsMap, excludedProperties)
66         }
67         return Yaml().dumpAs(uatAsMap, Tag.MAP, DumperOptions.FlowStyle.BLOCK)
68     }
69
70     fun toBare(): UatDefinition {
71         val newProcesses = processes.map { p ->
72             ProcessDefinition(p.name, p.request, null, p.responseNormalizerSpec)
73         }
74         return UatDefinition(newProcesses)
75     }
76
77     private fun pruneTree(node: Any?, excludedProperties: List<String>) {
78         when (node) {
79             is MutableMap<*, *> -> {
80                 excludedProperties.forEach { key -> node.remove(key) }
81                 node.forEach { (_, value) -> pruneTree(value, excludedProperties) }
82             }
83             is List<*> -> node.forEach { value -> pruneTree(value, excludedProperties) }
84         }
85     }
86
87     companion object {
88         fun load(mapper: ObjectMapper, spec: String): UatDefinition =
89                 mapper.convertValue(Yaml().load(spec), UatDefinition::class.java)
90
91     }
92 }