private void checkProject(String projectParam, Result<ActionPlan> result) { if (Strings.isNullOrEmpty(projectParam)) { result.addError(Result.Message.ofL10n(Validation.CANT_BE_EMPTY_MESSAGE, PROJECT_PARAM)); } else { ResourceDto project = resourceDao.getResource(ResourceQuery.create().setKey(projectParam)); if (project == null) { result.addError( Result.Message.ofL10n("action_plans.errors.project_does_not_exist", projectParam)); } } }
private void checkMandatorySizeParameter( String value, String paramName, Integer size, Result result) { checkMandatoryParameter(value, paramName, result); if (!Strings.isNullOrEmpty(value) && value.length() > size) { result.addError(Result.Message.ofL10n(Validation.IS_TOO_LONG_MESSAGE, paramName, size)); } }
/** Create manual issue */ public Result<DefaultIssue> create(Map<String, String> params) { Result<DefaultIssue> result = Result.of(); try { // mandatory parameters String componentKey = params.get("component"); if (StringUtils.isBlank(componentKey)) { result.addError("Component is not set"); } RuleKey ruleKey = null; String rule = params.get("rule"); if (StringUtils.isBlank(rule)) { result.addError(Result.Message.ofL10n("issue.manual.missing_rule")); } else { ruleKey = RuleKey.parse(rule); } if (result.ok()) { DefaultIssue issue = issueService.createManualIssue( componentKey, ruleKey, RubyUtils.toInteger(params.get("line")), params.get("message"), params.get("severity"), RubyUtils.toDouble(params.get("effortToFix")), UserSession.get()); result.set(issue); } } catch (Exception e) { result.addError(e.getMessage()); } return result; }
private Date checkAndReturnDeadline(String deadLineParam, Result<ActionPlan> result) { Date deadLine = null; if (!Strings.isNullOrEmpty(deadLineParam)) { try { deadLine = RubyUtils.toDate(deadLineParam); Date today = new Date(); if (deadLine != null && deadLine.before(today) && !org.apache.commons.lang.time.DateUtils.isSameDay(deadLine, today)) { result.addError(Result.Message.ofL10n("action_plans.date_cant_be_in_past")); } } catch (SonarException e) { result.addError(Result.Message.ofL10n("errors.is_not_valid", "date")); } } return deadLine; }
private Result<ActionPlan> createResultForExistingActionPlan(String actionPlanKey) { Result<ActionPlan> result = Result.of(); if (findActionPlan(actionPlanKey) == null) { result.addError( Result.Message.ofL10n( ACTION_PLANS_ERRORS_ACTION_PLAN_DOES_NOT_EXIST_MESSAGE, actionPlanKey)); } return result; }
@Test public void should_not_update_action_plan_when_action_plan_is_not_found() { when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(null); Result result = service.updateActionPlan("ABCD", null); assertThat(result.ok()).isFalse(); assertThat(result.errors()) .contains(Result.Message.ofL10n("action_plans.errors.action_plan_does_not_exist", "ABCD")); }
@Test public void should_get_error_on_action_plan_result_when_no_project() { Map<String, String> parameters = newHashMap(); parameters.put("name", "Long term"); parameters.put("description", "Long term issues"); Result result = service.createActionPlanResult(parameters); assertThat(result.ok()).isFalse(); assertThat(result.errors()).contains(Result.Message.ofL10n("errors.cant_be_empty", "project")); }
@Test public void should_get_error_on_action_plan_result_when_name_is_too_long() { Map<String, String> parameters = newHashMap(); parameters.put("name", createLongString(201)); parameters.put("description", "Long term issues"); parameters.put("project", "org.sonar.Sample"); Result result = service.createActionPlanResult(parameters); assertThat(result.ok()).isFalse(); assertThat(result.errors()).contains(Result.Message.ofL10n("errors.is_too_long", "name", 200)); }
@Test public void should_get_error_on_action_plan_result_when_dead_line_use_wrong_format() { Map<String, String> parameters = newHashMap(); parameters.put("name", "Long term"); parameters.put("description", "Long term issues"); parameters.put("project", "org.sonar.Sample"); parameters.put("deadLine", "18/05/2013"); Result result = service.createActionPlanResult(parameters); assertThat(result.ok()).isFalse(); assertThat(result.errors()).contains(Result.Message.ofL10n("errors.is_not_valid", "date")); }
@Test public void should_get_error_on_action_plan_result_when_dead_line_is_in_the_past() { Map<String, String> parameters = newHashMap(); parameters.put("name", "Long term"); parameters.put("description", "Long term issues"); parameters.put("project", "org.sonar.Sample"); parameters.put("deadLine", "2000-01-01"); Result result = service.createActionPlanResult(parameters); assertThat(result.ok()).isFalse(); assertThat(result.errors()) .contains(Result.Message.ofL10n("action_plans.date_cant_be_in_past")); }
@Test public void should_get_error_on_action_plan_result_when_name_is_already_used_for_project() { Map<String, String> parameters = newHashMap(); parameters.put("name", "Long term"); parameters.put("description", "Long term issues"); parameters.put("project", "org.sonar.Sample"); when(actionPlanService.isNameAlreadyUsedForProject(anyString(), anyString())).thenReturn(true); Result result = service.createActionPlanResult(parameters, DefaultActionPlan.create("Short term")); assertThat(result.ok()).isFalse(); assertThat(result.errors()) .contains(Result.Message.ofL10n("action_plans.same_name_in_same_project")); }
@Test public void should_get_error_on_action_plan_result_when_project_not_found() { Map<String, String> parameters = newHashMap(); parameters.put("name", "Long term"); parameters.put("description", "Long term issues"); parameters.put("project", "org.sonar.Sample"); when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(null); Result result = service.createActionPlanResult(parameters); assertThat(result.ok()).isFalse(); assertThat(result.errors()) .contains( Result.Message.ofL10n( "action_plans.errors.project_does_not_exist", "org.sonar.Sample")); }
@VisibleForTesting Result<ActionPlan> createActionPlanResult( Map<String, String> parameters, @Nullable DefaultActionPlan existingActionPlan) { Result<ActionPlan> result = Result.of(); String name = parameters.get(NAME_PARAM); String description = parameters.get(DESCRIPTION_PARAM); String deadLineParam = parameters.get("deadLine"); String projectParam = parameters.get(PROJECT_PARAM); checkMandatorySizeParameter(name, NAME_PARAM, 200, result); checkOptionalSizeParameter(description, DESCRIPTION_PARAM, 1000, result); // Can only set project on creation if (existingActionPlan == null) { checkProject(projectParam, result); } Date deadLine = checkAndReturnDeadline(deadLineParam, result); // TODO move this check in the service, on creation and update if (!Strings.isNullOrEmpty(projectParam) && !Strings.isNullOrEmpty(name) && isActionPlanNameAvailable(existingActionPlan, name, projectParam)) { result.addError(Result.Message.ofL10n("action_plans.same_name_in_same_project")); } if (result.ok()) { DefaultActionPlan actionPlan = DefaultActionPlan.create(name) .setDescription(description) .setUserLogin(UserSession.get().login()) .setDeadLine(deadLine); // Can only set project on creation if (existingActionPlan == null) { actionPlan.setProjectKey(projectParam); } else { actionPlan.setProjectKey(existingActionPlan.projectKey()); } result.set(actionPlan); } return result; }
public Result<ActionPlan> updateActionPlan(String key, Map<String, String> parameters) { DefaultActionPlan existingActionPlan = (DefaultActionPlan) actionPlanService.findByKey(key, UserSession.get()); if (existingActionPlan == null) { Result<ActionPlan> result = Result.of(); result.addError( Result.Message.ofL10n(ACTION_PLANS_ERRORS_ACTION_PLAN_DOES_NOT_EXIST_MESSAGE, key)); return result; } else { Result<ActionPlan> result = createActionPlanResult(parameters, existingActionPlan); if (result.ok()) { DefaultActionPlan actionPlan = (DefaultActionPlan) result.get(); actionPlan.setKey(existingActionPlan.key()); actionPlan.setUserLogin(existingActionPlan.userLogin()); result.set(actionPlanService.update(actionPlan, UserSession.get())); } return result; } }
private void checkMandatoryParameter(String value, String paramName, Result result) { if (Strings.isNullOrEmpty(value)) { result.addError(Result.Message.ofL10n(Validation.CANT_BE_EMPTY_MESSAGE, paramName)); } }