Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / test / java / org / openecomp / sdc / heat / services / tree / ToscaTreeManagerTest.java
1 /*
2  * Copyright © 2017-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
17 package org.openecomp.sdc.heat.services.tree;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.util.regex.Pattern;
23
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;
29
30 public class ToscaTreeManagerTest {
31
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();
37
38     @Test
39     public void testTreeWithDiffFileNames() throws IOException {
40         String inputDirectory = BASE_DIR + "diffFileNames/" + IN;
41         String outputFileName = BASE_DIR + "diffFileNames/" + OUT + EXPECTED_TREE_FILE;
42
43         testTreeManager(inputDirectory, outputFileName);
44     }
45
46     @Test
47     public void testDirectoriesWithSimilarNameUnderDifferentRoots() throws IOException {
48         String inputDirectory = BASE_DIR + "similarDirectoryName/" + IN;
49         String outputFileName = BASE_DIR + "similarDirectoryName/" + OUT + EXPECTED_TREE_FILE;
50
51         testTreeManager(inputDirectory, outputFileName);
52     }
53
54     @Test
55     public void testTwoFilesUnderSameDirectory() throws IOException {
56         String inputDirectory = BASE_DIR + "twoFilesUnderSameDirectory/" + IN;
57         String outputFileName = BASE_DIR + "twoFilesUnderSameDirectory/" + OUT + EXPECTED_TREE_FILE;
58
59         testTreeManager(inputDirectory, outputFileName);
60     }
61
62     private void testTreeManager(String inputDirectory, String outputFileName) throws IOException {
63         initTreeManager(inputDirectory);
64         toscaTreeManager.createTree();
65         HeatStructureTree tree = toscaTreeManager.getTree();
66
67         validateToscaTree(outputFileName, tree);
68     }
69
70     private void validateToscaTree(String outputFileName, HeatStructureTree tree) throws IOException {
71         File expectedTreeFile = new File(this.getClass().getResource(outputFileName).getFile());
72
73         String expectedTree;
74         try (FileInputStream fis = new FileInputStream(expectedTreeFile)) {
75             expectedTree = new String(FileUtils.toByteArray(fis));
76         }
77         Assert.assertNotNull(expectedTree);
78         expectedTree = expectedTree.trim().replace("\r", "");
79         String actualTree = JsonUtil.object2Json(tree);
80         Assert.assertEquals(expectedTree, actualTree);
81     }
82
83     private void initTreeManager(String inputDir) {
84         String fileName = inputDir.replace("/", File.separator);
85         File directory = new File(this.getClass().getResource(inputDir).getFile());
86
87         addFilesToTreeManager(fileName, directory.listFiles());
88     }
89
90     private void addFilesToTreeManager(String baseDir, File[] listFiles) {
91         for (File file : listFiles) {
92             if (file.isDirectory()) {
93                 addFilesToTreeManager(baseDir, file.listFiles());
94             } else {
95                 toscaTreeManager.addFile(getFileNameWithoutTestDirectory(baseDir, file.getPath()), new byte[2]);
96             }
97         }
98     }
99
100     private String getFileNameWithoutTestDirectory(String baseDir, String fileName) {
101         return fileName.split(Pattern.quote(baseDir) + Pattern.quote(File.separator))[1];
102     }
103 }