diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src')
2 files changed, 362 insertions, 79 deletions
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java index 4dadb97f71..50e76781b3 100644 --- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java @@ -3,6 +3,7 @@ package org.openecomp.sdc.healing.healers; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.common.utils.SdcCommon; import org.openecomp.sdc.healing.interfaces.Healer; import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; @@ -24,71 +25,69 @@ import java.util.Objects; public class ComponentQuestionnaireHealer implements Healer { - private static final ComponentDao componentDao = - ComponentDaoFactory.getInstance().createInterface(); - private static final ComputeDao computeDao = - ComputeDaoFactory.getInstance().createInterface(); - private static final ImageDao imageDao = - ImageDaoFactory.getInstance().createInterface(); - + 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 IO_OP_PER_SEC = "IOOperationsPerSec"; + private static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio"; + private static final String COMPUTE_MEMORY_RAM = "memoryRAM"; + private static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec"; private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + private final ComponentDao componentDao; + private final ComputeDao computeDao; + private final ImageDao imageDao; + + public ComponentQuestionnaireHealer() { + this.componentDao = ComponentDaoFactory.getInstance().createInterface(); + this.computeDao = ComputeDaoFactory.getInstance().createInterface(); + this.imageDao = ImageDaoFactory.getInstance().createInterface(); + } - public static final String GENERAL = "general"; - public static final String IMAGE = "image"; - public static final String FORMAT = "format"; - public static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio"; - public static final String MEMORY_RAM = "MemoryRAM"; - public static final String VM_SIZING = "vmSizing"; - public static final String COMPUTE = "compute"; - public static final String NUM_OF_VMS = "numOfVMs"; - public static final String DISK = "disk"; - public static final String IO_OP_PER_SEC = "IOOperationsPerSec"; - - public static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio"; - public static final String COMPUTE_MEMORY_RAM = "memoryRAM"; - public static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec"; - - public ComponentQuestionnaireHealer(){ - + public ComponentQuestionnaireHealer(ComponentDao componentDao, + ComputeDao computeDao, ImageDao imageDao) { + this.componentDao = componentDao; + this.computeDao = computeDao; + this.imageDao = imageDao; } + @Override public Object heal(Map<String, Object> healingParams) throws Exception { - mdcDataDebugMessage.debugEntryMessage(null, null); + mdcDataDebugMessage.debugEntryMessage("VSP ID", + (String) healingParams.get(SdcCommon.VSP_ID)); String vspId = (String) healingParams.get(SdcCommon.VSP_ID); Version version = (Version) healingParams.get(SdcCommon.VERSION); - String user = (String) healingParams.get(SdcCommon.USER); Collection<ComponentEntity> componentEntities = componentDao.list(new ComponentEntity(vspId, version, null)); componentEntities.forEach(componentEntity -> { ComponentEntity componentQuestionnaireData = componentDao.getQuestionnaireData(vspId, version, componentEntity.getId()); String questionnaire = Objects.isNull(componentQuestionnaireData) ? null - : componentQuestionnaireData.getQuestionnaireData(); + : componentQuestionnaireData.getQuestionnaireData(); - if (questionnaire != null) { + if (StringUtils.isNotBlank(questionnaire)) { JsonParser jsonParser = new JsonParser(); - JsonObject json = (JsonObject) jsonParser.parse(questionnaire); + JsonObject json = (JsonObject) jsonParser.parse(questionnaire); Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId, version, componentEntity.getId(), null)); - computeEntities.stream().forEach( - computeEntity -> { - populateComputeQuestionnaire(json, computeEntity); - } - ); + populateComputeQuestionnaire(json, computeEntities); Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId, version, componentEntity.getId(), null)); - imageEntities.stream().forEach( - imageEntity -> { - populateImageQuestionnaire(json, imageEntity); - } - ); + populateImageQuestionnaire(json, imageEntities); processDiskAttribute(json, "bootDiskSizePerVM"); processDiskAttribute(json, "ephemeralDiskSizePerVM"); String questionnaireData = json.toString(); + componentEntity.setQuestionnaireData(questionnaireData); //Added to validate data in Junit + componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(), questionnaireData); } @@ -98,9 +97,9 @@ public class ComponentQuestionnaireHealer implements Healer { /** * Move Disk Atributes from genral/image/ to genral/disk in component questionnaire itself - * @param json - * @param diskAttrName - * @return + * + * @param json Component Json + * @param diskAttrName Name of disk attribute */ private void processDiskAttribute(JsonObject json, String diskAttrName) { boolean isBootDisksizePerVM = isDiskAttributePresent(json, diskAttrName); @@ -119,71 +118,86 @@ public class ComponentQuestionnaireHealer implements Healer { } private boolean isDiskAttributePresent(JsonObject json, String diskAttrName) { - return json.getAsJsonObject(GENERAL) != null && - json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) != null && - json.getAsJsonObject(GENERAL).getAsJsonObject (IMAGE).get(diskAttrName) + return json.getAsJsonObject(GENERAL) != null + && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) != null + && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).get(diskAttrName) != null; } /** * Move the required attributes from component to Image Questionnaire - * @param json - * @param imageEntity + * + * @param json Component Json + * @param imageEntities All images present in component */ - private void populateImageQuestionnaire(JsonObject json, ImageEntity imageEntity) { + private void populateImageQuestionnaire(JsonObject json, Collection<ImageEntity> imageEntities) { JsonObject general = getJsonObject(json, GENERAL); boolean isImageFormat = general != null && json .getAsJsonObject(GENERAL) - .getAsJsonObject(IMAGE) != null && json.getAsJsonObject(GENERAL).getAsJsonObject - (IMAGE).get(FORMAT) != null; + .getAsJsonObject(IMAGE) != null && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) + .get(FORMAT) != null; if (isImageFormat) { JsonObject image = getJsonObject(general, IMAGE); JsonElement jsonElement = image.get(FORMAT); JsonObject jsonObject = new JsonObject(); jsonObject.add(FORMAT, jsonElement); - imageDao.updateQuestionnaireData(imageEntity.getVspId(), imageEntity.getVersion(), imageEntity - .getComponentId(),imageEntity.getId(), jsonObject.toString()); + imageEntities.forEach(imageEntity -> imageDao.updateQuestionnaireData(imageEntity.getVspId(), + imageEntity.getVersion(), imageEntity.getComponentId(), + imageEntity.getId(), jsonObject.toString())); image.remove(FORMAT); } } - /** - * Move the required attributes from component to Compute Questionnaire - * @param json - * @param computeEntity - */ - private void populateComputeQuestionnaire(JsonObject json, ComputeEntity computeEntity) { + private void populateComputeQuestionnaire(JsonObject json, Collection<ComputeEntity> + computeEntities) { JsonObject compute = getJsonObject(json, COMPUTE); + if (compute != null) { + JsonObject vmSizing = handleVmSizing(compute); + vmSizing = handleNumOfVm(compute, vmSizing); + + if (vmSizing != null) { + JsonObject computeQuestionnaireJsonObject = new JsonObject(); + computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing); + String computeQuestionnaire = computeQuestionnaireJsonObject.toString(); + computeEntities.forEach( + computeEntity -> computeDao.updateQuestionnaireData(computeEntity.getVspId(), + computeEntity.getVersion(), computeEntity.getComponentId(), + computeEntity.getId(), computeQuestionnaire)); + } + } + } + + private JsonObject handleVmSizing(JsonObject compute) { JsonObject vmSizing = getJsonObject(compute, VM_SIZING); - if (compute != null && vmSizing != null) { + if (vmSizing != null) { JsonElement ioOperationsPerSec = vmSizing.get(IO_OP_PER_SEC); if (ioOperationsPerSec != null) { vmSizing.addProperty(COMPUTE_IO_OP_PER_SEC, ioOperationsPerSec.getAsNumber()); vmSizing.remove(IO_OP_PER_SEC); } - - JsonObject numberOfVms = getJsonObject(compute, NUM_OF_VMS); - if (numberOfVms != null ) { - JsonElement cpuRatio = numberOfVms.get(CPU_OVER_SUBSCRIPTION_RATIO); - if (cpuRatio != null ) { - vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString()); - numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO); - } - JsonElement memoryRam = numberOfVms.get(MEMORY_RAM); - if (memoryRam != null ) { - vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString()); - numberOfVms.remove(MEMORY_RAM); - } - } - - JsonObject computeQuestionnaireJsonObject = new JsonObject(); - computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing); - String computeQuestionnaire = computeQuestionnaireJsonObject.toString(); - computeDao.updateQuestionnaireData(computeEntity.getVspId(), computeEntity.getVersion(), - computeEntity.getComponentId(), computeEntity.getId(), computeQuestionnaire); compute.remove(VM_SIZING); + } + return vmSizing; + } + private JsonObject handleNumOfVm(JsonObject compute, JsonObject vmSizing) { + JsonObject numberOfVms = getJsonObject(compute, NUM_OF_VMS); + if (numberOfVms != null) { + JsonElement cpuRatio = numberOfVms.get(CPU_OVER_SUBSCRIPTION_RATIO); + JsonElement memoryRam = numberOfVms.get(MEMORY_RAM); + if (vmSizing == null && (cpuRatio != null || memoryRam != null)) { + vmSizing = new JsonObject(); + } + if (cpuRatio != null) { + vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString()); + numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO); + } + if (memoryRam != null) { + vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString()); + numberOfVms.remove(MEMORY_RAM); + } } + return vmSizing; } private JsonObject getJsonObject(JsonObject json, String name) { 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()); + } +} |