@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"); }
@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); }
@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"); }
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; }
@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"); } }
@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); }
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(); }
@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(); }
@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"); } }
@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"); }
@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"); }
@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(); }
@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"); }