@Test public void createVMSuccess() 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); whenNew(VmUtils.class).withNoArguments().thenReturn(utilsMock); 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); whenNew(VmConfigSpecs.class).withNoArguments().thenReturn(configSpecsMock); when(configSpecsMock.getVmConfigSpec(any(VmInputs.class), any(ConnectionResources.class))) .thenReturn(virtualMachineConfigSpecMock); when(vimPortMock.createVMTask(any(ManagedObjectReference.class), any(VirtualMachineConfigSpec.class), any(ManagedObjectReference.class), any(ManagedObjectReference.class))).thenReturn(taskMorMock); VmService vmService = createResponseHelperForCreateAndCloneVM(folderMock, resourcePoolMock, hostMock, dataStoreMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMorMock, true)); Map<String, String> results = vmService.createVM(httpInputsMock, new VmInputs.VmInputsBuilder() .withDataCenterName("testDatacenter") .withHostname("testHostname") .withVirtualMachineName("nameToBeTested") .withDataStore("testDatastore") .withGuestOsId("testOS") .build()); 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(configSpecsMock, times(1)).getVmConfigSpec(any(VmInputs.class), any(ConnectionResources.class)); verify(vimPortMock, times(1)).createVMTask(any(ManagedObjectReference.class), any(VirtualMachineConfigSpec.class), any(ManagedObjectReference.class), any(ManagedObjectReference.class)); verify(taskMorMock, times(1)).getValue(); assertNotNull(results); assertEquals(0, Integer.parseInt(results.get("returnCode"))); assertEquals("Success: Created [nameToBeTested] VM. The taskId is: task-12345", results.get("returnResult")); }
public Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { VmUtils utils = new VmUtils(); ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null); VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources); ManagedObjectReference task = connectionResources.getVimPortType() .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor); return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" + vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(), "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM"); } 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> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { VmUtils utils = new VmUtils(); ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null); VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources); ManagedObjectReference task = connectionResources.getVimPortType() .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor); return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" + vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(), "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM"); } 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> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { VmUtils utils = new VmUtils(); ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null); VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources); ManagedObjectReference task = connectionResources.getVimPortType() .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor); return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" + vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(), "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM"); } 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> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { VmUtils utils = new VmUtils(); ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null); VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources); ManagedObjectReference task = connectionResources.getVimPortType() .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor); return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" + vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(), "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM"); } 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> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { VmUtils utils = new VmUtils(); ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources); ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null); VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources); ManagedObjectReference task = connectionResources.getVimPortType() .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor); return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" + vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(), "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM"); } 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);  }