@Override public void clearCache() { delegate.clearCache(); }
@Test public void activate_rule_override_severity() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); ruleIndexer.commitAndIndex(dbSession, rule.getId()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule wsActivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .setParam(PARAM_SEVERITY, "MINOR") .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile, rule.getKey()); Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRuleKey); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR); }
@Test public void no_changes() { RuleDto ruleDto = RuleTesting.newDto(RULE_KEY, db.getDefaultOrganization()) // the following fields are not supposed to be updated .setNoteData("my *note*") .setNoteUserUuid("me") .setTags(ImmutableSet.of("tag1")) .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) .setRemediationGapMultiplier("1d") .setRemediationBaseEffort("5min"); db.rules().insert(ruleDto.getDefinition()); db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY); assertThat(update.isEmpty()).isTrue(); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getNoteData()).isEqualTo("my *note*"); assertThat(rule.getNoteUserUuid()).isEqualTo("me"); assertThat(rule.getTags()).containsOnly("tag1"); assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()); assertThat(rule.getRemediationGapMultiplier()).isEqualTo("1d"); assertThat(rule.getRemediationBaseEffort()).isEqualTo("5min"); }
@Test public void override_debt_from_linear_with_offset_to_constant() { db.rules().insert(newRule(RULE_KEY) .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefRemediationGapMultiplier("1d") .setDefRemediationBaseEffort("5min")); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) .setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "10min")) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); // verify debt is overridden RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()); assertThat(rule.getRemediationGapMultiplier()).isNull(); assertThat(rule.getRemediationBaseEffort()).isEqualTo("10min"); assertThat(rule.getDefRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); assertThat(rule.getDefRemediationGapMultiplier()).isEqualTo("1d"); assertThat(rule.getDefRemediationBaseEffort()).isEqualTo("5min"); }
@Test public void override_debt() { db.rules().insert(newRule(RULE_KEY) .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefRemediationGapMultiplier("1d") .setDefRemediationBaseEffort("5min")); dbSession.commit(); DefaultDebtRemediationFunction fn = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "1min"); RuleUpdate update = createForPluginRule(RULE_KEY) .setDebtRemediationFunction(fn) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); // verify debt is overridden RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()); assertThat(rule.getRemediationGapMultiplier()).isNull(); assertThat(rule.getRemediationBaseEffort()).isEqualTo("1min"); assertThat(rule.getDefRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); assertThat(rule.getDefRemediationGapMultiplier()).isEqualTo("1d"); assertThat(rule.getDefRemediationBaseEffort()).isEqualTo("5min"); }
@Test public void override_debt_only_offset() { db.rules().insert(newRule(RULE_KEY) .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) .setDefRemediationGapMultiplier("1d") .setDefRemediationBaseEffort(null)); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) .setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "2d", null)) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); // verify debt is overridden RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name()); assertThat(rule.getRemediationGapMultiplier()).isEqualTo("2d"); assertThat(rule.getRemediationBaseEffort()).isNull(); assertThat(rule.getDefRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name()); assertThat(rule.getDefRemediationGapMultiplier()).isEqualTo("1d"); assertThat(rule.getDefRemediationBaseEffort()).isNull(); }
@Test public void activate_rule_diff_languages() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule("php", "toto"); ruleIndexer.commitAndIndex(dbSession, rule.getId()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); try { // 1. Activate Rule wsActivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .execute(); dbSession.clearCache(); fail(); } catch (BadRequestException e) { assertThat(e.getMessage()).isEqualTo("php rule blah:toto cannot be activated on java profile Pjava"); } }
@Test public void activate_rule() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); ruleIndexer.commitAndIndex(dbSession, rule.getId()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule wsActivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); }
@Test public void bulk_activate_rule() throws Exception { QProfileDto profile = createProfile("java"); createRule(profile.getLanguage(), "toto"); createRule(profile.getLanguage(), "tata"); createRule(profile.getLanguage(), "hello"); createRule(profile.getLanguage(), "world"); dbSession.commit(); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule wsActivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .setParam(PARAM_LANGUAGES, "java") .execute() .assertJson(getClass(), "bulk_activate_rule.json"); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4); }
@Test public void remove_tags() { RuleDto ruleDto = RuleTesting.newDto(RULE_KEY, db.getDefaultOrganization()) .setTags(Sets.newHashSet("security")) .setSystemTags(Sets.newHashSet("java8", "javadoc")); db.rules().insert(ruleDto.getDefinition()); db.rules().insertOrUpdateMetadata(ruleDto.getMetadata()); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) .setTags(null) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getTags()).isEmpty(); assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); // verify that tags are indexed in index List<String> tags = ruleIndex.listTags(db.getDefaultOrganization(), null, 10); assertThat(tags).containsExactly("java8", "javadoc"); }
@Test public void bulk_activate_rule_not_all() throws Exception { QProfileDto java = createProfile("java"); QProfileDto php = createProfile("php"); createRule(java.getLanguage(), "toto"); createRule(java.getLanguage(), "tata"); createRule(php.getLanguage(), "hello"); createRule(php.getLanguage(), "world"); dbSession.commit(); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).isEmpty(); // 1. Activate Rule wsActivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, php.getKee()) .setParam(PARAM_LANGUAGES, "php") .execute() .assertJson(getClass(), "bulk_activate_rule_not_all.json"); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2); }
@Test public void reset() { QProfileDto profile = QProfileTesting.newXooP1(organization); QProfileDto subProfile = QProfileTesting.newXooP2(organization).setParentKee(QProfileTesting.XOO_P1_KEY); dbClient.qualityProfileDao().insert(dbSession, profile, subProfile); RuleDefinitionDto rule = createRule(profile.getLanguage(), "rule"); ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()); ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule) .setSeverity("MINOR"); dbClient.activeRuleDao().insert(dbSession, active1); dbClient.activeRuleDao().insert(dbSession, active2); dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. assert rule child rule is minor Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey()); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR); // 1. reset child rule wsActivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, subProfile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .setParam(PARAM_RESET, "true") .execute(); dbSession.clearCache(); // 2. assert rule child rule is NOT minor activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey()); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isNotEqualTo(Severity.MINOR); }
@Test public void deactivate_rule() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); createActiveRule(rule, profile); ruleIndexer.commitAndIndex(dbSession, rule.getId()); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); // 1. Deactivate Rule wsDeactivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); }
@Test public void bulk_deactivate_rule_not_all() { QProfileDto profile = createProfile("java"); QProfileDto php = createProfile("php"); RuleDefinitionDto rule0 = createRule(profile.getLanguage(), "toto1"); RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "toto2"); createActiveRule(rule0, profile); createActiveRule(rule1, profile); createActiveRule(rule0, php); createActiveRule(rule1, php); dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2); // 1. Deactivate Rule wsDeactivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0); assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2); }
@Test public void reset_remediation_function() { RuleDto ruleDto = RuleTesting.newDto(RULE_KEY, db.getDefaultOrganization()) .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) .setDefRemediationGapMultiplier("1d") .setDefRemediationBaseEffort("5min") .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) .setRemediationGapMultiplier(null) .setRemediationBaseEffort("1min"); db.rules().insert(ruleDto.getDefinition()); db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) .setDebtRemediationFunction(null) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); // verify debt is coming from default values RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getDefRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name()); assertThat(rule.getDefRemediationGapMultiplier()).isEqualTo("1d"); assertThat(rule.getDefRemediationBaseEffort()).isEqualTo("5min"); assertThat(rule.getRemediationFunction()).isNull(); assertThat(rule.getRemediationGapMultiplier()).isNull(); assertThat(rule.getRemediationBaseEffort()).isNull(); }
@Test public void bulk_deactivate_rule_by_profile() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule0 = createRule(profile.getLanguage(), "hello"); RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "world"); createActiveRule(rule0, profile); createActiveRule(rule1, profile); dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2); // 1. Deactivate Rule wsDeactivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .setParam(Param.TEXT_QUERY, "hello") .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); }
@Test public void does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile() throws Exception { QProfileDto javaProfile = createProfile("java"); createRule(javaProfile.getLanguage(), "toto"); createRule(javaProfile.getLanguage(), "tata"); QProfileDto phpProfile = createProfile("php"); createRule(phpProfile.getLanguage(), "hello"); createRule(phpProfile.getLanguage(), "world"); dbSession.commit(); // 1. Activate Rule wsActivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, javaProfile.getKee()) .setParam(PARAM_QPROFILE, javaProfile.getKee()) .setParam("activation", "false") .execute() .assertJson(getClass(), "does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile.json"); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, javaProfile.getKee())).hasSize(2); }
@Test public void bulk_deactivate_rule() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule0 = createRule(profile.getLanguage(), "toto1"); RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "toto2"); RuleDefinitionDto rule2 = createRule(profile.getLanguage(), "toto3"); RuleDefinitionDto rule3 = createRule(profile.getLanguage(), "toto4"); createActiveRule(rule0, profile); createActiveRule(rule2, profile); createActiveRule(rule3, profile); createActiveRule(rule1, profile); dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4); // 1. Deactivate Rule wsDeactivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); }
@Test public void bulk_activate_rule_by_query() { QProfileDto profile = createProfile("java"); createRule(profile.getLanguage(), "toto"); createRule(profile.getLanguage(), "tata"); createRule(profile.getLanguage(), "hello"); createRule(profile.getLanguage(), "world"); dbSession.commit(); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule with query returning 0 hits wsActivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .setParam(Param.TEXT_QUERY, "php") .execute(); dbSession.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0); // 1. Activate Rule with query returning 1 hits wsActivateRules.newRequest().setMethod("POST") .setParam(PARAM_TARGET_KEY, profile.getKee()) .setParam(Param.TEXT_QUERY, "world") .execute(); dbSession.commit(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); }
@Test public void remove_markdown_note() { RuleDto ruleDto = RuleTesting.newDto(RULE_KEY, db.getDefaultOrganization()) .setNoteData("my *note*") .setNoteUserUuid("me"); db.rules().insert(ruleDto.getDefinition()); db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) .setMarkdownNote(null) .setOrganization(db.getDefaultOrganization()); underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule); dbSession.clearCache(); RuleDto rule = db.getDbClient().ruleDao().selectOrFailByKey(dbSession, db.getDefaultOrganization(), RULE_KEY); assertThat(rule.getNoteData()).isNull(); assertThat(rule.getNoteUserUuid()).isNull(); assertThat(rule.getNoteCreatedAt()).isNull(); assertThat(rule.getNoteUpdatedAt()).isNull(); }