3 * Copyright © 2017-2018 European Support Limited
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.openecomp.sdc.heat.services.tree;
24 import java.util.AbstractMap;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Objects;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
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;
46 public class HeatTreeManagerUtilTest {
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";
53 private HeatTreeManager heatTreeManager;
56 public void setUp() throws Exception {
57 MockitoAnnotations.openMocks(this);
61 public void testInitHeatTreeManager() {
62 FileContentHandler fileContentHandler = getFileContentHandler();
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());
70 public void testGetNestedFiles() {
71 HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
72 heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml"));
73 Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate);
75 Assert.assertNotNull(nestedFilesSet);
76 Assert.assertEquals(nestedFilesSet.size(), 1);
80 public void testGetResourceDefNested() {
81 HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
82 heatOrchestrationTemplate.setResources(getResourceMap("OS::Heat::ResourceGroup"));
84 Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate);
85 Assert.assertNotNull(nestedFilesSet);
86 Assert.assertTrue(nestedFilesSet.size() == 1 && nestedFilesSet.contains(TEST_YML));
90 public void testGetArtifactFiles() {
91 HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate();
92 heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml"));
93 Set<String> nestedFilesSet = HeatTreeManagerUtil.getArtifactFiles("filename.yml", heatOrchestrationTemplate,
96 Assert.assertNotNull(nestedFilesSet);
97 Assert.assertTrue(nestedFilesSet.contains(TEST));
101 public void testGetResourceDefIfProvidedResourceIsNull() {
102 Assert.assertNull(HeatTreeManagerUtil.getResourceDef(new Resource()));
106 public void testGetResourceDef() {
107 Resource resource = new Resource();
109 Map<String, Object> resourceMap = new HashMap<>();
110 Map<String, String> nestedResourceMap = new HashMap<String, String>() {{
111 put("type", TEST_YML);
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());
122 public void testCheckResourceGroupTypeValid() {
123 Resource resource = new Resource();
125 Map<String, Object> resourceMap = new HashMap<>();
126 Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
127 put("type", Collections.emptyList());
130 resourceMap.put(RESOURCE_DEF, nestedResourceMap);
131 resource.setProperties(resourceMap);
133 GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
134 Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
137 HeatTreeManagerUtil.checkResourceGroupTypeValid(TEST_YML, TEST, resource, globalValidationContextMock);
139 Mockito.verify(globalValidationContextMock, Mockito.times(1))
140 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
144 public void testCheckResourceTypeValid() {
145 Resource resource = new Resource();
147 Map<String, Object> resourceMap = new HashMap<>();
148 Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
149 put("properties", Collections.emptyList());
152 resourceMap.put(RESOURCE_DEF, nestedResourceMap);
153 resource.setProperties(resourceMap);
155 GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
156 Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
159 HeatTreeManagerUtil.checkResourceTypeValid(TEST_YML, TEST, resource, globalValidationContextMock);
161 Mockito.verify(globalValidationContextMock, Mockito.times(1))
162 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
166 public void testCheckIfResourceGroupTypeIsNested() {
167 Resource resource = new Resource();
169 Map<String, Object> resourceMap = new HashMap<>();
170 Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{
171 put("type", TEST_YML);
174 resourceMap.put(RESOURCE_DEF, nestedResourceMap);
175 resource.setProperties(resourceMap);
177 GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class);
178 Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito
181 boolean result = HeatTreeManagerUtil
182 .checkIfResourceGroupTypeIsNested(TEST_YML, TEST, resource, globalValidationContextMock);
184 Mockito.verify(globalValidationContextMock, Mockito.times(1))
185 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
187 Assert.assertTrue(result);
191 public void testCheckIfResourceGroupTypeIsNestedNull() {
192 Assert.assertFalse(HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(TEST_YML, TEST, new Resource(),
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()) {
203 for (File file : Objects.requireNonNull(files.listFiles())) {
204 byte[] bytesArray = new byte[(int) file.length()];
205 filesByteMap.put("File" + ++fileCount, bytesArray);
209 fileContentHandler.setFiles(filesByteMap);
211 return fileContentHandler;
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);
227 return Stream.of(new AbstractMap.SimpleEntry<>("Res1", resource))
228 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));