@Test public void cloneVMFailure() throws Exception { ManagedObjectReference folderMock = PowerMockito.mock(ManagedObjectReference.class); ManagedObjectReference resourcePoolMock = PowerMockito.mock(ManagedObjectReference.class); ManagedObjectReference hostMock = PowerMockito.mock(ManagedObjectReference.class); ManagedObjectReference dataStoreMock = PowerMockito.mock(ManagedObjectReference.class); VirtualMachineRelocateSpec vmRelocateSpecMock = PowerMockito.mock(VirtualMachineRelocateSpec.class); VirtualMachineCloneSpec cloneSpecMock = PowerMockito.mock(VirtualMachineCloneSpec.class); whenNew(VmUtils.class).withNoArguments().thenReturn(utilsMock); whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); VmService vmService = createResponseHelperForCreateAndCloneVM(folderMock, resourcePoolMock, hostMock, dataStoreMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMorMock, false)); when(utilsMock.getMorFolder(anyString(), any(ConnectionResources.class))).thenReturn(folderMock); when(utilsMock.getMorResourcePool(anyString(), any(ConnectionResources.class))).thenReturn(resourcePoolMock); when(utilsMock.getMorHost(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class))) .thenReturn(hostMock); when(utilsMock.getMorDataStore(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class), any(VmInputs.class))) .thenReturn(dataStoreMock); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("toCloneVM").withCloneName("cloneVM").build(); when(utilsMock.getVirtualMachineRelocateSpec(eq(resourcePoolMock), eq(hostMock), eq(dataStoreMock), eq(vmInputs))) .thenReturn(vmRelocateSpecMock); whenNew(VmConfigSpecs.class).withNoArguments().thenReturn(configSpecsMock); when(configSpecsMock.getCloneSpec(eq(vmInputs), eq(vmRelocateSpecMock))).thenReturn(cloneSpecMock); when(vimPortMock.cloneVMTask(eq(vmMorMock), eq(folderMock), eq("cloneVM"), eq(cloneSpecMock))).thenReturn(taskMorMock); Map<String, String> results = vmService.cloneVM(httpInputsMock, vmInputs); verifyConnection(); verify(utilsMock, times(1)).getMorFolder(anyString(), any(ConnectionResources.class)); verify(utilsMock, times(1)).getMorResourcePool(anyString(), any(ConnectionResources.class)); verify(utilsMock, times(1)).getMorHost(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class)); verify(utilsMock, times(1)).getMorDataStore(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class), any(VmInputs.class)); verify(utilsMock, times(1)).getVirtualMachineRelocateSpec(eq(resourcePoolMock), eq(hostMock), eq(dataStoreMock), eq(vmInputs)); verify(configSpecsMock, times(1)).getCloneSpec(eq(vmInputs), eq(vmRelocateSpecMock)); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(vimPortMock, times(1)).cloneVMTask(eq(vmMorMock), eq(folderMock), eq("cloneVM"), eq(cloneSpecMock)); verify(taskMorMock, times(1)).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Failure: The [toCloneVM] VM could not be cloned.", results.get("returnResult")); }
public Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { VmUtils utils = new VmUtils(); ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources); ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs); VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs); VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec); ManagedObjectReference taskMor = connectionResources.getVimPortType() .cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec); return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }
VmService { public Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { VmUtils utils = new VmUtils(); ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources); ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs); VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs); VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec); ManagedObjectReference taskMor = connectionResources.getVimPortType() .cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec); return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } } }
VmService { public Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { VmUtils utils = new VmUtils(); ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources); ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs); VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs); VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec); ManagedObjectReference taskMor = connectionResources.getVimPortType() .cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec); return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  }
VmService { public Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { VmUtils utils = new VmUtils(); ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources); ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs); VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs); VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec); ManagedObjectReference taskMor = connectionResources.getVimPortType() .cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec); return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  Map<String, String> getOsDescriptors(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> deleteVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOnVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOffVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> listVMsAndTemplates(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> updateVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs); }
VmService { public Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { VmUtils utils = new VmUtils(); ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources); ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs); VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs); VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec); ManagedObjectReference taskMor = connectionResources.getVimPortType() .cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec); return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  Map<String, String> getOsDescriptors(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> deleteVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOnVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOffVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> listVMsAndTemplates(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> updateVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs);  }