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