aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImpl.java21
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java48
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java5
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java143
4 files changed, 12 insertions, 205 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImpl.java
index 7359bc1c48c..dfec3d1690f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImpl.java
@@ -42,18 +42,17 @@ public class DefinedQProfileCreationImpl implements DefinedQProfileCreation {
@Override
public void create(DbSession session, DefinedQProfile qualityProfile, OrganizationDto organization, List<ActiveRuleChange> changes) {
QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(organization, qualityProfile.getName(), qualityProfile.getLanguage(), session);
- if (profileDto != null) {
- changes.addAll(profileFactory.deleteTree(session, profileDto.getKey(), true));
- }
- QualityProfileDto newQProfileDto = profileFactory.create(session, organization, qualityProfile.getQProfileName(), qualityProfile.isDefault());
- for (org.sonar.api.rules.ActiveRule activeRule : qualityProfile.getActiveRules()) {
- RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
- RuleActivation activation = new RuleActivation(ruleKey);
- activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
- for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
- activation.setParameter(param.getKey(), param.getValue());
+ if (profileDto == null) {
+ profileDto = profileFactory.create(session, organization, qualityProfile.getQProfileName(), qualityProfile.isDefault());
+ for (org.sonar.api.rules.ActiveRule activeRule : qualityProfile.getActiveRules()) {
+ RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
+ RuleActivation activation = new RuleActivation(ruleKey);
+ activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
+ for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
+ activation.setParameter(param.getKey(), param.getValue());
+ }
+ changes.addAll(ruleActivator.activate(session, activation, profileDto));
}
- changes.addAll(ruleActivator.activate(session, activation, newQProfileDto));
}
LoadedTemplateDto template = new LoadedTemplateDto(organization.getUuid(), qualityProfile.getLoadedTemplateType());
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
index 1f157db5319..f5427e1ba58 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
@@ -19,11 +19,8 @@
*/
package org.sonar.server.qualityprofile;
-import com.google.common.collect.Lists;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
-import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
@@ -32,12 +29,10 @@ import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
-import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
import static org.sonar.server.ws.WsUtils.checkRequest;
/**
@@ -112,42 +107,6 @@ public class QProfileFactory {
// ------------- DELETION
/**
- * Delete the profile with specified key and all its descendants from database. Elasticsearch
- * is not touched and still needs to be clean-up.
- *
- * Session is NOT committed. Profiles marked as "default" for a language can't be deleted,
- * except if the parameter <code>force</code> is true.
- */
- public List<ActiveRuleChange> deleteTree(DbSession session, String key, boolean force) {
- QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(session, key);
- List<QualityProfileDto> descendants = db.qualityProfileDao().selectDescendants(session, key);
- if (!force) {
- checkNotDefault(profile);
- for (QualityProfileDto descendant : descendants) {
- checkNotDefault(descendant);
- }
- }
- // delete bottom-up
- List<ActiveRuleChange> changes = new ArrayList<>();
- for (QualityProfileDto descendant : Lists.reverse(descendants)) {
- changes.addAll(doDelete(session, descendant));
- }
- changes.addAll(doDelete(session, profile));
- return changes;
- }
-
- private List<ActiveRuleChange> doDelete(DbSession session, QualityProfileDto profile) {
- db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
- 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.getId());
- return changes;
- }
-
- /**
* Deletes the profiles with specified keys from database and Elasticsearch.
* All related information are deleted. The profiles marked as "default"
* are deleted too. Deleting a parent profile does not delete descendants
@@ -165,11 +124,4 @@ public class QProfileFactory {
}
}
- // ------------- DEFAULT PROFILE
-
- private static void checkNotDefault(QualityProfileDto p) {
- if (p.isDefault()) {
- throw BadRequestException.create("The profile marked as default can not be deleted: " + p.getKey());
- }
- }
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java
index 28050992bf3..7622a437294 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java
@@ -133,7 +133,7 @@ public class DefinedQProfileCreationImplTest {
}
@Test
- public void create_deletes_qp_if_already_exists() {
+ public void create_does_not_update_existing_profile_if_it_already_exists() {
OrganizationDto organization = dbTester.organizations().insert();
DefinedQProfile definedQProfile = definedQProfileRepositoryRule.create(FOO_LANGUAGE, "foo1", false);
long date = 2_456_789L;
@@ -149,8 +149,7 @@ public class DefinedQProfileCreationImplTest {
dbSession.commit();
QualityProfileDto dto = getPersistedQP(organization, FOO_LANGUAGE, "foo1");
- assertThat(dto.getId()).isNotEqualTo(existing.getId());
- assertThat(dto.getKey()).isNotEqualTo(existing.getKey());
+ assertThat(dto.getId()).isEqualTo(existing.getId());
assertThat(dbTester.countRowsOfTable(dbTester.getSession(), TABLE_RULES_PROFILES)).isEqualTo(1);
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
index 613ca756b84..08c4c9a04af 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
@@ -19,38 +19,22 @@
*/
package org.sonar.server.qualityprofile;
-import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.server.rule.RuleParamType;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.RowNotFoundException;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.qualityprofile.QualityProfileTesting;
-import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleParamDto;
-import org.sonar.db.rule.RuleTesting;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleIndexer;
-import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.getDefaultOrganization;
public class QProfileFactoryMediumTest {
@@ -63,8 +47,6 @@ public class QProfileFactoryMediumTest {
private DbClient db;
private DbSession dbSession;
- private ActiveRuleIndexer activeRuleIndexer;
- private RuleIndexer ruleIndexer;
private QProfileFactory factory;
private OrganizationDto organization;
@@ -74,8 +56,6 @@ public class QProfileFactoryMediumTest {
db = tester.get(DbClient.class);
dbSession = db.openSession(false);
factory = tester.get(QProfileFactory.class);
- activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
- ruleIndexer = tester.get(RuleIndexer.class);
organization = OrganizationTesting.newOrganizationDto();
db.organizationDao().insert(dbSession, organization);
}
@@ -185,129 +165,6 @@ public class QProfileFactoryMediumTest {
assertThat(factory.create(dbSession, organization, name, true)).isNotNull();
}
- @Test
- public void delete() {
- initRules();
- db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(organization.getUuid()));
- tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
- dbSession.commit();
- dbSession.clearCache();
-
- List<ActiveRuleChange> changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
- dbSession.commit();
- activeRuleIndexer.index(changes);
-
- dbSession.clearCache();
- assertThat(db.qualityProfileDao().selectAll(dbSession, getDefaultOrganization(tester, db, dbSession))).isEmpty();
- assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
- assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
- assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
- assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).isEmpty();
- }
-
- @Test
- public void delete_descendants() {
- initRules();
-
- // create parent and child profiles
- db.qualityProfileDao().insert(dbSession,
- QProfileTesting.newXooP1(organization),
- QProfileTesting.newXooP2(organization),
- QProfileTesting.newXooP3(organization));
- 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));
- changes.addAll(tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY));
- dbSession.commit();
- dbSession.clearCache();
- activeRuleIndexer.index(changes);
-
- assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(3);
- assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3);
-
- changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
- dbSession.commit();
- activeRuleIndexer.index(changes);
-
- dbSession.clearCache();
- assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).isEmpty();
- assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
- assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
- assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
- assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY)).isEmpty();
- assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P3_KEY)).isEmpty();
- }
-
- @Test
- public void do_not_delete_default_profile() {
- QualityProfileDto profile = QualityProfileTesting.newQualityProfileDto()
- .setOrganizationUuid(organization.getUuid())
- .setDefault(true);
- db.qualityProfileDao().insert(dbSession, profile);
-
- dbSession.commit();
-
- try {
- List<ActiveRuleChange> changes = factory.deleteTree(dbSession, profile.getKey(), false);
- dbSession.commit();
- activeRuleIndexer.index(changes);
- fail();
- } catch (BadRequestException e) {
- assertThat(e).hasMessage("The profile marked as default can not be deleted: " + profile.getKey());
- assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(1);
- }
- }
-
- @Test
- public void do_not_delete_if_default_descendant() {
- QualityProfileDto parent = QualityProfileTesting.newQualityProfileDto()
- .setOrganizationUuid(organization.getUuid());
- QualityProfileDto childNonDefault = QualityProfileTesting.newQualityProfileDto()
- .setOrganizationUuid(organization.getUuid());
- QualityProfileDto childDefault = QualityProfileTesting.newQualityProfileDto()
- .setOrganizationUuid(organization.getUuid())
- .setDefault(true);
- db.qualityProfileDao().insert(dbSession, parent, childNonDefault, childDefault);
-
- List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, childNonDefault.getKey(), parent.getKey());
- changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, childDefault.getKey(), parent.getKey()));
- dbSession.commit();
- dbSession.clearCache();
- activeRuleIndexer.index(changes);
-
- try {
- changes = factory.deleteTree(dbSession, parent.getKey(), false);
- dbSession.commit();
- activeRuleIndexer.index(changes);
- fail();
- } catch (BadRequestException e) {
- assertThat(e).hasMessage("The profile marked as default can not be deleted: " + childDefault.getKey());
- assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(3);
- }
- }
-
- @Test
- public void fail_if_unknown_profile_to_be_deleted() {
- thrown.expect(RowNotFoundException.class);
- thrown.expectMessage("Quality profile not found: XOO_P1");
-
- List<ActiveRuleChange> changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
- dbSession.commit();
- activeRuleIndexer.index(changes);
- }
-
- private void initRules() {
- // create pre-defined rules
- RuleDto xooRule1 = RuleTesting.newXooX1();
- RuleDto xooRule2 = RuleTesting.newXooX2();
- db.ruleDao().insert(dbSession, xooRule1);
- db.ruleDao().insert(dbSession, xooRule2);
- db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
- .setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
- dbSession.commit();
- dbSession.clearCache();
- ruleIndexer.index();
- }
-
private void expectBadRequestException(String message) {
thrown.expect(BadRequestException.class);
thrown.expectMessage(message);