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));
}
}
Aggregations