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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
23 import com.google.common.collect.ImmutableMap;
25 import java.io.IOException;
26 import java.nio.file.Files;
28 import org.onap.cps.spi.model.DataNode;
31 * Common convenience methods for testing.
33 public class TestUtils {
36 * Convert a file in the test resource folder to file.
38 * @param filename to name of the file in test/resources
40 * @throws IOException when there is an IO issue
42 public static File readFile(final String filename) {
43 return new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile());
47 * Convert a file in the test resource folder to a string.
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
53 public static String getResourceFileContent(final String filename) throws IOException {
54 final File file = readFile(filename);
55 return new String(Files.readAllBytes(file.toPath()));
59 * Reads yang resources into map.
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
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);
72 return yangResourceNameToContentBuilder.build();
76 * Represents given data node object as flatten map by xpath.
77 * For easy finding child node within hierarchy.
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
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();
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));