@Override public void execute() { try { UserVm userVm = _userVmService.moveVMToUser(this); if (userVm == null) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm"); } UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", userVm).get(0); response.setResponseName(getCommandName()); setResponseObject(response); } catch (InvalidParameterValueException e){ e.printStackTrace(); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm " + e.getMessage()); } }
@Test public void testExecuteForNullResult() { updateCfgCmd._configService = configService; updateCfgCmd.setCfgName("some.cfg"); try { Mockito.when(configService.updateConfiguration(updateCfgCmd)).thenReturn(null); } catch (InvalidParameterValueException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { updateCfgCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to update config", exception.getDescription()); } }
@Test public void testExecuteForDiscoveryException() { addHostCmd._resourceService = resourceService; try { Mockito.when(resourceService.discoverHosts(addHostCmd)).thenThrow(DiscoveryException.class); } catch (InvalidParameterValueException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (DiscoveryException e) { e.printStackTrace(); } try { addHostCmd.execute(); } catch (ServerApiException exception) { Assert.assertNull(exception.getDescription()); } }
@Test public void testExecuteForNullResult() { ResourceService resourceService = Mockito.mock(ResourceService.class); addHostCmd._resourceService = resourceService; try { Mockito.when(resourceService.discoverHosts(addHostCmd)).thenReturn(null); } catch (InvalidParameterValueException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { addHostCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add host", exception.getDescription()); } }