Data fragment update by xpath - parsing and validation
[cps.git] / cps-service / src / test / java / org / onap / cps / TestUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps;
22
23 import com.google.common.collect.ImmutableMap;
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.util.Map;
28 import org.onap.cps.spi.model.DataNode;
29
30 /**
31  * Common convenience methods for testing.
32  */
33 public class TestUtils {
34
35     /**
36      * Convert a file in the test resource folder to file.
37      *
38      * @param filename to name of the file in test/resources
39      * @return the file
40      * @throws IOException when there is an IO issue
41      */
42     public static File readFile(final String filename) {
43         return new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile());
44     }
45
46     /**
47      * Convert a file in the test resource folder to a string.
48      *
49      * @param filename to name of the file in test/resources
50      * @return the content of the file as a String
51      * @throws IOException when there is an IO issue
52      */
53     public static String getResourceFileContent(final String filename) throws IOException {
54         final File file = readFile(filename);
55         return new String(Files.readAllBytes(file.toPath()));
56     }
57
58     /**
59      * Reads yang resources into map.
60      *
61      * @param resources list of file paths
62      * @return yang resource map where key is filename and value is file content
63      * @throws IOException when there an I/O issue
64      */
65     public static Map<String, String> getYangResourcesAsMap(final String... resources) throws IOException {
66         final ImmutableMap.Builder<String, String> yangResourceNameToContentBuilder = new ImmutableMap.Builder<>();
67         for (final String resourcePath : resources) {
68             final File file = readFile(resourcePath);
69             final String content = new String(Files.readAllBytes(file.toPath()));
70             yangResourceNameToContentBuilder.put(file.getName(), content);
71         }
72         return yangResourceNameToContentBuilder.build();
73     }
74
75     /**
76      * Represents given data node object as flatten map by xpath.
77      * For easy finding child node within hierarchy.
78      *
79      * @param dataNode data node representing a root of tree structure
80      * @return the map containing all the data nodes from given structure where key is xpath, value is datanode object
81      */
82     public static Map<String, DataNode> getFlattenMapByXpath(final DataNode dataNode) {
83         final ImmutableMap.Builder<String, DataNode> dataNodeMapBuilder = ImmutableMap.builder();
84         buildFlattenMapByXpath(dataNode, dataNodeMapBuilder);
85         return dataNodeMapBuilder.build();
86     }
87
88     private static void buildFlattenMapByXpath(final DataNode dataNode,
89         final ImmutableMap.Builder<String, DataNode> dataNodeMapBuilder) {
90         dataNodeMapBuilder.put(dataNode.getXpath(), dataNode);
91         dataNode.getChildDataNodes()
92             .forEach(childDataNode -> buildFlattenMapByXpath(childDataNode, dataNodeMapBuilder));
93     }
94 }