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