Refactor HEAT tree test for error handling
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / tree / TreeBaseTest.java
1 /*
2  * Copyright © 2016-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.vendorsoftwareproduct.tree;
18
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
27
28 /**
29  * Created by SHALOMB on 6/8/2016.
30  */
31 public abstract class TreeBaseTest {
32
33     HeatTreeManager initHeatTreeManager(String inputDirectory) throws URISyntaxException, IOException {
34
35         URL url = Thread.currentThread().getContextClassLoader().getResource(inputDirectory);
36         if (url == null) {
37             throw new FileNotFoundException("Directory " + inputDirectory + " not found in classpath");
38         }
39
40         File inputDir = new File(url.toURI());
41
42         File[] files = inputDir.listFiles();
43         if (files == null) {
44             throw new IllegalArgumentException("Directory " + inputDirectory + " does not contain files");
45         }
46
47         HeatTreeManager heatTreeManager = new HeatTreeManager();
48         for (File inputFile : files) {
49
50             String path = inputDirectory.replace("/", File.separator) + File.separator + inputFile.getName();
51             try (InputStream inputStream = FileUtils.loadFileToInputStream(path)) {
52                 heatTreeManager.addFile(inputFile.getName(), inputStream);
53             }
54         }
55
56         return heatTreeManager;
57     }
58 }