private void validateCustomRule(NewRule newRule, DbSession dbSession, RuleKey templateKey) {
    Errors errors = new Errors();

    validateRuleKey(errors, newRule.ruleKey());
    validateName(errors, newRule);
    validateDescription(errors, newRule);

    String severity = newRule.severity();
    if (Strings.isNullOrEmpty(severity)) {
      errors.add(Message.of("coding_rules.validation.missing_severity"));
    } else if (!Severity.ALL.contains(severity)) {
      errors.add(Message.of("coding_rules.validation.invalid_severity", severity));
    }
    if (newRule.status() == null) {
      errors.add(Message.of("coding_rules.validation.missing_status"));
    }

    for (RuleParamDto ruleParam :
        dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, templateKey)) {
      try {
        validateParam(ruleParam, newRule.parameter(ruleParam.getName()));
      } catch (BadRequestException validationError) {
        errors.add(validationError.errors());
      }
    }

    if (!errors.isEmpty()) {
      throw new BadRequestException(errors);
    }
  }
  @Test
  public void fail_to_create_sub_characteristic_when_parent_id_is_not_a_root_characteristic() {
    when(dao.selectById(1, session)).thenReturn(subCharacteristicDto);

    try {
      service.create("Compilation", 1);
      fail();
    } catch (BadRequestException e) {
      assertThat(e.firstError().getKey())
          .isEqualTo("A sub characteristic can not have a sub characteristic as parent.");
    }
  }
 @Test
 public void fail_on_invalid_option() {
   try {
     validation.validate("abc", newArrayList("a", "b", "c"));
     fail();
   } catch (Exception e) {
     assertThat(e).isInstanceOf(BadRequestException.class);
     BadRequestException badRequestException = (BadRequestException) e;
     assertThat(badRequestException.l10nParams().toArray()[0]).isEqualTo("abc");
     assertThat(badRequestException.l10nParams().toArray()[1]).isEqualTo("a, b, c");
   }
 }
  @Test
  public void fail_to_create_sub_characteristic_when_name_already_used() {
    when(dao.selectByName("Compilation", session)).thenReturn(new CharacteristicDto());
    when(dao.selectById(1, session)).thenReturn(characteristicDto);

    try {
      service.create("Compilation", 1);
      fail();
    } catch (BadRequestException e) {
      assertThat(e.firstError().getKey()).isEqualTo("errors.is_already_used");
      assertThat(e.firstError().getParams()[0]).isEqualTo("Compilation");
    }
  }
  @Test
  public void fail_to_move_sub_characteristic() {
    when(dao.selectById(10, session))
        .thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setParentId(1));
    when(dao.selectEnabledRootCharacteristics(session))
        .thenReturn(
            newArrayList(
                new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2),
                new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(3)));

    try {
      service.moveDown(10);
      fail();
    } catch (BadRequestException e) {
      assertThat(e.firstError().getKey()).isEqualTo("Sub characteristics can not be moved.");
    }
    verify(dao, never()).update(any(CharacteristicDto.class), eq(session));
  }
  @Test
  public void activate_rule_diff_languages() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule = createRule("php", "toto");
    session.commit();
    ruIndexer.index();

    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();

    try {
      // 1. Activate Rule
      WsTester.TestRequest request =
          wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
      request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
      request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString());
      request.execute();
      session.clearCache();
      fail();
    } catch (BadRequestException e) {
      assertThat(e.getMessage())
          .isEqualTo("Rule blah:toto and profile pjava have different languages");
    }
  }
 private void checkOptionalSizeParameter(String value, String paramName, Integer size) {
   if (!Strings.isNullOrEmpty(value) && value.length() > size) {
     throw BadRequestException.ofL10n(Validation.IS_TOO_LONG_MESSAGE, paramName, size);
   }
 }