2 * Copyright © 2017-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * 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.
17 package org.openecomp.sdc.heat.services.tree;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.util.regex.Pattern;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
30 public class ToscaTreeManagerTest {
32 private static final String BASE_DIR = "/mock/toscaTree/";
33 private static final String IN = "in";
34 private static final String OUT = "out";
35 private static final String EXPECTED_TREE_FILE = "/expectedTree.json";
36 private ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
39 public void testTreeWithDiffFileNames() throws IOException {
40 String inputDirectory = BASE_DIR + "diffFileNames/" + IN;
41 String outputFileName = BASE_DIR + "diffFileNames/" + OUT + EXPECTED_TREE_FILE;
43 testTreeManager(inputDirectory, outputFileName);
47 public void testDirectoriesWithSimilarNameUnderDifferentRoots() throws IOException {
48 String inputDirectory = BASE_DIR + "similarDirectoryName/" + IN;
49 String outputFileName = BASE_DIR + "similarDirectoryName/" + OUT + EXPECTED_TREE_FILE;
51 testTreeManager(inputDirectory, outputFileName);
55 public void testTwoFilesUnderSameDirectory() throws IOException {
56 String inputDirectory = BASE_DIR + "twoFilesUnderSameDirectory/" + IN;
57 String outputFileName = BASE_DIR + "twoFilesUnderSameDirectory/" + OUT + EXPECTED_TREE_FILE;
59 testTreeManager(inputDirectory, outputFileName);
62 private void testTreeManager(String inputDirectory, String outputFileName) throws IOException {
63 initTreeManager(inputDirectory);
64 toscaTreeManager.createTree();
65 HeatStructureTree tree = toscaTreeManager.getTree();
67 validateToscaTree(outputFileName, tree);
70 private void validateToscaTree(String outputFileName, HeatStructureTree tree) throws IOException {
71 File expectedTreeFile = new File(this.getClass().getResource(outputFileName).getFile());
74 try (FileInputStream fis = new FileInputStream(expectedTreeFile)) {
75 expectedTree = new String(FileUtils.toByteArray(fis));
77 Assert.assertNotNull(expectedTree);
78 expectedTree = expectedTree.trim().replace("\r", "");
79 String actualTree = JsonUtil.object2Json(tree);
80 Assert.assertEquals(expectedTree, actualTree);
83 private void initTreeManager(String inputDir) {
84 String fileName = inputDir.replace("/", File.separator);
85 File directory = new File(this.getClass().getResource(inputDir).getFile());
87 addFilesToTreeManager(fileName, directory.listFiles());
90 private void addFilesToTreeManager(String baseDir, File[] listFiles) {
91 for (File file : listFiles) {
92 if (file.isDirectory()) {
93 addFilesToTreeManager(baseDir, file.listFiles());
95 toscaTreeManager.addFile(getFileNameWithoutTestDirectory(baseDir, file.getPath()), new byte[2]);
100 private String getFileNameWithoutTestDirectory(String baseDir, String fileName) {
101 return fileName.split(Pattern.quote(baseDir) + Pattern.quote(File.separator))[1];