コード例 #1
0
  @Test
  public void fail_to_update_custom_rule_when_empty_description() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);

    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
    ruleDao.insert(dbSession, customRule);

    dbSession.commit();

    // Update custom rule
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setName("New name")
            .setMarkdownDescription("");
    try {
      updater.update(update, userSessionRule);
      fail();
    } catch (Exception e) {
      assertThat(e)
          .isInstanceOf(IllegalArgumentException.class)
          .hasMessage("The description is missing");
    }
  }
コード例 #2
0
  @Before
  public void before() {
    tester.clearDbAndIndexes();
    db = tester.get(DbClient.class);
    dbSession = db.openSession(false);
    ruleActivator = tester.get(RuleActivator.class);
    index = tester.get(ActiveRuleIndex.class);
    copier = tester.get(QProfileCopier.class);

    // create pre-defined rules
    RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
    RuleDto xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR");
    db.deprecatedRuleDao().insert(dbSession, xooRule1, xooRule2);
    db.deprecatedRuleDao()
        .insertRuleParam(
            dbSession,
            xooRule1,
            RuleParamDto.createFor(xooRule1)
                .setName("max")
                .setDefaultValue("10")
                .setType(RuleParamType.INTEGER.type()));

    // create pre-defined profile
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    dbSession.commit();
    dbSession.clearCache();
  }
コード例 #3
0
  @Test
  public void fail_to_update_custom_when_description_is_empty() {
    // Template rule
    RuleDto templateRule =
        ruleDao.insert(session, RuleTesting.newTemplateRule(RuleKey.of("java", "S001")));

    // Custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
    ruleDao.insert(session, customRule);
    session.commit();
    session.clearCache();

    WsTester.TestRequest request =
        wsTester
            .newPostRequest("api/rules", "update")
            .setParam("key", customRule.getKey().toString())
            .setParam("name", "My custom rule")
            .setParam("markdown_description", "");

    try {
      request.execute();
      fail();
    } catch (Exception e) {
      assertThat(e)
          .isInstanceOf(IllegalArgumentException.class)
          .hasMessage("The description is missing");
    }
  }
コード例 #4
0
ファイル: ListActionTest.java プロジェクト: dgageot/sonarqube
  @Test
  public void return_rules_in_protobuf() throws Exception {
    dbTester
        .getDbClient()
        .ruleDao()
        .insert(
            dbTester.getSession(),
            RuleTesting.newDto(RuleKey.of("java", "S001")).setConfigKey(null).setName(null));
    dbTester
        .getDbClient()
        .ruleDao()
        .insert(
            dbTester.getSession(),
            RuleTesting.newDto(RuleKey.of("java", "S002"))
                .setConfigKey("I002")
                .setName("Rule Two"));
    dbTester.getSession().commit();

    TestResponse response = tester.newRequest().setMediaType(MimeTypes.PROTOBUF).execute();

    assertThat(response.getMediaType()).isEqualTo(MimeTypes.PROTOBUF);
    Rules.ListResponse listResponse = Rules.ListResponse.parseFrom(response.getInputStream());
    assertThat(listResponse.getRulesCount()).isEqualTo(2);

    assertThat(listResponse.getRules(0).getKey()).isEqualTo("S001");
    assertThat(listResponse.getRules(0).getInternalKey()).isEqualTo("");
    assertThat(listResponse.getRules(0).getName()).isEqualTo("");
    assertThat(listResponse.getRules(1).getKey()).isEqualTo("S002");
    assertThat(listResponse.getRules(1).getInternalKey()).isEqualTo("I002");
    assertThat(listResponse.getRules(1).getName()).isEqualTo("Rule Two");
  }
コード例 #5
0
  @Test
  public void update_custom_rule() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex")
            .setDefaultValue(".*");
    RuleParamDto templateRuleParam2 =
        RuleParamDto.createFor(templateRule)
            .setName("format")
            .setType("STRING")
            .setDescription("Format");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule)
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.MINOR)
            .setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue(null));

    dbSession.commit();

    // Update custom rule
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity("MAJOR")
            .setStatus(RuleStatus.READY)
            .setParameters(ImmutableMap.of("regex", "b.*"));
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify custom rule is updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.name()).isEqualTo("New name");
    assertThat(customRuleReloaded.htmlDescription()).isEqualTo("New description");
    assertThat(customRuleReloaded.severity()).isEqualTo("MAJOR");
    assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.READY);
    assertThat(customRuleReloaded.params()).hasSize(2);

    assertThat(customRuleReloaded.params().get(0).defaultValue()).isEqualTo("b.*");
    assertThat(customRuleReloaded.params().get(1).defaultValue()).isNull();
  }
コード例 #6
0
  @Test
  public void count_rules_with_different_rules() {
    RulesAggregation rulesAggregation = new RulesAggregation();

    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("xoo", "S001")).setName("Rule name 1");
    rulesAggregation.add(ruleDto);
    rulesAggregation.add(ruleDto);
    rulesAggregation.add(RuleTesting.newDto(RuleKey.of("xoo", "S002")).setName("Rule name 2"));

    assertThat(rulesAggregation.rules()).hasSize(2);
  }
コード例 #7
0
 private void initRules() {
   // create pre-defined rules
   RuleDto xooRule1 = RuleTesting.newXooX1();
   RuleDto xooRule2 = RuleTesting.newXooX2();
   db.deprecatedRuleDao().insert(dbSession, xooRule1, xooRule2);
   db.deprecatedRuleDao()
       .insertRuleParam(
           dbSession,
           xooRule1,
           RuleParamDto.createFor(xooRule1)
               .setName("max")
               .setDefaultValue("10")
               .setType(RuleParamType.INTEGER.type()));
   dbSession.commit();
   dbSession.clearCache();
 }
コード例 #8
0
  @Test
  public void update_manual_rule() {
    // Create manual rule
    RuleDto manualRule =
        RuleTesting.newManualRule("My manual")
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.INFO);
    ruleDao.insert(dbSession, manualRule);

    dbSession.commit();

    // Update manual rule
    RuleUpdate update =
        RuleUpdate.createForManualRule(manualRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity(Severity.CRITICAL);
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify manual rule is updated
    Rule manualRuleReloaded = ruleIndex.getByKey(manualRule.getKey());
    assertThat(manualRuleReloaded).isNotNull();
    assertThat(manualRuleReloaded.name()).isEqualTo("New name");
    assertThat(manualRuleReloaded.htmlDescription()).isEqualTo("New description");
    assertThat(manualRuleReloaded.severity()).isEqualTo(Severity.CRITICAL);
  }
コード例 #9
0
  @Test
  public void no_changes() {
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            // the following fields are not supposed to be updated
            .setNoteData("my *note*")
            .setNoteUserLogin("me")
            .setTags(ImmutableSet.of("tag1"))
            .setSubCharacteristicId(33)
            .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
            .setRemediationCoefficient("1d")
            .setRemediationOffset("5min"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
    assertThat(update.isEmpty()).isTrue();
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isEqualTo("my *note*");
    assertThat(rule.getNoteUserLogin()).isEqualTo("me");
    assertThat(rule.getTags()).containsOnly("tag1");
    assertThat(rule.getSubCharacteristicId()).isEqualTo(33);
    assertThat(rule.getRemediationFunction())
        .isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
    assertThat(rule.getRemediationCoefficient()).isEqualTo("1d");
    assertThat(rule.getRemediationOffset()).isEqualTo("5min");
  }
コード例 #10
0
 private RuleDto createRule(String lang, String id) {
   RuleDto rule =
       RuleTesting.newDto(RuleKey.of("blah", id))
           .setLanguage(lang)
           .setSeverity(Severity.BLOCKER)
           .setStatus(RuleStatus.READY);
   db.ruleDao().insert(session, rule);
   return rule;
 }
コード例 #11
0
  @Test
  public void update_custom_rule_with_empty_parameter() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule)
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.MINOR)
            .setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam);

    dbSession.commit();

    // Update custom rule without setting a value for the parameter
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity("MAJOR")
            .setStatus(RuleStatus.READY);
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify custom rule is updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    RuleParam param = customRuleReloaded.params().get(0);
    assertThat(param.defaultValue()).isNull();
  }
コード例 #12
0
  @Test
  public void do_not_update_rule_with_removed_status() {
    ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setStatus(RuleStatus.REMOVED));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("java9"));
    try {
      updater.update(update, userSessionRule);
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Rule with REMOVED status cannot be updated: squid:S001");
    }
  }
コード例 #13
0
  @Test
  public void count_rules() {
    RulesAggregation rulesAggregation = new RulesAggregation();
    RuleKey ruleKey = RuleKey.of("xoo", "S001");
    RuleDto ruleDto = RuleTesting.newDto(ruleKey).setName("Rule name");
    rulesAggregation.add(ruleDto);
    rulesAggregation.add(ruleDto);

    RulesAggregation.Rule rule = new RulesAggregation.Rule(ruleKey, "Rule name");

    assertThat(rulesAggregation.rules()).hasSize(1);
    assertThat(rulesAggregation.rules().iterator().next().name()).isEqualTo("Rule name");
    assertThat(rulesAggregation.countRule(rule)).isEqualTo(2);
  }
コード例 #14
0
ファイル: DeleteActionTest.java プロジェクト: Godin/sonar
  private long insertNewProjectInDbAndReturnSnapshotId(int id) {
    String suffix = String.valueOf(id);
    ComponentDto project =
        ComponentTesting.newProjectDto("project-uuid-" + suffix).setKey("project-key-" + suffix);
    RuleDto rule = RuleTesting.newDto(RuleKey.of("sonarqube", "rule-" + suffix));
    dbClient.ruleDao().insert(dbSession, rule);
    IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix);
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot =
        dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newAnalysis(project));
    dbClient.issueDao().insert(dbSession, issue);
    dbSession.commit();

    return snapshot.getId();
  }
コード例 #15
0
  @Test
  public void remove_markdown_note() {
    ruleDao.insert(
        dbSession, RuleTesting.newDto(RULE_KEY).setNoteData("my *note*").setNoteUserLogin("me"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setMarkdownNote(null);
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isNull();
    assertThat(rule.getNoteUserLogin()).isNull();
    assertThat(rule.getNoteCreatedAt()).isNull();
    assertThat(rule.getNoteUpdatedAt()).isNull();
  }
コード例 #16
0
  @Test
  public void fail_to_update_manual_rule_if_status_is_set() {
    // Create manual rule
    RuleDto manualRule = RuleTesting.newManualRule("My manual");
    ruleDao.insert(dbSession, manualRule);

    dbSession.commit();

    try {
      // Update manual rule
      RuleUpdate.createForManualRule(manualRule.getKey()).setStatus(RuleStatus.BETA);
      fail();
    } catch (Exception e) {
      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not a custom rule");
    }
  }
コード例 #17
0
  @Test
  public void fail_to_update_manual_rule_if_parameters_are_set() {
    // Create manual rule
    RuleDto manualRule = RuleTesting.newManualRule("My manual");
    ruleDao.insert(dbSession, manualRule);

    dbSession.commit();

    try {
      // Update manual rule
      RuleUpdate.createForManualRule(manualRule.getKey())
          .setStatus(RuleStatus.BETA)
          .setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
      fail();
    } catch (Exception e) {
      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not a custom rule");
    }
  }
コード例 #18
0
  @Test
  public void fail_to_update_plugin_rule_if_severity_is_set() {
    // Create rule rule
    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("squid", "S01"));
    ruleDao.insert(dbSession, ruleDto);

    dbSession.commit();

    try {
      // Update rule
      RuleUpdate.createForPluginRule(ruleDto.getKey()).setSeverity(Severity.CRITICAL);
      fail();
    } catch (Exception e) {
      assertThat(e)
          .isInstanceOf(IllegalStateException.class)
          .hasMessage("Not a custom or a manual rule");
    }
  }
コード例 #19
0
  @Test
  public void override_debt() {
    insertDebtCharacteristics(dbSession);
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setDefaultSubCharacteristicId(hardReliabilityId)
            .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
            .setDefaultRemediationCoefficient("1d")
            .setDefaultRemediationOffset("5min")
            .setSubCharacteristicId(null)
            .setRemediationFunction(null)
            .setRemediationCoefficient(null)
            .setRemediationOffset(null));
    dbSession.commit();

    DefaultDebtRemediationFunction fn =
        new DefaultDebtRemediationFunction(
            DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "1min");
    RuleUpdate update =
        RuleUpdate.createForPluginRule(RULE_KEY)
            .setDebtSubCharacteristic("SOFT_RELIABILITY")
            .setDebtRemediationFunction(fn);
    updater.update(update, userSessionRule);
    dbSession.clearCache();

    // verify debt is overridden
    Rule indexedRule = tester.get(RuleIndex.class).getByKey(RULE_KEY);
    assertThat(indexedRule.debtCharacteristicKey()).isEqualTo("RELIABILITY");
    assertThat(indexedRule.debtSubCharacteristicKey()).isEqualTo("SOFT_RELIABILITY");
    assertThat(indexedRule.debtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
    assertThat(indexedRule.debtRemediationFunction().coefficient()).isNull();
    assertThat(indexedRule.debtRemediationFunction().offset()).isEqualTo("1min");

    assertThat(indexedRule.debtOverloaded()).isTrue();
    assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
    assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
    assertThat(indexedRule.defaultDebtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
    assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
    assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isEqualTo("5min");
  }
コード例 #20
0
  @Test
  public void remove_tags() {
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setTags(Sets.newHashSet("security"))
            .setSystemTags(Sets.newHashSet("java8", "javadoc")));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(null);
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getTags()).isEmpty();
    assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");

    // verify that tags are indexed in index
    Set<String> tags = tester.get(RuleService.class).listTags();
    assertThat(tags).containsOnly("java8", "javadoc");
  }
コード例 #21
0
  @Test
  public void unset_debt() {
    insertDebtCharacteristics(dbSession);
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setDefaultSubCharacteristicId(hardReliabilityId)
            .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
            .setDefaultRemediationCoefficient("1d")
            .setDefaultRemediationOffset("5min")
            .setSubCharacteristicId(softReliabilityId)
            .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
            .setRemediationCoefficient(null)
            .setRemediationOffset("1min"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setDebtSubCharacteristic(null);
    updater.update(update, userSessionRule);

    // verify db
    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getSubCharacteristicId()).isEqualTo(-1);
    assertThat(rule.getRemediationFunction()).isNull();
    assertThat(rule.getRemediationCoefficient()).isNull();
    assertThat(rule.getRemediationOffset()).isNull();

    assertThat(rule.getDefaultSubCharacteristicId()).isNotNull();
    assertThat(rule.getDefaultRemediationFunction()).isNotNull();
    assertThat(rule.getDefaultRemediationCoefficient()).isNotNull();
    assertThat(rule.getDefaultRemediationOffset()).isNotNull();

    // verify index
    Rule indexedRule = tester.get(RuleIndex.class).getByKey(RULE_KEY);
    assertThat(indexedRule.debtCharacteristicKey()).isEqualTo("NONE");
    assertThat(indexedRule.debtSubCharacteristicKey()).isEqualTo("NONE");
    assertThat(indexedRule.debtRemediationFunction()).isNull();
    assertThat(indexedRule.debtOverloaded()).isTrue();
  }
コード例 #22
0
  @Test
  public void reset_debt() {
    insertDebtCharacteristics(dbSession);
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setDefaultSubCharacteristicId(hardReliabilityId)
            .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
            .setDefaultRemediationCoefficient("1d")
            .setDefaultRemediationOffset("5min")
            .setSubCharacteristicId(softReliabilityId)
            .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
            .setRemediationCoefficient(null)
            .setRemediationOffset("1min"));
    dbSession.commit();

    RuleUpdate update =
        RuleUpdate.createForPluginRule(RULE_KEY)
            .setDebtSubCharacteristic(RuleUpdate.DEFAULT_DEBT_CHARACTERISTIC);
    updater.update(update, userSessionRule);
    dbSession.clearCache();

    // verify debt is coming from default values
    Rule indexedRule = tester.get(RuleIndex.class).getByKey(RULE_KEY);
    assertThat(indexedRule.debtCharacteristicKey()).isEqualTo("RELIABILITY");
    assertThat(indexedRule.debtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
    assertThat(indexedRule.debtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.LINEAR);
    assertThat(indexedRule.debtRemediationFunction().coefficient()).isEqualTo("1d");
    assertThat(indexedRule.debtRemediationFunction().offset()).isEqualTo("5min");

    assertThat(indexedRule.debtOverloaded()).isFalse();
    assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
    assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
    assertThat(indexedRule.defaultDebtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.LINEAR);
    assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
    assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isEqualTo("5min");
  }
コード例 #23
0
  @Test
  public void update_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule =
        ruleDao.insert(session, RuleTesting.newTemplateRule(RuleKey.of("java", "S001")));
    RuleParamDto param =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex")
            .setDefaultValue(".*");
    ruleDao.insertRuleParam(session, templateRule, param);
    session.commit();

    // Custom rule
    NewRule newRule =
        NewRule.createForCustomRule("MY_CUSTOM", templateRule.getKey())
            .setName("Old custom")
            .setHtmlDescription("Old description")
            .setSeverity(Severity.MINOR)
            .setStatus(RuleStatus.BETA)
            .setParameters(ImmutableMap.of("regex", "a"));
    RuleKey customRuleKey = ruleService.create(newRule);
    session.clearCache();

    WsTester.TestRequest request =
        wsTester
            .newPostRequest("api/rules", "update")
            .setParam("key", customRuleKey.toString())
            .setParam("name", "My custom rule")
            .setParam("markdown_description", "Description")
            .setParam("severity", "MAJOR")
            .setParam("status", "BETA")
            .setParam("params", "regex=a.*");
    request.execute().assertJson(getClass(), "update_custom_rule.json");
  }
コード例 #24
0
  @Test
  public void update_active_rule_parameters_when_updating_custom_rule() {
    // Create template rule with 3 parameters
    RuleDto templateRule =
        RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex")
            .setDefaultValue(".*");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    RuleParamDto templateRuleParam2 =
        RuleParamDto.createFor(templateRule)
            .setName("format")
            .setType("STRING")
            .setDescription("format")
            .setDefaultValue("csv");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
    RuleParamDto templateRuleParam3 =
        RuleParamDto.createFor(templateRule)
            .setName("message")
            .setType("STRING")
            .setDescription("message");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);

    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1();
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();

    // Activate the custom rule
    RuleActivation activation =
        new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
    dbSession.commit();
    dbSession.clearCache();

    // Update custom rule parameter 'regex', add 'message' and remove 'format'
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify custom rule parameters has been updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    assertThat(customRuleReloaded.params()).hasSize(3);
    assertThat(customRuleReloaded.param("regex")).isNotNull();
    assertThat(customRuleReloaded.param("regex").defaultValue()).isEqualTo("b.*");
    assertThat(customRuleReloaded.param("message")).isNotNull();
    assertThat(customRuleReloaded.param("message").defaultValue()).isEqualTo("a message");
    assertThat(customRuleReloaded.param("format")).isNotNull();
    assertThat(customRuleReloaded.param("format").defaultValue()).isNull();

    RuleParam param = customRuleReloaded.params().get(0);
    assertThat(param.defaultValue()).isEqualTo("b.*");

    // Verify active rule parameters has been updated
    ActiveRule activeRule =
        tester
            .get(ActiveRuleIndex.class)
            .getByKey(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
    assertThat(activeRule.params()).hasSize(2);
    assertThat(activeRule.params().get("regex")).isEqualTo("b.*");
    assertThat(activeRule.params().get("message")).isEqualTo("a message");
    assertThat(activeRule.params().get("format")).isNull();

    // Verify that severity has not changed
    assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
  }