Refactor HEAT tree test for error handling
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-core / src / test / java / org / openecomp / sdc / heat / services / tree / HeatTreeManagerTest.java
1 /*
2  * Copyright © 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.io.InputStream;
23 import java.net.URL;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29
30 public class HeatTreeManagerTest {
31
32   @Test
33   public void testHeatTreeCreation() throws IOException {
34
35     FileContentHandler fileContentMap = new FileContentHandler();
36     URL url = this.getClass().getResource("/heatTreeValidationOutput");
37
38     File templateDir = new File(url.getFile());
39     File[] files = templateDir.listFiles();
40
41     if (files == null || files.length == 0) {
42       return;
43     }
44
45     for (File file : files) {
46       fileContentMap.addFile(file.getName(), getFileContent(file));
47     }
48
49     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
50     heatTreeManager.createTree();
51     HeatStructureTree tree = heatTreeManager.getTree();
52     Assert.assertNotNull(tree);
53     Assert.assertEquals(tree.getHeat().size(), 2);
54   }
55
56   @Test
57   public void testHeatTreeArtifactsCreated() throws IOException {
58
59     FileContentHandler fileContentMap = new FileContentHandler();
60     URL url = this.getClass().getResource("/heatTreeArtifactsValidationOutput");
61
62     File templateDir = new File(url.getFile());
63     File[] files = templateDir.listFiles();
64
65     if (files == null || files.length == 0) {
66       return;
67     }
68
69     for (File file : files) {
70       fileContentMap.addFile(file.getName(), getFileContent(file));
71     }
72
73     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
74     heatTreeManager.createTree();
75     HeatStructureTree tree = heatTreeManager.getTree();
76     Assert.assertNotNull(tree);
77     Assert.assertEquals(tree.getHeat().size(), 3);
78     verifyHeatArtifacts(tree, "ocgmgr.yaml", 1);
79     verifyHeatArtifacts(tree, "ocgapp.yaml", 0);
80     verifyHeatArtifacts(tree, "base_ocg.yaml", 0);
81
82   }
83
84   private void verifyHeatArtifacts(HeatStructureTree tree, String heatName, int expectedArtifactNum ) {
85     HeatStructureTree heat = HeatStructureTree.getHeatStructureTreeByName(tree.getHeat(), heatName);
86     Assert.assertNotNull(heat);
87     if (expectedArtifactNum > 0) {
88       Assert.assertNotNull(heat.getArtifacts());
89       Assert.assertEquals(heat.getArtifacts().size(), expectedArtifactNum);
90     } else {
91       Assert.assertNull(heat.getArtifacts());
92     }
93   }
94
95
96   private byte[] getFileContent(File file) throws IOException {
97     try(InputStream inputStream = new FileInputStream(file)) {
98       return FileUtils.toByteArray(inputStream);
99     }
100   }
101 }