@Test public void shouldAcceptIfRulePatternDoesNotMatch() { WildcardPattern pattern = mock(WildcardPattern.class); when(pattern.match(ruleKey.toString())).thenReturn(false); underTest.addRuleExclusionPatternForComponent(component, pattern); assertThat(underTest.accept(issue, chain)).isFalse(); } }
@Test public void shouldRejectIfRulePatternMatches() { WildcardPattern pattern = mock(WildcardPattern.class); when(pattern.match(ruleKey.toString())).thenReturn(true); underTest.addRuleExclusionPatternForComponent(component, pattern); assertThat(underTest.accept(issue, chain)).isFalse(); }
@Override protected void execute(Context context) throws SQLException { Map<String, Integer> ruleKeysById = context.prepareSelect("select id, plugin_name, plugin_rule_key from rules") .list(row -> new Rule(row.getInt(1), RuleKey.of(row.getString(2), row.getString(3)).toString())) .stream() .collect(Collectors.toMap(r -> r.ruleKey, r -> r.ruleId)); MassUpdate massUpdate = context.prepareMassUpdate(); massUpdate.select("select kee,change_data from qprofile_changes"); massUpdate.update("update qprofile_changes set change_data=? where kee=?"); massUpdate.execute(((row, update) -> handle(row, update, ruleKeysById))); }
public Common.Rule.Builder formatRule(RuleDefinitionDto rule) { Common.Rule.Builder builder = Common.Rule.newBuilder() .setKey(rule.getKey().toString()) .setName(nullToEmpty(rule.getName())) .setStatus(Common.RuleStatus.valueOf(rule.getStatus().name())); builder.setLang(nullToEmpty(rule.getLanguage())); Language lang = languages.get(rule.getLanguage()); if (lang != null) { builder.setLangName(lang.getName()); } return builder; }
public void delete(RuleKey ruleKey) { try (DbSession dbSession = dbClient.openSession(false)) { RuleDefinitionDto rule = dbClient.ruleDao().selectOrFailDefinitionByKey(dbSession, ruleKey); checkArgument(rule.isCustomRule(), "Rule '%s' cannot be deleted because it is not a custom rule", rule.getKey().toString()); qProfileRules.deleteRule(dbSession, rule); rule.setStatus(RuleStatus.REMOVED); rule.setUpdatedAt(system2.now()); dbClient.ruleDao().update(dbSession, rule); ruleIndexer.commitAndIndex(dbSession, rule.getId()); } } }
@Test public void show_rule_key() { RuleDefinitionDto rule = db.rules().insert(); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .executeProtobuf(ShowResponse.class); assertThat(result.getRule()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); }
@SafeVarargs private final <T> void checkField(RuleDefinitionDto rule, String fieldName, Extractor<Rule, T> responseExtractor, T... expected) { SearchResponse result = ws.newRequest() .setParam("f", fieldName) .executeProtobuf(SearchResponse.class); assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); assertThat(result.getRulesList()).extracting(responseExtractor).containsExactly(expected); }
@Test public void fail_to_delete_if_not_custom() { logInAsQProfileAdministrator(); RuleDefinitionDto rule = dbTester.rules().insert(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Rule '" + rule.getKey().toString() + "' cannot be deleted because it is not a custom rule"); tester.newRequest() .setMethod("POST") .setParam("key", rule.getKey().toString()) .execute(); }
@Test public void show_external_rule() { RuleDefinitionDto externalRule = db.rules().insert(r -> r .setIsExternal(true) .setName("ext rule name")); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, externalRule.getKey().toString()) .executeProtobuf(ShowResponse.class); Rule resultRule = result.getRule(); assertThat(resultRule.getName()).isEqualTo("ext rule name"); }
@Test public void show_rule_tags_in_default_organization() { RuleDefinitionDto rule = db.rules().insert(); RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, db.getDefaultOrganization(), setTags("tag1", "tag2"), m -> m.setNoteData(null).setNoteUserUuid(null)); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .executeProtobuf(ShowResponse.class); assertThat(result.getRule().getTags().getTagsList()) .containsExactly(metadata.getTags().toArray(new String[0])); }
@Test public void throw_NotFoundException_if_organization_cannot_be_found() { RuleDefinitionDto rule = db.rules().insert(); thrown.expect(NotFoundException.class); ws.newRequest() .setParam("key", rule.getKey().toString()) .setParam("organization", "foo") .execute(); }
@Test public void show_rule_with_note_login() { RuleDefinitionDto rule = db.rules().insert(); UserDto user = db.users().insertUser(); OrganizationDto organization = db.organizations().insert(); db.rules().insertOrUpdateMetadata(rule, user, organization); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .setParam(PARAM_ORGANIZATION, organization.getKey()) .executeProtobuf(ShowResponse.class); assertThat(result.getRule().getNoteLogin()).isEqualTo(user.getLogin()); }
@Test public void fail_if_not_logged_in() { TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString()) .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE)); expectedException.expect(UnauthorizedException.class); request.execute(); }
@Test public void fail_if_not_logged_in() { TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString()) .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE)); expectedException.expect(UnauthorizedException.class); request.execute(); }
@Test public void throw_NotFoundException_if_organization_cannot_be_found() { logInAsQProfileAdministrator(); RuleDefinitionDto rule = db.rules().insert(); expectedException.expect(NotFoundException.class); ws.newRequest().setMethod("POST") .setParam("key", rule.getKey().toString()) .setParam("organization", "foo") .execute(); }
@Test public void filter_by_rule_key() { RuleDefinitionDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java")); RuleDefinitionDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java")); indexRules(); verify(r -> r.setParam(PARAM_RULE_KEY, rule1.getKey().toString()), rule1); verifyNoResults(r -> r.setParam(PARAM_RULE_KEY, "missing")); }
@Test public void throw_IllegalArgumentException_if_trying_to_update_builtin_rule_description() { logInAsQProfileAdministrator(); RuleDefinitionDto rule = db.rules().insert(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Not a custom rule"); ws.newRequest().setMethod("POST") .setParam("key", rule.getKey().toString()) .setParam("name", rule.getName()) .setParam("markdown_description", "New description") .execute(); }
@Test public void fail_if_not_organization_quality_profile_administrator() { userSession.logIn(db.users().insertUser()); QProfileDto qualityProfile = db.qualityProfiles().insert(organization); TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString()) .setParam(PARAM_KEY, qualityProfile.getKee()); expectedException.expect(ForbiddenException.class); request.execute(); }
@Test public void fail_if_not_organization_quality_profile_administrator() { RuleDefinitionDto rule = db.rules().insert(); userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); QProfileDto qualityProfile = db.qualityProfiles().insert(organization); TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, rule.getKey().toString()) .setParam(PARAM_KEY, qualityProfile.getKee()); expectedException.expect(ForbiddenException.class); request.execute(); }
@Test public void fail_activate_if_built_in_profile() { userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo")); TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString()) .setParam(PARAM_KEY, qualityProfile.getKee()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'"); request.execute(); }