@Test public void updateVMNotFound() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(null); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMorMock, false)); PowerMockito.doNothing().when(virtualMachineConfigSpecMock).setMemoryAllocation(any(ResourceAllocationInfo.class)); when(vimPortMock.reconfigVMTask(any(ManagedObjectReference.class), any(VirtualMachineConfigSpec.class))) .thenReturn(taskMorMock); Map<String, String> results = new VmService().updateVM(httpInputsMock, new VmInputs.VmInputsBuilder() .withVirtualMachineName("testVM").withOperation("update").withDevice("memory").withUpdateValue("low").build()); verify(connectionResourcesMock).getConnection(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(vimPortMock, never()).reconfigVMTask(any(ManagedObjectReference.class), any(VirtualMachineConfigSpec.class)); verify(connectionMock).disconnect(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Could not find the [testVM] VM.", results.get("returnResult")); }
public Map<String, String> updateVM(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) { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); String device = Device.getValue(vmInputs.getDevice()).toLowerCase(); if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) { vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device); } else { vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device); } ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec); return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured."); } 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> updateVM(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) { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); String device = Device.getValue(vmInputs.getDevice()).toLowerCase(); if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) { vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device); } else { vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device); } ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec); return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured."); } 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> updateVM(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) { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); String device = Device.getValue(vmInputs.getDevice()).toLowerCase(); if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) { vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device); } else { vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device); } ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec); return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured."); } 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> updateVM(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) { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); String device = Device.getValue(vmInputs.getDevice()).toLowerCase(); if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) { vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device); } else { vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device); } ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec); return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured."); } 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> updateVM(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) { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); String device = Device.getValue(vmInputs.getDevice()).toLowerCase(); if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) { vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device); } else { vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device); } ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec); return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured."); } 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);  }