2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.utils
22 import com.google.common.collect.ImmutableMap
23 import org.onap.cps.spi.model.DataNode
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import spock.lang.Specification
27 import static java.util.Arrays.asList
29 class DataMapUtilsSpec extends Specification {
31 DataNode dataNode = buildDataNode(
33 ImmutableMap.<String, Object> of("a", "b", "c", asList("d", "e")),
36 "/parent/child-list[@name='x']",
37 ImmutableMap.<String, Object> of("name", "x"),
38 Collections.emptyList()),
40 "/parent/child-list[@name='y']",
41 ImmutableMap.<String, Object> of("name", "y"),
42 Collections.emptyList()),
44 "/parent/child-object",
45 ImmutableMap.<String, Object> of("m", "n"),
48 "/parent/child-object/grand-child",
49 ImmutableMap.<String, Object> of("o", "p"),
50 Collections.emptyList()
56 static DataNode buildDataNode(String xpath, Map<String, Object> leaves, List<DataNode> children) {
57 return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
60 def 'Data node structure conversion to map.'() {
61 when: 'data node structure converted to map'
62 def result = DataMapUtils.toDataMap(dataNode)
63 then: 'root node leaves are top level elements'
64 assert result["a"] == "b"
65 assert ((Collection) result["c"]).containsAll("d", "e")
66 and: 'leaves of child list element are listed as structures under common identifier'
67 assert ((Collection) result["child-list"]).size() == 2
68 assert ((Collection) result["child-list"]).containsAll(["name": "x"], ["name": "y"])
69 and: 'leaves for child and grand-child elements are populated under their node identifiers'
70 assert result["child-object"]["m"] == "n"
71 assert result["child-object"]["grand-child"]["o"] == "p"