@Test
  public void change_project_association_with_key_and_uuid() throws Exception {
    ComponentDto project = ComponentTesting.newProjectDto("ABCD").setId(1L);
    db.componentDao().insert(session, project);
    QualityProfileDto profile1 = QProfileTesting.newXooP1();
    QualityProfileDto profile2 = QProfileTesting.newXooP2();
    db.qualityProfileDao().insert(session, profile1, profile2);
    db.qualityProfileDao()
        .insertProjectProfileAssociation(project.uuid(), profile1.getKey(), session);

    session.commit();

    wsTester
        .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
        .setParam("profileKey", profile2.getKee())
        .setParam("projectUuid", project.uuid())
        .execute()
        .assertNoContent();
    assertThat(
            tester
                .get(QProfileFactory.class)
                .getByProjectAndLanguage(session, project.getKey(), "xoo")
                .getKee())
        .isEqualTo(profile2.getKee());
  }
  @Test
  public void delete_descendants() {
    initRules();

    // create parent and child profiles
    db.qualityProfileDao()
        .insert(
            dbSession,
            QProfileTesting.newXooP1(),
            QProfileTesting.newXooP2(),
            QProfileTesting.newXooP3());
    tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
    tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY);
    tester
        .get(RuleActivator.class)
        .activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();
    assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3);
    assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3);

    factory.delete(XOO_P1_KEY);

    dbSession.clearCache();
    assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty();
    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P2_KEY)).isEmpty();
    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P3_KEY)).isEmpty();
  }
  @Test
  public void reset() throws Exception {
    QualityProfileDto profile = QProfileTesting.newXooP1();
    QualityProfileDto subProfile =
        QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY);
    db.qualityProfileDao().insert(session, profile, subProfile);

    RuleDto rule = createRule(profile.getLanguage(), "rule");
    ActiveRuleDto active1 =
        ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule).setSeverity("MINOR");
    db.activeRuleDao().insert(session, active1);
    db.activeRuleDao().insert(session, active2);

    session.commit();
    ruIndexer.index();
    activeRuIndexer.index();

    // 0. assert rule child rule is minor
    assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString())
        .isEqualTo("MINOR");

    // 1. reset child rule
    WsTester.TestRequest request =
        wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
    request.setParam("profile_key", subProfile.getKey());
    request.setParam("rule_key", rule.getKey().toString());
    request.setParam("reset", "true");
    request.execute();
    session.clearCache();

    // 2. assert rule child rule is NOT minor
    assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString())
        .isNotEqualTo("MINOR");
  }
  @Test
  public void deactivate_removed_rules_only_if_repository_still_exists() throws Exception {
    MockUserSession.set()
        .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
        .setLogin("me");

    // create a profile and activate rule
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    tester.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
    dbSession.clearCache();

    // restart, x2 still exists -> deactivate x1
    rulesDefinition.includeX1 = false;
    rulesDefinition.includeX2 = true;
    tester.get(Platform.class).executeStartupTasks();
    dbSession.clearCache();
    assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x1")).getStatus())
        .isEqualTo(RuleStatus.REMOVED);
    assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2")).getStatus())
        .isEqualTo(RuleStatus.READY);
    assertThat(db.activeRuleDao().findByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY))
        .hasSize(0);
  }
  @Test
  public void do_not_deactivate_removed_rules_if_repository_accidentaly_uninstalled()
      throws Exception {
    MockUserSession.set()
        .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
        .setLogin("me");

    // create a profile and activate rule
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    tester.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
    dbSession.clearCache();

    // restart without x1, x2, template1 -> keep active rule of x1
    rulesDefinition.includeX1 = false;
    rulesDefinition.includeX2 = false;
    rulesDefinition.includeTemplate1 = false;
    tester.get(Platform.class).executeStartupTasks();
    dbSession.clearCache();
    assertThat(db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1).getStatus())
        .isEqualTo(RuleStatus.REMOVED);
    assertThat(db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X2).getStatus())
        .isEqualTo(RuleStatus.REMOVED);
    assertThat(db.activeRuleDao().findByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY))
        .hasSize(1);
  }
  @Test
  public void get_profile_by_project_and_language() {
    ComponentDto project =
        new ComponentDto()
            .setId(1L)
            .setUuid("ABCD")
            .setKey("org.codehaus.sonar:sonar")
            .setName("SonarQube")
            .setLongName("SonarQube")
            .setQualifier("TRK")
            .setScope("TRK")
            .setEnabled(true);
    db.componentDao().insert(dbSession, project);

    QualityProfileDto profileDto = QProfileTesting.newXooP1();
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    dbSession.clearCache();
    assertThat(factory.getByProjectAndLanguage("org.codehaus.sonar:sonar", "xoo")).isNull();

    tester
        .get(QProfileProjectOperations.class)
        .addProject(
            profileDto.getKey(),
            project.uuid(),
            new MockUserSession("me").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN),
            dbSession);
    dbSession.commit();
    dbSession.clearCache();
    assertThat(factory.getByProjectAndLanguage("org.codehaus.sonar:sonar", "xoo").getKey())
        .isEqualTo(XOO_P1_KEY);
  }
Exemplo n.º 7
0
  @Test
  public void search_by_profile() throws InterruptedException {
    QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
    QualityProfileDto qualityProfileDto2 = QProfileTesting.newXooP2();
    db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);

    RuleDto rule1 = RuleTesting.newXooX1();
    RuleDto rule2 = RuleTesting.newXooX2();
    RuleDto rule3 = RuleTesting.newXooX3();
    dao.insert(dbSession, rule1, rule2, rule3);

    db.activeRuleDao()
        .insert(
            dbSession,
            ActiveRuleDto.createFor(qualityProfileDto1, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto2, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule2).setSeverity("BLOCKER"));
    dbSession.commit();
    dbSession.clearCache();

    // 1. get all active rules.
    Result<Rule> result = index.search(new RuleQuery().setActivation(true), new QueryContext());
    assertThat(result.getHits()).hasSize(2);

    // 2. get all inactive rules.
    result = index.search(new RuleQuery().setActivation(false), new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule3.getName());

    // 3. get all rules not active on profile
    index.search(
        new RuleQuery().setActivation(false).setQProfileKey(qualityProfileDto2.getKey()),
        new QueryContext());
    // TODO
    assertThat(result.getHits()).hasSize(1);

    // 4. get all active rules on profile
    result =
        index.search(
            new RuleQuery().setActivation(true).setQProfileKey(qualityProfileDto2.getKey()),
            new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule1.getName());
  }
  @Before
  public void before() {
    tester.clearDbAndIndexes();
    db = tester.get(DbClient.class);
    dbSession = db.openSession(false);
    service = tester.get(QProfileService.class);
    loader = tester.get(QProfileLoader.class);
    activator = tester.get(RuleActivator.class);

    // create pre-defined rules
    RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
    db.ruleDao().insert(dbSession, xooRule1);

    // create pre-defined profiles P1 and P2
    db.qualityProfileDao()
        .insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2());
    dbSession.commit();
    dbSession.clearCache();
  }
  @Test
  public void get_profile_by_name_and_language() {
    QualityProfileDto profileDto =
        QProfileTesting.newQProfileDto(new QProfileName("xoo", "SonarQube way"), "abcd");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    dbSession.clearCache();

    assertThat(factory.getByNameAndLanguage("SonarQube way", "xoo").getKey()).isEqualTo("abcd");
    assertThat(factory.getByNameAndLanguage("SonarQube way", "java")).isNull();
    assertThat(factory.getByNameAndLanguage("Unfound", "xoo")).isNull();
  }
  @Test
  public void set_default_profile() {
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    dbSession.commit();
    dbSession.clearCache();

    assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P1_KEY).isDefault()).isFalse();

    factory.setDefault(XOO_P1_KEY);
    dbSession.clearCache();

    assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P1_KEY).isDefault()).isTrue();
  }
  @Test
  public void do_not_delete_if_default_descendant() {
    db.qualityProfileDao()
        .insert(
            dbSession,
            QProfileTesting.newXooP1(),
            QProfileTesting.newXooP2(),
            QProfileTesting.newXooP3());
    tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
    tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY);
    factory.setDefault(dbSession, XOO_P3_KEY);
    dbSession.commit();
    dbSession.clearCache();

    try {
      factory.delete(XOO_P1_KEY);
      fail();
    } catch (BadRequestException e) {
      assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P3");
      assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3);
    }
  }
  @Test
  public void do_not_delete_default_profile() {
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    factory.setDefault(dbSession, XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();

    try {
      factory.delete(XOO_P1_KEY);
      fail();
    } catch (BadRequestException e) {
      assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P1");
      assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(1);
    }
  }
  @Test
  public void delete() {
    initRules();
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
    tester
        .get(RuleActivator.class)
        .activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();

    factory.delete(XOO_P1_KEY);

    dbSession.clearCache();
    assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty();
  }
Exemplo n.º 14
0
  @Test
  public void complex_param_value() throws InterruptedException {
    String value =
        "//expression[primary/qualifiedIdentifier[count(IDENTIFIER) = 2]/IDENTIFIER[2]/@tokenValue = 'firstOf' and primary/identifierSuffix/arguments/expression[not(primary) or primary[not(qualifiedIdentifier) or identifierSuffix]]]";

    QualityProfileDto profile = QProfileTesting.newXooP1();
    db.qualityProfileDao().insert(dbSession, profile);

    RuleDto rule = RuleTesting.newXooX1();
    dao.insert(dbSession, rule);

    RuleParamDto param =
        RuleParamDto.createFor(rule).setName("testing").setType("STRING").setDefaultValue(value);
    dao.addRuleParam(dbSession, rule, param);

    dbSession.commit();

    assertThat(index.getByKey(rule.getKey()).params()).hasSize(1);
    assertThat(index.getByKey(rule.getKey()).params().get(0).defaultValue()).isEqualTo(value);
  }
Exemplo n.º 15
0
  @Test
  public void add_project_with_name_language_and_key() throws Exception {
    ComponentDto project = ComponentTesting.newProjectDto("ABCD").setId(1L);
    db.componentDao().insert(session, project);
    QualityProfileDto profile = QProfileTesting.newXooP1();
    db.qualityProfileDao().insert(session, profile);

    session.commit();

    wsTester
        .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
        .setParam("language", "xoo")
        .setParam("profileName", profile.getName())
        .setParam("projectKey", project.getKey())
        .execute()
        .assertNoContent();
    assertThat(
            tester
                .get(QProfileFactory.class)
                .getByProjectAndLanguage(session, project.getKey(), "xoo")
                .getKee())
        .isEqualTo(profile.getKee());
  }
Exemplo n.º 16
0
 private QualityProfileDto createProfile(String lang) {
   QualityProfileDto profile =
       QProfileTesting.newQProfileDto(new QProfileName(lang, "P" + lang), "p" + lang);
   db.qualityProfileDao().insert(session, profile);
   return profile;
 }
Exemplo n.º 17
0
  @Test
  public void search_by_profile_and_inheritance() throws InterruptedException {
    QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
    QualityProfileDto qualityProfileDto2 =
        QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY);
    db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);

    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("xoo", "S001"));
    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("xoo", "S002"));
    RuleDto rule3 = RuleTesting.newDto(RuleKey.of("xoo", "S003"));
    RuleDto rule4 = RuleTesting.newDto(RuleKey.of("xoo", "S004"));
    dao.insert(dbSession, rule1, rule2, rule3, rule4);

    db.activeRuleDao()
        .insert(
            dbSession,
            ActiveRuleDto.createFor(qualityProfileDto1, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule2).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule3).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto2, rule1)
                .setSeverity("MINOR")
                .setInheritance(ActiveRule.Inheritance.INHERITED.name()),
            ActiveRuleDto.createFor(qualityProfileDto2, rule2)
                .setSeverity("BLOCKER")
                .setInheritance(ActiveRule.Inheritance.OVERRIDES.name()),
            ActiveRuleDto.createFor(qualityProfileDto2, rule3)
                .setSeverity("BLOCKER")
                .setInheritance(ActiveRule.Inheritance.INHERITED.name()));

    dbSession.commit();

    // 0. get all rules
    Result<Rule> result = index.search(new RuleQuery(), new QueryContext());
    assertThat(result.getHits()).hasSize(4);

    // 1. get all active rules
    result = index.search(new RuleQuery().setActivation(true), new QueryContext());
    assertThat(result.getHits()).hasSize(3);

    // 2. get all inactive rules.
    result = index.search(new RuleQuery().setActivation(false), new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule4.getName());

    // 3. get Inherited Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 4. get Inherited Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(2);

    // 5. get Overridden Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 6. get Overridden Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(1);

    // 7. get Inherited AND Overridden Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(
                    ImmutableSet.of(
                        ActiveRule.Inheritance.INHERITED.name(),
                        ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 8. get Inherited AND Overridden Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(
                    ImmutableSet.of(
                        ActiveRule.Inheritance.INHERITED.name(),
                        ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(3);
  }
Exemplo n.º 18
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);
  }