Package org.sonar.core.rule

Examples of org.sonar.core.rule.RuleDto


  }

  @Test
  public void delete_custom_rule() throws Exception {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo");
    dao.insert(dbSession, templateRule);

    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo");
    dao.insert(dbSession, customRule);

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

    // Activate the custom rule
    activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);

    // Delete custom rule
    deleter.delete(customRule.getKey());

    // Verify custom rule have status REMOVED
    Rule customRuleReloaded = index.getByKey(customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.REMOVED);

    // Verify there's no more active rule from custom rule
    List<ActiveRule> activeRules = tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey());
View Full Code Here


  }

  @Test
  public void delete_manual_rule() throws Exception {
    // Create manual rule
    RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule");
    dao.insert(dbSession, manualRule);

    dbSession.commit();

    // Delete manual rule
    deleter.delete(manualRule.getKey());

    // Verify custom rule have status REMOVED
    Rule result = index.getByKey(manualRule.getKey());
    assertThat(result).isNotNull();
    assertThat(result.status()).isEqualTo(RuleStatus.REMOVED);
  }
View Full Code Here

public class RuleTagHelperTest {

  @Test
  public void applyTags() throws Exception {
    RuleDto rule = new RuleDto().setTags(Sets.newHashSet("performance"));
    boolean changed = RuleTagHelper.applyTags(rule, Sets.newHashSet("java8", "security"));
    assertThat(rule.getTags()).containsOnly("java8", "security");
    assertThat(changed).isTrue();
  }
View Full Code Here

    assertThat(changed).isTrue();
  }

  @Test
  public void applyTags_remove_all_existing_tags() throws Exception {
    RuleDto rule = new RuleDto().setTags(Sets.newHashSet("performance"));
    boolean changed = RuleTagHelper.applyTags(rule, Collections.<String>emptySet());
    assertThat(rule.getTags()).isEmpty();
    assertThat(changed).isTrue();
  }
View Full Code Here

    assertThat(changed).isTrue();
  }

  @Test
  public void applyTags_no_changes() throws Exception {
    RuleDto rule = new RuleDto().setTags(Sets.newHashSet("performance"));
    boolean changed = RuleTagHelper.applyTags(rule, Sets.newHashSet("performance"));
    assertThat(rule.getTags()).containsOnly("performance");
    assertThat(changed).isFalse();
  }
View Full Code Here

    assertThat(changed).isFalse();
  }

  @Test
  public void applyTags_validate_format() throws Exception {
    RuleDto rule = new RuleDto();
    boolean changed = RuleTagHelper.applyTags(rule, Sets.newHashSet("java8", "security"));
    assertThat(rule.getTags()).containsOnly("java8", "security");
    assertThat(changed).isTrue();

    try {
      RuleTagHelper.applyTags(rule, Sets.newHashSet("Java Eight"));
      fail();
View Full Code Here

    }
  }

  @Test
  public void applyTags_do_not_duplicate_system_tags() throws Exception {
    RuleDto rule = new RuleDto()
      .setTags(Sets.newHashSet("performance"))
      .setSystemTags(Sets.newHashSet("security"));

    boolean changed = RuleTagHelper.applyTags(rule, Sets.newHashSet("java8", "security"));

    assertThat(changed).isTrue();
    assertThat(rule.getTags()).containsOnly("java8");
    assertThat(rule.getSystemTags()).containsOnly("security");
  }
View Full Code Here

    setupData("selectAll");
    List<RuleDto> ruleDtos = dao.findAll(session);

    assertThat(ruleDtos).hasSize(1);

    RuleDto ruleDto = ruleDtos.get(0);
    assertThat(ruleDto.getId()).isEqualTo(1);
    assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
    assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
    assertThat(ruleDto.getDescriptionFormat()).isEqualTo(Format.HTML);
    assertThat(ruleDto.getStatus()).isEqualTo(RuleStatus.READY);
    assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
    assertThat(ruleDto.getNoteData()).isEqualTo("Rule note with accents \u00e9\u00e8\u00e0");
    assertThat(ruleDto.getSubCharacteristicId()).isEqualTo(100);
    assertThat(ruleDto.getDefaultSubCharacteristicId()).isEqualTo(101);
    assertThat(ruleDto.getRemediationFunction()).isEqualTo("linear");
    assertThat(ruleDto.getDefaultRemediationFunction()).isEqualTo("linear_offset");
    assertThat(ruleDto.getRemediationCoefficient()).isEqualTo("1h");
    assertThat(ruleDto.getDefaultRemediationCoefficient()).isEqualTo("5d");
    assertThat(ruleDto.getRemediationOffset()).isEqualTo("5min");
    assertThat(ruleDto.getDefaultRemediationOffset()).isEqualTo("10h");
    assertThat(ruleDto.getEffortToFixDescription()).isEqualTo("squid.S115.effortToFix");
  }
View Full Code Here

  }

  public void delete(RuleKey ruleKey) {
    DbSession dbSession = dbClient.openSession(false);
    try {
      RuleDto rule = dbClient.ruleDao().getByKey(dbSession, ruleKey);
      if (rule.getTemplateId() == null && !rule.getRepositoryKey().equals(RuleDoc.MANUAL_REPOSITORY)) {
        throw new IllegalStateException("Only custom rules and manual rules can be deleted");
      }

      // For custom rule, first deactivate the rule on all profiles
      if (rule.getTemplateId() != null) {
        ruleActivator.deactivate(dbSession, rule);
      }

      rule.setStatus(RuleStatus.REMOVED);
      dbClient.ruleDao().update(dbSession, rule);

      dbSession.commit();
    } finally {
      dbSession.close();
View Full Code Here

  private RuleKey createCustomRule(NewRule newRule, DbSession dbSession){
    RuleKey templateKey = newRule.templateKey();
    if (templateKey == null) {
      throw new IllegalArgumentException("Rule template key should not be null");
    }
    RuleDto templateRule = dbClient.ruleDao().getByKey(dbSession, templateKey);
    if (!templateRule.isTemplate()) {
      throw new IllegalArgumentException("This rule is not a template rule: " + templateKey.toString());
    }
    validateCustomRule(newRule);

    RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());

    RuleDto existingRule = loadRule(customRuleKey, dbSession);
    if (existingRule != null) {
      updateExistingRule(existingRule, newRule, dbSession);
    } else {
      createCustomRule(customRuleKey, newRule, templateRule, dbSession);
    }
View Full Code Here

TOP

Related Classes of org.sonar.core.rule.RuleDto

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.