]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15142 - Persisting deletes only when deletedRows > 0
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Tue, 17 Aug 2021 07:35:50 +0000 (09:35 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 19 Aug 2021 20:08:15 +0000 (20:08 +0000)
40 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ProjectNewValue.java [deleted file]
server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookMapper.java
server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/ProjectNewValueTest.java [deleted file]
server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/GroupDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserPropertiesDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoWithPersisterTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java

index f2d29cf5313c63ca9289ce9c6019c9a04d792b94..e8ec8d446dcc6c87ee6ce6d4db320e8209297eec 100644 (file)
@@ -82,22 +82,22 @@ public class AlmPatDao implements Dao {
   }
 
   public void delete(DbSession dbSession, AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) {
-    getMapper(dbSession).deleteByUuid(almPatDto.getUuid());
-    if (auditPersister != null) {
+    int deletedRows = getMapper(dbSession).deleteByUuid(almPatDto.getUuid());
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey));
     }
   }
 
   public void deleteByUser(DbSession dbSession, UserDto user) {
-    getMapper(dbSession).deleteByUser(user.getUuid());
-    if (auditPersister != null) {
+    int deletedRows = getMapper(dbSession).deleteByUser(user.getUuid());
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(user));
     }
   }
 
   public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) {
-    getMapper(dbSession).deleteByAlmSetting(almSetting.getUuid());
-    if (auditPersister != null) {
+    int deletedRows = getMapper(dbSession).deleteByAlmSetting(almSetting.getUuid());
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almSetting));
     }
   }
index 3f2e0c255d8bd07ac6a3cea7cdd59dfa9372822d..32cd52f7b92b27ff8c85a18ba58dd32adea4e72d 100644 (file)
@@ -34,9 +34,9 @@ public interface AlmPatMapper {
 
   void update(@Param("dto") AlmPatDto almPatDto);
 
-  void deleteByUuid(@Param("uuid") String uuid);
+  int deleteByUuid(@Param("uuid") String uuid);
 
-  void deleteByUser(@Param("userUuid") String userUuid);
+  int deleteByUser(@Param("userUuid") String userUuid);
 
-  void deleteByAlmSetting(@Param("almSettingUuid") String almSettingUuid);
+  int deleteByAlmSetting(@Param("almSettingUuid") String almSettingUuid);
 }
index 35f8ae7fa2e5db73b9f94a95a02c461a5381a7d3..973e7e82f446e48a4cae6213427148d6f8609088 100644 (file)
@@ -79,9 +79,9 @@ public class AlmSettingDao implements Dao {
   }
 
   public void delete(DbSession dbSession, AlmSettingDto almSettingDto) {
-    getMapper(dbSession).deleteByKey(almSettingDto.getKey());
+    int deletedRows = getMapper(dbSession).deleteByKey(almSettingDto.getKey());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto.getUuid(), almSettingDto.getKey()));
     }
   }
index c45a34c45b8fa7b9bf625ed4d94adfe8c62ccba3..bd0397abba30f391c0ce381d5ff32fd750925a91 100644 (file)
@@ -39,5 +39,5 @@ public interface AlmSettingMapper {
 
   void update(@Param("dto") AlmSettingDto almSettingDto);
 
-  void deleteByKey(@Param("key") String key);
+  int deleteByKey(@Param("key") String key);
 }
index 6333987cbbd4b0e769bab4e8f0e93b33c76aac41..9668dcbda25f5c2865f15a74a98559af87910782 100644 (file)
@@ -73,9 +73,9 @@ public class ProjectAlmSettingDao implements Dao {
   }
 
   public void deleteByProject(DbSession dbSession, ProjectDto project) {
-    getMapper(dbSession).deleteByProjectUuid(project.getUuid());
+    int deletedRows = getMapper(dbSession).deleteByProjectUuid(project.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(project));
     }
   }
index 7c25eb34497f346a8e3f9a4360ab202938fbdcf9..fc4342f8071ae409ed3d7adb29856b3434de2de9 100644 (file)
@@ -34,7 +34,7 @@ public interface ProjectAlmSettingMapper {
 
   int update(@Param("dto") ProjectAlmSettingDto projectAlmSettingDto, @Param("now") long now);
 
-  void deleteByProjectUuid(@Param("projectUuid") String projectUuid);
+  int deleteByProjectUuid(@Param("projectUuid") String projectUuid);
   void deleteByAlmSettingUuid(@Param("almSettingUuid") String almSettingUuid);
 
   List<ProjectAlmSettingDto> selectByAlmSettingAndSlugs(@Param("almSettingUuid") String almSettingUuid, @Param("slugs") List<String> slugs);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ProjectNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ProjectNewValue.java
deleted file mode 100644 (file)
index ffe6c8a..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * 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.db.audit.model;
-
-import org.apache.commons.lang.ObjectUtils;
-
-public class ProjectNewValue extends NewValue{
-
-  private final String uuid;
-  private String name;
-  private String description;
-  private Boolean isPrivate;
-
-  public ProjectNewValue(String uuid, boolean isPrivate, String name, String description) {
-    this.uuid = uuid;
-    this.isPrivate = isPrivate;
-    this.name = name;
-    this.description = description;
-  }
-
-  public String getUuid() {
-    return uuid;
-  }
-
-  public String getDescription() {
-    return description;
-  }
-
-  public boolean isPrivate() {
-    return isPrivate;
-  }
-
-  public String getName() {
-    return name;
-  }
-
-  @Override
-  public String toString() {
-    StringBuilder sb = new StringBuilder("{");
-    addField(sb, "\"projectUuid\": ", this.uuid, true);
-    addField(sb, "\"description\": ", this.description, true);
-    addField(sb, "\"name\": ", this.name, true);
-    addField(sb, "\"isPrivate\": ", ObjectUtils.toString(this.isPrivate), false);
-    endString(sb);
-    return sb.toString();
-  }
-}
index 9f6aef56f4c72015514d0cf9378c70012e9c2ade..a95cccfdf584bec7fb356c4e5f1c9c073a92758e 100644 (file)
@@ -137,9 +137,9 @@ public class GroupPermissionDao implements Dao {
    * Delete all the permissions associated to a root component (project)
    */
   public void deleteByRootComponentUuid(DbSession dbSession, ComponentDto component) {
-    mapper(dbSession).deleteByRootComponentUuid(component.uuid());
+    int deletedRecords = mapper(dbSession).deleteByRootComponentUuid(component.uuid());
 
-    if (auditPersister != null) {
+    if (deletedRecords > 0 && auditPersister != null) {
       auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(null, null, null, component.uuid(), component.name(),
         component.qualifier(), null));
     }
@@ -152,7 +152,7 @@ public class GroupPermissionDao implements Dao {
   public int deleteByRootComponentUuidAndGroupUuid(DbSession dbSession, @Nullable String groupUuid, ComponentDto component) {
     int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndGroupUuid(component.uuid(), groupUuid);
 
-    if (auditPersister != null) {
+    if (deletedRecords > 0 && auditPersister != null) {
       auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(null, groupUuid, "", component.uuid(), component.name(),
         component.qualifier(), null));
     }
@@ -162,7 +162,7 @@ public class GroupPermissionDao implements Dao {
   public int deleteByRootComponentUuidForAnyOne(DbSession dbSession, ComponentDto component) {
     int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndGroupUuid(component.uuid(), null);
 
-    if (auditPersister != null) {
+    if (deletedRecords > 0 && auditPersister != null) {
       auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(null, null, null, component.uuid(),
         component.name(), component.qualifier(), null));
     }
@@ -176,7 +176,7 @@ public class GroupPermissionDao implements Dao {
   public int deleteByRootComponentUuidAndPermission(DbSession dbSession, String permission, ComponentDto component) {
     int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndPermission(component.uuid(), permission);
 
-    if (auditPersister != null) {
+    if (deletedRecords > 0 && auditPersister != null) {
       auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(permission, null, null, component.uuid(),
         component.name(), component.qualifier(), null));
     }
@@ -201,9 +201,9 @@ public class GroupPermissionDao implements Dao {
   public void delete(DbSession dbSession, String permission,
     @Nullable String groupUuid, @Nullable String groupName, @Nullable String rootComponentUuid, @Nullable ComponentDto componentDto) {
 
-    mapper(dbSession).delete(permission, groupUuid, rootComponentUuid);
+    int deletedRecords = mapper(dbSession).delete(permission, groupUuid, rootComponentUuid);
 
-    if (auditPersister != null) {
+    if (deletedRecords > 0 && auditPersister != null) {
       String projectName = (componentDto != null) ? componentDto.name() : null;
       String qualifier = (componentDto != null) ? componentDto.qualifier() : null;
       auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(permission, groupUuid, groupName, rootComponentUuid,
index 86f3c47c394b6e3e92b3c176cac5b50d44010fcb..441a514c0e3955f2cf321c5e51b9d1f651512abc 100644 (file)
@@ -39,7 +39,7 @@ public interface GroupPermissionMapper {
 
   void insert(GroupPermissionDto dto);
 
-  void delete(@Param("permission") String permission, @Nullable @Param("groupUuid") String groupUuid, @Nullable @Param("rootComponentUuid") String rootComponentUuid);
+  int delete(@Param("permission") String permission, @Nullable @Param("groupUuid") String groupUuid, @Nullable @Param("rootComponentUuid") String rootComponentUuid);
 
   List<String> selectGlobalPermissionsOfGroup(@Nullable @Param("groupUuid") String groupUuid);
 
@@ -54,7 +54,7 @@ public interface GroupPermissionMapper {
    */
   Set<String> selectGroupUuidsWithPermissionOnProjectBut(@Param("projectUuid") String projectUuid, @Param("role") String permission);
 
-  void deleteByRootComponentUuid(@Param("rootComponentUuid") String rootComponentUuid);
+  int deleteByRootComponentUuid(@Param("rootComponentUuid") String rootComponentUuid);
 
   int deleteByRootComponentUuidAndGroupUuid(@Param("rootComponentUuid") String rootComponentUuid, @Nullable @Param("groupUuid") String groupUuid);
 
index 42661794fbfb1bca24a69ed13599931e361ddc29..94868a084258b6668b69d6ba5d23e185eb413e37 100644 (file)
@@ -127,9 +127,9 @@ public class UserPermissionDao implements Dao {
    * Removes a single global permission from user
    */
   public void deleteGlobalPermission(DbSession dbSession, String userUuid, String permission) {
-    mapper(dbSession).deleteGlobalPermission(userUuid, permission);
+    int deletedRows = mapper(dbSession).deleteGlobalPermission(userUuid, permission);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, null,
         null, null, null, userUuid));
     }
@@ -139,9 +139,9 @@ public class UserPermissionDao implements Dao {
    * Removes a single project permission from user
    */
   public void deleteProjectPermission(DbSession dbSession, String userUuid, String permission, ComponentDto component) {
-    mapper(dbSession).deleteProjectPermission(userUuid, permission, component.uuid());
+    int deletedRows = mapper(dbSession).deleteProjectPermission(userUuid, permission, component.uuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, null, component.uuid(),
         component.name(), component.qualifier(), userUuid));
     }
@@ -151,9 +151,9 @@ public class UserPermissionDao implements Dao {
    * Deletes all the permissions defined on a project
    */
   public void deleteProjectPermissions(DbSession dbSession, ComponentDto component) {
-    mapper(dbSession).deleteProjectPermissions(component.uuid());
+    int deletedRows = mapper(dbSession).deleteProjectPermissions(component.uuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(null, null, null, component.uuid(), component.name(),
         component.qualifier(), null));
     }
@@ -165,7 +165,7 @@ public class UserPermissionDao implements Dao {
   public int deleteProjectPermissionOfAnyUser(DbSession dbSession, String permission, ComponentDto project) {
     int deletedRows = mapper(dbSession).deleteProjectPermissionOfAnyUser(project.uuid(), permission);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, null, project.uuid(), project.name(),
         project.qualifier(), null));
     }
@@ -174,9 +174,9 @@ public class UserPermissionDao implements Dao {
   }
 
   public void deleteByUserUuid(DbSession dbSession, String userUuid) {
-    mapper(dbSession).deleteByUserUuid(userUuid);
+    int deletedRows = mapper(dbSession).deleteByUserUuid(userUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(null, null, null, null,
         null, null, userUuid));
     }
index 6ac031ecf2142a8ae587938b0cbe41267bdff0f6..26e06af5e6eeae85a17b784f49d16c9f9911cd27 100644 (file)
@@ -54,12 +54,12 @@ public interface UserPermissionMapper {
 
   void insert(@Param("dto")UserPermissionDto dto);
 
-  void deleteGlobalPermission(@Param("userUuid") String userUuid, @Param("permission") String permission);
+  int deleteGlobalPermission(@Param("userUuid") String userUuid, @Param("permission") String permission);
 
-  void deleteProjectPermission(@Param("userUuid") String userUuid, @Param("permission") String permission,
+  int deleteProjectPermission(@Param("userUuid") String userUuid, @Param("permission") String permission,
     @Param("projectUuid") String projectUuid);
 
-  void deleteProjectPermissions(@Param("projectUuid") String projectUuid);
+  int deleteProjectPermissions(@Param("projectUuid") String projectUuid);
 
   int deleteProjectPermissionOfAnyUser(@Param("projectUuid") String projectUuid, @Param("permission") String permission);
 
@@ -67,5 +67,5 @@ public interface UserPermissionMapper {
 
   List<String> selectProjectPermissionsOfUser(@Param("userUuid") String userUuid, @Param("projectUuid") String projectUuid);
 
-  void deleteByUserUuid(@Param("userUuid") String userUuid);
+  int deleteByUserUuid(@Param("userUuid") String userUuid);
 }
index 8bf9c63f846919a9ab703af6e68f3d13b8420d29..4aa396f4456feddb2a8f9c49bb9409f0446ffa97 100644 (file)
@@ -170,9 +170,9 @@ public class PermissionTemplateDao implements Dao {
     mapper.deleteUserPermissionsByTemplateUuid(templateUuid);
     mapper.deleteGroupPermissionsByTemplateUuid(templateUuid);
     session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateUuid(templateUuid);
-    mapper.deleteByUuid(templateUuid);
+    int deletedRows = mapper.deleteByUuid(templateUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deletePermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName));
     }
   }
@@ -211,9 +211,9 @@ public class PermissionTemplateDao implements Dao {
       .setTemplateUuid(templateUuid)
       .setPermission(permission)
       .setUserUuid(userUuid);
-    mapper(session).deleteUserPermission(permissionTemplateUser);
+    int deletedRows = mapper(session).deleteUserPermission(permissionTemplateUser);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserFromPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName,
         permission, userUuid, userLogin, null, null));
     }
@@ -222,9 +222,9 @@ public class PermissionTemplateDao implements Dao {
   }
 
   public void deleteUserPermissionsByUserUuid(DbSession dbSession, String userUuid, String userLogin) {
-    mapper(dbSession).deleteUserPermissionsByUserUuid(userUuid);
+    int deletedRows = mapper(dbSession).deleteUserPermissionsByUserUuid(userUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserFromPermissionTemplate(dbSession, new PermissionTemplateNewValue(null, null,
         null, userUuid, userLogin, null, null));
     }
@@ -262,9 +262,9 @@ public class PermissionTemplateDao implements Dao {
       .setTemplateUuid(templateUuid)
       .setPermission(permission)
       .setGroupUuid(groupUuid);
-    mapper(session).deleteGroupPermission(permissionTemplateGroup);
+    int deletedRows = mapper(session).deleteGroupPermission(permissionTemplateGroup);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplateGroup.getTemplateUuid(), templateName,
         permissionTemplateGroup.getPermission(), null, null, permissionTemplateGroup.getGroupUuid(), groupName));
     }
@@ -284,9 +284,9 @@ public class PermissionTemplateDao implements Dao {
    * Remove a group from all templates (used when removing a group)
    */
   public void deleteByGroup(DbSession session, String groupUuid, String groupName) {
-    session.getMapper(PermissionTemplateMapper.class).deleteByGroupUuid(groupUuid);
+    int deletedRows = session.getMapper(PermissionTemplateMapper.class).deleteByGroupUuid(groupUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(null, null,
         null, null, null, groupUuid, groupName));
     }
index 4e653b357732a56e4d3813946736d6920e555e00..4f9373724d04adc6283b2d613306fb700e5f36f4 100644 (file)
@@ -36,7 +36,7 @@ public interface PermissionTemplateMapper {
 
   void update(PermissionTemplateDto permissionTemplate);
 
-  void deleteByUuid(String templateUuid);
+  int deleteByUuid(String templateUuid);
 
   void deleteByUuids(@Param("templateUuids") List<String> templateUuids);
 
@@ -44,15 +44,15 @@ public interface PermissionTemplateMapper {
 
   void deleteUserPermissionsByTemplateUuids(@Param("templateUuids") List<String> templateUuids);
 
-  void deleteUserPermissionsByUserUuid(@Param("userUuid") String userUuid);
+  int deleteUserPermissionsByUserUuid(@Param("userUuid") String userUuid);
 
-  void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
+  int deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
 
   void deleteGroupPermissionsByTemplateUuid(String templateUuid);
 
   void deleteGroupPermissionsByTemplateUuids(@Param("templateUuids") List<String> templateUuids);
 
-  void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
+  int deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
 
   PermissionTemplateDto selectByUuid(String templateUuid);
 
@@ -64,7 +64,7 @@ public interface PermissionTemplateMapper {
 
   void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
 
-  void deleteByGroupUuid(String groupUuid);
+  int deleteByGroupUuid(String groupUuid);
 
   PermissionTemplateDto selectByName(@Param("name") String name);
 
index 50ea889d17de38cf5bc76cb61edb833be1a0a270..45a32cc1906eee6cf76112a55cf4d25bd9335524 100644 (file)
@@ -103,7 +103,7 @@ public class ProjectDao implements Dao {
     return mapper(session).selectByUuids(uuids);
   }
 
-  public void updateVisibility(DbSession session, String uuid, boolean isPrivate, String qualifier, String name) {
+  public void updateVisibility(DbSession session, String uuid, boolean isPrivate) {
     mapper(session).updateVisibility(uuid, isPrivate, system2.now());
   }
 
index 29028f2c6fb0ae4b7beea022d1c6b935f7858046..ab1fa9fa6cb7424d92b25b4dc04f33920c0694dd 100644 (file)
@@ -263,7 +263,7 @@ public class PropertiesDao implements Dao {
   public int deleteByQuery(DbSession dbSession, PropertyQuery query) {
     int deletedRows = getMapper(dbSession).deleteByQuery(query);
 
-    if (auditPersister != null && query.key() != null && auditPersister.isTrackedProperty(query.key())) {
+    if (deletedRows > 0 && auditPersister != null && query.key() != null && auditPersister.isTrackedProperty(query.key())) {
       auditPersister.deleteProperty(dbSession, new PropertyNewValue(query.key(), query.componentUuid(),
         null, null, query.userUuid()), false);
     }
@@ -274,7 +274,7 @@ public class PropertiesDao implements Dao {
   public int delete(DbSession dbSession, PropertyDto dto, @Nullable String userLogin, @Nullable String projectName, @Nullable String qualifier) {
     int deletedRows = getMapper(dbSession).delete(dto.getKey(), dto.getUserUuid(), dto.getComponentUuid());
 
-    if (auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) {
+    if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) {
       auditPersister.deleteProperty(dbSession, new PropertyNewValue(dto, userLogin, projectName, qualifier), false);
     }
     return deletedRows;
@@ -288,18 +288,18 @@ public class PropertiesDao implements Dao {
   }
 
   public void deleteProjectProperty(DbSession session, String key, String projectUuid, String projectName, String qualifier) {
-    getMapper(session).deleteProjectProperty(key, projectUuid);
+    int deletedRows = getMapper(session).deleteProjectProperty(key, projectUuid);
 
-    if (auditPersister != null && auditPersister.isTrackedProperty(key)) {
+    if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) {
       auditPersister.deleteProperty(session, new PropertyNewValue(key, projectUuid, projectName, qualifier,
         null), false);
     }
   }
 
   public void deleteProjectProperties(String key, String value, DbSession session) {
-    getMapper(session).deleteProjectProperties(key, value);
+    int deletedRows = getMapper(session).deleteProjectProperties(key, value);
 
-    if (auditPersister != null && auditPersister.isTrackedProperty(key)) {
+    if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) {
       auditPersister.deleteProperty(session, new PropertyNewValue(key, value), false);
     }
   }
@@ -312,9 +312,9 @@ public class PropertiesDao implements Dao {
   }
 
   public void deleteGlobalProperty(String key, DbSession session) {
-    getMapper(session).deleteGlobalProperty(key);
+    int deletedRows = getMapper(session).deleteGlobalProperty(key);
 
-    if (auditPersister != null && auditPersister.isTrackedProperty(key)) {
+    if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) {
       auditPersister.deleteProperty(session, new PropertyNewValue(key), false);
     }
   }
@@ -343,9 +343,9 @@ public class PropertiesDao implements Dao {
   }
 
   public void deleteByKeyAndValue(DbSession dbSession, String key, String value) {
-    getMapper(dbSession).deleteByKeyAndValue(key, value);
+    int deletedRows = getMapper(dbSession).deleteByKeyAndValue(key, value);
 
-    if (auditPersister != null && auditPersister.isTrackedProperty(key)) {
+    if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) {
       auditPersister.deleteProperty(dbSession, new PropertyNewValue(key, value), false);
     }
   }
@@ -382,7 +382,7 @@ public class PropertiesDao implements Dao {
   }
 
   private void persistDeletedProperties(DbSession dbSession, @Nullable String userUuid, String userLogin, List<String> uuids) {
-    if (auditPersister != null) {
+    if (!uuids.isEmpty() && auditPersister != null) {
       List<PropertyDto> properties = executeLargeInputs(uuids, subList -> getMapper(dbSession).selectByUuids(subList));
 
       properties
index b3ea7429be50fce1fbd8d99aa32023842563c41a..e0b1f18b64478e0e811d99e2c31ab5ceeddfadab 100644 (file)
@@ -75,7 +75,7 @@ public interface PropertiesMapper {
 
   void deleteByUuids(@Param("uuids") List<String> uuids);
 
-  void deleteByKeyAndValue(@Param("key") String key, @Param("value") String value);
+  int deleteByKeyAndValue(@Param("key") String key, @Param("value") String value);
 
   List<PropertyDto> selectByUuids(@Param("uuids") List<String> uuids);
 
index 80b1c7dad4e69e42d0f37e85a68b27a2fb05458c..20e39b9357c1e6c870aa7c59234c9d71a5308206 100644 (file)
@@ -75,9 +75,9 @@ public class GroupDao implements Dao {
   }
 
   public void deleteByUuid(DbSession dbSession, String groupUuid, String groupName) {
-    mapper(dbSession).deleteByUuid(groupUuid);
+    int deletedRows = mapper(dbSession).deleteByUuid(groupUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserGroup(dbSession, new UserGroupNewValue(groupUuid, groupName));
     }
   }
index 7caed9add90857aa8eb2bc7616f4dc84e300670e..820a043deee55e4f9e43d95388f784884519c8b6 100644 (file)
@@ -42,7 +42,7 @@ public interface GroupMapper {
 
   int countByQuery(@Nullable @Param("query") String query);
 
-  void deleteByUuid(String groupUuid);
+  int deleteByUuid(String groupUuid);
 
   @CheckForNull
   GroupDto selectByName(@Param("name") String name);
index 1c1a3cf2a9083d83690cd8a23db04a0c434d4e71..7c7bf915947b050b59096f986794122a8403795c 100644 (file)
@@ -51,25 +51,25 @@ public class UserGroupDao implements Dao {
   }
 
   public void delete(DbSession session, GroupDto group, UserDto user) {
-    mapper(session).delete(group.getUuid(), user.getUuid());
+    int deletedRows = mapper(session).delete(group.getUuid(), user.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserFromGroup(session, new UserGroupNewValue(group, user));
     }
   }
 
   public void deleteByGroupUuid(DbSession session, String groupUuid, String groupName) {
-    mapper(session).deleteByGroupUuid(groupUuid);
+    int deletedRows = mapper(session).deleteByGroupUuid(groupUuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserFromGroup(session, new UserGroupNewValue(groupUuid, groupName));
     }
   }
 
   public void deleteByUserUuid(DbSession dbSession, UserDto userDto) {
-    mapper(dbSession).deleteByUserUuid(userDto.getUuid());
+    int deletedRows = mapper(dbSession).deleteByUserUuid(userDto.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserFromGroup(dbSession, new UserGroupNewValue(userDto));
     }
   }
index 763cb53de90ca7ca7323a44388a9539db8c6e021..903932ed75ec75b251bd4448fe7257256f994902 100644 (file)
@@ -28,9 +28,9 @@ public interface UserGroupMapper {
 
   Set<String> selectUserUuidsInGroup(@Param("groupUuid") String groupUuid);
 
-  void delete(@Param("groupUuid") String groupUuid, @Param("userUuid") String userUuid);
+  int delete(@Param("groupUuid") String groupUuid, @Param("userUuid") String userUuid);
 
-  void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
+  int deleteByGroupUuid(@Param("groupUuid") String groupUuid);
 
-  void deleteByUserUuid(@Param("userUuid") String userUuid);
+  int deleteByUserUuid(@Param("userUuid") String userUuid);
 }
index e42c92540d91936f2b588fdcbe53030371bbe287..01080f163bed5418f76a6f9d108cd15211dc6631 100644 (file)
@@ -70,9 +70,9 @@ public class UserPropertiesDao implements Dao {
 
   public void deleteByUser(DbSession session, UserDto user) {
     List<UserPropertyDto> userProperties = selectByUser(session, user);
-    mapper(session).deleteByUserUuid(user.getUuid());
+    int deletedRows = mapper(session).deleteByUserUuid(user.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       userProperties.stream()
         .filter(p -> auditPersister.isTrackedProperty(p.getKey()))
         .forEach(p -> auditPersister.deleteProperty(session, new PropertyNewValue(p, user.getLogin()), true));
index b933b4a335271639d77b450cb910a3f9288544a8..87fcfe5a9d92a05f05ca05d108ea8064111cb71a 100644 (file)
@@ -30,6 +30,6 @@ public interface UserPropertiesMapper {
 
   int update(@Param("userProperty") UserPropertyDto userPropertyDto, @Param("now") long now);
 
-  void deleteByUserUuid(@Param("userUuid") String userUuid);
+  int deleteByUserUuid(@Param("userUuid") String userUuid);
 
 }
index ed4053f92a6f308cfe644a5d4598a93c0febb91c..11165f9fd55fbe12ba3b0d600f79bb1285c2c1b9 100644 (file)
@@ -99,17 +99,17 @@ public class UserTokenDao implements Dao {
   }
 
   public void deleteByUser(DbSession dbSession, UserDto user) {
-    mapper(dbSession).deleteByUserUuid(user.getUuid());
+    int deletedRows = mapper(dbSession).deleteByUserUuid(user.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserToken(dbSession, new UserTokenNewValue(user));
     }
   }
 
   public void deleteByUserAndName(DbSession dbSession, UserDto user, String name) {
-    mapper(dbSession).deleteByUserUuidAndName(user.getUuid(), name);
+    int deletedRows = mapper(dbSession).deleteByUserUuidAndName(user.getUuid(), name);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteUserToken(dbSession, new UserTokenNewValue(user, name));
     }
   }
index 88337ab6fc66c6fc8e38be9fa7acaeb0eff82fe0..888456fd153d9858d5aa2ec8f2c6f3ad6f5fbb59 100644 (file)
@@ -34,9 +34,9 @@ public interface UserTokenMapper {
 
   List<UserTokenDto> selectByUserUuid(String userUuid);
 
-  void deleteByUserUuid(String userUuid);
+  int deleteByUserUuid(String userUuid);
 
-  void deleteByUserUuidAndName(@Param("userUuid") String userUuid, @Param("name") String name);
+  int deleteByUserUuidAndName(@Param("userUuid") String userUuid, @Param("name") String name);
 
   List<UserTokenCount> countTokensByUserUuids(@Param("userUuids") List<String> userUuids);
 }
index 7d906aec2facb86bcdfe84f09dee335855bbb13d..088e35f478a3c1cb9c4a7fcfa25007e9ee830d06 100644 (file)
@@ -73,17 +73,17 @@ public class WebhookDao implements Dao {
   }
 
   public void delete(DbSession dbSession, String uuid, String webhookName) {
-    mapper(dbSession).delete(uuid);
+    int deletedRows = mapper(dbSession).delete(uuid);
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteWebhook(dbSession, new WebhookNewValue(uuid, webhookName));
     }
   }
 
   public void deleteByProject(DbSession dbSession, ProjectDto projectDto) {
-    mapper(dbSession).deleteForProjectUuid(projectDto.getUuid());
+    int deletedRows = mapper(dbSession).deleteForProjectUuid(projectDto.getUuid());
 
-    if (auditPersister != null) {
+    if (deletedRows > 0 && auditPersister != null) {
       auditPersister.deleteWebhook(dbSession, new WebhookNewValue(projectDto));
     }
   }
index dbf24214302e45e6679fb06d4c01c8e2b99addf0..4d7547ab398460e9b90ae205bf78fe22409b0ba6 100644 (file)
@@ -36,7 +36,7 @@ public interface WebhookMapper {
 
   void update(WebhookDto dto);
 
-  void delete(@Param("uuid") String uuid);
+  int delete(@Param("uuid") String uuid);
 
-  void deleteForProjectUuid(@Param("projectUuid") String projectUuid);
+  int deleteForProjectUuid(@Param("projectUuid") String projectUuid);
 }
index db5c075f1f04eed2e71955f5f76eb588d93a1df7..73a736e5e3d804776eba79d8cd97cf7a6f4e0c56 100644 (file)
@@ -32,9 +32,12 @@ import org.sonar.db.audit.model.PersonalAccessTokenNewValue;
 import org.sonar.db.user.UserDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
 
@@ -99,6 +102,15 @@ public class AlmPatDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userLogin");
   }
 
+  @Test
+  public void deleteWithoutAffectedRowsIsNotPersisted() {
+    AlmPatDto almPat = newAlmPatDto();
+
+    underTest.delete(dbSession, almPat, null, null);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteByUserIsPersisted() {
     when(uuidFactory.create()).thenReturn(A_UUID);
@@ -119,6 +131,16 @@ public class AlmPatDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("patUuid");
   }
 
+  @Test
+  public void deleteByUserWithoutAffectedRowsIsNotPersisted() {
+    UserDto userDto = db.users().insertUser();
+
+    underTest.deleteByUser(dbSession, userDto);
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void deleteByAlmSettingIsPersisted() {
     when(uuidFactory.create()).thenReturn(A_UUID);
@@ -139,4 +161,13 @@ public class AlmPatDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userUuid");
   }
 
+  @Test
+  public void deleteByAlmSettingWithoutAffectedRowsIsNotPersisted() {
+    AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
+
+    underTest.deleteByAlmSetting(dbSession, almSettingDto);
+
+    verifyNoInteractions(auditPersister);
+  }
+
 }
index 1d33930bd2e6e4109836b14955a16d9b67e3901a..cb1bc266a1a36ea8434c0c8066238022f2c28bbe 100644 (file)
@@ -31,10 +31,12 @@ import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue;
 import org.sonar.db.project.ProjectDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto;
 
@@ -91,7 +93,7 @@ public class ProjectAlmSettingDaoWithPersisterTest {
   }
 
   @Test
-  public void deleteByProject() {
+  public void deleteByProjectIsPersisted() {
     when(uuidFactory.create()).thenReturn(A_UUID);
     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
     ProjectDto project = db.components().insertPrivateProjectDto();
@@ -109,7 +111,17 @@ public class ProjectAlmSettingDaoWithPersisterTest {
   }
 
   @Test
-  public void deleteByAlmSettingNotTracked() {
+  public void deleteByWithoutAffectedRowsProjectIsNotPersisted() {
+    ProjectDto project = db.components().insertPrivateProjectDto();
+
+    underTest.deleteByProject(dbSession, project);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
+  @Test
+  public void deleteByAlmSettingNotTrackedIsNotPersisted() {
     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
     underTest.deleteByAlmSetting(dbSession, githubAlmSetting);
 
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/ProjectNewValueTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/ProjectNewValueTest.java
deleted file mode 100644 (file)
index 45735b4..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * 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.db.audit.model;
-
-import org.json.simple.JSONObject;
-import org.json.simple.parser.JSONParser;
-import org.json.simple.parser.ParseException;
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ProjectNewValueTest {
-
-  @Test
-  public void toString_generatesValidJson() throws ParseException {
-    ProjectNewValue newValue = new ProjectNewValue("uuid", true, "name", "description");
-
-    JSONObject jsonObject = (JSONObject) new JSONParser().parse(newValue.toString());
-
-    assertThat(jsonObject.size()).isEqualTo(4);
-  }
-
-}
index 94c5311e9165599626188cc7262d38efcefa195a..4760eb99edae25f25988dc2194c560b8f7275542 100644 (file)
@@ -32,9 +32,12 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.user.GroupDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.sonar.api.web.UserRole.ADMIN;
 
 public class GroupPermissionDaoWithPersisterTest {
@@ -78,6 +81,13 @@ public class GroupPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void groupGlobalPermissionDeleteWithoutAffectedRowsIsNotPersisted() {
+    underTest.delete(dbSession, ADMIN, "group-uuid", "group-name", null, null);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void groupProjectPermissionDeleteByComponentIsPersisted() {
     GroupDto group = db.users().insertGroup();
@@ -106,11 +116,21 @@ public class GroupPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void groupProjectPermissionDeleteByComponentWithoutAffectedRowsIsNotPersisted() {
+    ComponentDto project = db.components().insertPrivateProject();
+
+    underTest.deleteByRootComponentUuid(dbSession, project);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void groupProjectPermissionDeleteByComponentAndGroupIsPersisted() {
     GroupDto group = db.users().insertGroup();
     ComponentDto project = db.components().insertPrivateProject();
-    GroupPermissionDto dto = getGroupPermission(group, project);
+    GroupPermissionDto dto = getGroupPermission(project);
     underTest.insert(dbSession, dto, project);
 
     verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture());
@@ -119,7 +139,7 @@ public class GroupPermissionDaoWithPersisterTest {
       .extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid,
         PermissionNewValue::getGroupName, PermissionNewValue::getComponentUuid,
         PermissionNewValue::getRole, PermissionNewValue::getComponentName, PermissionNewValue::getQualifier)
-      .containsExactly(dto.getUuid(), group.getUuid(), group.getName(), project.uuid(), dto.getRole(), project.name(), "project");
+      .containsExactly(dto.getUuid(), null, null, project.uuid(), dto.getRole(), project.name(), "project");
     assertThat(newValue.toString()).contains("componentUuid");
 
     underTest.deleteByRootComponentUuidForAnyOne(dbSession, project);
@@ -134,6 +154,16 @@ public class GroupPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void groupProjectPermissionDeleteByComponentAndGroupWithoutAffectedRowsIsNotPersisted() {
+    ComponentDto project = db.components().insertPrivateProject();
+
+    underTest.deleteByRootComponentUuidForAnyOne(dbSession, project);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void groupProjectPermissionDeleteByComponentAndPermissionIsPersisted() {
     GroupDto group = db.users().insertGroup();
@@ -161,6 +191,18 @@ public class GroupPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void groupProjectPermissionDeleteByComponentAndPermissionWithoutAffectedRowsIsNotPersisted() {
+    GroupDto group = db.users().insertGroup();
+    ComponentDto project = db.components().insertPrivateProject();
+    GroupPermissionDto dto = getGroupPermission(group, project);
+
+    underTest.deleteByRootComponentUuidAndPermission(dbSession, dto.getRole(), project);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   private GroupPermissionDto getGroupPermission(GroupDto group, ComponentDto project) {
     return new GroupPermissionDto()
       .setUuid(Uuids.createFast())
@@ -170,4 +212,12 @@ public class GroupPermissionDaoWithPersisterTest {
       .setComponentUuid(project.uuid())
       .setComponentName(project.name());
   }
+
+  private GroupPermissionDto getGroupPermission(ComponentDto project) {
+    return new GroupPermissionDto()
+      .setUuid(Uuids.createFast())
+      .setRole(ADMIN)
+      .setComponentUuid(project.uuid())
+      .setComponentName(project.name());
+  }
 }
index dbbfd2c85e6b03e0e8471e0a85b3a8424da12f67..c6ff53983fb82726a6f6a00bb47d7d43c4f5ae08 100644 (file)
@@ -33,9 +33,11 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.user.UserDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 
@@ -74,6 +76,16 @@ public class UserPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void userGlobalPermissionDeleteWithoutAffectedRowsIsNotPersisted() {
+    UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
+
+    underTest.deleteGlobalPermission(dbSession, user.getUuid(), SYSTEM_ADMIN);
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void userProjectPermissionInsertAndDeleteArePersisted() {
     UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
@@ -101,6 +113,18 @@ public class UserPermissionDaoWithPersisterTest {
       .doesNotContain("permissionUuid");
   }
 
+  @Test
+  public void userProjectPermissionDeleteWithoutAffectedRowsIsNotPersisted() {
+    UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
+    ComponentDto project = db.components().insertPrivateProject();
+
+    underTest.deleteProjectPermission(dbSession, user.getUuid(), SYSTEM_ADMIN, project);
+
+    verify(auditPersister).addUser(any(), any());
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void userPortfolioPermissionIsPersisted() {
     UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
@@ -152,6 +176,16 @@ public class UserPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userUuid");
   }
 
+  @Test
+  public void deleteUserPermissionOfAnyUserWithoutAffectedRowsIsNotPersisted() {
+    ComponentDto project = db.components().insertPrivateProject();
+
+    underTest.deleteProjectPermissionOfAnyUser(dbSession, SCAN_EXECUTION, project);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void deleteUserPermissionByUserUuidIsPersisted() {
     UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
@@ -169,6 +203,16 @@ public class UserPermissionDaoWithPersisterTest {
     assertThat(newValue.toString()).contains("userUuid");
   }
 
+  @Test
+  public void deleteUserPermissionByUserUuidWithoutAffectedRowsIsNotPersisted() {
+    UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"));
+
+    underTest.deleteByUserUuid(dbSession, user.getUuid());
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   private UserDto insertUser(Consumer<UserDto> populateUserDto) {
     UserDto user = db.users().insertUser(populateUserDto);
     return user;
index fe5d845c557590edb11348bf14ade25a080db976..4a2182eb9a182533fc3c94a29395619d94f120f3 100644 (file)
@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.sonar.api.web.UserRole.ADMIN;
 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
@@ -91,6 +92,13 @@ public class PermissionTemplateDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("keyPattern");
   }
 
+  @Test
+  public void deletePermissionTemplateWithoutAffectedRowsIsPersisted() {
+    underTest.deleteByUuid(session, "template-uuid", "template-name");
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void insertAndDeleteUserPermissionToTemplateIsPersisted() {
     PermissionTemplateDto dto = insertPermissionTemplate();
@@ -116,6 +124,14 @@ public class PermissionTemplateDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("groupUuid");
   }
 
+  @Test
+  public void deleteUserPermissionToTemplateWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteUserPermission(session, "template-uuid", "user-uuid", ADMIN,
+      "template-name", "user-login");
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void insertAndDeleteUserPermissionByUserUuidToTemplateIsPersisted() {
     PermissionTemplateDto dto = insertPermissionTemplate();
@@ -132,6 +148,13 @@ public class PermissionTemplateDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("groupUuid");
   }
 
+  @Test
+  public void deleteUserPermissionByUserUuidToTemplateWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteUserPermissionsByUserUuid(session, "user-uuid", "user-login");
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void insertAndDeleteGroupPermissionToTemplateIsPersisted() {
     PermissionTemplateDto dto = insertPermissionTemplate();
@@ -157,12 +180,21 @@ public class PermissionTemplateDaoWithPersisterTest {
     assertThat(newValue.toString()).contains("groupUuid");
   }
 
+  @Test
+  public void deleteGroupPermissionToTemplateWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteGroupPermission(session, "template-uuid", "group-uuid", ADMIN,
+      "template-name", "group-name");
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void insertAndDeleteGroupPermissionByGroupUuidToTemplateIsPersisted() {
     PermissionTemplateDto templateDto = insertPermissionTemplate();
     PermissionTemplateGroupDto templateGroupDto = new PermissionTemplateGroupDto()
       .setUuid(Uuids.createFast())
       .setGroupName("group")
+      .setGroupUuid("group-id")
       .setPermission(ADMIN)
       .setTemplateUuid(templateDto.getUuid())
       .setCreatedAt(new Date())
@@ -188,6 +220,13 @@ public class PermissionTemplateDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userUuid");
   }
 
+  @Test
+  public void deleteGroupPermissionByGroupUuidToTemplateWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteByGroup(session, "group-uid", "group-name");
+
+    verifyNoInteractions(auditPersister);
+  }
+
   private PermissionTemplateDto insertPermissionTemplate() {
     return underTest.insert(session, newPermissionTemplateDto()
       .setUuid("ABCD")
index b42ba5d0083f016a075a8f7a1794466e44b7a6d6..62a3d921d5b864ad092eb26f46d5e860f84314a4 100644 (file)
  */
 package org.sonar.db.project;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import javax.annotation.Nullable;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
@@ -30,17 +36,10 @@ import org.sonar.db.audit.AuditPersister;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.tuple;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
-import javax.annotation.Nullable;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Optional;
 
 public class ProjectDaoTest {
 
@@ -148,8 +147,8 @@ public class ProjectDaoTest {
     assertProject(projectsByUuids.get(0), "projectName_p1", "projectKee_o1_p1",  "uuid_o1_p1", "desc_p1", "tag1,tag2", true);
     assertProject(projectsByUuids.get(1), "projectName_p2", "projectKee_o1_p2",  "uuid_o1_p2", "desc_p2", "tag1,tag2", false);
 
-    projectDao.updateVisibility(db.getSession(), dto1.getUuid(), false, Qualifiers.PROJECT, dto1.getName());
-    projectDao.updateVisibility(db.getSession(), dto2.getUuid(), true, Qualifiers.PROJECT, dto2.getName());
+    projectDao.updateVisibility(db.getSession(), dto1.getUuid(), false);
+    projectDao.updateVisibility(db.getSession(), dto2.getUuid(), true);
 
     projectsByUuids = projectDao.selectByUuids(db.getSession(), new HashSet<>(Arrays.asList("uuid_o1_p1", "uuid_o1_p2")));
     assertThat(projectsByUuids).hasSize(2);
index 26ef4bf051d63987624c00b53392eaed4d60cc23..e430e2fa07b3bbfc5bf433fbf58c22c36d936c9e 100644 (file)
@@ -44,6 +44,7 @@ import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
@@ -121,7 +122,7 @@ public class PropertiesDaoWithPersisterTest {
   public void saveProjectTrackedPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
     underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
     verify(auditPersister).isTrackedProperty(KEY);
@@ -140,7 +141,7 @@ public class PropertiesDaoWithPersisterTest {
   public void saveApplicationTrackedPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
     underTest.saveProperty(session, propertyDto, USER_LOGIN, "app-name", Qualifiers.APP);
 
     verify(auditPersister).isTrackedProperty(KEY);
@@ -160,7 +161,7 @@ public class PropertiesDaoWithPersisterTest {
   public void savePortfolioTrackedPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
     underTest.saveProperty(session, propertyDto, USER_LOGIN, "portfolio-name", Qualifiers.VIEW);
 
     verify(auditPersister).isTrackedProperty(KEY);
@@ -180,7 +181,7 @@ public class PropertiesDaoWithPersisterTest {
   public void saveProjectTrackedAndSecuredPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true);
 
-    PropertyDto propertyDto = getPropertyDto(SECURED_KEY);
+    PropertyDto propertyDto = getPropertyDto(SECURED_KEY, PROJECT_UUID, USER_UUID);
     underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
     verify(auditPersister).isTrackedProperty(SECURED_KEY);
@@ -198,11 +199,13 @@ public class PropertiesDaoWithPersisterTest {
   @Test
   public void deleteTrackedPropertyByQueryIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
-
     PropertyQuery query = getPropertyQuery(KEY);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
+
     underTest.deleteByQuery(session, query);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -214,25 +217,37 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userLogin");
   }
 
+  @Test
+  public void deleteTrackedPropertyByQueryWithoutAffectedRowsIsNotPersisted() {
+    PropertyQuery query = getPropertyQuery(KEY);
+
+    underTest.deleteByQuery(session, query);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedPropertyByQueryIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
-
     PropertyQuery query = getPropertyQuery(KEY);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
+
     underTest.deleteByQuery(session, query);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
   @Test
   public void deleteTrackedPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
     underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -244,23 +259,36 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).contains("userLogin");
   }
 
+  @Test
+  public void deleteTrackedPropertyWithoutAffectedRowsIsNotPersisted() {
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+
+    underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedPropertyIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
     underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
   @Test
   public void deleteTrackedProjectPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, null);
+    underTest.saveProperty(propertyDto);
+
     underTest.deleteProjectProperty(KEY, PROJECT_UUID, PROJECT_NAME, Qualifiers.PROJECT);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -272,24 +300,34 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("userLogin");
   }
 
+  @Test
+  public void deleteTrackedProjectPropertyWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteProjectProperty(KEY, PROJECT_UUID, PROJECT_NAME, Qualifiers.PROJECT);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedProjectPropertyIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
-    PropertyDto propertyDto = getPropertyDto(KEY);
     underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME, Qualifiers.PROJECT);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
   @Test
   public void deleteTrackedProjectPropertiesIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, null);
+    underTest.saveProperty(propertyDto);
 
     underTest.deleteProjectProperties(KEY, VALUE);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -301,23 +339,34 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("projectUuid");
   }
 
+  @Test
+  public void deleteTrackedProjectPropertiesWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteProjectProperties(KEY, VALUE);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedProjectPropertiesIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
+    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, null);
+    underTest.saveProperty(propertyDto);
 
     underTest.deleteProjectProperties(KEY, VALUE);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
   @Test
   public void deleteTrackedGlobalPropertyIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
+    PropertyDto propertyDto = getPropertyDto(KEY, null, null);
+    underTest.saveProperty(session, propertyDto, null, null, null);
 
     underTest.deleteGlobalProperty(KEY, session);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -329,13 +378,22 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("projectUuid");
   }
 
+  @Test
+  public void deleteTrackedGlobalPropertyWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteGlobalProperty(KEY, session);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedGlobalPropertyIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
+    PropertyDto propertyDto = getPropertyDto(KEY, null, null);
+    underTest.saveProperty(session, propertyDto, null, null, null);
 
     underTest.deleteGlobalProperty(KEY, session);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
@@ -365,6 +423,13 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValues.get(1).toString()).doesNotContain("value");
   }
 
+  @Test
+  public void deletePropertyByUserWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteByUser(session, USER_UUID, USER_LOGIN);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deletePropertyByUserLoginIsPersisted() {
     UserDto user = setUserProperties(null);
@@ -391,13 +456,22 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValues.get(1).toString()).doesNotContain("value");
   }
 
+  @Test
+  public void deletePropertyByUserLoginWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteByMatchingLogin(session, USER_LOGIN, newArrayList(KEY, ANOTHER_KEY, SECURED_KEY));
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteTrackedPropertyByKeyAndValueIsPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
 
+    PropertyDto propertyDto = getPropertyDto(KEY, null, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null);
     underTest.deleteByKeyAndValue(session, KEY, VALUE);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
     PropertyNewValue newValue = newValueCaptor.getValue();
     assertThat(newValue)
@@ -409,22 +483,31 @@ public class PropertiesDaoWithPersisterTest {
     assertThat(newValue.toString()).doesNotContain("projectUuid");
   }
 
+  @Test
+  public void deleteTrackedPropertyByKeyAndValueWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteByKeyAndValue(session, KEY, VALUE);
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteNotTrackedPropertyByKeyAndValueIsNotPersisted() {
     when(auditPersister.isTrackedProperty(KEY)).thenReturn(false);
+    PropertyDto propertyDto = getPropertyDto(KEY, null, USER_UUID);
+    underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null);
 
     underTest.deleteByKeyAndValue(session, KEY, VALUE);
 
-    verify(auditPersister).isTrackedProperty(KEY);
+    verify(auditPersister, times(2)).isTrackedProperty(KEY);
     verifyNoMoreInteractions(auditPersister);
   }
 
-  private PropertyDto getPropertyDto(String key) {
+  private PropertyDto getPropertyDto(String key, @Nullable String projectUuid, @Nullable String userUuid) {
     return new PropertyDto()
       .setKey(key)
       .setValue(VALUE)
-      .setUserUuid(USER_UUID)
-      .setComponentUuid(PROJECT_UUID);
+      .setUserUuid(userUuid)
+      .setComponentUuid(projectUuid);
   }
 
   private PropertyQuery getPropertyQuery(String key) {
index 936a8993b753c170721b96a526ae30571db7caaa..04dedb3e50c23ebd4d17cb379c03bdd6971e7394 100644 (file)
@@ -35,6 +35,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 
 public class GroupDaoWithPersisterTest {
@@ -104,4 +105,11 @@ public class GroupDaoWithPersisterTest {
       .extracting(UserGroupNewValue::getGroupUuid, UserGroupNewValue::getName)
       .containsExactly(aGroup.getUuid(), aGroup.getName());
   }
+
+  @Test
+  public void deleteGroupWithoutAffectedRowsIsNotPersisted() {
+    underTest.deleteByUuid(db.getSession(), aGroup.getUuid(), aGroup.getName());
+
+    verifyNoInteractions(auditPersister);
+  }
 }
index f46b107377e56e6e4adbcc257996344dca7c356e..e18a167d6ad54a72271c5cabbe393d1cff3ccc71 100644 (file)
@@ -33,6 +33,8 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 public class UserGroupDaoWithPersisterTest {
   private final AuditPersister auditPersister = mock(AuditPersister.class);
@@ -84,6 +86,15 @@ public class UserGroupDaoWithPersisterTest {
       .containsExactly(group1.getUuid(), group1.getName());
   }
 
+  @Test
+  public void deleteUserGroupByGroupWithoutAffectedRowsIsNotPersisted() {
+    GroupDto group1 = db.users().insertGroup();
+    underTest.deleteByGroupUuid(db.getSession(), group1.getUuid(), group1.getName());
+    db.getSession().commit();
+
+    verifyNoInteractions(auditPersister);
+  }
+
   @Test
   public void deleteUserGroupByUserIsPersisted() {
     UserDto user1 = db.users().insertUser();
@@ -105,6 +116,16 @@ public class UserGroupDaoWithPersisterTest {
       .containsExactly(user1.getUuid(), user1.getLogin());
   }
 
+  @Test
+  public void deleteUserGroupByUserWithoutAffectedRowsIsNotPersisted() {
+    UserDto user1 = db.users().insertUser();
+    underTest.deleteByUserUuid(db.getSession(), user1);
+    db.getSession().commit();
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void delete_by_user_and_group() {
     UserDto user1 = db.users().insertUser();
@@ -122,4 +143,15 @@ public class UserGroupDaoWithPersisterTest {
       .extracting(UserGroupNewValue::getGroupUuid, UserGroupNewValue::getName, UserGroupNewValue::getUserUuid, UserGroupNewValue::getUserLogin)
       .containsExactly(group1.getUuid(), group1.getName(), user1.getUuid(), user1.getLogin());
   }
+
+  @Test
+  public void deletByUserAndGroupWithoutAffectedRowsIsNotPersisted() {
+    UserDto user1 = db.users().insertUser();
+    GroupDto group1 = db.users().insertGroup();
+    underTest.delete(db.getSession(), group1, user1);
+    db.getSession().commit();
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
 }
index 1d74a380629a37d71184ecf89d0a643190fe231b..5992048b02717392f156bf3ad483d15e884b83fb 100644 (file)
@@ -170,4 +170,14 @@ public class UserPropertiesDaoWithPersisterTest {
         PropertyNewValue::getUserLogin)
       .containsExactly(userSetting.getKey(), userSetting.getValue(), user.getUuid(), user.getLogin());
   }
+
+  @Test
+  public void deleteTrackedUserPropertyWithoutAffectedRowsIsNotPersisted() {
+    UserDto user = db.users().insertUser();
+
+    underTest.deleteByUser(db.getSession(), user);
+
+    verify(auditPersister).addUser(eq(db.getSession()), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
 }
index b2dd97a6243aee319a4f6f366dd944f3a47432dd..79940eeaf341326def416306f3ab87763a1dee2c 100644 (file)
@@ -31,9 +31,11 @@ import org.sonar.db.audit.AuditPersister;
 import org.sonar.db.audit.model.UserTokenNewValue;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.sonar.db.user.UserTokenTesting.newUserToken;
 
 public class UserTokenDaoWithPersisterTest {
@@ -50,7 +52,7 @@ public class UserTokenDaoWithPersisterTest {
   private final UserTokenDao underTest = dbClient.userTokenDao();
 
   @Test
-  public void insert_token() {
+  public void insert_token_is_persisted() {
     UserTokenDto userToken = newUserToken();
     underTest.insert(db.getSession(), userToken, "login");
 
@@ -70,7 +72,7 @@ public class UserTokenDaoWithPersisterTest {
   }
 
   @Test
-  public void update_token() {
+  public void update_token_is_persisted() {
     UserDto user1 = db.users().insertUser();
     UserTokenDto userToken1 = db.users().insertToken(user1);
 
@@ -86,7 +88,7 @@ public class UserTokenDaoWithPersisterTest {
   }
 
   @Test
-  public void delete_tokens_by_user() {
+  public void delete_tokens_by_user_is_persisted() {
     UserDto user1 = db.users().insertUser();
     UserDto user2 = db.users().insertUser();
     db.users().insertToken(user1);
@@ -104,7 +106,17 @@ public class UserTokenDaoWithPersisterTest {
   }
 
   @Test
-  public void delete_token_by_user_and_name() {
+  public void delete_tokens_by_user_without_affected_rows_is_not_persisted() {
+    UserDto user1 = db.users().insertUser();
+
+    underTest.deleteByUser(dbSession, user1);
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
+  @Test
+  public void delete_token_by_user_and_name_is_persisted() {
     UserDto user1 = db.users().insertUser();
     UserDto user2 = db.users().insertUser();
     db.users().insertToken(user1, t -> t.setName("name"));
@@ -120,4 +132,14 @@ public class UserTokenDaoWithPersisterTest {
       .extracting(UserTokenNewValue::getUserUuid, UserTokenNewValue::getUserLogin, UserTokenNewValue::getTokenName)
       .containsExactly(user1.getUuid(), user1.getLogin(), "name");
   }
+
+  @Test
+  public void delete_token_by_user_and_name_without_affected_rows_is_persisted() {
+    UserDto user1 = db.users().insertUser();
+
+    underTest.deleteByUserAndName(dbSession, user1, "name");
+
+    verify(auditPersister).addUser(any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
 }
index 7dd4c49e67445f527536030b9d287bcee74bcb44..97dc844b750745cd5f9a580cb7a2fe06fcb2f020 100644 (file)
@@ -32,9 +32,11 @@ import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.project.ProjectDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 public class WebhookDaoWithPersisterTest {
   private final AuditPersister auditPersister = mock(AuditPersister.class);
@@ -138,6 +140,16 @@ public class WebhookDaoWithPersisterTest {
     assertThat(newValue).hasToString("{\"projectUuid\": \"puuid\", \"projectName\": \"pname\" }");
   }
 
+  @Test
+  public void deleteProjectWebhooksWithoutAffectedRowsIsNotPersisted() {
+    ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(p -> p.setUuid("puuid").setName("pname"));
+
+    underTest.deleteByProject(dbSession, projectDto);
+
+    verify(auditPersister).addComponent(any(), any(), any());
+    verifyNoMoreInteractions(auditPersister);
+  }
+
   @Test
   public void deleteWebhookIsPersisted() {
     WebhookDto dto = webhookDbTester.insertGlobalWebhook();
@@ -151,4 +163,11 @@ public class WebhookDaoWithPersisterTest {
       .containsExactly(dto.getUuid(), dto.getName());
     assertThat(newValue).hasToString("{\"webhookUuid\": \"" + dto.getUuid() + "\", \"name\": \"" + dto.getName() + "\" }");
   }
+
+  @Test
+  public void deleteWebhookWithoutAffectedRowsIsNotPersisted() {
+    underTest.delete(dbSession, "webhook-uuid", "webhook-name");
+
+    verifyNoMoreInteractions(auditPersister);
+  }
 }
index b395be13fccd1b0159cfeb6c4e6257578f016033..0ad2ee8c32761f58c6b9d7eeaeadb7ed03e16ec7 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.project.ws;
 
 import com.google.common.collect.ImmutableSet;
+import java.util.Set;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.Request;
@@ -53,7 +54,6 @@ import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesEx
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_VISIBILITY;
-import java.util.Set;
 
 public class UpdateVisibilityAction implements ProjectsWsAction {
   private static final Set<String> AUTHORIZED_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP);
@@ -135,7 +135,7 @@ public class UpdateVisibilityAction implements ProjectsWsAction {
     dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, uuid, isPrivate, component.qualifier(), component.name());
 
     if (component.qualifier().equals(Qualifiers.PROJECT) || component.qualifier().equals(Qualifiers.APP)) {
-      dbClient.projectDao().updateVisibility(dbSession, uuid, isPrivate, component.qualifier(), component.name());
+      dbClient.projectDao().updateVisibility(dbSession, uuid, isPrivate);
     }
 
     ComponentMapper mapper = dbSession.getMapper(ComponentMapper.class);