From: Julien Lancelot Date: Wed, 15 Jun 2016 08:57:58 +0000 (+0200) Subject: SONAR-7746 Delete permission template characteristics when deleting a permission... X-Git-Tag: 6.0-RC1~302 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7e5f6db6be68c6451ea728ca60f07527776eeebf;p=sonarqube.git SONAR-7746 Delete permission template characteristics when deleting a permission template --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java index bbeecea43c5..7e5b5f56fde 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java @@ -19,6 +19,13 @@ */ package org.sonar.server.permission.ws; +import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; +import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters; +import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef; +import static org.sonar.server.ws.WsUtils.checkRequest; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; + import java.util.Set; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -30,13 +37,6 @@ import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder; import org.sonar.server.user.UserSession; import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest; -import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; -import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters; -import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef; -import static org.sonar.server.ws.WsUtils.checkRequest; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; - public class DeleteTemplateAction implements PermissionsWsAction { private final DbClient dbClient; private final UserSession userSession; diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java index fb26767c883..e0fc453cf1d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java @@ -19,7 +19,17 @@ */ package org.sonar.server.permission.ws.template; +import static com.google.common.primitives.Longs.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.internal.util.collections.Sets.newSet; +import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; + import java.util.Collections; +import java.util.Date; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; @@ -34,6 +44,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupTesting; import org.sonar.db.user.UserDto; @@ -51,14 +62,6 @@ import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.mockito.internal.util.collections.Sets.newSet; -import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; - public class DeleteTemplateActionTest { static final String TEMPLATE_UUID = "permission-template-uuid"; @@ -153,6 +156,22 @@ public class DeleteTemplateActionTest { newRequest(null); } + @Test + public void delete_perm_tpl_characteristic_when_delete_template() throws Exception { + dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(permissionTemplate.getId()) + .setWithProjectCreator(true) + .setCreatedAt(new Date().getTime()) + .setUpdatedAt(new Date().getTime())); + dbSession.commit(); + assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).hasSize(1); + + newRequest(TEMPLATE_UUID); + + assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).isEmpty(); + } + private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) { dbClient.permissionTemplateDao().insert(dbSession, template); UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true)); diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java index c802286ee81..61869c93115 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java @@ -19,6 +19,10 @@ */ package org.sonar.db.permission; +import static com.google.common.collect.Maps.newHashMap; +import static java.lang.String.format; +import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; + import com.google.common.annotations.VisibleForTesting; import java.util.Date; import java.util.HashMap; @@ -36,10 +40,7 @@ import org.sonar.api.web.UserRole; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; - -import static com.google.common.collect.Maps.newHashMap; -import static java.lang.String.format; -import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper; public class PermissionTemplateDao implements Dao { @@ -202,10 +203,6 @@ public class PermissionTemplateDao implements Dao { return mapper(dbSession).countAll(upperCasedNameQuery); } - public int countAll(DbSession session) { - return mapper(session).countAll(null); - } - private static String toUppercaseSqlQuery(String nameMatch) { String wildcard = "%"; return format("%s%s%s", wildcard, nameMatch.toUpperCase(Locale.ENGLISH), wildcard); @@ -254,6 +251,7 @@ public class PermissionTemplateDao implements Dao { PermissionTemplateMapper mapper = mapper(session); mapper.deleteUserPermissions(templateId); mapper.deleteGroupPermissions(templateId); + session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId); mapper.delete(templateId); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java index 05e9ff0360f..51204200c87 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java @@ -20,15 +20,15 @@ package org.sonar.db.permission.template; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Collections.emptyList; +import static java.util.Objects.requireNonNull; + import java.util.List; import java.util.Optional; import org.sonar.db.Dao; import org.sonar.db.DbSession; -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Collections.emptyList; -import static java.util.Objects.requireNonNull; - public class PermissionTemplateCharacteristicDao implements Dao { public List selectByTemplateIds(DbSession dbSession, List templateIds) { return templateIds.isEmpty() ? emptyList() : mapper(dbSession).selectByTemplateIds(templateIds); diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java index 666a7d19fdb..4cd80559984 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java @@ -33,4 +33,6 @@ public interface PermissionTemplateCharacteristicMapper { void insert(PermissionTemplateCharacteristicDto templatePermissionDto); void update(PermissionTemplateCharacteristicDto templatePermissionDto); + + void deleteByTemplateId(long id); } diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml index ae90a6cd456..59834cca7ad 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml @@ -49,4 +49,9 @@ updated_at=#{updatedAt, jdbcType=BIGINT} where id=#{id} + + + DELETE FROM perm_tpl_characteristics + WHERE template_id = #{permissionTemplateId} + diff --git a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java index ca3dbee1f0a..b99319fa790 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java @@ -19,6 +19,17 @@ */ package org.sonar.db.permission; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.web.UserRole.ADMIN; +import static org.sonar.api.web.UserRole.CODEVIEWER; +import static org.sonar.api.web.UserRole.ISSUE_ADMIN; +import static org.sonar.api.web.UserRole.USER; +import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto; +import static org.sonar.db.user.GroupTesting.newGroupDto; +import static org.sonar.db.user.UserTesting.newUserDto; + import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -38,17 +49,6 @@ import org.sonar.db.DbTester; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.api.web.UserRole.ADMIN; -import static org.sonar.api.web.UserRole.CODEVIEWER; -import static org.sonar.api.web.UserRole.ISSUE_ADMIN; -import static org.sonar.api.web.UserRole.USER; -import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto; -import static org.sonar.db.user.GroupTesting.newGroupDto; -import static org.sonar.db.user.UserTesting.newUserDto; - public class PermissionTemplateDaoTest { @@ -159,6 +159,7 @@ public class PermissionTemplateDaoTest { session.commit(); checkTemplateTables("deletePermissionTemplate-result.xml"); + db.assertDbUnitTable(getClass(), "deletePermissionTemplate-result.xml", "perm_tpl_characteristics"); } @Test diff --git a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java index 9d5f97bcedd..ba2f29918ff 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java @@ -20,6 +20,11 @@ package org.sonar.db.permission.template; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.primitives.Longs.asList; +import static java.util.Collections.emptyList; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.List; import java.util.Optional; import org.junit.Rule; @@ -30,10 +35,6 @@ import org.sonar.api.web.UserRole; import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import static com.google.common.collect.Lists.newArrayList; -import static java.util.Collections.emptyList; -import static org.assertj.core.api.Assertions.assertThat; - public class PermissionTemplateCharacteristicDaoTest { @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -179,4 +180,28 @@ public class PermissionTemplateCharacteristicDaoTest { .setCreatedAt(123_456_789L) .setUpdatedAt(2_000_000_000L)); } + + @Test + public void delete_by_permission_template_id() throws Exception { + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(123_456_789L) + .setUpdatedAt(2_000_000_000L)); + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(2L) + .setWithProjectCreator(true) + .setCreatedAt(123_456_789L) + .setUpdatedAt(2_000_000_000L)); + + assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(1); + assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(2); + + dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(1L); + + assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(0); + assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(1); + } } diff --git a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate-result.xml b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate-result.xml index 115b732cb64..29e39f9d91b 100644 --- a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate-result.xml @@ -2,7 +2,9 @@ - + - + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate.xml b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate.xml index 0520355af31..ad3dea1aabf 100644 --- a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate.xml +++ b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate.xml @@ -7,8 +7,14 @@ + + + + + +