Skip to content

Commit

Permalink
SONAR-8951 active rule dao: remove “selectAll”
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schwarz authored and bartfastiel committed Apr 14, 2017
1 parent 2b263da commit d6ee52b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 39 deletions.
Expand Up @@ -67,11 +67,6 @@ public List<ActiveRuleDto> selectByRuleIds(DbSession dbSession, List<Integer> id
return executeLargeInputs(ids, mapper(dbSession)::selectByRuleIds); return executeLargeInputs(ids, mapper(dbSession)::selectByRuleIds);
} }


// TODO As it's only used by MediumTest, it should be replaced by DbTester.countRowsOfTable()
public List<ActiveRuleDto> selectAll(DbSession dbSession) {
return mapper(dbSession).selectAll();
}

/** /**
* Active rule on removed rule are NOT returned * Active rule on removed rule are NOT returned
*/ */
Expand Down
Expand Up @@ -173,19 +173,6 @@ public void select_by_rule_ids() {
.extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey(), activeRule3.getKey()); .extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey(), activeRule3.getKey());
} }


@Test
public void select_all() {
ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(BLOCKER);
ActiveRuleDto activeRule3 = createFor(profile2, rule1).setSeverity(BLOCKER);
underTest.insert(dbSession, activeRule1);
underTest.insert(dbSession, activeRule2);
underTest.insert(dbSession, activeRule3);
dbSession.commit();

assertThat(underTest.selectAll(dbSession)).hasSize(3);
}

@Test @Test
public void select_by_profile() { public void select_by_profile() {
ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER); ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
Expand Down
Expand Up @@ -388,7 +388,7 @@ public void restore_profile_with_zero_rules() throws Exception {
organization, null); organization, null);


dbSession.clearCache(); dbSession.clearCache();
assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(0); assertThat(db.openSession(false).selectList("SELECT * FROM active_rules")).isEmpty();
List<QualityProfileDto> profiles = db.qualityProfileDao().selectAll(dbSession, organization); List<QualityProfileDto> profiles = db.qualityProfileDao().selectAll(dbSession, organization);
assertThat(profiles).hasSize(1); assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).getName()).isEqualTo("P1"); assertThat(profiles.get(0).getName()).isEqualTo("P1");
Expand Down
Expand Up @@ -21,7 +21,7 @@


import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import org.assertj.core.api.AbstractObjectAssert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand All @@ -35,9 +35,8 @@
import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleParamDto; import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
Expand All @@ -54,12 +53,12 @@ public class QProfileFactoryTest {


private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class); private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
private QProfileFactory underTest = new QProfileFactory(db.getDbClient(), new SequenceUuidFactory(), System2.INSTANCE, activeRuleIndexer); private QProfileFactory underTest = new QProfileFactory(db.getDbClient(), new SequenceUuidFactory(), System2.INSTANCE, activeRuleIndexer);
private RuleDto rule; private RuleDefinitionDto rule;
private RuleParamDto ruleParam; private RuleParamDto ruleParam;


@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
rule = db.rules().insertRule(RuleTesting.newRuleDto()); rule = db.rules().insert();
ruleParam = db.rules().insertRuleParam(rule); ruleParam = db.rules().insertRuleParam(rule);
} }


Expand All @@ -73,8 +72,10 @@ public void deleteByKeys_deletes_profiles_in_db_and_elasticsearch() {
List<String> profileKeys = asList(profile1.getKey(), profile2.getKey(), "does_not_exist"); List<String> profileKeys = asList(profile1.getKey(), profile2.getKey(), "does_not_exist");
underTest.deleteByKeys(db.getSession(), profileKeys); underTest.deleteByKeys(db.getSession(), profileKeys);


assertOnlyExists(org, profile3);
verify(activeRuleIndexer).deleteByProfileKeys(profileKeys); verify(activeRuleIndexer).deleteByProfileKeys(profileKeys);
assertQualityProfileFromDb(profile1).isNull();
assertQualityProfileFromDb(profile2).isNull();
assertQualityProfileFromDb(profile3).isNotNull();
} }


@Test @Test
Expand All @@ -84,20 +85,8 @@ public void deleteByKeys_accepts_empty_list_of_keys() {


underTest.deleteByKeys(db.getSession(), Collections.emptyList()); underTest.deleteByKeys(db.getSession(), Collections.emptyList());


assertOnlyExists(org, profile1);
verifyZeroInteractions(activeRuleIndexer); verifyZeroInteractions(activeRuleIndexer);
} assertQualityProfileFromDb(profile1).isNotNull();

private void assertOnlyExists(OrganizationDto org, QualityProfileDto profile) {
List<QualityProfileDto> profiles = db.getDbClient().qualityProfileDao().selectAll(db.getSession(), org);
assertThat(profiles).extracting(QualityProfileDto::getKey).containsOnly(profile.getKey());

List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectAll(db.getSession());
assertThat(activeRules).extracting(ActiveRuleDto::getProfileId).containsOnly(profile.getId());

List<ActiveRuleParamDto> activeParams = db.getDbClient().activeRuleDao().selectAllParams(db.getSession());
List<Integer> activeRuleIds = activeRules.stream().map(ActiveRuleDto::getId).collect(Collectors.toList());
assertThat(activeParams).extracting(ActiveRuleParamDto::getActiveRuleId).containsOnlyElementsOf(activeRuleIds);
} }


private QualityProfileDto createRandomProfile(OrganizationDto org) { private QualityProfileDto createRandomProfile(OrganizationDto org) {
Expand All @@ -109,10 +98,16 @@ private QualityProfileDto createRandomProfile(OrganizationDto org) {
.setRuleId(rule.getId()) .setRuleId(rule.getId())
.setSeverity(Severity.BLOCKER); .setSeverity(Severity.BLOCKER);
db.getDbClient().activeRuleDao().insert(db.getSession(), activeRuleDto); db.getDbClient().activeRuleDao().insert(db.getSession(), activeRuleDto);
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setRulesParameterId(ruleParam.getId()).setKey("foo").setValue("bar"); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto()
.setRulesParameterId(ruleParam.getId())
.setKey("foo")
.setValue("bar");
db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleDto, activeRuleParam); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleDto, activeRuleParam);
db.getSession().commit(); db.getSession().commit();
return profile; return profile;
} }


private AbstractObjectAssert<?, QualityProfileDto> assertQualityProfileFromDb(QualityProfileDto profile) {
return assertThat(db.getDbClient().qualityProfileDao().selectByKey(db.getSession(), profile.getKey()));
}
} }

0 comments on commit d6ee52b

Please sign in to comment.