@Test public void testHandleRequest__SystemException() throws Exception { Request request = createStrictMock(Request.class); ResourceInstance resource = createStrictMock(ResourceInstance.class); Query query = createMock(Query.class); Predicate predicate = createMock(Predicate.class); Renderer renderer = new DefaultRenderer(); expect(request.getResource()).andReturn(resource); expect(resource.getQuery()).andReturn(query); expect(request.getPageRequest()).andReturn(null); expect(request.getSortRequest()).andReturn(null); expect(request.getRenderer()).andReturn(renderer); expect(request.getBody()).andReturn(null); expect(request.getFields()).andReturn(Collections.<String, TemporalInfo>emptyMap()); expect(request.getQueryPredicate()).andReturn(predicate); query.setUserPredicate(predicate); query.setPageRequest(null); query.setSortRequest(null); query.setRenderer(renderer); SystemException systemException = new SystemException("testMsg", new RuntimeException()); expect(query.execute()).andThrow(systemException); replay(request, resource, query, predicate); ReadHandler handler = new ReadHandler(); Result result = handler.handleRequest(request); assertEquals(ResultStatus.STATUS.SERVER_ERROR, result.getStatus().getStatus()); assertEquals(systemException.toString(), result.getStatus().getMessage()); verify(request, resource, query, predicate); }
@Override public Result handleRequest(Request request) { Query query = request.getResource().getQuery(); query.setPageRequest(request.getPageRequest()); query.setSortRequest(request.getSortRequest()); query.setRenderer(request.getRenderer()); RequestBody body = request.getBody(); if(body != null) { query.setRequestInfoProps(body.getRequestInfoProperties()); } try { addFieldsToQuery(request, query); } catch (IllegalArgumentException e) { return new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } Result result; Predicate p = null; try { p = request.getQueryPredicate(); query.setUserPredicate(p); result = query.execute(); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); } catch (AuthorizationException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.FORBIDDEN, e.getMessage())); } catch (SystemException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.SERVER_ERROR, e)); } catch (NoSuchParentResourceException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } catch (UnsupportedPropertyException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } catch (NoSuchResourceException e) { if (p == null) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } else { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK, e)); result.getResultTree().setProperty("isCollection", "true"); } } catch (IllegalArgumentException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, "Invalid Request: " + e.getMessage())); LOG.error("Bad request: ", e); } catch (RuntimeException e) { if (LOG.isErrorEnabled()) { LOG.error("Caught a runtime exception executing a query", e); } throw e; } return result; }
ReadHandler implements RequestHandler { @Override public Result handleRequest(Request request) { Query query = request.getResource().getQuery(); query.setPageRequest(request.getPageRequest()); query.setSortRequest(request.getSortRequest()); query.setRenderer(request.getRenderer()); RequestBody body = request.getBody(); if(body != null) { query.setRequestInfoProps(body.getRequestInfoProperties()); } try { addFieldsToQuery(request, query); } catch (IllegalArgumentException e) { return new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } Result result; Predicate p = null; try { p = request.getQueryPredicate(); query.setUserPredicate(p); result = query.execute(); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); } catch (AuthorizationException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.FORBIDDEN, e.getMessage())); } catch (SystemException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.SERVER_ERROR, e)); } catch (NoSuchParentResourceException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } catch (UnsupportedPropertyException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } catch (NoSuchResourceException e) { if (p == null) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } else { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK, e)); result.getResultTree().setProperty("isCollection", "true"); } } catch (IllegalArgumentException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, "Invalid Request: " + e.getMessage())); LOG.error("Bad request: ", e); } catch (RuntimeException e) { if (LOG.isErrorEnabled()) { LOG.error("Caught a runtime exception executing a query", e); } throw e; } return result; } }
ReadHandler implements RequestHandler { @Override public Result handleRequest(Request request) { Query query = request.getResource().getQuery(); query.setPageRequest(request.getPageRequest()); query.setSortRequest(request.getSortRequest()); query.setRenderer(request.getRenderer()); RequestBody body = request.getBody(); if(body != null) { query.setRequestInfoProps(body.getRequestInfoProperties()); } try { addFieldsToQuery(request, query); } catch (IllegalArgumentException e) { return new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } Result result; Predicate p = null; try { p = request.getQueryPredicate(); query.setUserPredicate(p); result = query.execute(); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); } catch (AuthorizationException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.FORBIDDEN, e.getMessage())); } catch (SystemException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.SERVER_ERROR, e)); } catch (NoSuchParentResourceException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } catch (UnsupportedPropertyException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } catch (NoSuchResourceException e) { if (p == null) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } else { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK, e)); result.getResultTree().setProperty("isCollection", "true"); } } catch (IllegalArgumentException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, "Invalid Request: " + e.getMessage())); LOG.error("Bad request: ", e); } catch (RuntimeException e) { if (LOG.isErrorEnabled()) { LOG.error("Caught a runtime exception executing a query", e); } throw e; } return result; }  }
ReadHandler implements RequestHandler { @Override public Result handleRequest(Request request) { Query query = request.getResource().getQuery(); query.setPageRequest(request.getPageRequest()); query.setSortRequest(request.getSortRequest()); query.setRenderer(request.getRenderer()); RequestBody body = request.getBody(); if(body != null) { query.setRequestInfoProps(body.getRequestInfoProperties()); } try { addFieldsToQuery(request, query); } catch (IllegalArgumentException e) { return new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } Result result; Predicate p = null; try { p = request.getQueryPredicate(); query.setUserPredicate(p); result = query.execute(); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); } catch (AuthorizationException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.FORBIDDEN, e.getMessage())); } catch (SystemException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.SERVER_ERROR, e)); } catch (NoSuchParentResourceException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } catch (UnsupportedPropertyException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } catch (NoSuchResourceException e) { if (p == null) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } else { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK, e)); result.getResultTree().setProperty("isCollection", "true"); } } catch (IllegalArgumentException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, "Invalid Request: " + e.getMessage())); LOG.error("Bad request: ", e); } catch (RuntimeException e) { if (LOG.isErrorEnabled()) { LOG.error("Caught a runtime exception executing a query", e); } throw e; } return result; }  @Override Result handleRequest(Request request); }
ReadHandler implements RequestHandler { @Override public Result handleRequest(Request request) { Query query = request.getResource().getQuery(); query.setPageRequest(request.getPageRequest()); query.setSortRequest(request.getSortRequest()); query.setRenderer(request.getRenderer()); RequestBody body = request.getBody(); if(body != null) { query.setRequestInfoProps(body.getRequestInfoProperties()); } try { addFieldsToQuery(request, query); } catch (IllegalArgumentException e) { return new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } Result result; Predicate p = null; try { p = request.getQueryPredicate(); query.setUserPredicate(p); result = query.execute(); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); } catch (AuthorizationException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.FORBIDDEN, e.getMessage())); } catch (SystemException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.SERVER_ERROR, e)); } catch (NoSuchParentResourceException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } catch (UnsupportedPropertyException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, e.getMessage())); } catch (NoSuchResourceException e) { if (p == null) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, e.getMessage())); } else { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK, e)); result.getResultTree().setProperty("isCollection", "true"); } } catch (IllegalArgumentException e) { result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.BAD_REQUEST, "Invalid Request: " + e.getMessage())); LOG.error("Bad request: ", e); } catch (RuntimeException e) { if (LOG.isErrorEnabled()) { LOG.error("Caught a runtime exception executing a query", e); } throw e; } return result; }  @Override Result handleRequest(Request request);  }