aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2017-03-21 16:03:45 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-03-23 17:38:34 +0100
commitb358fd07398a5179b34905cedc9bae32f9ac76ee (patch)
tree0c0f71c380db200dca9ca5cfb90c45107ae90ada
parent48121158e6ad4f6c65fa180609d9de670dc77ef7 (diff)
downloadsonarqube-b358fd07398a5179b34905cedc9bae32f9ac76ee.tar.gz
sonarqube-b358fd07398a5179b34905cedc9bae32f9ac76ee.zip
SONAR-8889 drop Quality profiles when deleting an organization
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java17
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java24
2 files changed, 39 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java
index 8375c73a9ee..fd412a9c6b6 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java
@@ -23,14 +23,17 @@ import java.util.List;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.organization.DefaultOrganization;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.OrganizationFlags;
+import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
@@ -48,14 +51,16 @@ public class DeleteAction implements OrganizationsWsAction {
private final DefaultOrganizationProvider defaultOrganizationProvider;
private final ComponentCleanerService componentCleanerService;
private final OrganizationFlags organizationFlags;
+ private final QProfileFactory qProfileFactory;
public DeleteAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
- ComponentCleanerService componentCleanerService, OrganizationFlags organizationFlags) {
+ ComponentCleanerService componentCleanerService, OrganizationFlags organizationFlags, QProfileFactory qProfileFactory) {
this.userSession = userSession;
this.dbClient = dbClient;
this.defaultOrganizationProvider = defaultOrganizationProvider;
this.componentCleanerService = componentCleanerService;
this.organizationFlags = organizationFlags;
+ this.qProfileFactory = qProfileFactory;
}
@Override
@@ -99,6 +104,7 @@ public class DeleteAction implements OrganizationsWsAction {
deleteProjects(dbSession, organization);
deletePermissions(dbSession, organization);
deleteGroups(dbSession, organization);
+ deleteQualityProfiles(dbSession, organization);
deleteOrganization(dbSession, organization);
response.noContent();
@@ -124,6 +130,15 @@ public class DeleteAction implements OrganizationsWsAction {
dbSession.commit();
}
+ private void deleteQualityProfiles(DbSession dbSession, OrganizationDto organization) {
+ List<QualityProfileDto> profiles = dbClient.qualityProfileDao().selectAll(dbSession, organization);
+ List<String> profileKeys = profiles.stream()
+ .map(QualityProfileDto::getKey)
+ .collect(Collectors.toArrayList(profiles.size()));
+ qProfileFactory.deleteByKeys(dbSession, profileKeys);
+ dbSession.commit();
+ }
+
private void deleteOrganization(DbSession dbSession, OrganizationDto organization) {
dbClient.organizationMemberDao().deleteByOrganizationUuid(dbSession, organization.getUuid());
dbClient.organizationDao().deleteByUuid(dbSession, organization.getUuid());
diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java
index 4b67ebadaf2..61919a60993 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java
@@ -27,6 +27,7 @@ import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
@@ -34,6 +35,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentCleanerService;
@@ -42,6 +44,8 @@ import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.organization.TestOrganizationFlags;
+import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
@@ -67,7 +71,8 @@ public class DeleteActionTest {
private ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class);
private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone().setEnabled(true);
private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private DeleteAction underTest = new DeleteAction(userSession, db.getDbClient(), defaultOrganizationProvider, componentCleanerService, organizationFlags);
+ private QProfileFactory qProfileFactory = new QProfileFactory(dbClient, mock(UuidFactory.class), System2.INSTANCE, mock(ActiveRuleIndexer.class));
+ private DeleteAction underTest = new DeleteAction(userSession, db.getDbClient(), defaultOrganizationProvider, componentCleanerService, organizationFlags, qProfileFactory);
private WsActionTester wsTester = new WsActionTester(underTest);
@Test
@@ -295,6 +300,23 @@ public class DeleteActionTest {
assertThat(db.getDbClient().organizationMemberDao().select(db.getSession(), otherOrg.getUuid(), user1.getId())).isPresent();
}
+ @Test
+ public void request_also_deletes_quality_profiles_of_specified_organization() {
+ OrganizationDto org = db.organizations().insert();
+ OrganizationDto otherOrg = db.organizations().insert();
+ QualityProfileDto profileInOrg = db.qualityProfiles().insert(org);
+ QualityProfileDto profileInOtherOrg = db.qualityProfiles().insert(otherOrg);
+
+ logInAsAdministrator(org);
+
+ sendRequest(org);
+
+ verifyOrganizationDoesNotExist(org);
+ assertThat(db.select("select kee as \"profileKey\" from rules_profiles"))
+ .extracting(row -> (String) row.get("profileKey"))
+ .containsOnly(profileInOtherOrg.getKey());
+ }
+
private void verifyOrganizationDoesNotExist(OrganizationDto organization) {
assertThat(db.getDbClient().organizationDao().selectByKey(session, organization.getKey()))
.isEmpty();