public ActiveRuleDto activateRule(QProfileDto profile, RuleDefinitionDto rule) { return activateRule(profile, rule, ar -> { }); }
@Test public void test_commitAndIndex() { ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1); ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1); ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2); commitAndIndex(rule1, ar1, ar2); verifyOnlyIndexed(ar1, ar2); assertThatEsQueueTableIsEmpty(); }
@Test public void export_profile() { QProfileDto profile = createProfile(); db.qualityProfiles().activateRule(profile, rule); StringWriter writer = new StringWriter(); underTest.export(db.getSession(), profile, "standard", writer); assertThat(writer.toString()).isEqualTo("standard -> " + profile.getName() + " -> 1"); writer = new StringWriter(); underTest.export(db.getSession(), profile, "xootool", writer); assertThat(writer.toString()).isEqualTo("xoo -> " + profile.getName() + " -> 1"); }
@Test public void indexOnStartup_indexes_all_data() { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile1, rule1); underTest.indexOnStartup(emptySet()); List<ActiveRuleDoc> docs = es.getDocuments(INDEX_TYPE_ACTIVE_RULE, ActiveRuleDoc.class); assertThat(docs).hasSize(1); verify(docs.get(0), profile1, activeRule); assertThatEsQueueTableIsEmpty(); }
@Test public void commitDeletionOfProfiles_does_nothing_if_profiles_are_not_indexed() { db.qualityProfiles().activateRule(profile1, rule1); indexAll(); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(1); underTest.commitDeletionOfProfiles(db.getSession(), singletonList(profile2)); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(1); }
@Test public void commitAndIndex_empty_list() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); underTest.commitAndIndex(db.getSession(), Collections.emptyList()); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(0); assertThatEsQueueTableIsEmpty(); }
@Test public void commitDeletionOfProfiles() { ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1); ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1); ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2); indexAll(); db.getDbClient().qualityProfileDao().deleteRulesProfilesByUuids(db.getSession(), singletonList(profile2.getRulesProfileUuid())); underTest.commitDeletionOfProfiles(db.getSession(), singletonList(profile2)); verifyOnlyIndexed(ar1); }
@Test public void countActiveRulesByQuery_filter_by_inheritance() { db.qualityProfiles().activateRule(profile1, rule1); db.qualityProfiles().activateRule(profile1, rule2, ar -> ar.setInheritance(OVERRIDES)); db.qualityProfiles().activateRule(profile1, removedRule, ar -> ar.setInheritance(OVERRIDES)); db.qualityProfiles().activateRule(profile2, rule1, ar -> ar.setInheritance(OVERRIDES)); db.qualityProfiles().activateRule(profile2, rule2, ar -> ar.setInheritance(INHERITED)); ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization); assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setInheritance(OVERRIDES).build())) .containsOnly(entry(profile1.getKee(), 1L), entry(profile2.getKee(), 1L)); assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setInheritance(INHERITED).build())) .containsOnly(entry(profile2.getKee(), 1L)); }
@Test public void commitAndIndex_deletes_the_documents_that_dont_exist_in_database() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); indexAll(); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(1); db.getDbClient().activeRuleDao().delete(db.getSession(), ar.getKey()); commitAndIndex(rule1, ar); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(0); assertThatEsQueueTableIsEmpty(); }
@Test public void countActiveRulesByQuery_filter_by_organization() { db.qualityProfiles().activateRule(profile1, rule1); OrganizationDto anotherOrganization = db.organizations().insert(); QProfileDto profileOnAnotherOrganization = db.qualityProfiles().insert(anotherOrganization); db.qualityProfiles().activateRule(profileOnAnotherOrganization, rule1); assertThat(underTest.countActiveRulesByQuery(dbSession, ActiveRuleCountQuery.builder().setOrganization(organization).setProfiles(asList(profile1, profileOnAnotherOrganization)).build())) .containsOnly(entry(profile1.getKee(), 1L)); }
@Test public void commitAndIndex_keeps_elements_to_recover_in_ES_QUEUE_on_errors() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); es.lockWrites(INDEX_TYPE_ACTIVE_RULE); commitAndIndex(rule1, ar); EsQueueDto expectedItem = EsQueueDto.create(INDEX_TYPE_ACTIVE_RULE.format(), "" + ar.getId(), "activeRuleId", valueOf(ar.getRuleId())); assertThatEsQueueContainsExactly(expectedItem); es.unlockWrites(INDEX_TYPE_ACTIVE_RULE); }
@Test public void facet_by_activation_severity_is_ignored_when_profile_is_not_specified() { RuleDefinitionDto rule = createJavaRule(); QProfileDto profile = createJavaProfile(); db.qualityProfiles().activateRule(profile, rule); index(); RuleQuery query = newRuleQuery(); verifyNoFacet(query, RuleIndex.FACET_ACTIVE_SEVERITIES); }
private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, RuleParamDto param) { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(param) .setValue("20") .setActiveRuleId(activeRule.getId()); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto); return activeRule; }
@Test public void activation_parameter_is_ignored_if_profile_is_not_set() { RuleDefinitionDto rule1 = createJavaRule(); RuleDefinitionDto rule2 = createJavaRule(); QProfileDto profile1 = createJavaProfile(); db.qualityProfiles().activateRule(profile1, rule1); index(); // all rules are returned verifySearch(newRuleQuery().setActivation(true), rule1, rule2); }
@Test public void active_rules_are_not_returned_when_not_member_of_paid_organization() { OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID)); RuleDefinitionDto rule = db.rules().insert(); QProfileDto qProfile = db.qualityProfiles().insert(organization); ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .setParam(PARAM_ACTIVES, "true") .setParam(PARAM_ORGANIZATION, organization.getKey()) .executeProtobuf(ShowResponse.class); assertThat(result.getActivesList()).isEmpty(); }
@Test public void statistics_on_active_rules() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey())); RuleDefinitionDto rule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition(); RuleDefinitionDto deprecatedRule1 = db.rules().insertRule(r -> r.setStatus(DEPRECATED)).getDefinition(); RuleDefinitionDto deprecatedRule2 = db.rules().insertRule(r -> r.setStatus(DEPRECATED)).getDefinition(); RuleDefinitionDto inactiveRule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition(); db.qualityProfiles().activateRule(profile, rule); db.qualityProfiles().activateRule(profile, deprecatedRule1); db.qualityProfiles().activateRule(profile, deprecatedRule2); SearchWsResponse result = call(ws.newRequest()); assertThat(result.getProfilesList()) .extracting(QualityProfile::getActiveRuleCount, QualityProfile::getActiveDeprecatedRuleCount) .containsExactlyInAnyOrder(tuple(3L, 2L)); }
@Test public void test_completeShow() { OrganizationDto organization = dbTester.organizations().insert(); ActiveRuleCompleter underTest = new ActiveRuleCompleter(dbTester.getDbClient(), new Languages()); RuleDefinitionDto rule = dbTester.rules().insert(); QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization); ActiveRuleDto activeRule = dbTester.qualityProfiles().activateRule(qualityProfile, rule); List<Rules.Active> result = underTest.completeShow(dbTester.getSession(), organization, rule); assertThat(result).extracting(Rules.Active::getQProfile).containsExactlyInAnyOrder(qualityProfile.getKee()); assertThat(result).extracting(Rules.Active::getSeverity).containsExactlyInAnyOrder(activeRule.getSeverityString()); } }
@Test public void selectBuiltInRuleProfilesWithActiveRules() { // a quality profile without active rules but not builtin db.qualityProfiles().insert(db.getDefaultOrganization(), qp -> qp.setIsBuiltIn(false)); // a built-in quality profile without active rules db.qualityProfiles().insert(db.getDefaultOrganization(), qp -> qp.setIsBuiltIn(true)); // a built-in quality profile with active rules QProfileDto builtInQPWithActiveRules = db.qualityProfiles().insert(db.getDefaultOrganization(), qp -> qp.setIsBuiltIn(true)); RuleDefinitionDto ruleDefinitionDto = db.rules().insert(); db.qualityProfiles().activateRule(builtInQPWithActiveRules, ruleDefinitionDto); dbSession.commit(); List<RulesProfileDto> rulesProfileDtos = underTest.selectBuiltInRuleProfilesWithActiveRules(dbSession); assertThat(rulesProfileDtos).extracting(RulesProfileDto::getName) .containsOnly(builtInQPWithActiveRules.getName()); }
@Test public void active_rules_are_returned_when_member_of_paid_organization() { OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID)); RuleDefinitionDto rule = db.rules().insert(); QProfileDto qProfile = db.qualityProfiles().insert(organization); ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule); userSession.logIn(db.users().insertUser()).addMembership(organization); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .setParam(PARAM_ACTIVES, "true") .setParam(PARAM_ORGANIZATION, organization.getKey()) .executeProtobuf(ShowResponse.class); assertThat(result.getActivesList()).isNotEmpty(); }
private QProfileDto createCustomProfile(OrganizationDto org) { QProfileDto profile = db.qualityProfiles().insert(org, p -> p.setLanguage("xoo").setIsBuiltIn(false)); ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() .setRulesParameterId(ruleParam.getId()) .setKey("foo") .setValue("bar"); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam); db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto() .setChangeType(ActiveRuleChange.Type.ACTIVATED.name()) .setRulesProfileUuid(profile.getRulesProfileUuid())); db.commit(); return profile; }