DeleteAnalysisDataHandler extends RestActionHandler { @Override public void handlePost(ActionParameters params) throws ActionException { params.requireLoggedInUser(); final long id = ConversionHelper.getLong(params.getHttpParam(PARAM_ID), -1); if(id == -1) { throw new ActionParamsException("Parameter missing or non-numeric: " + PARAM_ID + "=" + params.getHttpParam(PARAM_ID)); } final Analysis analysis = analysisDataService.getAnalysisById(id); if(analysis == null) { throw new ActionParamsException("Analysis id didn't match any analysis: " + id); } if(!analysis.isOwnedBy(params.getUser().getUuid())) { throw new ActionDeniedException("Analysis belongs to another user"); } try { analysisDataService.deleteAnalysis(analysis); AuditLog.user(params.getClientIp(), params.getUser()) .withParam("id", id) .deleted(AuditLog.ResourceType.ANALYSIS); ResponseHelper.writeResponse(params, JSONHelper.createJSONObject("result", "success")); } catch (ServiceException ex) { throw new ActionException("Error deleting analysis", ex); } }  void setAnalysisDataService(final AnalysisDbService service); @Override void init(); @Override void handlePost(ActionParameters params);  }
@Test(expected = ActionDeniedException.class) public void testWithGuest() throws Exception { handler.handlePost(createActionParams()); checkDeleteNotCalled(); fail("ActionDeniedException should have been thrown"); }
@Test(expected = ActionParamsException.class) public void testWithMissingParam() throws Exception { final ActionParameters params = createActionParams(getLoggedInUser()); handler.handlePost(params); checkDeleteNotCalled(); fail("ActionParamsException should have been thrown"); }
@Test(expected = ActionParamsException.class) public void testWithInvalidParamType() throws Exception { final Map<String, String> parameters = new HashMap<String, String>(); parameters.put("id", "this should be of type long"); final ActionParameters params = createActionParams(parameters, getLoggedInUser()); handler.handlePost(params); checkDeleteNotCalled(); fail("ActionParamsException should have been thrown"); }
@Test(expected = ActionParamsException.class) public void testWithNonMatchingIdParam() throws Exception { final Map<String, String> parameters = new HashMap<String, String>(); parameters.put("id", NON_MATCHING_ID.toString()); final ActionParameters params = createActionParams(parameters, getLoggedInUser()); handler.handlePost(params); checkDeleteNotCalled(); fail("ActionParamsException should have been thrown"); }
@Test(expected = ActionDeniedException.class) public void testWithInvalidUser() throws Exception { final Map<String, String> parameters = new HashMap<String, String>(); parameters.put("id", ANOTHER_USERS_ANALYSIS_ID.toString()); final ActionParameters params = createActionParams(parameters, getLoggedInUser()); handler.handlePost(params); checkDeleteNotCalled(); fail("ActionDeniedException should have been thrown"); }
@Test public void testWithValidUser() throws Exception { final Map<String, String> parameters = new HashMap<String, String>(); parameters.put("id", VALID_ANALYSIS_ID.toString()); final ActionParameters params = createActionParams(parameters, getLoggedInUser()); handler.handlePost(params); try { verify(service, times(1)).deleteAnalysis(analysisValid); } catch (MockitoAssertionError e) { throw new MockitoAssertionError("Was expecting delete to have been called with valid analysis object"); } }