@Test public void cloneVMNotFound() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(null); whenNew(VmUtils.class).withNoArguments().thenReturn(utilsMock); VmService vmService = createResponseHelperForCreateAndCloneVM(null, null, null, null); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMorMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("toCloneVM").withCloneName("cloneVM").build(); Map<String, String> results = vmService.cloneVM(httpInputsMock, vmInputs); verify(connectionResourcesMock).getConnection(); verify(connectionResourcesMock, never()).getVimPortType(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(vimPortMock, never()).cloneVMTask(any(ManagedObjectReference.class), any(ManagedObjectReference.class), anyString(), any(VirtualMachineCloneSpec.class)); verify(taskMorMock, never()).getValue(); verify(connectionMock).disconnect(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Could not find the [toCloneVM] VM.", 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);  }