diff options
author | siddharth0905 <siddharth.singh4@amdocs.com> | 2017-11-22 13:12:05 +0530 |
---|---|---|
committer | siddharth0905 <siddharth.singh4@amdocs.com> | 2017-11-22 13:12:05 +0530 |
commit | 9f459654bdd58ad7bbf303fa5f5446acddd2de8e (patch) | |
tree | 7ba20ac307b01f1526241e38543a7236111509c6 /openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test | |
parent | e077e4031a7b2aa76ec60237372f9014c27d6d78 (diff) |
Handle VSP without Image while healing
Healing was not working correctly in same of image count was zero
Change-Id: Iba607a09b770c78202e94815ca49041aa54c701a
Issue-ID: SDC-692
Signed-off-by: siddharth0905 <siddharth.singh4@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test')
-rw-r--r-- | openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealerTest.java | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealerTest.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealerTest.java new file mode 100644 index 0000000000..69f4287cd1 --- /dev/null +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealerTest.java @@ -0,0 +1,269 @@ +package org.openecomp.sdc.healing.healers; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; + +public class ComponentQuestionnaireHealerTest { + private static final String HANDLE_NUM_OF_VMS_METHOD = "handleNumOfVm"; + private static final String GENERAL = "general"; + private static final String IMAGE = "image"; + private static final String FORMAT = "format"; + private static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio"; + private static final String MEMORY_RAM = "MemoryRAM"; + private static final String VM_SIZING = "vmSizing"; + private static final String COMPUTE = "compute"; + private static final String NUM_OF_VMS = "numOfVMs"; + private static final String DISK = "disk"; + private static final String BOOT_DISK_SIZE_PER_VM = "bootDiskSizePerVM"; + private static final String EPHEMERAL_DISK_SIZE_PER_VM = "ephemeralDiskSizePerVM"; + private static final Version VERSION = new Version(0, 1); + private static final String DUMMY_VSP_ID = "1495ef442f964cbfb00d82bd54292f89"; + private static final String DUMMY_COMPONENT_ID = "2495ef442f964cbfb00d82bd54292f89"; + private static final String DUMMY_COMPUTE_ID = "3495ef442f964cbfb00d82bd54292f89"; + private static final String DUMMY_IMAGE_ID = "4495ef442f964cbfb00d82bd54292f89"; + private static Map<String, Object> healingParams = new HashMap<>(); + private static final String componentQuestionnaireData = "{\"compute\": {" + + "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}," + + "\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}}," + + "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," + + "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"},\"hypervisor\": {" + + "\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final String componentQuestionnaireMissingDiskAttrData = "{\"compute\": {" + + "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\"" + + ": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}},\"general\": " + + "{\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"}," + + "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final String componentQuestionnaireWithoutVMSizingData = "{\"compute\": {" + + "\"guestOS\": {\"bitSize\": 64},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\"," + + "\"maximum\": \"400\"," + + "\"MemoryRAM\": \"2 GB\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\"" + + ":\"qcow2\",\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," + + "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final String componentQuestionnaireWithoutNumOfVMData = "{\"compute\": " + + "{\"guestOS\": {\"bitSize\": 64}," + + "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}}," + + "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," + + "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," + + "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final String componentQuestionnaireWithMemoryRamData = "{\"compute\": " + + "{\"guestOS\": {\"bitSize\": 64}," + + "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\": {\"MemoryRAM\": \"2 GB\"}}," + + "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," + + "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," + + "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final String componentQuestionnaireWithCPURatioData = "{\"compute\": " + + "{\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": " + + "\"0\"},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": " + + "\"1:1\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," + + "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," + + "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" + + "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" + + "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," + + "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" + + "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}"; + + private static final JsonParser jsonParser = new JsonParser(); + private ComponentEntity componentEntity; + + @Mock + private ImageDao imageDao; + + @Mock + private ComputeDao computeDao; + + @Mock + private ComponentDao componentDao; + + @InjectMocks + private ComponentQuestionnaireHealer componentQuestionnaireHealer; + + @Before + public void init() throws Exception { + MockitoAnnotations.initMocks(ComponentQuestionnaireHealerTest.this); + + healingParams.put(SdcCommon.VSP_ID, DUMMY_VSP_ID); + healingParams.put(SdcCommon.VERSION, VERSION); + } + + @Test + public void healQuestionnaireNullTest() throws Exception { + prepareHealingData(); + componentEntity.setQuestionnaireData(null); + Object returnObject = componentQuestionnaireHealer.heal(healingParams); + Assert.assertTrue(returnObject instanceof Collection); + Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject; + componentEntities.forEach(componentEntity -> { + Assert.assertNull(componentEntity.getQuestionnaireData()); + }); + } + + @Test + public void healAllCasesTest() throws Exception { + prepareHealingData(); + + Object returnObject = componentQuestionnaireHealer.heal(healingParams); + Assert.assertTrue(returnObject instanceof Collection); + Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject; + componentEntities.forEach(componentEntity -> { + JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData()); + Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK)); + Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK) + .getAsJsonPrimitive(BOOT_DISK_SIZE_PER_VM)); + Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK) + .getAsJsonPrimitive(EPHEMERAL_DISK_SIZE_PER_VM)); + Assert.assertNotNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)); + Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS) + .getAsJsonPrimitive(MEMORY_RAM)); + Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS) + .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO)); + Assert.assertNull(json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) + .getAsJsonPrimitive(FORMAT)); + Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING)); + }); + } + + @Test + public void healDiskAttrMissingTest() throws Exception { + prepareHealingData(); + componentEntity.setQuestionnaireData(componentQuestionnaireMissingDiskAttrData); + Object returnObject = componentQuestionnaireHealer.heal(healingParams); + Assert.assertTrue(returnObject instanceof Collection); + Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject; + componentEntities.forEach(componentEntity -> { + JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData()); + Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING)); + }); + } + + @Test + public void handleVMSizingWithVMSizingTest() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithoutNumOfVMData); + Method method = ComponentQuestionnaireHealer.class.getDeclaredMethod("handleVmSizing", + JsonObject.class); + method.setAccessible(true); + method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE)); + + Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING)); + } + + @Test + public void handleNumOfVMWithoutVMSizingTest() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + JsonObject jsonObject = (JsonObject) jsonParser + .parse(componentQuestionnaireWithoutVMSizingData); + provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject); + + Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)); + Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS) + .getAsJsonPrimitive("maximum")); + } + + @Test + public void handleVMSizingWithOnlyMemoryRAMTest() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithMemoryRamData); + provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject); + + Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)); + Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS) + .getAsJsonPrimitive(MEMORY_RAM)); + } + + @Test + public void handleVMSizingWithOnlyCpuRatioTest() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithCPURatioData); + provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject); + + Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)); + Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS) + .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO)); + } + + private void provideAccessToPrivateMethod(String methodName, JsonObject jsonObject) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = ComponentQuestionnaireHealer.class + .getDeclaredMethod(methodName, JsonObject.class, JsonObject.class); + method.setAccessible(true); + + method.invoke(componentQuestionnaireHealer,jsonObject.getAsJsonObject(COMPUTE), null); + } + + private void prepareHealingData() { + componentEntity = new ComponentEntity(DUMMY_VSP_ID, VERSION, DUMMY_COMPONENT_ID); + componentEntity.setQuestionnaireData(componentQuestionnaireData); + + Collection<ComponentEntity> componentEntities = new ArrayList<>(); + componentEntities.add(componentEntity); + doReturn(componentEntities).when(componentDao).list(anyObject()); + doReturn(componentEntity).when(componentDao).getQuestionnaireData(DUMMY_VSP_ID, + VERSION, DUMMY_COMPONENT_ID); + + ComputeEntity computeEntity = new ComputeEntity(DUMMY_VSP_ID, VERSION, + DUMMY_COMPONENT_ID, DUMMY_COMPUTE_ID); + Collection<ComputeEntity> computeEntities = new ArrayList<>(); + computeEntities.add(computeEntity); + doReturn(computeEntities).when(computeDao).list(anyObject()); + + ImageEntity imageEntity = new ImageEntity(DUMMY_VSP_ID, VERSION, + DUMMY_COMPONENT_ID, DUMMY_IMAGE_ID); + Collection<ImageEntity> imageEntities = new ArrayList<>(); + imageEntities.add(imageEntity); + doReturn(imageEntities).when(imageDao).list(anyObject()); + + doNothing().when(componentDao).updateQuestionnaireData(anyObject(), + anyObject(), anyObject(), anyObject()); + } +} |