Search in sources :

Example 1 with DeleteTemplateCmd

use of org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd in project cloudstack by apache.

the class TemplateManagerImplTest method testForceDeleteTemplate.

@Test
public void testForceDeleteTemplate() {
    // In this Unit test all the conditions related to "force template delete flag" are tested.
    DeleteTemplateCmd cmd = mock(DeleteTemplateCmd.class);
    VMTemplateVO template = mock(VMTemplateVO.class);
    TemplateAdapter templateAdapter = mock(TemplateAdapter.class);
    TemplateProfile templateProfile = mock(TemplateProfile.class);
    List<VMInstanceVO> vmInstanceVOList = new ArrayList<VMInstanceVO>();
    List<TemplateAdapter> adapters = new ArrayList<TemplateAdapter>();
    adapters.add(templateAdapter);
    when(cmd.getId()).thenReturn(0L);
    when(_tmpltDao.findById(cmd.getId())).thenReturn(template);
    when(cmd.getZoneId()).thenReturn(null);
    when(template.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.None);
    when(template.getFormat()).thenReturn(Storage.ImageFormat.VMDK);
    templateManager.setTemplateAdapters(adapters);
    when(templateAdapter.getName()).thenReturn(TemplateAdapter.TemplateAdapterType.Hypervisor.getName().toString());
    when(templateAdapter.prepareDelete(any(DeleteTemplateCmd.class))).thenReturn(templateProfile);
    when(templateAdapter.delete(templateProfile)).thenReturn(true);
    // case 1: When Force delete flag is 'true' but VM instance VO list is empty.
    when(cmd.isForced()).thenReturn(true);
    templateManager.deleteTemplate(cmd);
    // case 2.1: When Force delete flag is 'false' and VM instance VO list is empty.
    when(cmd.isForced()).thenReturn(false);
    templateManager.deleteTemplate(cmd);
    // case 2.2: When Force delete flag is 'false' and VM instance VO list is non empty.
    when(cmd.isForced()).thenReturn(false);
    VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
    when(vmInstanceVO.getInstanceName()).thenReturn("mydDummyVM");
    vmInstanceVOList.add(vmInstanceVO);
    when(_vmInstanceDao.listNonExpungedByTemplate(anyLong())).thenReturn(vmInstanceVOList);
    try {
        templateManager.deleteTemplate(cmd);
    } catch (Exception e) {
        assertTrue("Invalid Parameter Value Exception is expected", (e instanceof InvalidParameterValueException));
    }
}
Also used : DeleteTemplateCmd(org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TemplateProfile(com.cloud.storage.TemplateProfile) Test(org.junit.Test)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 TemplateProfile (com.cloud.storage.TemplateProfile)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DeleteTemplateCmd (org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd)1 Test (org.junit.Test)1