@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();
  }
 public boolean rename(String key, String newName) {
   Verifications.check(StringUtils.isNotBlank(newName), "Name must be set");
   Verifications.check(
       newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
   DbSession dbSession = db.openSession(false);
   try {
     QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key);
     if (!StringUtils.equals(newName, profile.getName())) {
       if (db.qualityProfileDao().getByNameAndLanguage(newName, profile.getLanguage(), dbSession)
           != null) {
         throw new BadRequestException("Quality profile already exists: " + newName);
       }
       String previousName = profile.getName();
       profile.setName(newName);
       db.qualityProfileDao().update(dbSession, profile);
       db.propertiesDao()
           .updateProperties(
               PROFILE_PROPERTY_PREFIX + profile.getLanguage(), previousName, newName, dbSession);
       dbSession.commit();
       return true;
     }
     return false;
   } finally {
     dbSession.close();
   }
 }
  @Test
  public void create_target_profile_with_same_parent_than_source() {
    // two profiles : parent and its child
    db.qualityProfileDao()
        .insert(dbSession, QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));

    // parent and child with x1 activated
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    activation.setSeverity(Severity.BLOCKER);
    activation.setParameter("max", "7");
    ruleActivator.activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();

    // copy child -> profile2 is created with parent P1
    copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName());

    verifyOneActiveRule(
        QProfileTesting.XOO_P2_KEY,
        Severity.BLOCKER,
        ActiveRuleDto.INHERITED,
        ImmutableMap.of("max", "7"));
    QualityProfileDto profile2Dto =
        db.qualityProfileDao().selectByKey(dbSession, QProfileTesting.XOO_P2_KEY);
    assertThat(profile2Dto.getParentKee()).isEqualTo(QProfileTesting.XOO_P1_KEY);
  }
Example #4
0
  void setParent(
      DbSession dbSession, QualityProfileKey key, @Nullable QualityProfileKey parentKey) {
    QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key);
    if (parentKey == null) {
      // unset if parent is defined, else nothing to do
      removeParent(dbSession, profile);

    } else if (profile.getParentKey() == null || !profile.getParentKey().equals(parentKey)) {
      QualityProfileDto parentProfile =
          db.qualityProfileDao().getNonNullByKey(dbSession, parentKey);
      if (isDescendant(dbSession, profile, parentProfile)) {
        throw new BadRequestException(
            String.format(
                "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, key));
      }
      removeParent(dbSession, profile);

      // set new parent
      profile.setParent(parentKey.name());
      db.qualityProfileDao().update(dbSession, profile);
      for (ActiveRuleDto parentActiveRule :
          db.activeRuleDao().findByProfileKey(dbSession, parentKey)) {
        RuleActivation activation =
            new RuleActivation(ActiveRuleKey.of(key, parentActiveRule.getKey().ruleKey()));
        activate(dbSession, activation);
      }
    }
  }
  @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_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 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 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 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);
  }
 private void doDelete(DbSession session, QualityProfileDto profile) {
   db.activeRuleDao().deleteByProfileKey(session, profile.getKey());
   db.qualityProfileDao().delete(session, profile);
   db.propertiesDao()
       .deleteProjectProperties(
           PROFILE_PROPERTY_PREFIX + profile.getLanguage(), profile.getName(), session);
 }
  @Test
  public void update_target_profile() {
    // source with x1 activated
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    activation.setSeverity(Severity.BLOCKER);
    activation.setParameter("max", "7");
    ruleActivator.activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();

    // create target with both x1 and x2 activated
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP2());
    activation = new RuleActivation(RuleTesting.XOO_X1);
    activation.setSeverity(Severity.CRITICAL);
    activation.setParameter("max", "20");
    ruleActivator.activate(dbSession, activation, QProfileTesting.XOO_P2_KEY);
    activation = new RuleActivation(RuleTesting.XOO_X2);
    activation.setSeverity(Severity.CRITICAL);
    ruleActivator.activate(dbSession, activation, QProfileTesting.XOO_P2_KEY);
    dbSession.commit();
    dbSession.clearCache();

    // copy -> reset x1 and deactivate x2
    copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName());

    verifyOneActiveRule(
        QProfileTesting.XOO_P2_KEY, Severity.BLOCKER, null, ImmutableMap.of("max", "7"));
  }
  @Override
  public void handle(Request request, Response response) throws Exception {
    DbSession session = dbClient.openSession(false);
    try {
      String profileKey =
          QProfileIdentificationParamUtils.getProfileKeyFromParameters(
              request, profileFactory, session);
      if (dbClient.qualityProfileDao().getByKey(session, profileKey) == null) {
        throw new NotFoundException(
            String.format("Could not find a profile with key '%s'", profileKey));
      }

      QProfileActivityQuery query = new QProfileActivityQuery().setQprofileKey(profileKey);
      Date since = request.paramAsDateTime(PARAM_SINCE);
      if (since != null) {
        query.setSince(since);
      }
      Date to = request.paramAsDateTime(PARAM_TO);
      if (to != null) {
        query.setTo(to);
      }
      SearchOptions options = new SearchOptions();

      int page = request.mandatoryParamAsInt(Param.PAGE);
      options.setPage(page, request.mandatoryParamAsInt(Param.PAGE_SIZE));

      Result<QProfileActivity> result = searchActivities(query, options);
      writeResponse(
          response.newJsonWriter(),
          result,
          Paging.create(options.getLimit(), page, (int) result.getTotal()));
    } finally {
      session.close();
    }
  }
 /**
  * Session is NOT committed. Profiles marked as "default" for a language can't be deleted, except
  * if the parameter <code>force</code> is true.
  */
 void delete(DbSession session, String key, boolean force) {
   QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(session, key);
   List<QualityProfileDto> descendants = db.qualityProfileDao().findDescendants(session, key);
   if (!force) {
     QualityProfileDto defaultProfile = getDefault(session, profile.getLanguage());
     checkNotDefault(defaultProfile, profile);
     for (QualityProfileDto descendant : descendants) {
       checkNotDefault(defaultProfile, descendant);
     }
   }
   // delete bottom-up
   for (QualityProfileDto descendant : Lists.reverse(descendants)) {
     doDelete(session, descendant);
   }
   doDelete(session, profile);
 }
Example #14
0
  private List<ActiveRuleChange> cascadeDeactivation(
      ActiveRuleKey key, DbSession dbSession, boolean isCascade, boolean force) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    RuleActivatorContext context = contextFactory.create(key, dbSession);
    ActiveRuleChange change;
    if (context.activeRule() == null) {
      return changes;
    }
    if (!force && !isCascade && context.activeRule().getInheritance() != null) {
      throw new IllegalStateException("Cannot deactivate inherited rule '" + key.ruleKey() + "'");
    }
    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
    changes.add(change);
    persist(change, context, dbSession);

    // get all inherited profiles
    List<QualityProfileDto> profiles =
        db.qualityProfileDao().findByParentKey(dbSession, key.qProfile());

    for (QualityProfileDto profile : profiles) {
      ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
      changes.addAll(cascadeDeactivation(activeRuleKey, dbSession, true, force));
    }

    if (!changes.isEmpty()) {
      log.write(dbSession, Activity.Type.ACTIVE_RULE, changes);
      previewCache.reportGlobalModification();
    }

    return changes;
  }
  @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();
  }
 QualityProfileDto getOrCreate(DbSession dbSession, QProfileName name) {
   QualityProfileDto profile =
       db.qualityProfileDao().getByNameAndLanguage(name.getName(), name.getLanguage(), dbSession);
   if (profile == null) {
     profile = doCreate(dbSession, name);
   }
   return profile;
 }
 public QualityProfileDto create(DbSession dbSession, QProfileName name) {
   QualityProfileDto dto =
       db.qualityProfileDao().getByNameAndLanguage(name.getName(), name.getLanguage(), dbSession);
   if (dto != null) {
     throw new BadRequestException("Quality profile already exists: " + name);
   }
   return doCreate(dbSession, name);
 }
  @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();
  }
 private void verifyOneActiveRule(
     QProfileName profileName,
     String expectedSeverity,
     @Nullable String expectedInheritance,
     Map<String, String> expectedParams) {
   QualityProfileDto dto =
       db.qualityProfileDao()
           .selectByNameAndLanguage(profileName.getName(), profileName.getLanguage(), dbSession);
   verifyOneActiveRule(dto.getKey(), expectedSeverity, expectedInheritance, expectedParams);
 }
  @Test
  public void create() {
    QualityProfileDto dto = factory.create(dbSession, new QProfileName("xoo", "P1"));
    dbSession.commit();
    dbSession.clearCache();
    assertThat(dto.getKey()).startsWith("xoo-p1-");
    assertThat(dto.getName()).isEqualTo("P1");
    assertThat(dto.getLanguage()).isEqualTo("xoo");
    assertThat(dto.getId()).isNotNull();

    // reload the dto
    dto = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
    assertThat(dto.getLanguage()).isEqualTo("xoo");
    assertThat(dto.getName()).isEqualTo("P1");
    assertThat(dto.getKey()).startsWith("xoo-p1");
    assertThat(dto.getId()).isNotNull();
    assertThat(dto.getParentKee()).isNull();

    assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(1);
  }
  @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 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 ignore_renaming_if_same_name() {
    QualityProfileDto dto = factory.create(dbSession, new QProfileName("xoo", "P1"));
    dbSession.commit();
    dbSession.clearCache();
    String key = dto.getKey();

    assertThat(factory.rename(key, "P1")).isFalse();
    dbSession.clearCache();

    QualityProfileDto reloaded = db.qualityProfileDao().selectByKey(dbSession, dto.getKee());
    assertThat(reloaded.getKey()).isEqualTo(key);
    assertThat(reloaded.getName()).isEqualTo("P1");
  }
  private QualityProfileDto doCreate(DbSession dbSession, QProfileName name) {
    if (StringUtils.isEmpty(name.getName())) {
      throw new BadRequestException("quality_profiles.profile_name_cant_be_blank");
    }
    Date now = new Date();
    for (int i = 0; i < 20; i++) {
      String key =
          Slug.slugify(
              String.format(
                  "%s %s %s",
                  name.getLanguage(), name.getName(), RandomStringUtils.randomNumeric(5)));
      QualityProfileDto dto =
          QualityProfileDto.createFor(key)
              .setName(name.getName())
              .setLanguage(name.getLanguage())
              .setRulesUpdatedAtAsDate(now);
      if (db.qualityProfileDao().getByKey(dbSession, dto.getKey()) == null) {
        db.qualityProfileDao().insert(dbSession, dto);
        return dto;
      }
    }

    throw new IllegalStateException("Failed to create an unique quality profile for " + name);
  }
Example #25
0
 /** Does not commit */
 private void removeParent(DbSession dbSession, QualityProfileDto profileDto) {
   if (profileDto.getParent() != null) {
     profileDto.setParent(null);
     db.qualityProfileDao().update(dbSession, profileDto);
     for (ActiveRuleDto activeRule :
         db.activeRuleDao().findByProfileKey(dbSession, profileDto.getKey())) {
       if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
         deactivate(dbSession, activeRule.getKey(), true);
       } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
         activeRule.setInheritance(null);
         db.activeRuleDao().update(dbSession, activeRule);
       }
     }
   }
 }
Example #26
0
 boolean isDescendant(
     DbSession dbSession,
     QualityProfileDto childProfile,
     @Nullable QualityProfileDto parentProfile) {
   QualityProfileDto currentParent = parentProfile;
   while (currentParent != null) {
     if (childProfile.getName().equals(currentParent.getName())) {
       return true;
     }
     if (currentParent.getParent() != null) {
       currentParent = db.qualityProfileDao().getByKey(dbSession, currentParent.getParentKey());
     } else {
       currentParent = null;
     }
   }
   return false;
 }
Example #27
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 QueryOptions());
    assertThat(result.getHits()).hasSize(2);

    // 2. get all inactive rules.
    result = index.search(new RuleQuery().setActivation(false), new QueryOptions());
    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 QueryOptions());
    // TODO
    assertThat(result.getHits()).hasSize(1);

    // 4. get all active rules on profile
    result =
        index.search(
            new RuleQuery().setActivation(true).setQProfileKey(qualityProfileDto2.getKey()),
            new QueryOptions());
    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();
  }
Example #29
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);
  }
Example #30
0
  private List<ActiveRuleChange> cascadeActivation(DbSession session, RuleActivation activation) {
    List<ActiveRuleChange> changes = Lists.newArrayList();

    // get all inherited profiles
    List<QualityProfileDto> profiles =
        db.qualityProfileDao().findByParentKey(session, activation.getKey().qProfile());

    for (QualityProfileDto profile : profiles) {
      ActiveRuleKey activeRuleKey =
          ActiveRuleKey.of(profile.getKey(), activation.getKey().ruleKey());
      changes.addAll(
          this.activate(
              session,
              new RuleActivation(activeRuleKey)
                  .isCascade(true)
                  .setParameters(activation.getParameters())
                  .setSeverity(activation.getSeverity())));
    }
    return changes;
  }