Improve testing stability
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / test / java / org / openecomp / sdc / heat / services / tree / HeatTreeManagerUtilTest.java
1 /*
2  *
3  *  Copyright © 2017-2018 European Support Limited
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  * Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  * /
17  *
18  */
19
20 package org.openecomp.sdc.heat.services.tree;
21
22 import java.io.File;
23 import java.net.URL;
24 import java.util.AbstractMap;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40 import org.openecomp.core.utilities.file.FileContentHandler;
41 import org.openecomp.core.utilities.file.FileUtils;
42 import org.openecomp.core.validation.types.GlobalValidationContext;
43 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
44 import org.openecomp.sdc.heat.datatypes.model.Resource;
45
46 public class HeatTreeManagerUtilTest {
47
48     private static final String TEST_YML = "test.yml";
49     private static final String TEST = "test";
50     private static final String RESOURCE_DEF = "resource_def";
51
52     @Mock
53     private HeatTreeManager heatTreeManager;
54
55     @Before
56     public void setUp() throws Exception {
57         MockitoAnnotations.openMocks(this);
58     }
59
60     @Test
61     public void testInitHeatTreeManager() {
62         FileContentHandler fileContentHandler = getFileContentHandler();
63
64         Mockito.doNothing().when(heatTreeManager).addFile(Mockito.any(), Mockito.any());
65         HeatTreeManagerUtil.initHeatTreeManager(fileContentHandler);
66         Mockito.verify(heatTreeManager, Mockito.times(0)).addFile(Mockito.any(), Mockito.any());
67     }
68
69     @Test
70     public void testGetNestedFiles() {
71         HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
72         heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml"));
73         Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate);
74
75         Assert.assertNotNull(nestedFilesSet);
76         Assert.assertEquals(nestedFilesSet.size(), 1);
77     }
78
79     @Test
80     public void testGetResourceDefNested() {
81         HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
82         heatOrchestrationTemplate.setResources(getResourceMap("OS::Heat::ResourceGroup"));
83
84         Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate);
85         Assert.assertNotNull(nestedFilesSet);
86         Assert.assertTrue(nestedFilesSet.size() ==  1 && nestedFilesSet.contains(TEST_YML));
87     }
88
89     @Test
90     public void testGetArtifactFiles() {
91         HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
92         heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml"));
93         Set<String> nestedFilesSet = HeatTreeManagerUtil.getArtifactFiles("filename.yml", heatOrchestrationTemplate,
94                 null);
95
96         Assert.assertNotNull(nestedFilesSet);
97         Assert.assertTrue(nestedFilesSet.contains(TEST));
98     }
99
100     @Test
101     public void testGetResourceDefIfProvidedResourceIsNull() {
102         Assert.assertNull(HeatTreeManagerUtil.getResourceDef(new Resource()));
103     }
104
105     @Test
106     public void testGetResourceDef() {
107         Resource resource = new Resource();
108
109         Map<String, Object> resourceMap = new HashMap<>();
110         Map<String, String> nestedResourceMap = new HashMap<String, String>() {{
111             put("type", TEST_YML);
112         }};
113
114         resourceMap.put(RESOURCE_DEF, nestedResourceMap);
115         resource.setProperties(resourceMap);
116         Resource resultResource = HeatTreeManagerUtil.getResourceDef(resource);
117         Assert.assertNotNull(resultResource);
118         Assert.assertEquals(TEST_YML, resultResource.getType());
119     }
120
121     @Test
122     public void testCheckResourceGroupTypeValid() {
123         Resource resource = new Resource();
124
125         Map<String, Object> resourceMap = new HashMap<>();
126         Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
127             put("type", Collections.emptyList());
128         }};
129
130         resourceMap.put(RESOURCE_DEF, nestedResourceMap);
131         resource.setProperties(resourceMap);
132
133         GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
134         Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
135                 .anyString());
136
137         HeatTreeManagerUtil.checkResourceGroupTypeValid(TEST_YML, TEST, resource, globalValidationContextMock);
138
139         Mockito.verify(globalValidationContextMock, Mockito.times(1))
140                 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
141     }
142
143     @Test
144     public void testCheckResourceTypeValid() {
145         Resource resource = new Resource();
146
147         Map<String, Object> resourceMap = new HashMap<>();
148         Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
149             put("properties", Collections.emptyList());
150         }};
151
152         resourceMap.put(RESOURCE_DEF, nestedResourceMap);
153         resource.setProperties(resourceMap);
154
155         GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
156         Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
157                 .anyString());
158
159         HeatTreeManagerUtil.checkResourceTypeValid(TEST_YML, TEST, resource, globalValidationContextMock);
160
161         Mockito.verify(globalValidationContextMock, Mockito.times(1))
162                 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
163     }
164
165     @Test
166     public void testCheckIfResourceGroupTypeIsNested() {
167         Resource resource = new Resource();
168
169         Map<String, Object> resourceMap = new HashMap<>();
170         Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
171             put("type", TEST_YML);
172         }};
173
174         resourceMap.put(RESOURCE_DEF, nestedResourceMap);
175         resource.setProperties(resourceMap);
176
177         GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
178         Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
179                 .anyString());
180
181         boolean result = HeatTreeManagerUtil
182                 .checkIfResourceGroupTypeIsNested(TEST_YML, TEST, resource, globalValidationContextMock);
183
184         Mockito.verify(globalValidationContextMock, Mockito.times(1))
185                 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
186
187         Assert.assertTrue(result);
188     }
189
190     @Test
191     public void testCheckIfResourceGroupTypeIsNestedNull() {
192         Assert.assertFalse(HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(TEST_YML, TEST, new Resource(),
193                 null));
194     }
195
196     private FileContentHandler getFileContentHandler() {
197         FileContentHandler fileContentHandler = new FileContentHandler();
198         Map<String, byte[]> filesByteMap = new HashMap<>();
199         List<URL> urlList = FileUtils.getAllLocations("mock/model");
200         File files = new File(urlList.get(0).getPath());
201         if (files.isDirectory()) {
202             int fileCount = 0;
203             for (File file : Objects.requireNonNull(files.listFiles())) {
204                 byte[] bytesArray = new byte[(int) file.length()];
205                 filesByteMap.put("File" + ++fileCount, bytesArray);
206             }
207         }
208
209         fileContentHandler.setFiles(filesByteMap);
210
211         return fileContentHandler;
212     }
213
214     private Map<String, Resource> getResourceMap(String type) {
215         Resource resource = new Resource();
216         resource.setType(type);
217         Map<String, String> map = new HashMap<>();
218         map.put("get_file", TEST);
219         resource.setProperties(new HashMap<String, Object>() {{
220             put("get_file", Collections.singletonList(map));
221             put("resource_def", new HashMap<String, Object>() {{
222                 put("type", TEST_YML);
223             }});
224         }});
225
226
227         return Stream.of(new AbstractMap.SimpleEntry<>("Res1", resource))
228                 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
229     }
230 }