private void checkResponseTimeOut(final Exception e) { if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException) { throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, "Operation timed out, please try again."); } }
@Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException { RunDiagnosticsResponse response = new RunDiagnosticsResponse(); try { final Map<String, String> answerMap = diagnosticsService.runDiagnosticsCommand(this); if (CollectionUtils.isNotEmpty(Collections.singleton(answerMap))) { response.setStdout(answerMap.get(ApiConstants.STDOUT)); response.setStderr(answerMap.get(ApiConstants.STDERR)); response.setExitCode(answerMap.get(ApiConstants.EXITCODE)); response.setObjectName("diagnostics"); response.setResponseName(getCommandName()); this.setResponseObject(response); } } catch (final ServerApiException e) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); } } }
if (paramObj != null) { if (!parameterAnnotation.acceptedOnAdminPort()) { throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, "Parameter " + parameterAnnotation.name() + " can't be passed through the API integration port"); } catch (final ServerApiException se) { final String responseText = getSerializedApiError(se, parameterMap, responseType); writeResponse(response, responseText, se.getErrorCode().getHttpCode(), responseType, se.getDescription()); sb.append(" " + se.getErrorCode() + " " + se.getDescription()); } catch (final RuntimeException e) {
if (ex.getErrorCode() == ApiErrorCode.UNSUPPORTED_ACTION_ERROR || apiCommandParams == null || apiCommandParams.isEmpty()) { responseName = "errorresponse"; } else { apiResponse.setErrorCode(ex.getErrorCode().getHttpCode()); apiResponse.setErrorText(ex.getDescription()); apiResponse.setResponseName(responseName); final ArrayList<ExceptionProxyObject> idList = ex.getIdProxyList(); if (idList != null) { for (int i = 0; i < idList.size(); i++) { apiResponse.setCSErrorCode(ex.getCSErrorCode());
throw new ServerApiException(ApiErrorCode.UNSUPPORTED_ACTION_ERROR, "Invalid request, no command sent"); } else { s_logger.warn(errorString); auditTrailSb.append(" " + errorString); throw new ServerApiException(ApiErrorCode.UNSUPPORTED_ACTION_ERROR, errorString); throw new ServerApiException(ApiErrorCode.PARAM_ERROR, ex.getMessage(), ex); } catch (final IllegalArgumentException ex) { s_logger.info(ex.getMessage()); throw new ServerApiException(ApiErrorCode.PARAM_ERROR, ex.getMessage(), ex); } catch (final PermissionDeniedException ex) { final ArrayList<ExceptionProxyObject> idList = ex.getIdProxyList(); throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, ex.getMessage(), ex); } catch (final AccountLimitException ex) { s_logger.info(ex.getMessage()); throw new ServerApiException(ApiErrorCode.ACCOUNT_RESOURCE_LIMIT_ERROR, ex.getMessage(), ex); } catch (final InsufficientCapacityException ex) { s_logger.info(ex.getMessage()); throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, errorMsg, ex); } catch (final ResourceAllocationException ex) { s_logger.info(ex.getMessage()); throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage(), ex); } catch (final ResourceUnavailableException ex) { s_logger.info(ex.getMessage()); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, errorMsg, ex);
@Test public void testExecuteWithNullPassword() { ReflectionTestUtils.setField(createAccountCmd, "password", null); try { createAccountCmd.execute(); Assert.fail("should throw exception for a null password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUserAccount(null, null, null, null, null, null, null, accountType, roleId, domainId, null, null, null, null); }
httpResponseCode = e.getErrorCode().getHttpCode(); responseString = e.getMessage(); s_logger.debug("Authentication failure: " + e.getMessage()); resp.setHeader("X-Description", se.getDescription()); HttpUtils.writeHttpResponse(resp, serializedResponseText, se.getErrorCode().getHttpCode(), responseType, ApiServer.JSONcontentType.value()); auditTrailSb.append(" " + se.getErrorCode() + " " + se.getDescription()); } catch (final Exception ex) { s_logger.error("unknown exception writing api response", ex);
@Test public void testCreateFailure() { RegionService regionService = Mockito.mock(RegionService.class); Region region = Mockito.mock(Region.class); Mockito.when(regionService.addRegion(Matchers.anyInt(), Matchers.anyString(), Matchers.anyString())).thenReturn(null); addRegionCmd._regionService = regionService; try { addRegionCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add Region", exception.getDescription()); } }
@Test public void testZoneidListMoreThanMinusOne() throws ResourceAllocationException { try { registerTemplateCmdByAdmin = new RegisterTemplateCmdByAdmin(); registerTemplateCmdByAdmin.zoneIds = new ArrayList<>(); registerTemplateCmdByAdmin.zoneIds.add(-1L); registerTemplateCmdByAdmin.zoneIds.add(1L); registerTemplateCmdByAdmin.execute(); } catch (ServerApiException e) { if (e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Parameter zoneids cannot combine all zones (-1) option with other zones"); } } } @Test
@Test public void testExecuteWithNullPassword() { ReflectionTestUtils.setField(createUserCmd, "password", null); try { createUserCmd.execute(); Assert.fail("should throw exception for a null password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR,e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUser(null, null, null, null, null, null, null, null, null); }
@Test public void testExecuteForEmptyResult() { ResourceService resourceService = Mockito.mock(ResourceService.class); addClusterCmd._resourceService = resourceService; try { addClusterCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add cluster", exception.getDescription()); } }
@Test public void testZoneidListMoreThanMinusOne() throws ResourceAllocationException { try { registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneIds = new ArrayList<>(); registerTemplateCmd.zoneIds.add(-1L); registerTemplateCmd.zoneIds.add(1L); registerTemplateCmd.execute(); } catch (ServerApiException e) { if (e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Parameter zoneids cannot combine all zones (-1) option with other zones"); } } } @Test
@Override public void execute() throws ServerApiException { // We should never reach here throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "This is an authentication api, cannot be used directly"); }
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, ipve.getMessage()); } finally { CallContext.unregister(); } else { ServerApiException sApiEx = (ServerApiException)e; errorMsg = sApiEx.getDescription(); errorCode = sApiEx.getErrorCode().getHttpCode();
@Test public void testExecuteWithEmptyPassword() { ReflectionTestUtils.setField(createAccountCmd, "password", ""); try { createAccountCmd.execute(); Assert.fail("should throw exception for a empty password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUserAccount(null, null, null, null, null, null, null, accountType, roleId, domainId, null, null, null, null); } }
@Test public void testFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { UserVmService userVmService = Mockito.mock(UserVmService.class); updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class); Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(null); updateVmNicIpCmd._userVmService = userVmService; updateVmNicIpCmd._responseGenerator = responseGenerator; try { updateVmNicIpCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to update ip address on vm NIC. Refer to server logs for details.", exception.getDescription()); } }
@Test public void testDestZoneidAndDestZoneIdListBothPresent() throws ResourceAllocationException { try { copyTemplateCmdByAdmin = new CopyTemplateCmdByAdmin(); copyTemplateCmdByAdmin.destZoneId = -1L; copyTemplateCmdByAdmin.destZoneIds = new ArrayList<>(); copyTemplateCmdByAdmin.destZoneIds.add(-1L); copyTemplateCmdByAdmin.execute(); } catch (ServerApiException e) { if(e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Api should fail when both destzoneid and destzoneids are passed"); } } }
@Override public void execute() throws ServerApiException { // We should never reach here throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "This is an authentication api, cannot be used directly"); }
@Test public void testExecuteWithEmptyPassword() { ReflectionTestUtils.setField(createUserCmd, "password", ""); try { createUserCmd.execute(); Assert.fail("should throw exception for a empty password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR,e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUser(null, null, null, null, null, null, null, null, null); } }
@Test public void testExecuteForEmptyCfgName() { updateCfgCmd._configService = configService; try { updateCfgCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Empty configuration name provided", exception.getDescription()); } }