aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserTokenNewValue.java7
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/user/TokenType.java (renamed from server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java)2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java8
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java2
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml4
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java15
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java36
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java2
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java1
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/UserTokenAuthentication.java1
-rw-r--r--server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/TokenGeneratorImplTest.java1
-rw-r--r--server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenAuthenticationTest.java1
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java2
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java2
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java12
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java8
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java2
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java2
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java51
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java8
20 files changed, 148 insertions, 19 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserTokenNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserTokenNewValue.java
index 48d060b23bd..96f2956a518 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserTokenNewValue.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserTokenNewValue.java
@@ -22,6 +22,7 @@ package org.sonar.db.audit.model;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.utils.DateUtils;
+import org.sonar.db.user.TokenType;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserTokenDto;
@@ -29,6 +30,7 @@ public class UserTokenNewValue extends NewValue {
@Nullable
private String tokenUuid;
+ @Nullable
private String userUuid;
@Nullable
@@ -66,6 +68,11 @@ public class UserTokenNewValue extends NewValue {
this.tokenName = tokenName;
}
+ public UserTokenNewValue(String projectKey) {
+ this.projectKey = projectKey;
+ this.type = TokenType.PROJECT_ANALYSIS_TOKEN.name();
+ }
+
@CheckForNull
public String getTokenUuid() {
return this.tokenUuid;
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/TokenType.java
index 0fcfbd7626a..3499f4cfd5f 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/TokenType.java
@@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.server.usertoken;
+package org.sonar.db.user;
public enum TokenType {
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java
index a3627d1ce63..e1191ee266c 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java
@@ -103,6 +103,14 @@ public class UserTokenDao implements Dao {
}
}
+ public void deleteByProjectKey(DbSession dbSession, String projectKey) {
+ int deletedRows = mapper(dbSession).deleteByProjectKey(projectKey);
+
+ if (deletedRows > 0) {
+ auditPersister.deleteUserToken(dbSession, new UserTokenNewValue(projectKey));
+ }
+ }
+
private static UserTokenMapper mapper(DbSession dbSession) {
return dbSession.getMapper(UserTokenMapper.class);
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java
index 2094fabd08c..435d445c35d 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java
@@ -38,5 +38,7 @@ public interface UserTokenMapper {
int deleteByUserUuidAndName(@Param("userUuid") String userUuid, @Param("name") String name);
+ int deleteByProjectKey(@Param("projectKey") String projectKey);
+
List<UserTokenCount> countTokensByUserUuids(@Param("userUuids") List<String> userUuids);
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml
index 3977a794045..ba43d789d86 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml
@@ -85,4 +85,8 @@
DELETE FROM user_tokens WHERE user_uuid=#{userUuid, jdbcType=VARCHAR} and name=#{name, jdbcType=VARCHAR}
</delete>
+ <delete id="deleteByProjectKey">
+ DELETE FROM user_tokens WHERE project_key=#{projectKey, jdbcType=VARCHAR}
+ </delete>
+
</mapper>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java
index 2568efd1b22..2f5dc9c7d89 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java
@@ -145,6 +145,21 @@ public class UserTokenDaoTest {
}
@Test
+ public void delete_tokens_by_projectKey() {
+ UserDto user1 = db.users().insertUser();
+ UserDto user2 = db.users().insertUser();
+ db.users().insertToken(user1, t -> t.setProjectKey("projectKey1"));
+ db.users().insertToken(user1, t -> t.setProjectKey("projectKey2"));
+ db.users().insertToken(user2, t -> t.setProjectKey("projectKey1"));
+
+ underTest.deleteByProjectKey(dbSession, "projectKey1");
+ db.commit();
+
+ assertThat(underTest.selectByUser(dbSession, user1)).hasSize(1);
+ assertThat(underTest.selectByUser(dbSession, user2)).isEmpty();
+ }
+
+ @Test
public void count_tokens_by_user() {
UserDto user = db.users().insertUser();
db.users().insertToken(user, t -> t.setName("name"));
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java
index 67da1273d81..f189d239757 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java
@@ -138,7 +138,7 @@ public class UserTokenDaoWithPersisterTest {
}
@Test
- public void delete_token_by_user_and_name_without_affected_rows_is_persisted() {
+ public void delete_token_by_user_and_name_without_affected_rows_is_not_persisted() {
UserDto user1 = db.users().insertUser();
underTest.deleteByUserAndName(dbSession, user1, "name");
@@ -146,4 +146,38 @@ public class UserTokenDaoWithPersisterTest {
verify(auditPersister).addUser(any(), any());
verifyNoMoreInteractions(auditPersister);
}
+
+
+ @Test
+ public void delete_token_by_projectKey_is_persisted() {
+ UserDto user1 = db.users().insertUser();
+ UserDto user2 = db.users().insertUser();
+ db.users().insertToken(user1, t -> t.setProjectKey("projectToDelete"));
+ db.users().insertToken(user1, t -> t.setProjectKey("projectToKeep"));
+ db.users().insertToken(user2, t -> t.setProjectKey("projectToDelete"));
+
+ underTest.deleteByProjectKey(dbSession, "projectToDelete");
+
+ assertThat(underTest.selectByUser(dbSession, user1)).hasSize(1);
+ assertThat(underTest.selectByUser(dbSession, user2)).isEmpty();
+ verify(auditPersister).deleteUserToken(eq(db.getSession()), newValueCaptor.capture());
+ assertThat(newValueCaptor.getValue())
+ .extracting(UserTokenNewValue::getProjectKey, UserTokenNewValue::getType)
+ .containsExactly("projectToDelete", "PROJECT_ANALYSIS_TOKEN");
+ }
+
+ @Test
+ public void delete_token_by_projectKey_without_affected_rows_is_not_persisted() {
+ UserDto user1 = db.users().insertUser();
+
+ db.users().insertToken(user1, t -> t.setProjectKey("projectToKeep"));
+
+ underTest.deleteByProjectKey(dbSession, "projectToDelete");
+
+ assertThat(underTest.selectByUser(dbSession, user1)).hasSize(1);
+
+ verify(auditPersister).addUser(any(), any());
+ verify(auditPersister).addUserToken(any(), any());
+ verifyNoMoreInteractions(auditPersister);
+ }
}
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
index b41644530bf..1df9e1f8dad 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
@@ -19,6 +19,8 @@
*/
package org.sonar.server.usertoken;
+import org.sonar.db.user.TokenType;
+
public interface TokenGenerator {
/**
* Generate a token. It must be unique and non deterministic.<br />
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
index 235a2fb0285..fee313bd1f0 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
@@ -22,6 +22,7 @@ package org.sonar.server.usertoken;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
+import org.sonar.db.user.TokenType;
public class TokenGeneratorImpl implements TokenGenerator {
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/UserTokenAuthentication.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/UserTokenAuthentication.java
index 1bed33677d6..03af54aa1c4 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/UserTokenAuthentication.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/UserTokenAuthentication.java
@@ -24,6 +24,7 @@ import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.user.TokenType;
import org.sonar.db.user.UserTokenDto;
import org.sonar.server.authentication.UserLastConnectionDatesUpdater;
diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/TokenGeneratorImplTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/TokenGeneratorImplTest.java
index d0816bca086..08a20be69b2 100644
--- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/TokenGeneratorImplTest.java
+++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/TokenGeneratorImplTest.java
@@ -20,6 +20,7 @@
package org.sonar.server.usertoken;
import org.junit.Test;
+import org.sonar.db.user.TokenType;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenAuthenticationTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenAuthenticationTest.java
index 7957812dab1..32b2c3948ba 100644
--- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenAuthenticationTest.java
+++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenAuthenticationTest.java
@@ -24,6 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
+import org.sonar.db.user.TokenType;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserTokenDto;
import org.sonar.server.authentication.UserLastConnectionDatesUpdater;
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java
index 6298e21679a..22111431fd7 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java
@@ -31,7 +31,7 @@ import org.sonar.db.project.ProjectDto;
import org.sonar.db.project.ProjectBadgeTokenDto;
import org.sonar.server.user.UserSession;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import org.sonarqube.ws.ProjectBadgeToken.TokenWsResponse;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java
index 23905c9d668..f89f55f4178 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java
@@ -29,7 +29,7 @@ import org.sonar.db.DbSession;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.user.UserSession;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java
index 630c3f55c60..2ca216902c0 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java
@@ -68,6 +68,7 @@ public class ComponentCleanerService {
public void delete(DbSession dbSession, ProjectDto project) {
dbClient.purgeDao().deleteProject(dbSession, project.getUuid(), project.getQualifier(), project.getName(), project.getKey());
dbClient.userDao().cleanHomepage(dbSession, project);
+ dbClient.userTokenDao().deleteByProjectKey(dbSession, project.getKey());
projectIndexers.commitAndIndexProjects(dbSession, singletonList(project), PROJECT_DELETION);
}
@@ -78,19 +79,20 @@ public class ComponentCleanerService {
}
public void delete(DbSession dbSession, ComponentDto project) {
- checkArgument(!hasNotProjectScope(project) && !isNotDeletable(project) && project.getMainBranchProjectUuid() == null, "Only projects can be deleted");
+ checkArgument(hasProjectScope(project) && isDeletable(project) && project.getMainBranchProjectUuid() == null, "Only projects can be deleted");
dbClient.purgeDao().deleteProject(dbSession, project.uuid(), project.qualifier(), project.name(), project.getKey());
dbClient.userDao().cleanHomepage(dbSession, project);
+ dbClient.userTokenDao().deleteByProjectKey(dbSession, project.getKey());
projectIndexers.commitAndIndexComponents(dbSession, singletonList(project), PROJECT_DELETION);
}
- private static boolean hasNotProjectScope(ComponentDto project) {
- return !Scopes.PROJECT.equals(project.scope());
+ private static boolean hasProjectScope(ComponentDto project) {
+ return Scopes.PROJECT.equals(project.scope());
}
- private boolean isNotDeletable(ComponentDto project) {
+ private boolean isDeletable(ComponentDto project) {
ResourceType resourceType = resourceTypes.get(project.qualifier());
// this essentially means PROJECTS, VIEWS and APPS (not SUBVIEWS)
- return resourceType == null || !resourceType.getBooleanProperty("deletable");
+ return resourceType != null && resourceType.getBooleanProperty("deletable");
}
}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java
index 79992ae442f..ccecb44f153 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java
@@ -31,7 +31,7 @@ import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserTokenDto;
import org.sonar.server.exceptions.ServerException;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import org.sonarqube.ws.UserTokens;
import org.sonarqube.ws.UserTokens.GenerateWsResponse;
@@ -39,9 +39,9 @@ import static com.google.common.base.Preconditions.checkArgument;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
-import static org.sonar.server.usertoken.TokenType.GLOBAL_ANALYSIS_TOKEN;
-import static org.sonar.server.usertoken.TokenType.PROJECT_ANALYSIS_TOKEN;
-import static org.sonar.server.usertoken.TokenType.USER_TOKEN;
+import static org.sonar.db.user.TokenType.GLOBAL_ANALYSIS_TOKEN;
+import static org.sonar.db.user.TokenType.PROJECT_ANALYSIS_TOKEN;
+import static org.sonar.db.user.TokenType.USER_TOKEN;
import static org.sonar.server.usertoken.ws.UserTokenSupport.ACTION_GENERATE;
import static org.sonar.server.usertoken.ws.UserTokenSupport.PARAM_LOGIN;
import static org.sonar.server.usertoken.ws.UserTokenSupport.PARAM_NAME;
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java
index 46fe9733d4b..b078773fc77 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java
@@ -29,7 +29,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java
index 65b2ad86325..37a02948c42 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java
@@ -33,7 +33,7 @@ import org.sonar.db.project.ProjectDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java
index 6e207d2e7de..21612c44734 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.component;
+import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.resources.ResourceType;
@@ -85,6 +86,38 @@ public class ComponentCleanerServiceTest {
}
@Test
+ public void delete_list_of_components_from_db() {
+ ComponentDto componentDto1 = db.components().insertPublicProject();
+ ComponentDto componentDto2 = db.components().insertPublicProject();
+ ComponentDto componentDto3 = db.components().insertPublicProject();
+
+ mockResourceTypeAsValidProject();
+
+ underTest.deleteComponents(dbSession, asList(componentDto1, componentDto2));
+ dbSession.commit();
+
+ assertNotExists(componentDto1);
+ assertNotExists(componentDto2);
+ assertExists(componentDto3);
+ }
+
+ @Test
+ public void fail_with_IAE_if_project_non_deletable() {
+ ComponentDto componentDto1 = db.components().insertPublicProject();
+ ComponentDto componentDto2 = db.components().insertPublicProject();
+
+ mockResourceTypeAsNonDeletable();
+
+ dbSession.commit();
+
+ List<ComponentDto> componentDtos = asList(componentDto1, componentDto2);
+
+ assertThatThrownBy(() -> underTest.deleteComponents(dbSession, componentDtos))
+ .withFailMessage("Only projects can be deleted")
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
public void delete_application_from_db_and_index() {
DbData data1 = insertProjectData();
DbData data2 = insertProjectData();
@@ -176,6 +209,12 @@ public class ComponentCleanerServiceTest {
when(mockResourceTypes.get(anyString())).thenReturn(resourceType);
}
+ private void mockResourceTypeAsNonDeletable() {
+ ResourceType resourceType = mock(ResourceType.class);
+ when(resourceType.getBooleanProperty("deletable")).thenReturn(false);
+ when(mockResourceTypes.get(anyString())).thenReturn(resourceType);
+ }
+
private void assertNotExists(DbData data) {
assertDataInDb(data, false);
assertThat(projectIndexers.hasBeenCalled(data.branch.getUuid(), PROJECT_DELETION)).isTrue();
@@ -198,6 +237,18 @@ public class ComponentCleanerServiceTest {
assertThat(dbClient.branchDao().selectByUuid(dbSession, appOrProject.getUuid()).isPresent()).isEqualTo(exists);
}
+ private void assertNotExists(ComponentDto componentDto) {
+ assertComponentExists(componentDto, false);
+ }
+
+ private void assertExists(ComponentDto componentDto) {
+ assertComponentExists(componentDto, true);
+ }
+
+ private void assertComponentExists(ComponentDto componentDto, boolean exists) {
+ assertThat(dbClient.componentDao().selectByUuid(dbSession, componentDto.uuid()).isPresent()).isEqualTo(exists);
+ }
+
private static class DbData {
final ProjectDto project;
final BranchDto branch;
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java
index 3928d32c23d..a56be8b720c 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java
@@ -35,7 +35,7 @@ import org.sonar.server.exceptions.ServerException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usertoken.TokenGenerator;
-import org.sonar.server.usertoken.TokenType;
+import org.sonar.db.user.TokenType;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.MediaTypes;
@@ -47,9 +47,9 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.permission.GlobalPermission.SCAN;
-import static org.sonar.server.usertoken.TokenType.GLOBAL_ANALYSIS_TOKEN;
-import static org.sonar.server.usertoken.TokenType.PROJECT_ANALYSIS_TOKEN;
-import static org.sonar.server.usertoken.TokenType.USER_TOKEN;
+import static org.sonar.db.user.TokenType.GLOBAL_ANALYSIS_TOKEN;
+import static org.sonar.db.user.TokenType.PROJECT_ANALYSIS_TOKEN;
+import static org.sonar.db.user.TokenType.USER_TOKEN;
import static org.sonar.server.usertoken.ws.UserTokenSupport.PARAM_LOGIN;
import static org.sonar.server.usertoken.ws.UserTokenSupport.PARAM_NAME;
import static org.sonar.server.usertoken.ws.UserTokenSupport.PARAM_PROJECT_KEY;