private void restoreRules( List<CharacteristicDto> allCharacteristicDtos, List<RuleDto> rules, List<RuleDebt> ruleDebts, ValidationMessages validationMessages, Date updateDate, DbSession session) { for (RuleDto rule : rules) { RuleDebt ruleDebt = ruleDebt(rule.getRepositoryKey(), rule.getRuleKey(), ruleDebts); String subCharacteristicKey = ruleDebt != null ? ruleDebt.subCharacteristicKey() : null; CharacteristicDto subCharacteristicDto = subCharacteristicKey != null ? characteristicByKey(ruleDebt.subCharacteristicKey(), allCharacteristicDtos, true) : null; ruleOperations.updateRule( rule, subCharacteristicDto, ruleDebt != null ? ruleDebt.function() : null, ruleDebt != null ? ruleDebt.coefficient() : null, ruleDebt != null ? ruleDebt.offset() : null, session); rule.setUpdatedAt(updateDate); ruleDebts.remove(ruleDebt); } for (RuleDebt ruleDebt : ruleDebts) { validationMessages.addWarningText( String.format("The rule '%s' does not exist.", ruleDebt.ruleKey())); } }
@Test public void search_by_template_key() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); dao.insert(dbSession, templateRule); dao.insert( dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId())); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // Only custom rule query = new RuleQuery().setTemplateKey("java:S001"); results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(1); assertThat(Iterables.getFirst(results.getHits(), null).key().rule()) .isEqualTo("S001_MY_CUSTOM"); assertThat(Iterables.getFirst(results.getHits(), null).templateKey()) .isEqualTo(RuleKey.of("java", "S001")); // null => no filter query = new RuleQuery().setTemplateKey(null); assertThat(index.search(query, new QueryContext()).getHits()).hasSize(2); }
@Test public void remove_debt_rule() throws Exception { verifyRulesInDb(); // Set some default debt on x2 rule, which has no debt provided by th plugin RuleDto ruleDto = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X2); db.ruleDao() .update( dbSession, ruleDto .setDefaultSubCharacteristicId( db.debtCharacteristicDao() .selectByKey( RulesDefinition.SubCharacteristics.INTEGRATION_TESTABILITY, dbSession) .getId()) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("2h") .setDefaultRemediationOffset("35min")); dbSession.commit(); dbSession.clearCache(); // Re-execute startup tasks tester.get(Platform.class).executeStartupTasks(); // Verify default debt has been removed Rule ruleReloaded = index.getByKey(RuleTesting.XOO_X2); assertThat(ruleReloaded.debtSubCharacteristicKey()).isNull(); assertThat(ruleReloaded.debtRemediationFunction()).isNull(); }
@Test public void search_by_template_key_with_params() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule) .setName("regex") .setType("STRING") .setDescription("Reg ex") .setDefaultValue(".*"); dao.insert(dbSession, templateRule); dao.addRuleParam(dbSession, templateRule, ruleParamDto); RuleDto customRule = RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId()); RuleParamDto customRuleParam = RuleParamDto.createFor(customRule) .setName("regex") .setType("STRING") .setDescription("Reg ex") .setDefaultValue("a.*"); dao.insert(dbSession, customRule); dao.addRuleParam(dbSession, customRule, customRuleParam); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // get params assertThat(index.getByKey(templateRule.getKey()).params()).hasSize(1); assertThat(index.getByKey(customRule.getKey()).params()).hasSize(1); }
@Test public void update_debt_rule() throws Exception { verifyRulesInDb(); // Update x1 rule RuleDto ruleDto = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); db.ruleDao() .update( dbSession, ruleDto .setDefaultSubCharacteristicId(123456) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("2h") .setDefaultRemediationOffset("35min")); dbSession.commit(); dbSession.clearCache(); // Re-execute startup tasks tester.get(Platform.class).executeStartupTasks(); // Verify default debt has been reset to plugin definition Rule ruleReloaded = index.getByKey(RuleTesting.XOO_X1); assertThat(ruleReloaded.debtSubCharacteristicKey()) .isEqualTo(RulesDefinition.SubCharacteristics.INTEGRATION_TESTABILITY); assertThat(ruleReloaded.debtRemediationFunction().type()) .isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET); assertThat(ruleReloaded.debtRemediationFunction().coefficient()).isEqualTo("1h"); assertThat(ruleReloaded.debtRemediationFunction().offset()).isEqualTo("30min"); }
private String backupFromLanguage(@Nullable String languageKey) { checkPermission(); DbSession session = dbClient.openSession(false); try { DebtModel debtModel = new DebtModel(); List<CharacteristicDto> characteristicDtos = dbClient.debtCharacteristicDao().selectEnabledCharacteristics(session); for (CharacteristicDto characteristicDto : characteristicDtos) { if (characteristicDto.getParentId() == null) { debtModel.addRootCharacteristic(toDebtCharacteristic(characteristicDto)); for (CharacteristicDto sub : subCharacteristics(characteristicDto.getId(), characteristicDtos)) { debtModel.addSubCharacteristic(toDebtCharacteristic(sub), characteristicDto.getKey()); } } } List<RuleDebt> rules = newArrayList(); for (RuleDto rule : dbClient.ruleDao().selectEnabledAndNonManual(session)) { if (languageKey == null || languageKey.equals(rule.getLanguage())) { RuleDebt ruleDebt = toRuleDebt(rule, debtModel); if (ruleDebt != null) { rules.add(ruleDebt); } } } return debtModelXMLExporter.export(debtModel, rules); } finally { MyBatis.closeQuietly(session); } }
@Test public void do_not_copy_rule_tags_on_existing_issues() { inputIssue.setIsNew(false); rule.setTags(ImmutableSet.of("bug", "performance")); rule.setSystemTags(ImmutableSet.of("blocker")); process(); assertThat(Iterators.getOnlyElement(outputIssues.traverse()).tags()).isEmpty(); }
@Test public void copy_rule_tags_on_new_issues() { inputIssue.setIsNew(true); rule.setTags(ImmutableSet.of("bug", "performance")); rule.setSystemTags(ImmutableSet.of("blocker")); process(); assertThat(Iterators.getOnlyElement(outputIssues.traverse()).tags()) .containsOnly("blocker", "bug", "performance"); }
@Test public void mark_rule_as_removed() throws Exception { verifyRulesInDb(); rulesDefinition.includeX2 = false; tester.get(Platform.class).executeStartupTasks(); verifyRulesInDb(); RuleDto rule = db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2")); assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED); }
@Test public void scroll_byIds() throws Exception { Set<Integer> ids = new HashSet<Integer>(); for (int i = 0; i < 150; i++) { RuleDto rule = RuleTesting.newDto(RuleKey.of("scroll", "r_" + i)); dao.insert(dbSession, rule); dbSession.commit(); ids.add(rule.getId()); } List<Rule> rules = index.getByIds(ids); assertThat(rules).hasSize(ids.size()); }
@Override public boolean equals(Object obj) { if (!(obj instanceof RuleDto)) { return false; } if (this == obj) { return true; } RuleDto other = (RuleDto) obj; return new EqualsBuilder() .append(repositoryKey, other.getRepositoryKey()) .append(ruleKey, other.getRuleKey()) .isEquals(); }
private void updateExistingRule(RuleDto ruleDto, NewRule newRule, DbSession dbSession) { if (ruleDto.getStatus().equals(RuleStatus.REMOVED)) { if (newRule.isPreventReactivation()) { throw new ReactivationException( String.format( "A removed rule with the key '%s' already exists", ruleDto.getKey().rule()), ruleDto.getKey()); } else { ruleDto.setStatus(RuleStatus.READY); dbClient.ruleDao().update(dbSession, ruleDto); } } else { throw new IllegalArgumentException( String.format("A rule with the key '%s' already exists", ruleDto.getKey().rule())); } }
@CheckForNull String requestParamValue(RuleActivation request, String key) { if (rule.getTemplateId() != null) { return null; } return request.getParameters().get(key); }
@Test public void search_qprofile_activity() throws InterruptedException { MockUserSession.set() .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN) .setLogin("david"); UserDto user = new UserDto() .setLogin("david") .setName("David") .setEmail("*****@*****.**") .setCreatedAt(new Date()) .setUpdatedAt(new Date()); db.userDao().insert(dbSession, user); // We need an actual rule in DB to test RuleName in Activity RuleDto rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); tester .get(ActivityService.class) .write( dbSession, Activity.Type.QPROFILE, ActiveRuleChange.createFor( ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) .setSeverity(Severity.MAJOR) .setParameter("max", "10")); dbSession.commit(); Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new QueryContext()); assertThat(activities.getHits()).hasSize(1); QProfileActivity activity = activities.getHits().get(0); assertThat(activity.type()).isEqualTo(Activity.Type.QPROFILE); assertThat(activity.action()).isEqualTo(ActiveRuleChange.Type.ACTIVATED.name()); assertThat(activity.ruleKey()).isEqualTo(RuleTesting.XOO_X1); assertThat(activity.profileKey()).isEqualTo(XOO_P1_KEY); assertThat(activity.severity()).isEqualTo(Severity.MAJOR); assertThat(activity.ruleName()).isEqualTo(rule.getName()); assertThat(activity.login()).isEqualTo("david"); assertThat(activity.authorName()).isEqualTo("David"); assertThat(activity.parameters()).hasSize(1); assertThat(activity.parameters().get("max")).isEqualTo("10"); }
@Test public void delete_sub_characteristic() { DbSession batchSession = mock(DbSession.class); when(dbClient.openSession(true)).thenReturn(batchSession); when(ruleDao.findRulesByDebtSubCharacteristicId(batchSession, 2)) .thenReturn( newArrayList( new RuleDto() .setSubCharacteristicId(2) .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setRemediationCoefficient("2h") .setRemediationOffset("5min") .setDefaultSubCharacteristicId(10) .setDefaultRemediationFunction( DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setDefaultRemediationCoefficient("4h") .setDefaultRemediationOffset("15min"))); when(dao.selectById(2, batchSession)).thenReturn(subCharacteristicDto); service.delete(2); verify(ruleDao).update(eq(batchSession), ruleCaptor.capture()); RuleDto ruleDto = ruleCaptor.getValue(); assertThat(ruleDto.getUpdatedAt()).isEqualTo(now); // Overridden debt data are disabled assertThat(ruleDto.getSubCharacteristicId()).isEqualTo(-1); assertThat(ruleDto.getRemediationFunction()).isNull(); assertThat(ruleDto.getRemediationCoefficient()).isNull(); assertThat(ruleDto.getRemediationOffset()).isNull(); // Default debt data should not be touched assertThat(ruleDto.getDefaultSubCharacteristicId()).isEqualTo(10); assertThat(ruleDto.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET"); assertThat(ruleDto.getDefaultRemediationCoefficient()).isEqualTo("4h"); assertThat(ruleDto.getDefaultRemediationOffset()).isEqualTo("15min"); verify(dao).update(characteristicCaptor.capture(), eq(batchSession)); CharacteristicDto characteristicDto = characteristicCaptor.getValue(); // Sub characteristic is disable assertThat(characteristicDto.getId()).isEqualTo(2); assertThat(characteristicDto.isEnabled()).isFalse(); assertThat(characteristicDto.getUpdatedAt()).isEqualTo(now); }
@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()); }
@Test public void reactivate_disabled_rules() { verifyRulesInDb(); // Disable plugin X1 rulesDefinition.includeX1 = false; tester.get(Platform.class).executeStartupTasks(); RuleDto rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED); dbSession.clearCache(); // Reactivate plugin X1 rulesDefinition.includeX1 = true; tester.get(Platform.class).executeStartupTasks(); RuleDto ruleReloaded = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); assertThat(ruleReloaded.getStatus()).isEqualTo(RuleStatus.READY); }
@Test public void show_custom_rule() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); dao.insert(dbSession, templateRule); dao.insert( dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId())); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // find custom rule assertThat(index.getByKey(RuleKey.of("java", "S001_MY_CUSTOM")).templateKey()) .isEqualTo(RuleKey.of("java", "S001")); }
@Test public void update_custom_rule_from_template() throws Exception { Rule templateRule = index.getByKey(RuleKey.of("xoo", "template1")); // Create custom rule RuleKey customRuleKey = tester .get(RuleCreator.class) .create( NewRule.createForCustomRule("CUSTOM_RULE", templateRule.key()) .setName("My custom") .setHtmlDescription("Some description") .setSeverity(Severity.MAJOR) .setStatus(RuleStatus.READY) .setParameters(ImmutableMap.of("format", "txt"))); // Update custom rule RuleDto customRuleDto = db.ruleDao().getByKey(dbSession, customRuleKey); db.ruleDao() .update( dbSession, customRuleDto .setLanguage("other language") .setConfigKey("other config key") .setDefaultSubCharacteristicId(45) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("1h") .setDefaultRemediationOffset("5min") .setEffortToFixDescription("effort to fix desc")); dbSession.commit(); dbSession.clearCache(); // Re-execute startup tasks tester.get(Platform.class).executeStartupTasks(); // Verify custom rule has been restore from the template Rule customRule = index.getByKey(customRuleKey); assertThat(customRule.language()).isEqualTo("xoo"); assertThat(customRule.internalKey()).isNull(); assertThat(customRule.debtSubCharacteristicKey()).isNull(); assertThat(customRule.debtRemediationFunction()).isNull(); }
@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); }
private RuleKey createManualRule(RuleKey ruleKey, NewRule newRule, DbSession dbSession) { RuleDto ruleDto = RuleDto.createFor(ruleKey) .setName(newRule.name()) .setDescription(newRule.markdownDescription()) .setDescriptionFormat(Format.MARKDOWN) .setSeverity(newRule.severity()) .setStatus(RuleStatus.READY); dbClient.ruleDao().insert(dbSession, ruleDto); return ruleKey; }
private Result<QProfileActivity> searchActivities( QProfileActivityQuery query, SearchOptions options) { DbSession session = dbClient.openSession(false); try { SearchResponse response = activityIndex.doSearch(query, options); Result<QProfileActivity> result = new Result<>(response); for (SearchHit hit : response.getHits().getHits()) { QProfileActivity profileActivity = new QProfileActivity(hit.getSource()); RuleDto ruleDto = dbClient.ruleDao().getNullableByKey(session, profileActivity.ruleKey()); profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null); String login = profileActivity.getLogin(); if (login != null) { UserDto user = dbClient.userDao().selectActiveUserByLogin(session, login); profileActivity.authorName(user != null ? user.getName() : null); } result.getHits().add(profileActivity); } return result; } finally { session.close(); } }
@Test public void count_by_deprecated() throws Exception { MockUserSession.set() .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN) .setLogin("me"); // create deprecated rule RuleDto deprecatedXooRule = RuleTesting.newDto(RuleKey.of("xoo", "deprecated1")) .setSeverity("MINOR") .setLanguage("xoo") .setStatus(RuleStatus.DEPRECATED); db.ruleDao().insert(dbSession, deprecatedXooRule); dbSession.commit(); // active some rules service.activate( XOO_P1_KEY, new RuleActivation(deprecatedXooRule.getKey()).setSeverity("BLOCKER")); service.activate(XOO_P1_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER")); dbSession.commit(); assertThat(loader.countDeprecatedActiveRulesByProfile(XOO_P1_KEY)).isEqualTo(1); }
@Test public void remove_end_user_tags_that_are_declared_as_system() { verifyRulesInDb(); Rule rule = index.getByKey(RuleTesting.XOO_X1); assertThat(rule.systemTags()).contains("tag1"); assertThat(rule.tags()).isEmpty(); // Add a user tag tester .get(RuleUpdater.class) .update( RuleUpdate.createForPluginRule(rule.key()).setTags(newHashSet("user-tag")), UserSession.get()); dbSession.clearCache(); // Verify tags Rule ruleUpdated = index.getByKey(RuleTesting.XOO_X1); assertThat(ruleUpdated.systemTags()).contains("tag1"); assertThat(ruleUpdated.tags()).contains("user-tag"); // The plugin X1 will be updated rulesDefinition.includeX1 = false; rulesDefinition.includeX1bis = true; tester.get(Platform.class).executeStartupTasks(); dbSession.clearCache(); // User tag should become a system tag RuleDto ruleDtoReloaded = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); assertThat(ruleDtoReloaded.getSystemTags()).contains("tag1", "tag2", "user-tag"); assertThat(ruleDtoReloaded.getTags()).isEmpty(); // User tag should become a system tag Rule ruleReloaded = index.getByKey(RuleTesting.XOO_X1); assertThat(ruleReloaded.systemTags()).contains("tag1", "tag2", "user-tag"); assertThat(ruleReloaded.tags()).isEmpty(); }
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, dbSession, templateKey); 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); } dbSession.commit(); return customRuleKey; }
void verifyForActivation() { if (RuleStatus.REMOVED == rule.getStatus()) { throw new BadRequestException("Rule was removed: " + rule.getKey()); } if (rule.isTemplate()) { throw new BadRequestException( "Rule template can't be activated on a Quality profile: " + rule.getKey()); } if (rule.getKey().isManual()) { throw new BadRequestException( "Manual rule can't be activated on a Quality profile: " + rule.getKey()); } if (!profile.getLanguage().equals(rule.getLanguage())) { throw new BadRequestException( String.format( "Rule %s and profile %s have different languages", rule.getKey(), profile.getKey())); } }
@Test public void getByKey() throws InterruptedException { RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("javascript", "S001")); dao.insert(dbSession, ruleDto); dbSession.commit(); Rule rule = index.getByKey(RuleKey.of("javascript", "S001")); assertThat(rule.htmlDescription()).isEqualTo(ruleDto.getDescription()); assertThat(rule.key()).isEqualTo(ruleDto.getKey()); // TODO // assertThat(rule.debtSubCharacteristicKey()) // .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString()); assertThat(rule.debtRemediationFunction().type().name()) .isEqualTo(ruleDto.getRemediationFunction()); assertThat(Sets.newHashSet(rule.tags())).isEqualTo(ruleDto.getTags()); assertThat(Sets.newHashSet(rule.systemTags())).isEqualTo(ruleDto.getSystemTags()); }
ActiveRuleKey activeRuleKey() { return ActiveRuleKey.of(profile.getKee(), rule.getKey()); }
@CheckForNull String defaultSeverity() { return rule.getSeverityString(); }
@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); }