Skip to content

Commit

Permalink
SONAR-7330 QProfileFactory is now using ActiveRuleIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Feb 29, 2016
1 parent 0e4c8a9 commit 08af1c6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 50 deletions.
Expand Up @@ -20,19 +20,23 @@
package org.sonar.server.qualityprofile; package org.sonar.server.qualityprofile;


import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.sonar.core.util.Slug; import org.sonar.core.util.Slug;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.Verifications; import org.sonar.server.exceptions.Verifications;


import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;

/** /**
* Create, delete, rename and set as default profile. * Create, delete, rename and set as default profile.
*/ */
Expand Down Expand Up @@ -84,21 +88,11 @@ private QualityProfileDto doCreate(DbSession dbSession, QProfileName name) {


// ------------- DELETION // ------------- DELETION


void delete(String key) {
DbSession session = db.openSession(false);
try {
delete(session, key, false);
session.commit();
} finally {
session.close();
}
}

/** /**
* Session is NOT committed. Profiles marked as "default" for a language can't be deleted, * Session is NOT committed. Profiles marked as "default" for a language can't be deleted,
* except if the parameter <code>force</code> is true. * except if the parameter <code>force</code> is true.
*/ */
public void delete(DbSession session, String key, boolean force) { public List<ActiveRuleChange> delete(DbSession session, String key, boolean force) {
QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(session, key); QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(session, key);
List<QualityProfileDto> descendants = db.qualityProfileDao().selectDescendants(session, key); List<QualityProfileDto> descendants = db.qualityProfileDao().selectDescendants(session, key);
if (!force) { if (!force) {
Expand All @@ -108,16 +102,23 @@ public void delete(DbSession session, String key, boolean force) {
} }
} }
// delete bottom-up // delete bottom-up
List<ActiveRuleChange> changes = new ArrayList<>();
for (QualityProfileDto descendant : Lists.reverse(descendants)) { for (QualityProfileDto descendant : Lists.reverse(descendants)) {
doDelete(session, descendant); changes.addAll(doDelete(session, descendant));
} }
doDelete(session, profile); changes.addAll(doDelete(session, profile));
return changes;
} }


private void doDelete(DbSession session, QualityProfileDto profile) { private List<ActiveRuleChange> doDelete(DbSession session, QualityProfileDto profile) {
db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session); db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
db.activeRuleDao().deleteByProfileKey(session, profile.getKey()); List<ActiveRuleChange> changes = new ArrayList<>();
for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(session, profile.getKey())) {
db.activeRuleDao().delete(session, activeRule.getKey());
changes.add(ActiveRuleChange.createFor(DEACTIVATED, activeRule.getKey()));
}
db.qualityProfileDao().delete(session, profile); db.qualityProfileDao().delete(session, profile);
return changes;
} }


// ------------- DEFAULT PROFILE // ------------- DEFAULT PROFILE
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.qualityprofile; package org.sonar.server.qualityprofile;


import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
Expand All @@ -27,18 +28,19 @@
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.sonar.api.server.rule.RuleParamType; import org.sonar.api.server.rule.RuleParamType;
import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException; import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto; import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting; import org.sonar.db.rule.RuleTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex; import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
import org.sonar.server.search.IndexClient; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.MockUserSession; import org.sonar.server.tester.MockUserSession;
import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule; import org.sonar.server.tester.UserSessionRule;
Expand All @@ -52,24 +54,30 @@
public class QProfileFactoryMediumTest { public class QProfileFactoryMediumTest {


@ClassRule @ClassRule
public static ServerTester tester = new ServerTester(); public static ServerTester tester = new ServerTester().withEsIndexes();
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Rule @Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);


DbClient db; DbClient db;
DbSession dbSession; DbSession dbSession;
IndexClient index; ActiveRuleIndex2 activeRuleIndex;
ActiveRuleIndexer activeRuleIndexer;
RuleIndexer ruleIndexer;
QProfileFactory factory; QProfileFactory factory;


@Before @Before
public void before() { public void before() {
tester.clearDbAndIndexes(); tester.clearDbAndIndexes();
db = tester.get(DbClient.class); db = tester.get(DbClient.class);
dbSession = db.openSession(false); dbSession = db.openSession(false);
index = tester.get(IndexClient.class);
factory = tester.get(QProfileFactory.class); factory = tester.get(QProfileFactory.class);
activeRuleIndex = tester.get(ActiveRuleIndex2.class);
activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
activeRuleIndexer.setEnabled(true);
ruleIndexer = tester.get(RuleIndexer.class);
ruleIndexer.setEnabled(true);
} }


@After @After
Expand Down Expand Up @@ -208,13 +216,15 @@ public void delete() {
dbSession.commit(); dbSession.commit();
dbSession.clearCache(); dbSession.clearCache();


factory.delete(XOO_P1_KEY); List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
dbSession.commit();
activeRuleIndexer.index(changes);


dbSession.clearCache(); dbSession.clearCache();
assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty(); assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
assertThat(db.deprecatedActiveRuleDao().selectAllParams(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty(); assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
} }


@Test @Test
Expand All @@ -223,23 +233,27 @@ public void delete_descendants() {


// create parent and child profiles // create parent and child profiles
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3()); db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3());
tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY); List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY); changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY));
tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY); changes.addAll(tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY));
dbSession.commit(); dbSession.commit();
dbSession.clearCache(); dbSession.clearCache();
activeRuleIndexer.index(changes);

assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3); assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3);
assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).hasSize(3); assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3);


factory.delete(XOO_P1_KEY); changes = factory.delete(dbSession, XOO_P1_KEY, false);
dbSession.commit();
activeRuleIndexer.index(changes);


dbSession.clearCache(); dbSession.clearCache();
assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty(); assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
assertThat(db.deprecatedActiveRuleDao().selectAllParams(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty(); assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P2_KEY)).isEmpty(); assertThat(activeRuleIndex.findByProfile(XOO_P2_KEY)).isEmpty();
assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P3_KEY)).isEmpty(); assertThat(activeRuleIndex.findByProfile(XOO_P3_KEY)).isEmpty();
} }


@Test @Test
Expand All @@ -250,7 +264,9 @@ public void do_not_delete_default_profile() {
dbSession.clearCache(); dbSession.clearCache();


try { try {
factory.delete(XOO_P1_KEY); List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
dbSession.commit();
activeRuleIndexer.index(changes);
fail(); fail();
} catch (BadRequestException e) { } catch (BadRequestException e) {
assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P1"); assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P1");
Expand All @@ -261,14 +277,18 @@ public void do_not_delete_default_profile() {
@Test @Test
public void do_not_delete_if_default_descendant() { public void do_not_delete_if_default_descendant() {
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3()); db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3());
tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY); List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY));
factory.setDefault(dbSession, XOO_P3_KEY); factory.setDefault(dbSession, XOO_P3_KEY);
dbSession.commit(); dbSession.commit();
dbSession.clearCache(); dbSession.clearCache();
activeRuleIndexer.index(changes);


try { try {
factory.delete(XOO_P1_KEY); changes = factory.delete(dbSession, XOO_P1_KEY, false);
dbSession.commit();
activeRuleIndexer.index(changes);
fail(); fail();
} catch (BadRequestException e) { } catch (BadRequestException e) {
assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P3"); assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P3");
Expand All @@ -281,7 +301,9 @@ public void fail_if_unknown_profile_to_be_deleted() {
thrown.expect(RowNotFoundException.class); thrown.expect(RowNotFoundException.class);
thrown.expectMessage("Quality profile not found: XOO_P1"); thrown.expectMessage("Quality profile not found: XOO_P1");


factory.delete(XOO_P1_KEY); List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
dbSession.commit();
activeRuleIndexer.index(changes);
} }


@Test @Test
Expand Down Expand Up @@ -348,10 +370,12 @@ private void initRules() {
// create pre-defined rules // create pre-defined rules
RuleDto xooRule1 = RuleTesting.newXooX1(); RuleDto xooRule1 = RuleTesting.newXooX1();
RuleDto xooRule2 = RuleTesting.newXooX2(); RuleDto xooRule2 = RuleTesting.newXooX2();
db.deprecatedRuleDao().insert(dbSession, xooRule1, xooRule2); db.ruleDao().insert(dbSession, xooRule1);
db.deprecatedRuleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1) db.ruleDao().insert(dbSession, xooRule2);
db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type())); .setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
dbSession.commit(); dbSession.commit();
dbSession.clearCache(); dbSession.clearCache();
ruleIndexer.index();
} }
} }
Expand Up @@ -57,6 +57,11 @@ public List<ActiveRuleDto> selectByRule(DbSession dbSession, RuleDto rule) {
return mapper(dbSession).selectByRuleId(rule.getId()); return mapper(dbSession).selectByRuleId(rule.getId());
} }


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

public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) { public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
return mapper(dbSession).selectAllParams(); return mapper(dbSession).selectAllParams();
} }
Expand Down Expand Up @@ -122,13 +127,6 @@ public void deleteParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleP
mapper(session).deleteParameter(activeRuleParam.getId()); mapper(session).deleteParameter(activeRuleParam.getId());
} }


public void deleteByProfileKey(DbSession session, String profileKey) {
/** Functional cascade for params */
for (ActiveRuleDto activeRule : selectByProfileKey(session, profileKey)) {
delete(session, activeRule.getKey());
}
}

public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) { public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
return mapper(session).selectByProfileKey(profileKey); return mapper(session).selectByProfileKey(profileKey);
} }
Expand Down

0 comments on commit 08af1c6

Please sign in to comment.