From edf44a7fcc351eb304bc2f84e8d71414014cb0c0 Mon Sep 17 00:00:00 2001 From: talio Date: Tue, 14 Nov 2017 16:07:58 +0200 Subject: forwarder test add unit tests for forwarder healer Issue-Id : SDC-653 Change-Id: I06954e855aac09d16422929203926aa983269b92 Signed-off-by: talio --- .../healers/ForwarderCapabilityHealerTest.java | 148 +++++++++++++++++++++ .../sdc/healing/healers/util/TestUtil.java | 108 +++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealerTest.java create mode 100644 openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/util/TestUtil.java (limited to 'openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org') diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealerTest.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealerTest.java new file mode 100644 index 0000000000..e1d6a9ded8 --- /dev/null +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealerTest.java @@ -0,0 +1,148 @@ +package org.openecomp.sdc.healing.healers; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.model.dao.ServiceModelDao; +import org.openecomp.core.model.types.ServiceElement; +import org.openecomp.sdc.common.togglz.ToggleableFeature; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.healing.healers.util.TestUtil; +import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.togglz.testing.TestFeatureManager; +import org.togglz.testing.TestFeatureManagerProvider; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; + +public class ForwarderCapabilityHealerTest { + private static final String IN_SUFFIX = "/in"; + private static final String OUT_SUFFIX = "/out"; + private static final String BASE_DIRECTORY = "/mock/healers/forwarder"; + private static final String ENTRY_DEFINITION_SERVICE_TEMPLATE = "MainServiceTemplate.yaml"; + private static TestFeatureManager manager; + + private Map params = new HashMap<>(); + + @Mock + private ServiceModelDao serviceModelDao; + @Mock + private ToscaAnalyzerService toscaAnalyzerService; + @InjectMocks + private ForwarderCapabilityHealer forwarderCapabilityHealer; + + @BeforeClass + public static void enableForwarderFeature(){ + manager = new TestFeatureManager(ToggleableFeature.class); + if (!ToggleableFeature.FORWARDER_CAPABILITY.isActive()) { + manager.enable(ToggleableFeature.FORWARDER_CAPABILITY); + } + } + + @AfterClass + public static void disableForwarderFeature() { + manager.disable(ToggleableFeature.FORWARDER_CAPABILITY); + manager = null; + TestFeatureManagerProvider.setFeatureManager(null); + } + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(ForwarderCapabilityHealerTest.this); + params.put(SdcCommon.VSP_ID,"1"); + params.put(SdcCommon.VERSION, new Version(1,1)); + } + + + @Test + public void testHealingSubstitutionMappingsNeutronPort() throws Exception { + testForwarderHealer( + "/testSubMappingNeutronPort", "org.openecomp.resource.cp.nodes.heat.network.neutron.Port", true); + } + + @Test + public void testHealingSubstitutionMappingsContrailPort() throws Exception { + testForwarderHealer( + "/testSubMappingContrailPort", "org.openecomp.resource.cp.nodes.heat.network.contrail.Port", true); + } + + @Test + public void testHealingSubstitutionMappingsExtNeutronPort() throws Exception { + testForwarderHealer( + "/testSubMappingExtNeutronPort", "org.openecomp.resource.cp.v2.extNeutronCP", true); + } + + @Test + public void testHealingSubstitutionMappingsExtContrailPort() throws Exception { + testForwarderHealer( + "/testSubMappingExtContrailPort", "org.openecomp.resource.cp.v2.extContrailCP", true); + } + + @Test + public void testHealingGlobalServiceTemplates () throws Exception { + testForwarderHealer("/testGlobalServiceTemplates", null, false); + } + + @Test + public void testHealingNoPorts() throws Exception { + testForwarderHealer("/testNoPorts", null, false); + } + + private void testForwarderHealer(String testDirectory, + String portType, + boolean needToTestSubMapping) throws Exception { + + ToscaServiceModel toscaServiceModel = TestUtil.loadToscaServiceModel( + BASE_DIRECTORY + testDirectory + IN_SUFFIX, null, ENTRY_DEFINITION_SERVICE_TEMPLATE); + + Mockito.doReturn(toscaServiceModel) + .when(serviceModelDao).getServiceModel(any(), any()); + + if(needToTestSubMapping) { + Mockito.doReturn(true) + .when(toscaAnalyzerService).isTypeOf( + eq(getMockPortNodeTemplate(portType)), + eq(ToscaNodeType.NATIVE_NETWORK_PORT), + anyObject(), anyObject()); + } + + validateServiceModelAfterHealing(testDirectory); + } + + private void validateServiceModelAfterHealing(String testDirectory) throws Exception { + Optional serviceModelObject = + (Optional) forwarderCapabilityHealer.heal(params); + + Assert.assertTrue(serviceModelObject.isPresent()); + TestUtil + .compareToscaServiceModels( + BASE_DIRECTORY + testDirectory + OUT_SUFFIX, serviceModelObject.get()); + } + + private NodeTemplate getMockPortNodeTemplate(String portType) { + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType(portType); + + Map properties = new HashMap<>(); + properties.put("exCP_naming", "port_pd01_port_exCP_naming"); + nodeTemplate.setProperties(properties); + + return nodeTemplate; + } + +} diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/util/TestUtil.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/util/TestUtil.java new file mode 100644 index 0000000000..20ba5ebf19 --- /dev/null +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/util/TestUtil.java @@ -0,0 +1,108 @@ +package org.openecomp.sdc.healing.healers.util; + +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.springframework.util.Assert; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.NotDirectoryException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class TestUtil { + private final static Logger log = (Logger) LoggerFactory.getLogger + (TestUtil.class.getName()); + + public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath, + String globalServiceTemplatesPath, + String entryDefinitionServiceTemplate) + throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + Map serviceTemplates = new HashMap<>(); + if (entryDefinitionServiceTemplate == null) { + entryDefinitionServiceTemplate = "MainServiceTemplate.yaml"; + } + + loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + if (globalServiceTemplatesPath != null) { + loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + } + + return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate); + } + + private static void loadServiceTemplates(String serviceTemplatesPath, + ToscaExtensionYamlUtil toscaExtensionYamlUtil, + Map serviceTemplates) + throws IOException { + URL urlFile = TestUtil.class.getResource(serviceTemplatesPath); + if (urlFile != null) { + File pathFile = new File(urlFile.getFile()); + Collection files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true); + if (files != null) { + addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil); + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } + + private static void addServiceTemplateFiles(Map serviceTemplates, + Collection files, + ToscaExtensionYamlUtil toscaExtensionYamlUtil) + throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml); + try { + yamlFile.close(); + } catch (IOException ignore) { + log.debug("",ignore); + } + } catch (FileNotFoundException exception) { + throw exception; + } catch (IOException exception) { + throw exception; + } + } + } + + public static void compareToscaServiceModels(String expectedServiceModelPath, + ToscaServiceModel actualServiceModel) + throws IOException { + ToscaServiceModel expectedServiceModel = + loadToscaServiceModel(expectedServiceModelPath, null, null); + + Map expectedServiceTemplates = + new HashMap<>(expectedServiceModel.getServiceTemplates()); + Map actualServiceTemplates = + new HashMap<>(actualServiceModel.getServiceTemplates()); + + for (Map.Entry expectedServiceTemplateEntry : expectedServiceTemplates.entrySet()) { + String serviceTemplateName = expectedServiceTemplateEntry.getKey(); + ServiceTemplate actualServiceTemplate = + actualServiceTemplates.get(serviceTemplateName); + + Assert.notNull(actualServiceTemplate, + "Missing service template in service model : " + serviceTemplateName); + org.junit.Assert.assertEquals("Difference in file " + serviceTemplateName, + JsonUtil.object2Json(expectedServiceTemplateEntry.getValue()), + JsonUtil.object2Json(actualServiceTemplate)); + } + } +} -- cgit 1.2.3-korg