diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-heat-lib/src/test/java/org')
6 files changed, 729 insertions, 73 deletions
diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatResourceUtilTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatResourceUtilTest.java new file mode 100644 index 0000000000..93f8fc9f23 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatResourceUtilTest.java @@ -0,0 +1,89 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.heat.services; + +import java.util.Optional; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; + +public class HeatResourceUtilTest { + + private static final String ROLE = "role"; + + @Test + public void testEvaluateNetworkRoleFromResourceIdEmpty() { + Assert.assertFalse(HeatResourceUtil.evaluateNetworkRoleFromResourceId(null, null).isPresent()); + } + + @Test + public void testEvaluateNetworkRoleFromResourceIdNeutronExternal() { + Optional<String> networkRole = HeatResourceUtil.evaluateNetworkRoleFromResourceId( + "vm_type_1_role_port_1", HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()); + + Assert.assertTrue(networkRole.isPresent()); + Assert.assertEquals(networkRole.get(), ROLE); + } + + @Test + public void testEvaluateNetworkRoleFromResourceIdNeutronInternal() { + Optional<String> networkRole = HeatResourceUtil.evaluateNetworkRoleFromResourceId( + "vm_type_1_int_role_port_1", HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()); + + Assert.assertTrue(networkRole.isPresent()); + Assert.assertEquals(networkRole.get(), ROLE); + } + + @Test + public void testEvaluateNetworkRoleFromResourceIdVMIExternal() { + Optional<String> networkRole = HeatResourceUtil.evaluateNetworkRoleFromResourceId( + "vm_type_1_role_vmi_1", HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE + .getHeatResource()); + + Assert.assertTrue(networkRole.isPresent()); + Assert.assertEquals(networkRole.get(), ROLE); + } + + @Test + public void testEvaluateNetworkRoleFromResourceIdVMIInternal() { + Optional<String> networkRole = HeatResourceUtil.evaluateNetworkRoleFromResourceId( + "vm_type_1_int_role_vmi_1", HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE + .getHeatResource()); + + Assert.assertTrue(networkRole.isPresent()); + Assert.assertEquals(networkRole.get(), ROLE); + } + + @Test + public void testExtractNetworkRoleFromSubInterfaceIdEmpty() { + Assert.assertFalse(HeatResourceUtil.extractNetworkRoleFromSubInterfaceId(null, null).isPresent()); + } + + @Test + public void testExtractNetworkRoleFromSubInterfaceId() { + Optional<String> networkRole = HeatResourceUtil.extractNetworkRoleFromSubInterfaceId( + "vm_type_1_subint_role_vmi_1", HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE + .getHeatResource()); + + Assert.assertTrue(networkRole.isPresent()); + Assert.assertEquals(networkRole.get(), ROLE); + } +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatStructureUtilTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatStructureUtilTest.java new file mode 100644 index 0000000000..70f149be2c --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/HeatStructureUtilTest.java @@ -0,0 +1,146 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.heat.services; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions; + +public class HeatStructureUtilTest { + + @Mock + private GlobalValidationContext globalValidationContextMock; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testIsResourceNestedFalse() { + Assert.assertFalse(HeatStructureUtil.isNestedResource("Test.txt")); + } + + @Test + public void testIsResourceNestedNull() { + Assert.assertFalse(HeatStructureUtil.isNestedResource(null)); + } + + @Test + public void testIsResourceNestedTrue() { + Assert.assertTrue(HeatStructureUtil.isNestedResource("Test.yml")); + } + + @Test + public void testGetReferencedValuesByFunctionNameAddMessageCall() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap.put(ResourceReferenceFunctions.GET_RESOURCE.getFunction(), Collections.emptyList()); + + Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), + Mockito.anyString()); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_RESOURCE.getFunction(), propertyMap, globalValidationContextMock); + + Mockito.verify(globalValidationContextMock, Mockito.times(1)) + .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString()); + Assert.assertTrue(valueNames.isEmpty()); + } + + @Test + public void testGetReferencedValuesByFunctionNameGetFile() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(), "file:///filename"); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock); + + Assert.assertFalse(valueNames.isEmpty()); + Assert.assertTrue(valueNames.contains("filename")); + } + + @Test + public void testGetReferencedValuesByFunctionNameGetFileValueList() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap + .put(ResourceReferenceFunctions.GET_FILE.getFunction(), Collections.singletonList("file:///filename")); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock); + + Assert.assertFalse(valueNames.isEmpty()); + Assert.assertTrue(valueNames.contains("filename")); + } + + @Test + public void testGetReferencedValuesByFunctionNameGetFileValueListWithAnotherList() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(), + Collections.singletonList(Collections.emptyList())); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock); + + Assert.assertTrue(valueNames.isEmpty()); + } + + @Test + public void testGetReferencedValuesByFunctionNamePassingPropertyMapWithSet() { + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), Collections.singletonList(new HashSet<>()), + globalValidationContextMock); + + Assert.assertTrue(valueNames.isEmpty()); + } + + @Test + public void testGetReferencedValuesByFunctionNameGetFileValueListSet() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(), + new HashSet<>()); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock); + + Assert.assertTrue(valueNames.isEmpty()); + } + + @Test + public void testGetReferencedValuesByFunctionNameIncorrectKeyWithSet() { + Map<String, Object> propertyMap = new HashMap<>(); + propertyMap.put("test", new HashSet<>()); + + Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml", + ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock); + + Assert.assertTrue(valueNames.isEmpty()); + } +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/manifest/ManifestUtilTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/manifest/ManifestUtilTest.java new file mode 100644 index 0000000000..13cf5c901e --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/manifest/ManifestUtilTest.java @@ -0,0 +1,109 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.heat.services.manifest; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; + +public class ManifestUtilTest { + + @Test + public void testGetFileAndItsEnvNull() { + Assert.assertTrue(ManifestUtil.getFileAndItsEnv(new ManifestContent()).isEmpty()); + } + + @Test + public void testGetFileAndItsEnv() { + + Assert.assertEquals(ManifestUtil.getFileAndItsEnv(createManifestContent()).size(), 1); + } + + @Test + public void testGetFileTypeMapDataNull() { + Assert.assertTrue(ManifestUtil.getFileTypeMap(new ManifestContent()).isEmpty()); + } + + @Test + public void testGetFileTypeMap() { + Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(createManifestContent()); + + Assert.assertEquals(fileTypeMap.size(), 2); + Assert.assertTrue(fileTypeMap.containsKey("Main.yml") && fileTypeMap.containsKey("Test.yml")); + } + + @Test + public void testGetFileTypeMapDataBlank() { + ManifestContent manifestContent = new ManifestContent(); + manifestContent.setData(Collections.emptyList()); + + Assert.assertTrue(ManifestUtil.getFileTypeMap(manifestContent).isEmpty()); + } + + @Test + public void testGetArtifactsDataNull() { + Assert.assertTrue(ManifestUtil.getArtifacts(new ManifestContent()).isEmpty()); + } + + @Test + public void testGetArtifacts() { + ManifestContent manifestContent = createManifestContent(); + manifestContent.getData().get(0).getData().get(0).setType(FileData.Type.OTHER); + + Set<String> typeSet = ManifestUtil.getArtifacts(manifestContent); + Assert.assertEquals(typeSet.size(), 1); + Assert.assertTrue(typeSet.contains("Test.yml")); + } + + @Test + public void testGetBaseFilesDataNull() { + Assert.assertTrue(ManifestUtil.getArtifacts(new ManifestContent()).isEmpty()); + } + + @Test + public void testGetBaseFiles() { + Set<String> typeSet = ManifestUtil.getBaseFiles(createManifestContent()); + Assert.assertEquals(typeSet.size(), 1); + Assert.assertTrue(typeSet.contains("Main.yml")); + } + + private ManifestContent createManifestContent() { + + FileData fileData1 = new FileData(); + fileData1.setFile("Test.yml"); + fileData1.setType(FileData.Type.HEAT_ENV); + + FileData fileData = new FileData(); + fileData.setFile("Main.yml"); + fileData.setType(FileData.Type.HEAT_ENV); + fileData.setBase(true); + + fileData.setData(Collections.singletonList(fileData1)); + + ManifestContent manifestContent = new ManifestContent(); + manifestContent.setData(Collections.singletonList(fileData)); + + return manifestContent; + } +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java new file mode 100644 index 0000000000..e3e19792f5 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java @@ -0,0 +1,65 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.heat.services.tree; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; + +public class HeatTreeManagerTest { + + @Test + public void testCreateTree() throws IOException { + HeatTreeManager heatTreeManager = new HeatTreeManager(); + addFile(heatTreeManager, "mock/model/MANIFEST.json", SdcCommon.MANIFEST_NAME); + addFile(heatTreeManager, "mock/model/first.yaml", "first.yaml"); + addFile(heatTreeManager, "mock/model/second.yaml", "second.yaml"); + addFile(heatTreeManager, "mock/model/first.env", "first.env"); + addFile(heatTreeManager, "mock/model/base_cscf_volume.yaml", "base_cscf_volume.yaml"); + addFile(heatTreeManager, "mock/model/network.yml", "network.yml"); + addFile(heatTreeManager, "mock/model/testHeat.yml", "testHeat.yml"); + addFile(heatTreeManager, "mock/model/nested.yml", "nested.yml"); + addFile(heatTreeManager, "mock/model/base_cscf_volume.env", "base_cscf_volume.env"); + + heatTreeManager.createTree(); + + HeatStructureTree heatStructureTree = heatTreeManager.getTree(); + Assert.assertNotNull(heatStructureTree); + Assert.assertEquals(1, heatStructureTree.getHeat().size()); + Assert.assertEquals(1, heatStructureTree.getNetwork().size()); + } + + private void addFile(HeatTreeManager heatTreeManager, String fileLocation, String fileName) + throws IOException { + + List<URL> urlList = FileUtils.getAllLocations(fileLocation); + try (InputStream inputStream = new FileInputStream(new File(urlList.get(0).getPath()))) { + heatTreeManager.addFile(fileName, inputStream); + } + } +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtilTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtilTest.java new file mode 100644 index 0000000000..6e34350d19 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtilTest.java @@ -0,0 +1,230 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.heat.services.tree; + +import java.io.File; +import java.net.URL; +import java.util.AbstractMap; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.heat.datatypes.model.Resource; + +public class HeatTreeManagerUtilTest { + + private static final String TEST_YML = "test.yml"; + private static final String TEST = "test"; + private static final String RESOURCE_DEF = "resource_def"; + + @Mock + private HeatTreeManager heatTreeManager; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testInitHeatTreeManager() { + FileContentHandler fileContentHandler = getFileContentHandler(); + + Mockito.doNothing().when(heatTreeManager).addFile(Mockito.any(), Mockito.any()); + HeatTreeManagerUtil.initHeatTreeManager(fileContentHandler); + Mockito.verify(heatTreeManager, Mockito.times(0)).addFile(Mockito.any(), Mockito.any()); + } + + @Test + public void testGetNestedFiles() { + HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate(); + heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml")); + Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate); + + Assert.assertNotNull(nestedFilesSet); + Assert.assertEquals(nestedFilesSet.size(), 1); + } + + @Test + public void testGetResourceDefNested() { + HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate(); + heatOrchestrationTemplate.setResources(getResourceMap("OS::Heat::ResourceGroup")); + + Set<String> nestedFilesSet = HeatTreeManagerUtil.getNestedFiles(heatOrchestrationTemplate); + Assert.assertNotNull(nestedFilesSet); + Assert.assertTrue(nestedFilesSet.size() == 1 && nestedFilesSet.contains(TEST_YML)); + } + + @Test + public void testGetArtifactFiles() { + HeatOrchestrationTemplate heatOrchestrationTemplate = new HeatOrchestrationTemplate(); + heatOrchestrationTemplate.setResources(getResourceMap("Type1.yml")); + Set<String> nestedFilesSet = HeatTreeManagerUtil.getArtifactFiles("filename.yml", heatOrchestrationTemplate, + null); + + Assert.assertNotNull(nestedFilesSet); + Assert.assertTrue(nestedFilesSet.contains(TEST)); + } + + @Test + public void testGetResourceDefIfProvidedResourceIsNull() { + Assert.assertNull(HeatTreeManagerUtil.getResourceDef(new Resource())); + } + + @Test + public void testGetResourceDef() { + Resource resource = new Resource(); + + Map<String, Object> resourceMap = new HashMap<>(); + Map<String, String> nestedResourceMap = new HashMap<String, String>() {{ + put("type", TEST_YML); + }}; + + resourceMap.put(RESOURCE_DEF, nestedResourceMap); + resource.setProperties(resourceMap); + Resource resultResource = HeatTreeManagerUtil.getResourceDef(resource); + Assert.assertNotNull(resultResource); + Assert.assertEquals(TEST_YML, resultResource.getType()); + } + + @Test + public void testCheckResourceGroupTypeValid() { + Resource resource = new Resource(); + + Map<String, Object> resourceMap = new HashMap<>(); + Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{ + put("type", Collections.emptyList()); + }}; + + resourceMap.put(RESOURCE_DEF, nestedResourceMap); + resource.setProperties(resourceMap); + + GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class); + Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito + .anyString()); + + HeatTreeManagerUtil.checkResourceGroupTypeValid(TEST_YML, TEST, resource, globalValidationContextMock); + + Mockito.verify(globalValidationContextMock, Mockito.times(1)) + .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString()); + } + + @Test + public void testCheckResourceTypeValid() { + Resource resource = new Resource(); + + Map<String, Object> resourceMap = new HashMap<>(); + Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{ + put("properties", Collections.emptyList()); + }}; + + resourceMap.put(RESOURCE_DEF, nestedResourceMap); + resource.setProperties(resourceMap); + + GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class); + Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito + .anyString()); + + HeatTreeManagerUtil.checkResourceTypeValid(TEST_YML, TEST, resource, globalValidationContextMock); + + Mockito.verify(globalValidationContextMock, Mockito.times(1)) + .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString()); + } + + @Test + public void testCheckIfResourceGroupTypeIsNested() { + Resource resource = new Resource(); + + Map<String, Object> resourceMap = new HashMap<>(); + Map<String, Object> nestedResourceMap = new HashMap<String, Object>() {{ + put("type", TEST_YML); + }}; + + resourceMap.put(RESOURCE_DEF, nestedResourceMap); + resource.setProperties(resourceMap); + + GlobalValidationContext globalValidationContextMock = Mockito.mock(GlobalValidationContext.class); + Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(), Mockito + .anyString()); + + boolean result = HeatTreeManagerUtil + .checkIfResourceGroupTypeIsNested(TEST_YML, TEST, resource, globalValidationContextMock); + + Mockito.verify(globalValidationContextMock, Mockito.times(1)) + .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString()); + + Assert.assertTrue(result); + } + + @Test + public void testCheckIfResourceGroupTypeIsNestedNull() { + Assert.assertFalse(HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(TEST_YML, TEST, new Resource(), + null)); + } + + private FileContentHandler getFileContentHandler() { + FileContentHandler fileContentHandler = new FileContentHandler(); + Map<String, byte[]> filesByteMap = new HashMap<>(); + List<URL> urlList = FileUtils.getAllLocations("mock/model"); + File files = new File(urlList.get(0).getPath()); + if (files.isDirectory()) { + int fileCount = 0; + for (File file : Objects.requireNonNull(files.listFiles())) { + byte[] bytesArray = new byte[(int) file.length()]; + filesByteMap.put("File" + ++fileCount, bytesArray); + } + } + + fileContentHandler.setFiles(filesByteMap); + + return fileContentHandler; + } + + private Map<String, Resource> getResourceMap(String type) { + Resource resource = new Resource(); + resource.setType(type); + Map<String, String> map = new HashMap<>(); + map.put("get_file", TEST); + resource.setProperties(new HashMap<String, Object>() {{ + put("get_file", Collections.singletonList(map)); + put("resource_def", new HashMap<String, Object>() {{ + put("type", TEST_YML); + }}); + }}); + + + return Stream.of(new AbstractMap.SimpleEntry<>("Res1", resource)) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + } +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManagerTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManagerTest.java index 0cfac52318..4ccf7ea623 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManagerTest.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManagerTest.java @@ -1,86 +1,103 @@ +/* + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + package org.openecomp.sdc.heat.services.tree; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.regex.Pattern; + import org.junit.Assert; import org.junit.Test; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.json.JsonUtil; import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.regex.Pattern; - public class ToscaTreeManagerTest { - private static final String BASE_DIR = "/mock/toscaTree/"; - private static final String IN = "in"; - private static final String OUT = "out"; - private static final String EXPECTED_TREE_FILE = "/expectedTree.json"; - private ToscaTreeManager toscaTreeManager = new ToscaTreeManager(); - - @Test - public void testTreeWithDiffFileNames() throws IOException { - String inputDirectory = BASE_DIR + "diffFileNames/" + IN; - String outputFileName = BASE_DIR + "diffFileNames/" + OUT + EXPECTED_TREE_FILE; - - testTreeManager(inputDirectory, outputFileName); - } - - @Test - public void testDirectoriesWithSimilarNameUnderDifferentRoots() throws IOException { - String inputDirectory = BASE_DIR + "similarDirectoryName/" + IN; - String outputFileName = BASE_DIR + "similarDirectoryName/" + OUT + EXPECTED_TREE_FILE; - - testTreeManager(inputDirectory, outputFileName); - } - - @Test - public void testTwoFilesUnderSameDirectory() throws IOException { - String inputDirectory = BASE_DIR + "twoFilesUnderSameDirectory/" + IN; - String outputFileName = BASE_DIR + "twoFilesUnderSameDirectory/" + OUT + EXPECTED_TREE_FILE; - - testTreeManager(inputDirectory, outputFileName); - } - - private void testTreeManager(String inputDirectory, String outputFileName) throws IOException { - initTreeManager(inputDirectory); - toscaTreeManager.createTree(); - HeatStructureTree tree = toscaTreeManager.getTree(); - - validateToscaTree(outputFileName, tree); - } - - private void validateToscaTree(String outputFileName, HeatStructureTree tree) throws IOException { - String actualTree = JsonUtil.object2Json(tree); - File expectedTreeFile = new File(this.getClass().getResource(outputFileName).getFile()); - - String expectedTree; - try(FileInputStream fis = new FileInputStream(expectedTreeFile)) { - expectedTree = new String(FileUtils.toByteArray(fis)); + + private static final String BASE_DIR = "/mock/toscaTree/"; + private static final String IN = "in"; + private static final String OUT = "out"; + private static final String EXPECTED_TREE_FILE = "/expectedTree.json"; + private ToscaTreeManager toscaTreeManager = new ToscaTreeManager(); + + @Test + public void testTreeWithDiffFileNames() throws IOException { + String inputDirectory = BASE_DIR + "diffFileNames/" + IN; + String outputFileName = BASE_DIR + "diffFileNames/" + OUT + EXPECTED_TREE_FILE; + + testTreeManager(inputDirectory, outputFileName); + } + + @Test + public void testDirectoriesWithSimilarNameUnderDifferentRoots() throws IOException { + String inputDirectory = BASE_DIR + "similarDirectoryName/" + IN; + String outputFileName = BASE_DIR + "similarDirectoryName/" + OUT + EXPECTED_TREE_FILE; + + testTreeManager(inputDirectory, outputFileName); + } + + @Test + public void testTwoFilesUnderSameDirectory() throws IOException { + String inputDirectory = BASE_DIR + "twoFilesUnderSameDirectory/" + IN; + String outputFileName = BASE_DIR + "twoFilesUnderSameDirectory/" + OUT + EXPECTED_TREE_FILE; + + testTreeManager(inputDirectory, outputFileName); } - Assert.assertNotNull(expectedTree); - expectedTree = expectedTree.trim().replace("\r", ""); - Assert.assertEquals(expectedTree, actualTree); - } - - private void initTreeManager(String inputDir) throws IOException { - String fileName = inputDir.replace("/", File.separator); - File directory = new File(this.getClass().getResource(inputDir).getFile()); - - addFilesToTreeManager(fileName, directory.listFiles()); - } - - private void addFilesToTreeManager(String baseDir, File[] listFiles) throws IOException { - for (File file : listFiles) { - if (file.isDirectory()) { - addFilesToTreeManager(baseDir, file.listFiles()); - } else { - toscaTreeManager.addFile(getFileNameWithoutTestDirectory(baseDir, file.getPath()), new byte[2]); - } + + private void testTreeManager(String inputDirectory, String outputFileName) throws IOException { + initTreeManager(inputDirectory); + toscaTreeManager.createTree(); + HeatStructureTree tree = toscaTreeManager.getTree(); + + validateToscaTree(outputFileName, tree); } - } - private String getFileNameWithoutTestDirectory(String baseDir, String fileName) { - return fileName.split(Pattern.quote(baseDir) + Pattern.quote(File.separator))[1]; - } + private void validateToscaTree(String outputFileName, HeatStructureTree tree) throws IOException { + File expectedTreeFile = new File(this.getClass().getResource(outputFileName).getFile()); + + String expectedTree; + try (FileInputStream fis = new FileInputStream(expectedTreeFile)) { + expectedTree = new String(FileUtils.toByteArray(fis)); + } + Assert.assertNotNull(expectedTree); + expectedTree = expectedTree.trim().replace("\r", ""); + String actualTree = JsonUtil.object2Json(tree); + Assert.assertEquals(expectedTree, actualTree); + } + + private void initTreeManager(String inputDir) { + String fileName = inputDir.replace("/", File.separator); + File directory = new File(this.getClass().getResource(inputDir).getFile()); + + addFilesToTreeManager(fileName, directory.listFiles()); + } + + private void addFilesToTreeManager(String baseDir, File[] listFiles) { + for (File file : listFiles) { + if (file.isDirectory()) { + addFilesToTreeManager(baseDir, file.listFiles()); + } else { + toscaTreeManager.addFile(getFileNameWithoutTestDirectory(baseDir, file.getPath()), new byte[2]); + } + } + } + + private String getFileNameWithoutTestDirectory(String baseDir, String fileName) { + return fileName.split(Pattern.quote(baseDir) + Pattern.quote(File.separator))[1]; + } }
\ No newline at end of file |