diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-08-17 14:35:44 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-08-26 20:04:29 +0000 |
commit | 79b13ac2a166eea535deceade405e8b99b098c64 (patch) | |
tree | 0e979a6eebb7119d74b780a38dc387af5325ffe3 | |
parent | 680aed78d5a58249ae56ca36b5f495bb579bce1a (diff) | |
download | sonarqube-79b13ac2a166eea535deceade405e8b99b098c64.tar.gz sonarqube-79b13ac2a166eea535deceade405e8b99b098c64.zip |
SONAR-15142 Always add AuditPersister to container and use typed NewValue for each audit
67 files changed, 715 insertions, 752 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java index 69ff271cd71..7b296daa4c4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectPersister.java @@ -20,6 +20,7 @@ package org.sonar.ce.task.projectanalysis.component; import org.apache.commons.lang.StringUtils; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -69,6 +70,8 @@ public class ProjectPersister { projectDto.setName(root.getName()); projectDto.setDescription(root.getDescription()); projectDto.setUpdatedAt(system2.now()); + projectDto.setKey(root.getKey()); + projectDto.setQualifier(root.getType().equals(Component.Type.PROJECT) ? Qualifiers.PROJECT : Qualifiers.APP); return projectDto; } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java index 734a9ff75b0..881fad42933 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java @@ -52,7 +52,6 @@ import org.sonar.db.component.ComponentUpdateDto; import static java.util.Optional.ofNullable; import static org.sonar.api.resources.Qualifiers.PROJECT; -import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT; import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR; @@ -108,8 +107,7 @@ public class PersistComponentsStep implements ComputationStep { .visit(treeRootHolder.getRoot()); disableRemainingComponents(dbSession, existingDtosByUuids.values()); - ensureConsistentVisibility(dbSession, projectUuid, isRootPrivate, treeRootHolder.getRoot().getType(), treeRootHolder.getRoot().getName()); - + dbClient.componentDao().setPrivateForRootComponentUuidWithoutAudit(dbSession, projectUuid, isRootPrivate); dbSession.commit(); } } @@ -135,18 +133,6 @@ public class PersistComponentsStep implements ComputationStep { disabledComponentsHolder.setUuids(uuids); } - private void ensureConsistentVisibility(DbSession dbSession, String projectUuid, boolean isRootPrivate, - Component.Type type, String componentName) { - String qualifier = null; - if (type == Component.Type.PROJECT) { - qualifier = PROJECT; - } else if (type == Component.Type.VIEW) { - qualifier = VIEW; - } - dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, projectUuid, isRootPrivate, - "", qualifier, componentName, false); - } - private static boolean isRootPrivate(Component root, Map<String, ComponentDto> existingDtosByUuids) { ComponentDto rootDto = existingDtosByUuids.get(root.getUuid()); if (rootDto == null) { diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index e8204854e61..23fd2289ce2 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -83,6 +83,8 @@ import org.sonar.db.DaoModule; import org.sonar.db.DbClient; import org.sonar.db.DefaultDatabase; import org.sonar.db.MyBatis; +import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.purge.PurgeProfiler; import org.sonar.process.NetworkUtilsImpl; import org.sonar.process.Props; @@ -203,6 +205,9 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { level1.getComponentByType(CECoreExtensionsInstaller.class) .install(level1, hasPlatformLevel(1), noAdditionalSideFilter()); + if (level1.getComponentByType(AuditPersister.class) == null) { + level1.add(NoOpAuditPersister.class); + } level1.startComponents(); } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/db/ReadOnlyPropertiesDao.java b/server/sonar-ce/src/main/java/org/sonar/ce/db/ReadOnlyPropertiesDao.java index ea1c3b3475c..ab6511fc7b3 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/db/ReadOnlyPropertiesDao.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/db/ReadOnlyPropertiesDao.java @@ -25,6 +25,7 @@ import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactory; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.property.PropertiesDao; import org.sonar.db.property.PropertyDto; @@ -39,7 +40,7 @@ import org.sonar.db.property.PropertyDto; */ public class ReadOnlyPropertiesDao extends PropertiesDao { public ReadOnlyPropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory) { - super(mybatis, system2, uuidFactory); + super(mybatis, system2, uuidFactory, new NoOpAuditPersister()); } @Override diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatDao.java index e8ec8d446dc..9462dce5e7f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/pat/AlmPatDao.java @@ -34,12 +34,7 @@ public class AlmPatDao implements Dao { private final System2 system2; private final UuidFactory uuidFactory; - private AuditPersister auditPersister; - - public AlmPatDao(System2 system2, UuidFactory uuidFactory) { - this.system2 = system2; - this.uuidFactory = uuidFactory; - } + private final AuditPersister auditPersister; public AlmPatDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.system2 = system2; @@ -67,37 +62,33 @@ public class AlmPatDao implements Dao { almPatDto.setUpdatedAt(now); getMapper(dbSession).insert(almPatDto); - if (auditPersister != null) { - auditPersister.addPersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); - } + auditPersister.addPersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); } public void update(DbSession dbSession, AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) { long now = system2.now(); almPatDto.setUpdatedAt(now); getMapper(dbSession).update(almPatDto); - if (auditPersister != null) { - auditPersister.updatePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); - } + auditPersister.updatePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); } public void delete(DbSession dbSession, AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) { int deletedRows = getMapper(dbSession).deleteByUuid(almPatDto.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); } } public void deleteByUser(DbSession dbSession, UserDto user) { int deletedRows = getMapper(dbSession).deleteByUser(user.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(user)); } } public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) { int deletedRows = getMapper(dbSession).deleteByAlmSetting(almSetting.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almSetting)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java index e623b6407eb..ef7fb4798d2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java @@ -33,16 +33,11 @@ public class AlmSettingDao implements Dao { private final System2 system2; private final UuidFactory uuidFactory; + private final AuditPersister auditPersister; - private AuditPersister auditPersister; - - public AlmSettingDao(System2 system2, UuidFactory uuidFactory) { + public AlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.system2 = system2; this.uuidFactory = uuidFactory; - } - - public AlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { - this(system2, uuidFactory); this.auditPersister = auditPersister; } @@ -58,9 +53,7 @@ public class AlmSettingDao implements Dao { almSettingDto.setUpdatedAt(now); getMapper(dbSession).insert(almSettingDto); - if (auditPersister != null) { - auditPersister.addDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto)); - } + auditPersister.addDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto)); } public Optional<AlmSettingDto> selectByUuid(DbSession dbSession, String uuid) { @@ -82,7 +75,7 @@ public class AlmSettingDao implements Dao { public void delete(DbSession dbSession, AlmSettingDto almSettingDto) { int deletedRows = getMapper(dbSession).deleteByKey(almSettingDto.getKey()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto.getUuid(), almSettingDto.getKey())); } } @@ -91,12 +84,9 @@ public class AlmSettingDao implements Dao { long now = system2.now(); almSettingDto.setUpdatedAt(now); getMapper(dbSession).update(almSettingDto); - - if (auditPersister != null) { - if (updateSecret) { - auditPersister.updateDevOpsPlatformSecret(dbSession, new SecretNewValue("DevOpsPlatform", almSettingDto.getRawAlm())); - } - auditPersister.updateDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto)); + if (updateSecret) { + auditPersister.updateDevOpsPlatformSecret(dbSession, new SecretNewValue("DevOpsPlatform", almSettingDto.getRawAlm())); } + auditPersister.updateDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingDao.java index 4f4965e56b9..637381f350c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ProjectAlmSettingDao.java @@ -36,20 +36,15 @@ public class ProjectAlmSettingDao implements Dao { private final System2 system2; private final UuidFactory uuidFactory; - private AuditPersister auditPersister; + private final AuditPersister auditPersister; - public ProjectAlmSettingDao(System2 system2, UuidFactory uuidFactory) { + public ProjectAlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.system2 = system2; this.uuidFactory = uuidFactory; - } - - public ProjectAlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { - this(system2, uuidFactory); this.auditPersister = auditPersister; } - public void insertOrUpdate(DbSession dbSession, ProjectAlmSettingDto projectAlmSettingDto, String key, - String projectName, String projectKey) { + public void insertOrUpdate(DbSession dbSession, ProjectAlmSettingDto projectAlmSettingDto, String key, String projectName, String projectKey) { String uuid = uuidFactory.create(); long now = system2.now(); ProjectAlmSettingMapper mapper = getMapper(dbSession); @@ -63,21 +58,18 @@ public class ProjectAlmSettingDao implements Dao { } projectAlmSettingDto.setUpdatedAt(now); - if (auditPersister != null) { - DevOpsPlatformSettingNewValue value = new DevOpsPlatformSettingNewValue(projectAlmSettingDto, key, - projectName, projectKey); - if (isUpdate) { - auditPersister.updateDevOpsPlatformSetting(dbSession, value); - } else { - auditPersister.addDevOpsPlatformSetting(dbSession, value); - } + DevOpsPlatformSettingNewValue value = new DevOpsPlatformSettingNewValue(projectAlmSettingDto, key, projectName, projectKey); + if (isUpdate) { + auditPersister.updateDevOpsPlatformSetting(dbSession, value); + } else { + auditPersister.addDevOpsPlatformSetting(dbSession, value); } } public void deleteByProject(DbSession dbSession, ProjectDto project) { int deletedRows = getMapper(dbSession).deleteByProjectUuid(project.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(project)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditPersister.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditPersister.java index ef4b852a6f5..66b351a4720 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditPersister.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditPersister.java @@ -21,111 +21,123 @@ package org.sonar.db.audit; import org.sonar.core.extension.PlatformLevel; import org.sonar.db.DbSession; -import org.sonar.db.audit.model.NewValue; - -import javax.annotation.Nullable; +import org.sonar.db.audit.model.ComponentKeyNewValue; +import org.sonar.db.audit.model.ComponentNewValue; +import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; +import org.sonar.db.audit.model.GroupPermissionNewValue; +import org.sonar.db.audit.model.LicenseNewValue; +import org.sonar.db.audit.model.PermissionTemplateNewValue; +import org.sonar.db.audit.model.PersonalAccessTokenNewValue; +import org.sonar.db.audit.model.PluginNewValue; +import org.sonar.db.audit.model.PropertyNewValue; +import org.sonar.db.audit.model.SecretNewValue; +import org.sonar.db.audit.model.UserGroupNewValue; +import org.sonar.db.audit.model.UserNewValue; +import org.sonar.db.audit.model.UserPermissionNewValue; +import org.sonar.db.audit.model.UserTokenNewValue; +import org.sonar.db.audit.model.WebhookNewValue; @PlatformLevel(1) public interface AuditPersister { - void addUserGroup(DbSession dbSession, NewValue newValue); + void addUserGroup(DbSession dbSession, UserGroupNewValue newValue); - void updateUserGroup(DbSession dbSession, NewValue newValue); + void updateUserGroup(DbSession dbSession, UserGroupNewValue newValue); - void deleteUserGroup(DbSession dbSession, NewValue newValue); + void deleteUserGroup(DbSession dbSession, UserGroupNewValue newValue); - void addUser(DbSession dbSession, NewValue newValue); + void addUser(DbSession dbSession, UserNewValue newValue); - void updateUser(DbSession dbSession, NewValue newValue); + void updateUser(DbSession dbSession, UserNewValue newValue); - void updateUserPassword(DbSession dbSession, NewValue newValue); + void updateUserPassword(DbSession dbSession, SecretNewValue newValue); - void updateWebhookSecret(DbSession dbSession, NewValue newValue); + void updateWebhookSecret(DbSession dbSession, SecretNewValue newValue); - void updateDevOpsPlatformSecret(DbSession dbSession, NewValue newValue); + void updateDevOpsPlatformSecret(DbSession dbSession, SecretNewValue newValue); - void deactivateUser(DbSession dbSession, NewValue newValue); + void deactivateUser(DbSession dbSession, UserNewValue newValue); - void addUserToGroup(DbSession dbSession, NewValue newValue); + void addUserToGroup(DbSession dbSession, UserGroupNewValue newValue); - void deleteUserFromGroup(DbSession dbSession, NewValue newValue); + void deleteUserFromGroup(DbSession dbSession, UserGroupNewValue newValue); - void addProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); + void addProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty); - void updateProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); + void updateProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty); - void deleteProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); + void deleteProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty); - void addUserToken(DbSession dbSession, NewValue newValue); + void addUserToken(DbSession dbSession, UserTokenNewValue newValue); - void updateUserToken(DbSession dbSession, NewValue newValue); + void updateUserToken(DbSession dbSession, UserTokenNewValue newValue); - void deleteUserToken(DbSession dbSession, NewValue newValue); + void deleteUserToken(DbSession dbSession, UserTokenNewValue newValue); - void addGroupPermission(DbSession dbSession, NewValue newValue); + void addGroupPermission(DbSession dbSession, GroupPermissionNewValue newValue); - void deleteGroupPermission(DbSession dbSession, NewValue newValue); + void deleteGroupPermission(DbSession dbSession, GroupPermissionNewValue newValue); - void addUserPermission(DbSession dbSession, NewValue newValue); + void addUserPermission(DbSession dbSession, UserPermissionNewValue newValue); - void deleteUserPermission(DbSession dbSession, NewValue newValue); + void deleteUserPermission(DbSession dbSession, UserPermissionNewValue newValue); - void addPermissionTemplate(DbSession dbSession, NewValue newValue); + void addPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void updatePermissionTemplate(DbSession dbSession, NewValue newValue); + void updatePermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void deletePermissionTemplate(DbSession dbSession, NewValue newValue); + void deletePermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void addUserToPermissionTemplate(DbSession dbSession, NewValue newValue); + void addUserToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void deleteUserFromPermissionTemplate(DbSession dbSession, NewValue newValue); + void deleteUserFromPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void addGroupToPermissionTemplate(DbSession dbSession, NewValue newValue); + void addGroupToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void deleteGroupFromPermissionTemplate(DbSession dbSession, NewValue newValue); + void deleteGroupFromPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void addCharacteristicToPermissionTemplate(DbSession dbSession, NewValue newValue); + void addCharacteristicToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void updateCharacteristicInPermissionTemplate(DbSession dbSession, NewValue newValue); + void updateCharacteristicInPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue); - void addPlugin(DbSession dbSession, NewValue newValue); + void addPlugin(DbSession dbSession, PluginNewValue newValue); - void updatePlugin(DbSession dbSession, NewValue newValue); + void updatePlugin(DbSession dbSession, PluginNewValue newValue); void generateSecretKey(DbSession dbSession); - void setLicense(DbSession dbSession, boolean isSet, NewValue newValue); + void setLicense(DbSession dbSession, boolean isSet, LicenseNewValue newValue); - void addWebhook(DbSession dbSession, NewValue newValue); + void addWebhook(DbSession dbSession, WebhookNewValue newValue); - void updateWebhook(DbSession dbSession, NewValue newValue); + void updateWebhook(DbSession dbSession, WebhookNewValue newValue); - void deleteWebhook(DbSession dbSession, NewValue newValue); + void deleteWebhook(DbSession dbSession, WebhookNewValue newValue); - void addDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); + void addDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue); - void updateDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); + void updateDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue); - void deleteDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); + void deleteDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue); - void addPersonalAccessToken(DbSession dbSession, NewValue newValue); + void addPersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue); - void updatePersonalAccessToken(DbSession dbSession, NewValue newValue); + void updatePersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue); - void deletePersonalAccessToken(DbSession dbSession, NewValue newValue); + void deletePersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue); boolean isTrackedProperty(String propertyKey); - void addComponent(DbSession dbSession, NewValue newValue, String qualifier); + void addComponent(DbSession dbSession, ComponentNewValue newValue); - void deleteComponent(DbSession dbSession, NewValue newValue, @Nullable String qualifier); + void deleteComponent(DbSession dbSession, ComponentNewValue newValue); - void updateComponent(DbSession dbSession, NewValue newValue, String qualifier); + void updateComponent(DbSession dbSession, ComponentNewValue newValue); - void updateComponentVisibility(DbSession session, NewValue componentNewValue, @Nullable String qualifier); + void updateComponentVisibility(DbSession session, ComponentNewValue componentNewValue); - void componentKeyUpdate(DbSession session, NewValue componentKeyNewValue, String qualifier); + void componentKeyUpdate(DbSession session, ComponentKeyNewValue componentKeyNewValue, String qualifier); - void componentKeyBranchUpdate(DbSession session, NewValue componentKeyNewValue, String qualifier); + void componentKeyBranchUpdate(DbSession session, ComponentKeyNewValue componentKeyNewValue, String qualifier); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java new file mode 100644 index 00000000000..d2de8d4d9e5 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java @@ -0,0 +1,289 @@ +/* + * 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; + +import org.sonar.db.DbSession; +import org.sonar.db.audit.model.ComponentKeyNewValue; +import org.sonar.db.audit.model.ComponentNewValue; +import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; +import org.sonar.db.audit.model.GroupPermissionNewValue; +import org.sonar.db.audit.model.LicenseNewValue; +import org.sonar.db.audit.model.PermissionTemplateNewValue; +import org.sonar.db.audit.model.PersonalAccessTokenNewValue; +import org.sonar.db.audit.model.PluginNewValue; +import org.sonar.db.audit.model.PropertyNewValue; +import org.sonar.db.audit.model.SecretNewValue; +import org.sonar.db.audit.model.UserGroupNewValue; +import org.sonar.db.audit.model.UserNewValue; +import org.sonar.db.audit.model.UserPermissionNewValue; +import org.sonar.db.audit.model.UserTokenNewValue; +import org.sonar.db.audit.model.WebhookNewValue; + +public class NoOpAuditPersister implements AuditPersister { + @Override + public void addUserGroup(DbSession dbSession, UserGroupNewValue newValue) { + // no op + } + + @Override + public void updateUserGroup(DbSession dbSession, UserGroupNewValue newValue) { + // no op + } + + @Override + public void deleteUserGroup(DbSession dbSession, UserGroupNewValue newValue) { + // no op + } + + @Override + public void addUser(DbSession dbSession, UserNewValue newValue) { + // no op + } + + @Override + public void updateUser(DbSession dbSession, UserNewValue newValue) { + // no op + } + + @Override + public void updateUserPassword(DbSession dbSession, SecretNewValue newValue) { + // no op + } + + @Override + public void updateWebhookSecret(DbSession dbSession, SecretNewValue newValue) { + // no op + } + + @Override + public void updateDevOpsPlatformSecret(DbSession dbSession, SecretNewValue newValue) { + // no op + } + + @Override + public void deactivateUser(DbSession dbSession, UserNewValue newValue) { + // no op + } + + @Override + public void addUserToGroup(DbSession dbSession, UserGroupNewValue newValue) { + // no op + } + + @Override + public void deleteUserFromGroup(DbSession dbSession, UserGroupNewValue newValue) { + // no op + } + + @Override + public void addProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty) { + // no op + } + + @Override + public void updateProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty) { + // no op + } + + @Override + public void deleteProperty(DbSession dbSession, PropertyNewValue newValue, boolean isUserProperty) { + // no op + } + + @Override + public void addUserToken(DbSession dbSession, UserTokenNewValue newValue) { + // no op + } + + @Override + public void updateUserToken(DbSession dbSession, UserTokenNewValue newValue) { + // no op + } + + @Override + public void deleteUserToken(DbSession dbSession, UserTokenNewValue newValue) { + // no op + } + + @Override + public void addGroupPermission(DbSession dbSession, GroupPermissionNewValue newValue) { + // no op + } + + @Override + public void deleteGroupPermission(DbSession dbSession, GroupPermissionNewValue newValue) { + // no op + } + + @Override + public void addUserPermission(DbSession dbSession, UserPermissionNewValue newValue) { + // no op + } + + @Override + public void deleteUserPermission(DbSession dbSession, UserPermissionNewValue newValue) { + // no op + } + + @Override + public void addPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void updatePermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void deletePermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void addUserToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void deleteUserFromPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void addGroupToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void deleteGroupFromPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void addCharacteristicToPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void updateCharacteristicInPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue) { + // no op + } + + @Override + public void addPlugin(DbSession dbSession, PluginNewValue newValue) { + // no op + } + + @Override + public void updatePlugin(DbSession dbSession, PluginNewValue newValue) { + // no op + } + + @Override + public void generateSecretKey(DbSession dbSession) { + // no op + } + + @Override + public void setLicense(DbSession dbSession, boolean isSet, LicenseNewValue newValue) { + // no op + } + + @Override + public void addWebhook(DbSession dbSession, WebhookNewValue newValue) { + // no op + } + + @Override + public void updateWebhook(DbSession dbSession, WebhookNewValue newValue) { + // no op + } + + @Override + public void deleteWebhook(DbSession dbSession, WebhookNewValue newValue) { + // no op + } + + @Override + public void addDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue) { + // no op + } + + @Override + public void updateDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue) { + // no op + } + + @Override + public void deleteDevOpsPlatformSetting(DbSession dbSession, DevOpsPlatformSettingNewValue newValue) { + // no op + } + + @Override + public void addPersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue) { + // no op + } + + @Override + public void updatePersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue) { + // no op + } + + @Override + public void deletePersonalAccessToken(DbSession dbSession, PersonalAccessTokenNewValue newValue) { + // no op + } + + @Override + public boolean isTrackedProperty(String propertyKey) { + return false; + } + + @Override + public void addComponent(DbSession dbSession, ComponentNewValue newValue) { + // no op + } + + @Override + public void deleteComponent(DbSession dbSession, ComponentNewValue newValue) { + // no op + } + + @Override + public void updateComponent(DbSession dbSession, ComponentNewValue newValue) { + // no op + } + + @Override + public void updateComponentVisibility(DbSession session, ComponentNewValue componentNewValue) { + // no op + } + + @Override + public void componentKeyUpdate(DbSession session, ComponentKeyNewValue componentKeyNewValue, String qualifier) { + // no op + } + + @Override + public void componentKeyBranchUpdate(DbSession session, ComponentKeyNewValue componentKeyNewValue, String qualifier) { + // no op + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentKeyNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentKeyNewValue.java index 500771ddef8..a44e19ef433 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentKeyNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentKeyNewValue.java @@ -19,13 +19,16 @@ */ package org.sonar.db.audit.model; -public class ComponentKeyNewValue extends NewValue{ +import static com.google.common.base.Preconditions.checkNotNull; + +public class ComponentKeyNewValue extends NewValue { private final String componentUuid; private final String oldKey; private final String newKey; public ComponentKeyNewValue(String componentUuid, String oldKey, String newKey) { + checkNotNull(componentUuid, oldKey, newKey); this.componentUuid = componentUuid; this.oldKey = oldKey; this.newKey = newKey; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentNewValue.java index b3c359b04be..75b64af6947 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentNewValue.java @@ -22,51 +22,54 @@ package org.sonar.db.audit.model; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.ObjectUtils; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; -public class ComponentNewValue extends NewValue { +import static java.util.Objects.requireNonNull; - private String componentUuid; - private String componentName; - @Nullable - private String description; - private String rootComponentUuid; - private String path; - private String componentKey; - private Boolean isPrivate; +public class ComponentNewValue extends NewValue { + private final String componentUuid; + private final String componentKey; + private final String componentName; + private final String description; + private final Boolean isPrivate; + private final String qualifier; private Boolean isEnabled; - private String qualifier; + private String path; - public ComponentNewValue(String componentUuid, String name, String componentKey, @Nullable String qualifier) { - this.componentUuid = componentUuid; - this.componentName = name; - this.componentKey = componentKey; - this.qualifier = getQualifier(qualifier); + public ComponentNewValue(ProjectDto project) { + this(project.getUuid(), project.getName(), project.getKey(), project.isPrivate(), project.getDescription(), project.getQualifier()); } - public ComponentNewValue(String componentUuid, String name, String componentKey, boolean isPrivate, String qualifier) { - this.componentUuid = componentUuid; - this.componentName = name; - this.componentKey = componentKey; - this.isPrivate = isPrivate; - this.qualifier = getQualifier(qualifier); + public ComponentNewValue(ComponentDto component) { + this(component.uuid(), component.name(), component.getKey(), component.isPrivate(), component.description(), component.qualifier()); } - public ComponentNewValue(String uuid, String name, String componentKey, boolean enabled, String path, @Nullable String qualifier) { - this.componentUuid = uuid; - this.componentName = name; + public ComponentNewValue(String componentUuid, String componentName, String componentKey, String qualifier) { + this(componentUuid, componentName, componentKey, null, null, qualifier); + } + + public ComponentNewValue(String componentUuid, String componentName, String componentKey, boolean isPrivate, String qualifier) { + this(componentUuid, isPrivate, componentName, componentKey, null, qualifier); + } + + public ComponentNewValue(String uuid, String name, String key, boolean enabled, String path, String qualifier) { + this(uuid, name, key, null, null, qualifier); this.isEnabled = enabled; this.path = path; - this.componentKey = componentKey; - this.qualifier = getQualifier(qualifier); } - public ComponentNewValue(String uuid, boolean isPrivate, String name, String componentKey, @Nullable String description, @Nullable String qualifier) { - this.componentUuid = uuid; - this.isPrivate = isPrivate; + public ComponentNewValue(String uuid, @Nullable Boolean isPrivate, String name, String key, @Nullable String description, String qualifier) { + this(uuid, name, key, isPrivate, description, qualifier); + } + + private ComponentNewValue(String uuid, String name, String key, @Nullable Boolean isPrivate, @Nullable String description, String qualifier) { + this.componentUuid = requireNonNull(uuid); this.componentName = name; - this.componentKey = componentKey; + this.componentKey = key; + this.isPrivate = isPrivate; this.description = description; - this.qualifier = getQualifier(qualifier); + this.qualifier = qualifier; } public String getComponentUuid() { @@ -86,6 +89,7 @@ public class ComponentNewValue extends NewValue { return componentKey; } + @CheckForNull public boolean isPrivate() { return isPrivate; } @@ -98,10 +102,9 @@ public class ComponentNewValue extends NewValue { public String toString() { StringBuilder sb = new StringBuilder("{"); addField(sb, "\"componentUuid\": ", this.componentUuid, true); - addField(sb, "\"rootComponentUuid\": ", this.rootComponentUuid, true); addField(sb, "\"componentKey\": ", this.componentKey, true); addField(sb, "\"componentName\": ", this.componentName, true); - addField(sb, "\"qualifier\": ", this.qualifier, true); + addField(sb, "\"qualifier\": ", getQualifier(qualifier), true); addField(sb, "\"description\": ", this.description, true); addField(sb, "\"path\": ", this.path, true); addField(sb, "\"isPrivate\": ", ObjectUtils.toString(this.isPrivate), false); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GroupPermissionNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GroupPermissionNewValue.java index e18885ebc9b..1577998751b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GroupPermissionNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GroupPermissionNewValue.java @@ -31,23 +31,18 @@ public class GroupPermissionNewValue extends PermissionNewValue { @Nullable private String groupName; - public GroupPermissionNewValue(String uuid, String rootComponentUuid, String componentKey, String componentName, - String role, String groupUuid, String groupName, String qualifier, - @Nullable PermissionTemplateDto permissionTemplateDto) { - super(uuid, rootComponentUuid, componentKey, componentName, role, qualifier, permissionTemplateDto); + public GroupPermissionNewValue(String uuid, String componentUuid, String componentKey, String componentName, String role, String groupUuid, String groupName, + String qualifier, @Nullable PermissionTemplateDto permissionTemplate) { + super(uuid, componentUuid, componentKey, componentName, role, qualifier, permissionTemplate); this.groupUuid = groupUuid; this.groupName = groupName; } - - public GroupPermissionNewValue(String rootComponentUuid, String componentKey, String componentName, String role, - String groupUuid, String groupName, String qualifier) { - this(null, rootComponentUuid, componentKey, componentName, role, groupUuid, groupName, qualifier, null); + public GroupPermissionNewValue(String componentUuid, String componentKey, String componentName, String role, String groupUuid, String groupName, String qualifier) { + this(null, componentUuid, componentKey, componentName, role, groupUuid, groupName, qualifier, null); } - public GroupPermissionNewValue(GroupPermissionDto dto, @Nullable String componentKey, @Nullable String qualifier, - @Nullable PermissionTemplateDto permissionTemplateDto) { - this(dto.getUuid(), dto.getComponentUuid(), componentKey, dto.getComponentName(), dto.getRole(), dto.getGroupUuid(), - dto.getGroupName(), qualifier, permissionTemplateDto); + public GroupPermissionNewValue(GroupPermissionDto dto, String componentKey, String qualifier, PermissionTemplateDto permissionTemplate) { + this(dto.getUuid(), dto.getComponentUuid(), componentKey, dto.getComponentName(), dto.getRole(), dto.getGroupUuid(), dto.getGroupName(), qualifier, permissionTemplate); } @Nullable @@ -72,7 +67,7 @@ public class GroupPermissionNewValue extends PermissionNewValue { addField(sb, "\"componentName\": ", this.componentName, true); addField(sb, "\"permissionTemplateUuid\": ", this.permissionTemplateId, true); addField(sb, "\"permissionTemplateName\": ", this.permissionTemplateName, true); - addField(sb, "\"qualifier\": ", this.qualifier, true); + addField(sb, "\"qualifier\": ", getQualifier(this.qualifier), true); endString(sb); return sb.toString(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/LicenseNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/LicenseNewValue.java index c03b67b8514..ff9824a8ee5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/LicenseNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/LicenseNewValue.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; public class LicenseNewValue extends NewValue { @Nullable - private String edition; + private final String edition; public LicenseNewValue(@Nullable String edition) { this.edition = edition; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/NewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/NewValue.java index 1969fe7777f..629f1d7c06b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/NewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/NewValue.java @@ -19,6 +19,7 @@ */ package org.sonar.db.audit.model; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import static com.google.common.base.Strings.isNullOrEmpty; @@ -28,7 +29,7 @@ import static org.sonar.api.resources.Qualifiers.VIEW; public abstract class NewValue { - protected void addField(StringBuilder sb, String field, String value, boolean isString) { + protected void addField(StringBuilder sb, String field, @Nullable String value, boolean isString) { if (!isNullOrEmpty(value)) { sb.append(field); addQuote(sb, isString); @@ -52,6 +53,7 @@ public abstract class NewValue { } } + @CheckForNull protected static String getQualifier(@Nullable String qualifier) { if (qualifier == null) { return null; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PermissionNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PermissionNewValue.java index ad2e0badcb7..1034832d3f8 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PermissionNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PermissionNewValue.java @@ -54,7 +54,7 @@ public abstract class PermissionNewValue extends NewValue { this.componentUuid = componentUuid; this.componentKey = componentKey; this.componentName = componentName; - this.qualifier = getQualifier(qualifier); + this.qualifier = qualifier; this.permission = permission; this.permissionTemplateId = permissionTemplateDto == null ? null : permissionTemplateDto.getUuid(); this.permissionTemplateName = permissionTemplateDto == null ? null : permissionTemplateDto.getName(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PluginNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PluginNewValue.java index faff2a7521a..5bdbf439f20 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PluginNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PluginNewValue.java @@ -22,10 +22,10 @@ package org.sonar.db.audit.model; import org.sonar.db.plugin.PluginDto; public class PluginNewValue extends NewValue { - private String pluginUuid; - private String kee; - private String basePluginKey; - private String type; + private final String pluginUuid; + private final String kee; + private final String basePluginKey; + private final String type; public PluginNewValue(PluginDto pluginDto) { this.pluginUuid = pluginDto.getUuid(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PropertyNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PropertyNewValue.java index 2de53c1714a..1ab794efc6f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PropertyNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PropertyNewValue.java @@ -25,7 +25,6 @@ import org.sonar.db.property.PropertyDto; import org.sonar.db.user.UserPropertyDto; public class PropertyNewValue extends NewValue { - @Nullable private String propertyKey; @Nullable @@ -57,15 +56,14 @@ public class PropertyNewValue extends NewValue { setValue(propertyKey, userPropertyDto.getValue()); } - public PropertyNewValue(PropertyDto propertyDto, @Nullable String userLogin, @Nullable String componentKey, - @Nullable String componentName, @Nullable String qualifier) { + public PropertyNewValue(PropertyDto propertyDto, @Nullable String userLogin, @Nullable String componentKey, @Nullable String componentName, @Nullable String qualifier) { this.propertyKey = propertyDto.getKey(); this.userUuid = propertyDto.getUserUuid(); this.userLogin = userLogin; this.componentUuid = propertyDto.getComponentUuid(); this.componentKey = componentKey; this.componentName = componentName; - this.qualifier = getQualifier(qualifier); + this.qualifier = qualifier; setValue(propertyKey, propertyDto.getValue()); } @@ -86,17 +84,16 @@ public class PropertyNewValue extends NewValue { setValue(propertyKey, propertyValue); } - public PropertyNewValue(@Nullable String propertyKey, @Nullable String projectUuid, @Nullable String componentKey, + public PropertyNewValue(String propertyKey, @Nullable String projectUuid, @Nullable String componentKey, @Nullable String componentName, @Nullable String qualifier, @Nullable String userUuid) { this.propertyKey = propertyKey; this.componentUuid = projectUuid; this.componentKey = componentKey; this.componentName = componentName; this.userUuid = userUuid; - this.qualifier = getQualifier(qualifier); + this.qualifier = qualifier; } - @CheckForNull public String getPropertyKey() { return this.propertyKey; } @@ -146,7 +143,7 @@ public class PropertyNewValue extends NewValue { addField(sb, "\"componentUuid\": ", this.componentUuid, true); addField(sb, "\"componentKey\": ", this.componentKey, true); addField(sb, "\"componentName\": ", this.componentName, true); - addField(sb, "\"qualifier\": ", this.qualifier, true); + addField(sb, "\"qualifier\": ", getQualifier(this.qualifier), true); endString(sb); return sb.toString(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserGroupNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserGroupNewValue.java index f908aac657b..75daabe328e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserGroupNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserGroupNewValue.java @@ -25,51 +25,42 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; -public class UserGroupNewValue extends NewValue { - - @Nullable - private String groupUuid; - - @Nullable - private String name; +import static com.google.common.base.Preconditions.checkState; - @Nullable - private String description; - - @Nullable - private String userUuid; - - @Nullable - private String userLogin; +public class UserGroupNewValue extends NewValue { + private final String groupUuid; + private final String name; + private final String userUuid; + private final String userLogin; + private final String description; public UserGroupNewValue(String groupUuid, String name) { - this.groupUuid = groupUuid; - this.name = name; + this(groupUuid, name, null, null, null); } public UserGroupNewValue(GroupDto groupDto) { - this.groupUuid = groupDto.getUuid(); - this.name = groupDto.getName(); - this.description = groupDto.getDescription(); + this(groupDto.getUuid(), groupDto.getName(), null, null, groupDto.getDescription()); } public UserGroupNewValue(GroupDto groupDto, UserDto userDto) { - this.groupUuid = groupDto.getUuid(); - this.name = groupDto.getName(); - this.userUuid = userDto.getUuid(); - this.userLogin = userDto.getLogin(); + this(groupDto.getUuid(), groupDto.getName(), userDto.getUuid(), userDto.getLogin(), null); } public UserGroupNewValue(UserDto userDto) { - this.userUuid = userDto.getUuid(); - this.userLogin = userDto.getLogin(); + this(null, null, userDto.getUuid(), userDto.getLogin(), null); } public UserGroupNewValue(UserGroupDto userGroupDto, String groupName, String userLogin) { - this.groupUuid = userGroupDto.getGroupUuid(); - this.userUuid = userGroupDto.getUserUuid(); - this.name = groupName; + this(userGroupDto.getGroupUuid(), groupName, userGroupDto.getUserUuid(), userLogin, null); + } + + private UserGroupNewValue(@Nullable String groupUuid, @Nullable String name, @Nullable String userUuid, @Nullable String userLogin, @Nullable String description) { + checkState((groupUuid != null && name != null) || (userUuid != null && userLogin != null)); + this.groupUuid = groupUuid; + this.name = name; + this.userUuid = userUuid; this.userLogin = userLogin; + this.description = description; } @CheckForNull diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java index 62bd6e87bb1..a63af8f8571 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java @@ -24,6 +24,8 @@ import javax.annotation.Nullable; import org.apache.commons.lang.ObjectUtils; import org.sonar.db.user.UserDto; +import static java.util.Objects.requireNonNull; + public class UserNewValue extends NewValue { private String userUuid; private String userLogin; @@ -62,8 +64,8 @@ public class UserNewValue extends NewValue { private Long lastConnectionDate; public UserNewValue(String userUuid, String userLogin) { - this.userUuid = userUuid; - this.userLogin = userLogin; + this.userUuid = requireNonNull(userUuid); + this.userLogin = requireNonNull(userLogin); } public UserNewValue(UserDto userDto) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserPermissionNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserPermissionNewValue.java index cd441d1cfeb..83941d59fd0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserPermissionNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserPermissionNewValue.java @@ -34,8 +34,7 @@ public class UserPermissionNewValue extends PermissionNewValue { public UserPermissionNewValue(UserPermissionDto permissionDto, @Nullable String componentKey, @Nullable String componentName, @Nullable UserId userId, String qualifier, @Nullable PermissionTemplateDto templateDto) { - super(permissionDto.getUuid(), permissionDto.getComponentUuid(), componentKey, componentName, permissionDto.getPermission(), - qualifier, templateDto); + super(permissionDto.getUuid(), permissionDto.getComponentUuid(), componentKey, componentName, permissionDto.getPermission(), qualifier, templateDto); this.userUuid = userId != null ? userId.getUuid() : null; this.userLogin = userId != null ? userId.getLogin() : null; } @@ -73,7 +72,7 @@ public class UserPermissionNewValue extends PermissionNewValue { addField(sb, "\"permissionTemplateName\": ", this.permissionTemplateName, true); addField(sb, "\"userUuid\": ", this.userUuid, true); addField(sb, "\"userLogin\": ", this.userLogin, true); - addField(sb, "\"qualifier\": ", this.qualifier, true); + addField(sb, "\"qualifier\": ", getQualifier(this.qualifier), true); endString(sb); return sb.toString(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 40888a9a045..cf48ef999f3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -53,11 +53,7 @@ import static org.sonar.db.component.ComponentDto.generateBranchKey; import static org.sonar.db.component.ComponentDto.generatePullRequestKey; public class ComponentDao implements Dao { - private AuditPersister auditPersister; - - public ComponentDao() { - //intentionally empty - } + private final AuditPersister auditPersister; public ComponentDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -338,10 +334,8 @@ public class ComponentDao implements Dao { } public void insert(DbSession session, ComponentDto item) { - if (auditPersister != null) { - auditPersister.addComponent(session, new ComponentNewValue(item.uuid(), item.name(), item.getKey(), item.qualifier()), item.qualifier()); - } mapper(session).insert(item); + auditPersister.addComponent(session, new ComponentNewValue(item)); } public void insert(DbSession session, Collection<ComponentDto> items) { @@ -357,10 +351,8 @@ public class ComponentDao implements Dao { } public void update(DbSession session, ComponentUpdateDto component, String qualifier) { - if (auditPersister != null) { - auditPersister.updateComponent(session, new ComponentNewValue(component.getUuid(), component.getBName(), - component.getBKey(), component.isBEnabled(), component.getBPath(), qualifier), qualifier); - } + auditPersister.updateComponent(session, new ComponentNewValue(component.getUuid(), component.getBName(), + component.getBKey(), component.isBEnabled(), component.getBPath(), qualifier)); mapper(session).update(component); } @@ -376,12 +368,14 @@ public class ComponentDao implements Dao { mapper(session).resetBChangedForRootComponentUuid(projectUuid); } + public void setPrivateForRootComponentUuidWithoutAudit(DbSession session, String projectUuid, boolean isPrivate) { + mapper(session).setPrivateForRootComponentUuid(projectUuid, isPrivate); + } + public void setPrivateForRootComponentUuid(DbSession session, String projectUuid, boolean isPrivate, - String key, @Nullable String qualifier, String componentName, boolean track) { - if (track && auditPersister != null) { - ComponentNewValue componentNewValue = new ComponentNewValue(projectUuid, componentName, key, isPrivate, qualifier); - auditPersister.updateComponentVisibility(session, componentNewValue, qualifier); - } + @Nullable String qualifier, String componentKey, String componentName) { + ComponentNewValue componentNewValue = new ComponentNewValue(projectUuid, componentName, componentKey, isPrivate, qualifier); + auditPersister.updateComponentVisibility(session, componentNewValue); mapper(session).setPrivateForRootComponentUuid(projectUuid, isPrivate); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java index d08b963b2eb..80638b887e6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java @@ -42,11 +42,7 @@ import static org.sonar.db.component.ComponentDto.generateBranchKey; * @since 3.2 */ public class ComponentKeyUpdaterDao implements Dao { - - private AuditPersister auditPersister; - - public ComponentKeyUpdaterDao() { - } + private final AuditPersister auditPersister; public ComponentKeyUpdaterDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -86,9 +82,7 @@ public class ComponentKeyUpdaterDao implements Dao { appBranch.setKey(newAppBranchKey); mapper.updateComponent(appBranch); - if(auditPersister != null) { - auditPersister.componentKeyBranchUpdate(dbSession, new ComponentKeyNewValue(appBranchUuid, appBranchOldKey, newAppBranchKey), Qualifiers.APP); - } + auditPersister.componentKeyBranchUpdate(dbSession, new ComponentKeyNewValue(appBranchUuid, appBranchOldKey, newAppBranchKey), Qualifiers.APP); String oldAppBranchFragment = appBranchOldKey.replace(BRANCH_KEY_SEPARATOR, ""); String newAppBranchFragment = appKey + newBranchName; @@ -117,10 +111,7 @@ public class ComponentKeyUpdaterDao implements Dao { } mapper.updateComponent(resource); if (resource.getScope().equals(Scopes.PROJECT) && (resource.getQualifier().equals(Qualifiers.PROJECT) || resource.getQualifier().equals(Qualifiers.APP))) { - if(auditPersister != null) { - auditPersister.componentKeyUpdate(dbSession, - new ComponentKeyNewValue(resource.getUuid(), oldResourceKey, newResourceKey), resource.getQualifier()); - } + auditPersister.componentKeyUpdate(dbSession, new ComponentKeyNewValue(resource.getUuid(), oldResourceKey, newResourceKey), resource.getQualifier()); mapper.updateProject(oldResourceKey, newResourceKey); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java index 31c8e799749..3b53d056f72 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java @@ -41,10 +41,7 @@ public class GroupPermissionDao implements Dao { private static final String ANYONE_GROUP_PARAMETER = "anyoneGroup"; - private AuditPersister auditPersister; - - public GroupPermissionDao() { - } + private final AuditPersister auditPersister; public GroupPermissionDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -124,15 +121,12 @@ public class GroupPermissionDao implements Dao { return mapper(session).selectGroupUuidsWithPermissionOnProjectBut(projectUuid, permission); } - public void insert(DbSession dbSession, GroupPermissionDto groupPermissionDto, @Nullable ComponentDto componentDto, - @Nullable PermissionTemplateDto permissionTemplateDto) { + public void insert(DbSession dbSession, GroupPermissionDto groupPermissionDto, @Nullable ComponentDto componentDto, @Nullable PermissionTemplateDto permissionTemplateDto) { mapper(dbSession).insert(groupPermissionDto); - if (auditPersister != null) { - String componentKey = (componentDto != null) ? componentDto.getKey() : null; - String qualifier = (componentDto != null) ? componentDto.qualifier() : null; - auditPersister.addGroupPermission(dbSession, new GroupPermissionNewValue(groupPermissionDto, componentKey, qualifier, permissionTemplateDto)); - } + String componentKey = (componentDto != null) ? componentDto.getKey() : null; + String qualifier = (componentDto != null) ? componentDto.qualifier() : null; + auditPersister.addGroupPermission(dbSession, new GroupPermissionNewValue(groupPermissionDto, componentKey, qualifier, permissionTemplateDto)); } /** @@ -141,7 +135,7 @@ public class GroupPermissionDao implements Dao { public void deleteByRootComponentUuid(DbSession dbSession, ComponentDto component) { int deletedRecords = mapper(dbSession).deleteByRootComponentUuid(component.uuid()); - if (deletedRecords > 0 && auditPersister != null) { + if (deletedRecords > 0) { auditPersister.deleteGroupPermission(dbSession, new GroupPermissionNewValue(component.uuid(), component.getKey(), component.name(), null, null, null, component.qualifier())); } @@ -154,7 +148,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 (deletedRecords > 0 && auditPersister != null) { + if (deletedRecords > 0) { auditPersister.deleteGroupPermission(dbSession, new GroupPermissionNewValue(component.uuid(), component.getKey(), component.name(), null, groupUuid, "", component.qualifier())); } @@ -164,7 +158,7 @@ public class GroupPermissionDao implements Dao { public int deleteByRootComponentUuidForAnyOne(DbSession dbSession, ComponentDto component) { int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndGroupUuid(component.uuid(), null); - if (deletedRecords > 0 && auditPersister != null) { + if (deletedRecords > 0) { auditPersister.deleteGroupPermission(dbSession, new GroupPermissionNewValue(component.uuid(), component.getKey(), component.name(), null, null, null, component.qualifier())); } @@ -178,7 +172,7 @@ public class GroupPermissionDao implements Dao { public int deleteByRootComponentUuidAndPermission(DbSession dbSession, String permission, ComponentDto component) { int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndPermission(component.uuid(), permission); - if (deletedRecords > 0 && auditPersister != null) { + if (deletedRecords > 0) { auditPersister.deleteGroupPermission(dbSession, new GroupPermissionNewValue(component.uuid(), component.getKey(), component.name(), permission, null, null, component.qualifier())); } @@ -205,7 +199,7 @@ public class GroupPermissionDao implements Dao { int deletedRecords = mapper(dbSession).delete(permission, groupUuid, rootComponentUuid); - if (deletedRecords > 0 && auditPersister != null) { + if (deletedRecords > 0) { String qualifier = (componentDto != null) ? componentDto.qualifier() : null; String componentKey = (componentDto != null) ? componentDto.getKey() : null; String componentName = (componentDto != null) ? componentDto.name() : null; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java index f269ee5acad..c1857fe9c06 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java @@ -39,10 +39,7 @@ import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class UserPermissionDao implements Dao { - private AuditPersister auditPersister; - - public UserPermissionDao() { - } + private final AuditPersister auditPersister; public UserPermissionDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -120,13 +117,11 @@ public class UserPermissionDao implements Dao { @Nullable UserId userId, @Nullable PermissionTemplateDto templateDto) { mapper(dbSession).insert(dto); - if (auditPersister != null) { - String componentName = (componentDto != null) ? componentDto.name() : null; - String componentKey = (componentDto != null) ? componentDto.getKey() : null; - String qualifier = (componentDto != null) ? componentDto.qualifier() : null; - auditPersister.addUserPermission(dbSession, new UserPermissionNewValue(dto, componentKey, componentName, userId, qualifier, - templateDto)); - } + String componentName = (componentDto != null) ? componentDto.name() : null; + String componentKey = (componentDto != null) ? componentDto.getKey() : null; + String qualifier = (componentDto != null) ? componentDto.qualifier() : null; + auditPersister.addUserPermission(dbSession, new UserPermissionNewValue(dto, componentKey, componentName, userId, qualifier, + templateDto)); } /** @@ -135,7 +130,7 @@ public class UserPermissionDao implements Dao { public void deleteGlobalPermission(DbSession dbSession, UserId user, String permission) { int deletedRows = mapper(dbSession).deleteGlobalPermission(user.getUuid(), permission); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(permission, null, null, null, user, null)); } } @@ -146,9 +141,8 @@ public class UserPermissionDao implements Dao { public void deleteProjectPermission(DbSession dbSession, UserId user, String permission, ComponentDto component) { int deletedRows = mapper(dbSession).deleteProjectPermission(user.getUuid(), permission, component.uuid()); - if (deletedRows > 0 && auditPersister != null) { - auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(permission, component.uuid(), component.getKey(), - component.name(), user, component.qualifier())); + if (deletedRows > 0) { + auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(permission, component.uuid(), component.getKey(), component.name(), user, component.qualifier())); } } @@ -158,7 +152,7 @@ public class UserPermissionDao implements Dao { public void deleteProjectPermissions(DbSession dbSession, ComponentDto component) { int deletedRows = mapper(dbSession).deleteProjectPermissions(component.uuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(null, component.uuid(), component.getKey(), component.name(), null, component.qualifier())); } @@ -170,9 +164,8 @@ public class UserPermissionDao implements Dao { public int deleteProjectPermissionOfAnyUser(DbSession dbSession, String permission, ComponentDto project) { int deletedRows = mapper(dbSession).deleteProjectPermissionOfAnyUser(project.uuid(), permission); - if (deletedRows > 0 && auditPersister != null) { - auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(permission, project.uuid(), project.getKey(), - project.name(), null, project.qualifier())); + if (deletedRows > 0) { + auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(permission, project.uuid(), project.getKey(), project.name(), null, project.qualifier())); } return deletedRows; @@ -181,7 +174,7 @@ public class UserPermissionDao implements Dao { public void deleteByUserUuid(DbSession dbSession, UserId userId) { int deletedRows = mapper(dbSession).deleteByUserUuid(userId.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserPermission(dbSession, new UserPermissionNewValue(userId, null)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java index ab4cede2f87..b63d9570163 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java @@ -31,10 +31,7 @@ import static java.util.Objects.requireNonNull; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class PermissionTemplateCharacteristicDao implements Dao { - private AuditPersister auditPersister; - - public PermissionTemplateCharacteristicDao() { - } + private final AuditPersister auditPersister; public PermissionTemplateCharacteristicDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -53,10 +50,8 @@ public class PermissionTemplateCharacteristicDao implements Dao { checkArgument(dto.getCreatedAt() != 0L && dto.getUpdatedAt() != 0L); mapper(dbSession).insert(dto); - if (auditPersister != null) { - auditPersister.addCharacteristicToPermissionTemplate(dbSession, new PermissionTemplateNewValue(dto.getTemplateUuid(), - dto.getPermission(), templateName, dto.getWithProjectCreator())); - } + auditPersister.addCharacteristicToPermissionTemplate(dbSession, new PermissionTemplateNewValue(dto.getTemplateUuid(), + dto.getPermission(), templateName, dto.getWithProjectCreator())); return dto; } @@ -66,10 +61,8 @@ public class PermissionTemplateCharacteristicDao implements Dao { requireNonNull(templatePermissionDto.getUuid()); mapper(dbSession).update(templatePermissionDto); - if (auditPersister != null) { - auditPersister.updateCharacteristicInPermissionTemplate(dbSession, new PermissionTemplateNewValue(templatePermissionDto.getTemplateUuid(), - templatePermissionDto.getPermission(), templateName, templatePermissionDto.getWithProjectCreator())); - } + auditPersister.updateCharacteristicInPermissionTemplate(dbSession, new PermissionTemplateNewValue(templatePermissionDto.getTemplateUuid(), + templatePermissionDto.getPermission(), templateName, templatePermissionDto.getWithProjectCreator())); return templatePermissionDto; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java index 4aa396f4456..32a2d05328a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java @@ -49,16 +49,11 @@ public class PermissionTemplateDao implements Dao { private final System2 system; private final UuidFactory uuidFactory; + private final AuditPersister auditPersister; - private AuditPersister auditPersister; - - public PermissionTemplateDao(UuidFactory uuidFactory, System2 system) { + public PermissionTemplateDao(UuidFactory uuidFactory, System2 system, AuditPersister auditPersister) { this.uuidFactory = uuidFactory; this.system = system; - } - - public PermissionTemplateDao(UuidFactory uuidFactory, System2 system, AuditPersister auditPersister) { - this(uuidFactory, system); this.auditPersister = auditPersister; } @@ -124,10 +119,7 @@ public class PermissionTemplateDao implements Dao { dto.setUuid(uuidFactory.create()); } mapper(session).insert(dto); - - if (auditPersister != null) { - auditPersister.addPermissionTemplate(session, new PermissionTemplateNewValue(dto.getUuid(), dto.getName())); - } + auditPersister.addPermissionTemplate(session, new PermissionTemplateNewValue(dto.getUuid(), dto.getName())); return dto; } @@ -172,16 +164,14 @@ public class PermissionTemplateDao implements Dao { session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateUuid(templateUuid); int deletedRows = mapper.deleteByUuid(templateUuid); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deletePermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName)); } } public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) { mapper(session).update(permissionTemplate); - if (auditPersister != null) { - auditPersister.updatePermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplate)); - } + auditPersister.updatePermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplate)); return permissionTemplate; } @@ -197,10 +187,7 @@ public class PermissionTemplateDao implements Dao { mapper(session).insertUserPermission(permissionTemplateUser); - if (auditPersister != null) { - auditPersister.addUserToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, - permission, userUuid, userLogin, null, null)); - } + auditPersister.addUserToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, permission, userUuid, userLogin, null, null)); session.commit(); } @@ -213,9 +200,8 @@ public class PermissionTemplateDao implements Dao { .setUserUuid(userUuid); int deletedRows = mapper(session).deleteUserPermission(permissionTemplateUser); - if (deletedRows > 0 && auditPersister != null) { - auditPersister.deleteUserFromPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, - permission, userUuid, userLogin, null, null)); + if (deletedRows > 0) { + auditPersister.deleteUserFromPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, permission, userUuid, userLogin, null, null)); } session.commit(); @@ -224,9 +210,8 @@ public class PermissionTemplateDao implements Dao { public void deleteUserPermissionsByUserUuid(DbSession dbSession, String userUuid, String userLogin) { int deletedRows = mapper(dbSession).deleteUserPermissionsByUserUuid(userUuid); - if (deletedRows > 0 && auditPersister != null) { - auditPersister.deleteUserFromPermissionTemplate(dbSession, new PermissionTemplateNewValue(null, null, - null, userUuid, userLogin, null, null)); + if (deletedRows > 0) { + auditPersister.deleteUserFromPermissionTemplate(dbSession, new PermissionTemplateNewValue(null, null, null, userUuid, userLogin, null, null)); } } @@ -241,19 +226,14 @@ public class PermissionTemplateDao implements Dao { .setUpdatedAt(now()); mapper(session).insertGroupPermission(permissionTemplateGroup); - if (auditPersister != null) { - auditPersister.addGroupToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, - permission, null, null, groupUuid, groupName)); - } + auditPersister.addGroupToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, permission, null, null, groupUuid, groupName)); } public void insertGroupPermission(DbSession session, PermissionTemplateGroupDto permissionTemplateGroup, String templateName) { mapper(session).insertGroupPermission(permissionTemplateGroup); - if (auditPersister != null) { - auditPersister.addGroupToPermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplateGroup.getTemplateUuid(), templateName, - permissionTemplateGroup.getPermission(), null, null, permissionTemplateGroup.getGroupUuid(), permissionTemplateGroup.getGroupName())); - } + auditPersister.addGroupToPermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplateGroup.getTemplateUuid(), templateName, + permissionTemplateGroup.getPermission(), null, null, permissionTemplateGroup.getGroupUuid(), permissionTemplateGroup.getGroupName())); } public void deleteGroupPermission(DbSession session, String templateUuid, @Nullable String groupUuid, String permission, String templateName, @@ -264,7 +244,7 @@ public class PermissionTemplateDao implements Dao { .setGroupUuid(groupUuid); int deletedRows = mapper(session).deleteGroupPermission(permissionTemplateGroup); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplateGroup.getTemplateUuid(), templateName, permissionTemplateGroup.getPermission(), null, null, permissionTemplateGroup.getGroupUuid(), groupName)); } @@ -286,9 +266,8 @@ public class PermissionTemplateDao implements Dao { public void deleteByGroup(DbSession session, String groupUuid, String groupName) { int deletedRows = session.getMapper(PermissionTemplateMapper.class).deleteByGroupUuid(groupUuid); - if (deletedRows > 0 && auditPersister != null) { - auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(null, null, - null, null, null, groupUuid, groupName)); + if (deletedRows > 0) { + auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(null, null, null, null, null, groupUuid, groupName)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java index 489e22bc694..740be6bbaf0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java @@ -27,11 +27,7 @@ import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.PluginNewValue; public class PluginDao implements Dao { - - private AuditPersister auditPersister; - - public PluginDao() { - } + private final AuditPersister auditPersister; public PluginDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -53,18 +49,12 @@ public class PluginDao implements Dao { public void insert(DbSession dbSession, PluginDto dto) { mapper(dbSession).insert(dto); - - if (auditPersister != null) { - auditPersister.addPlugin(dbSession, new PluginNewValue(dto)); - } + auditPersister.addPlugin(dbSession, new PluginNewValue(dto)); } public void update(DbSession dbSession, PluginDto dto) { mapper(dbSession).update(dto); - - if (auditPersister != null) { - auditPersister.updatePlugin(dbSession, new PluginNewValue(dto)); - } + auditPersister.updatePlugin(dbSession, new PluginNewValue(dto)); } private static PluginMapper mapper(DbSession dbSession) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index 45a32cc1906..4167cead120 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -31,11 +31,7 @@ import org.sonar.db.audit.model.ComponentNewValue; public class ProjectDao implements Dao { private final System2 system2; - private AuditPersister auditPersister; - - public ProjectDao(System2 system2) { - this.system2 = system2; - } + private final AuditPersister auditPersister; public ProjectDao(System2 system2, AuditPersister auditPersister) { this.system2 = system2; @@ -47,9 +43,8 @@ public class ProjectDao implements Dao { } public void insert(DbSession session, ProjectDto project, boolean track) { - if (track && auditPersister != null) { - auditPersister.addComponent(session, new ComponentNewValue(project.getUuid(), project.getName(), project.getKey(), project.getQualifier()), - project.getQualifier()); + if (track) { + auditPersister.addComponent(session, new ComponentNewValue(project)); } mapper(session).insert(project); } @@ -112,11 +107,7 @@ public class ProjectDao implements Dao { } public void update(DbSession session, ProjectDto project) { - if (auditPersister != null) { - auditPersister.updateComponent(session, new ComponentNewValue(project.getUuid(), project.isPrivate(), - project.getName(), project.getKey(), project.getDescription(), project.getQualifier()), project.getQualifier()); - } - + auditPersister.updateComponent(session, new ComponentNewValue(project)); mapper(session).update(project); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java index 522d7cdbf33..45089251dcb 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -55,16 +55,12 @@ public class PropertiesDao implements Dao { private final MyBatis mybatis; private final System2 system2; private final UuidFactory uuidFactory; - private AuditPersister auditPersister; + private final AuditPersister auditPersister; - public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory) { + public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.mybatis = mybatis; this.system2 = system2; this.uuidFactory = uuidFactory; - } - - public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { - this(mybatis, system2, uuidFactory); this.auditPersister = auditPersister; } @@ -109,9 +105,9 @@ public class PropertiesDao implements Dao { } try (DbSession session = mybatis.openSession(false); - Connection connection = session.getConnection(); - PreparedStatement pstmt = createStatement(projectUuid, dispatcherKeys, connection); - ResultSet rs = pstmt.executeQuery()) { + Connection connection = session.getConnection(); + PreparedStatement pstmt = createStatement(projectUuid, dispatcherKeys, connection); + ResultSet rs = pstmt.executeQuery()) { return rs.next() && rs.getInt(1) > 0; } catch (SQLException e) { throw new IllegalStateException("Fail to execute SQL for hasProjectNotificationSubscribersForDispatchers", e); @@ -214,16 +210,14 @@ public class PropertiesDao implements Dao { saveProperty(session, property, null, null, null, null); } - public void saveProperty(DbSession session, PropertyDto property, @Nullable String userLogin, @Nullable String projectKey, - @Nullable String projectName, @Nullable String qualifier) { + public void saveProperty(DbSession session, PropertyDto property, @Nullable String userLogin, @Nullable String projectKey, @Nullable String projectName, + @Nullable String qualifier) { int affectedRows = save(getMapper(session), property.getKey(), property.getUserUuid(), property.getComponentUuid(), property.getValue()); - if (auditPersister != null && auditPersister.isTrackedProperty(property.getKey())) { - if (affectedRows > 0) { - auditPersister.updateProperty(session, new PropertyNewValue(property, userLogin, projectKey, projectName, qualifier), false); - } else { - auditPersister.addProperty(session, new PropertyNewValue(property, userLogin, projectKey, projectName, qualifier), false); - } + if (affectedRows > 0) { + auditPersister.updateProperty(session, new PropertyNewValue(property, userLogin, projectKey, projectName, qualifier), false); + } else { + auditPersister.addProperty(session, new PropertyNewValue(property, userLogin, projectKey, projectName, qualifier), false); } } @@ -272,7 +266,7 @@ public class PropertiesDao implements Dao { public int deleteByQuery(DbSession dbSession, PropertyQuery query) { int deletedRows = getMapper(dbSession).deleteByQuery(query); - if (deletedRows > 0 && auditPersister != null && query.key() != null && auditPersister.isTrackedProperty(query.key())) { + if (deletedRows > 0 && query.key() != null) { auditPersister.deleteProperty(dbSession, new PropertyNewValue(query.key(), query.componentUuid(), null, null, null, query.userUuid()), false); } @@ -284,7 +278,7 @@ public class PropertiesDao implements Dao { @Nullable String projectName, @Nullable String qualifier) { int deletedRows = getMapper(dbSession).delete(dto.getKey(), dto.getUserUuid(), dto.getComponentUuid()); - if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) { + if (deletedRows > 0) { auditPersister.deleteProperty(dbSession, new PropertyNewValue(dto, userLogin, projectKey, projectName, qualifier), false); } @@ -302,7 +296,7 @@ public class PropertiesDao implements Dao { String projectName, String qualifier) { int deletedRows = getMapper(session).deleteProjectProperty(key, projectUuid); - if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { auditPersister.deleteProperty(session, new PropertyNewValue(key, projectUuid, projectKey, projectName, qualifier, null), false); } @@ -311,7 +305,7 @@ public class PropertiesDao implements Dao { public void deleteProjectProperties(String key, String value, DbSession session) { int deletedRows = getMapper(session).deleteProjectProperties(key, value); - if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { auditPersister.deleteProperty(session, new PropertyNewValue(key, value), false); } } @@ -326,7 +320,7 @@ public class PropertiesDao implements Dao { public void deleteGlobalProperty(String key, DbSession session) { int deletedRows = getMapper(session).deleteGlobalProperty(key); - if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { auditPersister.deleteProperty(session, new PropertyNewValue(key), false); } } @@ -357,7 +351,7 @@ public class PropertiesDao implements Dao { public void deleteByKeyAndValue(DbSession dbSession, String key, String value) { int deletedRows = getMapper(dbSession).deleteByKeyAndValue(key, value); - if (deletedRows > 0 && auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { auditPersister.deleteProperty(dbSession, new PropertyNewValue(key, value), false); } } @@ -368,7 +362,7 @@ public class PropertiesDao implements Dao { properties.forEach((key, value) -> { int affectedRows = save(mapper, key, null, null, value); - if (auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (auditPersister.isTrackedProperty(key)) { if (affectedRows > 0) { auditPersister.updateProperty(session, new PropertyNewValue(key, value), false); } else { @@ -397,7 +391,7 @@ public class PropertiesDao implements Dao { } private void persistDeletedProperties(DbSession dbSession, @Nullable String userUuid, String userLogin, List<String> uuids) { - if (!uuids.isEmpty() && auditPersister != null) { + if (!uuids.isEmpty()) { List<PropertyDto> properties = executeLargeInputs(uuids, subList -> getMapper(dbSession).selectByUuids(subList)); properties diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java index cf36fb63216..46b3d523ac4 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -54,11 +54,7 @@ public class PurgeDao implements Dao { private static final String SCOPE_PROJECT = "PRJ"; private final System2 system2; - private AuditPersister auditPersister; - - public PurgeDao(System2 system2) { - this.system2 = system2; - } + private final AuditPersister auditPersister; public PurgeDao(System2 system2, AuditPersister auditPersister) { this.system2 = system2; @@ -195,19 +191,14 @@ public class PurgeDao implements Dao { long start = System2.INSTANCE.now(); List<String> branchUuids = session.getMapper(BranchMapper.class).selectByProjectUuid(uuid).stream() - .filter(branch -> !uuid.equals(branch.getUuid())) .map(BranchDto::getUuid) + .filter(branchUuid -> !uuid.equals(branchUuid)) .collect(Collectors.toList()); - branchUuids.stream() - .forEach(id -> deleteRootComponent(id, purgeMapper, purgeCommands)); + branchUuids.forEach(id -> deleteRootComponent(id, purgeMapper, purgeCommands)); deleteRootComponent(uuid, purgeMapper, purgeCommands); - - if (auditPersister != null) { - auditPersister.deleteComponent(session, new ComponentNewValue(uuid, name, key, qualifier), qualifier); - } - + auditPersister.deleteComponent(session, new ComponentNewValue(uuid, name, key, qualifier)); logProfiling(profiler, start); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java index 20e39b9357c..b4ece4dd8e6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java @@ -41,14 +41,10 @@ import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class GroupDao implements Dao { private final System2 system; - private AuditPersister auditPersister; - - public GroupDao(System2 system) { - this.system = system; - } + private final AuditPersister auditPersister; public GroupDao(System2 system, AuditPersister auditPersister) { - this(system); + this.system = system; this.auditPersister = auditPersister; } @@ -77,7 +73,7 @@ public class GroupDao implements Dao { public void deleteByUuid(DbSession dbSession, String groupUuid, String groupName) { int deletedRows = mapper(dbSession).deleteByUuid(groupUuid); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserGroup(dbSession, new UserGroupNewValue(groupUuid, groupName)); } } @@ -95,22 +91,14 @@ public class GroupDao implements Dao { item.setCreatedAt(createdAt) .setUpdatedAt(createdAt); mapper(session).insert(item); - - if (auditPersister != null) { - auditPersister.addUserGroup(session, new UserGroupNewValue(item.getUuid(), item.getName())); - } - + auditPersister.addUserGroup(session, new UserGroupNewValue(item.getUuid(), item.getName())); return item; } public GroupDto update(DbSession session, GroupDto item) { item.setUpdatedAt(new Date(system.now())); mapper(session).update(item); - - if (auditPersister != null) { - auditPersister.updateUserGroup(session, new UserGroupNewValue(item)); - } - + auditPersister.updateUserGroup(session, new UserGroupNewValue(item)); return item; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java index fda3d03005d..56e53fe8bb0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java @@ -49,16 +49,11 @@ public class UserDao implements Dao { private static final long WEEK_IN_MS = DAYS.toMillis(7L); private final System2 system2; private final UuidFactory uuidFactory; + private final AuditPersister auditPersister; - private AuditPersister auditPersister; - - public UserDao(System2 system2, UuidFactory uuidFactory) { + public UserDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.system2 = system2; this.uuidFactory = uuidFactory; - } - - public UserDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { - this(system2, uuidFactory); this.auditPersister = auditPersister; } @@ -114,11 +109,7 @@ public class UserDao implements Dao { public UserDto insert(DbSession session, UserDto dto) { long now = system2.now(); mapper(session).insert(dto.setUuid(uuidFactory.create()).setCreatedAt(now).setUpdatedAt(now)); - - if (auditPersister != null) { - auditPersister.addUser(session, new UserNewValue(dto.getUuid(), dto.getLogin())); - } - + auditPersister.addUser(session, new UserNewValue(dto.getUuid(), dto.getLogin())); return dto; } @@ -128,7 +119,7 @@ public class UserDao implements Dao { public UserDto update(DbSession session, UserDto dto, boolean track) { mapper(session).update(dto.setUpdatedAt(system2.now())); - if (track && auditPersister != null) { + if (track) { auditPersister.updateUser(session, new UserNewValue(dto)); } return dto; @@ -144,10 +135,7 @@ public class UserDao implements Dao { public void deactivateUser(DbSession dbSession, UserDto user) { mapper(dbSession).deactivateUser(user.getLogin(), system2.now()); - - if (auditPersister != null) { - auditPersister.deactivateUser(dbSession, new UserNewValue(user.getUuid(), user.getLogin())); - } + auditPersister.deactivateUser(dbSession, new UserNewValue(user.getUuid(), user.getLogin())); } public void cleanHomepage(DbSession dbSession, ProjectDto project) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java index 7c7bf915947..a90ff80a41a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java @@ -26,11 +26,7 @@ import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.UserGroupNewValue; public class UserGroupDao implements Dao { - - private AuditPersister auditPersister; - - public UserGroupDao() { - } + private final AuditPersister auditPersister; public UserGroupDao(AuditPersister auditPersister) { this.auditPersister = auditPersister; @@ -38,11 +34,7 @@ public class UserGroupDao implements Dao { public UserGroupDto insert(DbSession session, UserGroupDto dto, String groupName, String login) { mapper(session).insert(dto); - - if (auditPersister != null) { - auditPersister.addUserToGroup(session, new UserGroupNewValue(dto, groupName, login)); - } - + auditPersister.addUserToGroup(session, new UserGroupNewValue(dto, groupName, login)); return dto; } @@ -53,7 +45,7 @@ public class UserGroupDao implements Dao { public void delete(DbSession session, GroupDto group, UserDto user) { int deletedRows = mapper(session).delete(group.getUuid(), user.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserFromGroup(session, new UserGroupNewValue(group, user)); } } @@ -61,7 +53,7 @@ public class UserGroupDao implements Dao { public void deleteByGroupUuid(DbSession session, String groupUuid, String groupName) { int deletedRows = mapper(session).deleteByGroupUuid(groupUuid); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserFromGroup(session, new UserGroupNewValue(groupUuid, groupName)); } } @@ -69,7 +61,7 @@ public class UserGroupDao implements Dao { public void deleteByUserUuid(DbSession dbSession, UserDto userDto) { int deletedRows = mapper(dbSession).deleteByUserUuid(userDto.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserFromGroup(dbSession, new UserGroupNewValue(userDto)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesDao.java index 01080f163be..cca3809c127 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserPropertiesDao.java @@ -32,16 +32,11 @@ public class UserPropertiesDao implements Dao { private final System2 system2; private final UuidFactory uuidFactory; + private final AuditPersister auditPersister; - private AuditPersister auditPersister; - - public UserPropertiesDao(System2 system2, UuidFactory uuidFactory) { + public UserPropertiesDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { this.system2 = system2; this.uuidFactory = uuidFactory; - } - - public UserPropertiesDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { - this(system2, uuidFactory); this.auditPersister = auditPersister; } @@ -57,12 +52,10 @@ public class UserPropertiesDao implements Dao { isUpdate = false; } - if (auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) { - if (isUpdate) { - auditPersister.updateProperty(session, new PropertyNewValue(dto, login), true); - } else { - auditPersister.addProperty(session, new PropertyNewValue(dto, login), true); - } + if (isUpdate) { + auditPersister.updateProperty(session, new PropertyNewValue(dto, login), true); + } else { + auditPersister.addProperty(session, new PropertyNewValue(dto, login), true); } return dto; @@ -72,7 +65,7 @@ public class UserPropertiesDao implements Dao { List<UserPropertyDto> userProperties = selectByUser(session, user); int deletedRows = mapper(session).deleteByUserUuid(user.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { userProperties.stream() .filter(p -> auditPersister.isTrackedProperty(p.getKey())) .forEach(p -> auditPersister.deleteProperty(session, new PropertyNewValue(p, user.getLogin()), true)); 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 11165f9fd55..21e129d4fd8 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 @@ -35,38 +35,27 @@ import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class UserTokenDao implements Dao { - - private UuidFactory uuidFactory; - private AuditPersister auditPersister; - - public UserTokenDao(UuidFactory uuidFactory) { - this.uuidFactory = uuidFactory; - } + private final UuidFactory uuidFactory; + private final AuditPersister auditPersister; public UserTokenDao(UuidFactory uuidFactory, AuditPersister auditPersister) { - this(uuidFactory); + this.uuidFactory = uuidFactory; this.auditPersister = auditPersister; } public void insert(DbSession dbSession, UserTokenDto userTokenDto, String userLogin) { userTokenDto.setUuid(uuidFactory.create()); mapper(dbSession).insert(userTokenDto); - - if (auditPersister != null) { - auditPersister.addUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); - } + auditPersister.addUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); } - public void update(DbSession session, UserTokenDto userTokenDto, @Nullable String userLogin) { - update(session, userTokenDto, true, userLogin); + public void update(DbSession dbSession, UserTokenDto userTokenDto, @Nullable String userLogin) { + mapper(dbSession).update(userTokenDto); + auditPersister.updateUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); } - public void update(DbSession dbSession, UserTokenDto userTokenDto, boolean track, @Nullable String userLogin) { + public void updateWithoutAudit(DbSession dbSession, UserTokenDto userTokenDto) { mapper(dbSession).update(userTokenDto); - - if (track && auditPersister != null) { - auditPersister.updateUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); - } } @CheckForNull @@ -101,7 +90,7 @@ public class UserTokenDao implements Dao { public void deleteByUser(DbSession dbSession, UserDto user) { int deletedRows = mapper(dbSession).deleteByUserUuid(user.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserToken(dbSession, new UserTokenNewValue(user)); } } @@ -109,7 +98,7 @@ public class UserTokenDao implements Dao { public void deleteByUserAndName(DbSession dbSession, UserDto user, String name) { int deletedRows = mapper(dbSession).deleteByUserUuidAndName(user.getUuid(), name); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteUserToken(dbSession, new UserTokenNewValue(user, name)); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java index 38c6be76329..8e619c28a5f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDao.java @@ -33,11 +33,7 @@ import org.sonar.db.project.ProjectDto; public class WebhookDao implements Dao { private final System2 system2; - private AuditPersister auditPersister; - - public WebhookDao(System2 system2) { - this.system2 = system2; - } + private final AuditPersister auditPersister; public WebhookDao(System2 system2, AuditPersister auditPersister) { this.system2 = system2; @@ -58,27 +54,21 @@ public class WebhookDao implements Dao { public void insert(DbSession dbSession, WebhookDto dto, @Nullable String projectKey, @Nullable String projectName) { mapper(dbSession).insert(dto.setCreatedAt(system2.now()).setUpdatedAt(system2.now())); - - if (auditPersister != null) { - auditPersister.addWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName)); - } + auditPersister.addWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName)); } public void update(DbSession dbSession, WebhookDto dto, @Nullable String projectKey, @Nullable String projectName) { mapper(dbSession).update(dto.setUpdatedAt(system2.now())); - - if (auditPersister != null) { - if (dto.getSecret() != null) { - auditPersister.updateWebhookSecret(dbSession, new SecretNewValue("webhook_name", dto.getName())); - } - auditPersister.updateWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName)); + if (dto.getSecret() != null) { + auditPersister.updateWebhookSecret(dbSession, new SecretNewValue("webhook_name", dto.getName())); } + auditPersister.updateWebhook(dbSession, new WebhookNewValue(dto, projectKey, projectName)); } public void delete(DbSession dbSession, String uuid, String webhookName) { int deletedRows = mapper(dbSession).delete(uuid); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteWebhook(dbSession, new WebhookNewValue(uuid, webhookName)); } } @@ -86,7 +76,7 @@ public class WebhookDao implements Dao { public void deleteByProject(DbSession dbSession, ProjectDto projectDto) { int deletedRows = mapper(dbSession).deleteForProjectUuid(projectDto.getUuid()); - if (deletedRows > 0 && auditPersister != null) { + if (deletedRows > 0) { auditPersister.deleteWebhook(dbSession, new WebhookNewValue(projectDto)); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoTest.java index 5b4ac5b94e8..365fdeeb26f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoTest.java @@ -27,6 +27,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDao; import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.user.UserDto; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -46,9 +47,9 @@ public class AlmPatDaoTest { private DbSession dbSession = db.getSession(); private UuidFactory uuidFactory = mock(UuidFactory.class); - private AlmSettingDao almSettingDao = new AlmSettingDao(system2, uuidFactory); + private AlmSettingDao almSettingDao = new AlmSettingDao(system2, uuidFactory, new NoOpAuditPersister()); - private AlmPatDao underTest = new AlmPatDao(system2, uuidFactory); + private AlmPatDao underTest = new AlmPatDao(system2, uuidFactory, new NoOpAuditPersister()); @Test public void selectByUuid() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoWithPersisterTest.java index 73a736e5e3d..44433fd239e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/pat/AlmPatDaoWithPersisterTest.java @@ -34,6 +34,7 @@ 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.clearInvocations; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -45,16 +46,17 @@ public class AlmPatDaoWithPersisterTest { private static final long NOW = 1000000L; private static final String A_UUID = "SOME_UUID"; + private final AuditPersister auditPersister = mock(AuditPersister.class); private final ArgumentCaptor<PersonalAccessTokenNewValue> newValueCaptor = ArgumentCaptor.forClass(PersonalAccessTokenNewValue.class); - private TestSystem2 system2 = new TestSystem2().setNow(NOW); + private final TestSystem2 system2 = new TestSystem2().setNow(NOW); @Rule public DbTester db = DbTester.create(system2, auditPersister); - private DbSession dbSession = db.getSession(); - private UuidFactory uuidFactory = mock(UuidFactory.class); + private final DbSession dbSession = db.getSession(); + private final UuidFactory uuidFactory = mock(UuidFactory.class); - private AlmPatDao underTest = db.getDbClient().almPatDao(); + private final AlmPatDao underTest = db.getDbClient().almPatDao(); @Test public void insertAndUpdateArePersisted() { @@ -164,7 +166,7 @@ public class AlmPatDaoWithPersisterTest { @Test public void deleteByAlmSettingWithoutAffectedRowsIsNotPersisted() { AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); - + clearInvocations(auditPersister); underTest.deleteByAlmSetting(dbSession, almSettingDto); verifyNoInteractions(auditPersister); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java index b2b3a20797b..acebd2bc6c5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java @@ -27,6 +27,7 @@ import org.sonar.core.util.UuidFactory; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.almsettings.AlmSettingsTesting; +import org.sonar.db.audit.NoOpAuditPersister; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -44,7 +45,7 @@ public class AlmSettingDaoTest { private DbSession dbSession = db.getSession(); private UuidFactory uuidFactory = mock(UuidFactory.class); - private AlmSettingDao underTest = new AlmSettingDao(system2, uuidFactory); + private AlmSettingDao underTest = new AlmSettingDao(system2, uuidFactory, new NoOpAuditPersister()); @Test public void selectByUuid() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoTest.java index 6824adb0734..c972d2d6ab6 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoTest.java @@ -27,6 +27,7 @@ import org.sonar.api.impl.utils.TestSystem2; import org.sonar.core.util.UuidFactory; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.project.ProjectDto; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +49,7 @@ public class ProjectAlmSettingDaoTest { private DbSession dbSession = db.getSession(); private UuidFactory uuidFactory = mock(UuidFactory.class); - private ProjectAlmSettingDao underTest = new ProjectAlmSettingDao(system2, uuidFactory); + private ProjectAlmSettingDao underTest = new ProjectAlmSettingDao(system2, uuidFactory, new NoOpAuditPersister()); @Test public void select_by_project() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java index 018bda5d50e..e1ce9095f12 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; import org.sonar.api.impl.utils.TestSystem2; import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.audit.AuditPersister; @@ -38,6 +39,7 @@ 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.newGithubAlmSettingDto; import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto; public class ProjectAlmSettingDaoWithPersisterTest { @@ -53,13 +55,12 @@ public class ProjectAlmSettingDaoWithPersisterTest { public final DbTester db = DbTester.create(system2, auditPersister); private final DbSession dbSession = db.getSession(); - private final UuidFactory uuidFactory = mock(UuidFactory.class); + private final UuidFactory uuidFactory = UuidFactoryFast.getInstance(); private final ProjectAlmSettingDao underTest = db.getDbClient().projectAlmSettingDao(); @Test public void insertAndUpdateExistingBindingArePersisted() { - when(uuidFactory.create()).thenReturn(A_UUID); - AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); + AlmSettingDto githubAlmSetting = newGithubAlmSettingDto().setUuid(uuidFactory.create()); ProjectDto project = db.components().insertPrivateProjectDto(); ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project) .setSummaryCommentEnabled(false); @@ -95,8 +96,7 @@ public class ProjectAlmSettingDaoWithPersisterTest { @Test public void deleteByProjectIsPersisted() { - when(uuidFactory.create()).thenReturn(A_UUID); - AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); + AlmSettingDto githubAlmSetting = newGithubAlmSettingDto().setUuid(uuidFactory.create()); ProjectDto project = db.components().insertPrivateProjectDto(); ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project) .setSummaryCommentEnabled(false); @@ -118,15 +118,7 @@ public class ProjectAlmSettingDaoWithPersisterTest { underTest.deleteByProject(dbSession, project); - verify(auditPersister).addComponent(any(), any(), any()); + verify(auditPersister).addComponent(any(), any()); verifyNoMoreInteractions(auditPersister); } - - @Test - public void deleteByAlmSettingNotTrackedIsNotPersisted() { - AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); - underTest.deleteByAlmSetting(dbSession, githubAlmSetting); - - verifyNoInteractions(auditPersister); - } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java index dde9a0beda0..56054397785 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -42,7 +42,6 @@ import org.assertj.core.api.ListAssert; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; @@ -51,6 +50,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.audit.model.ComponentNewValue; import org.sonar.db.issue.IssueDto; import org.sonar.db.metric.MetricDto; @@ -72,7 +72,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -118,7 +117,7 @@ public class ComponentDaoTest { private final Random random = new Random(); private final DbSession dbSession = db.getSession(); - private final ComponentDao underTest = new ComponentDao(); + private final ComponentDao underTest = new ComponentDao(new NoOpAuditPersister()); private final ComponentDao underTestWithAuditPersister = new ComponentDao(auditPersister); private static ComponentTreeQuery.Builder newTreeQuery(String baseUuid) { @@ -1772,7 +1771,7 @@ public class ComponentDaoTest { db.components().insertComponent(newPrivateProjectDto().setRootUuid(uuid1).setProjectUuid("foo").setPrivate(false)).uuid(), }; - underTest.setPrivateForRootComponentUuid(db.getSession(), uuid1, true, "key", null, "name", true); + underTest.setPrivateForRootComponentUuidWithoutAudit(db.getSession(), uuid1, true); assertThat(privateFlagOfUuid(uuids[0])).isTrue(); assertThat(privateFlagOfUuid(uuids[1])).isTrue(); @@ -1780,7 +1779,7 @@ public class ComponentDaoTest { assertThat(privateFlagOfUuid(uuids[3])).isFalse(); assertThat(privateFlagOfUuid(uuids[4])).isFalse(); - underTest.setPrivateForRootComponentUuid(db.getSession(), uuid1, false, "key", null, "name", true); + underTest.setPrivateForRootComponentUuidWithoutAudit(db.getSession(), uuid1, false); assertThat(privateFlagOfUuid(uuids[0])).isFalse(); assertThat(privateFlagOfUuid(uuids[1])).isFalse(); @@ -1788,7 +1787,7 @@ public class ComponentDaoTest { assertThat(privateFlagOfUuid(uuids[3])).isFalse(); assertThat(privateFlagOfUuid(uuids[4])).isFalse(); - underTest.setPrivateForRootComponentUuid(db.getSession(), uuid2, false, "key", null, "name", true); + underTest.setPrivateForRootComponentUuidWithoutAudit(db.getSession(), uuid2, false); assertThat(privateFlagOfUuid(uuids[0])).isFalse(); assertThat(privateFlagOfUuid(uuids[1])).isFalse(); @@ -1796,7 +1795,7 @@ public class ComponentDaoTest { assertThat(privateFlagOfUuid(uuids[3])).isFalse(); assertThat(privateFlagOfUuid(uuids[4])).isFalse(); - underTest.setPrivateForRootComponentUuid(db.getSession(), uuid2, true, "key", null, "name", true); + underTest.setPrivateForRootComponentUuidWithoutAudit(db.getSession(), uuid2, true); assertThat(privateFlagOfUuid(uuids[0])).isFalse(); assertThat(privateFlagOfUuid(uuids[1])).isFalse(); @@ -1884,28 +1883,26 @@ public class ComponentDaoTest { @Test public void setPrivateForRootComponentUuid_auditPersisterIsCalled() { - underTestWithAuditPersister.setPrivateForRootComponentUuid(dbSession, "anyUuid", false, "key", APP, "appName", true); + underTestWithAuditPersister.setPrivateForRootComponentUuid(dbSession, "anyUuid", false, "key", APP, "appName"); - verify(auditPersister, Mockito.times(1)) - .updateComponentVisibility(any(DbSession.class), any(ComponentNewValue.class), anyString()); + verify(auditPersister).updateComponentVisibility(any(DbSession.class), any(ComponentNewValue.class)); } @Test - public void setPrivateForRootComponentUuid_withoutTrack_auditPersisterIsNotCalled() { - underTestWithAuditPersister.setPrivateForRootComponentUuid(dbSession, "anyUuid", false, "key", APP, "appName", false); + public void setPrivateForRootComponentUuidWithoutAudit_auditPersisterIsNotCalled() { + underTestWithAuditPersister.setPrivateForRootComponentUuidWithoutAudit(dbSession, "anyUuid", false); verifyNoInteractions(auditPersister); } @Test public void update_auditPersisterIsCalled() { - ComponentUpdateDto app = new ComponentUpdateDto(); + ComponentUpdateDto app = new ComponentUpdateDto().setUuid("uuid"); app.setBQualifier(APP); underTestWithAuditPersister.update(dbSession, app, APP); - verify(auditPersister, Mockito.times(1)) - .updateComponent(any(DbSession.class), any(ComponentNewValue.class), anyString()); + verify(auditPersister).updateComponent(any(DbSession.class), any(ComponentNewValue.class)); } @Test @@ -1914,8 +1911,7 @@ public class ComponentDaoTest { underTestWithAuditPersister.insert(dbSession, app); - verify(auditPersister, Mockito.times(1)) - .addComponent(any(DbSession.class), any(ComponentNewValue.class), anyString()); + verify(auditPersister).addComponent(any(DbSession.class), any(ComponentNewValue.class)); } private boolean privateFlagOfUuid(String uuid) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ScrollForFileMoveComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ScrollForFileMoveComponentDaoTest.java index 78f42bb40fd..7650e3a64a8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ScrollForFileMoveComponentDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ScrollForFileMoveComponentDaoTest.java @@ -35,6 +35,7 @@ import org.junit.runner.RunWith; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.source.FileSourceDto; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; @@ -50,7 +51,7 @@ public class ScrollForFileMoveComponentDaoTest { private Random random = new Random(); private DbSession dbSession = db.getSession(); - private ComponentDao underTest = new ComponentDao(); + private ComponentDao underTest = new ComponentDao(new NoOpAuditPersister()); @Test public void scrollAllFilesForFileMove_has_no_effect_if_project_does_not_exist() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java index 24d8075b28e..77aeec19786 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java @@ -33,6 +33,7 @@ import org.sonar.api.web.UserRole; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.component.ComponentDto; import org.sonar.db.user.GroupDto; @@ -59,7 +60,7 @@ public class GroupPermissionDaoTest { public DbTester db = DbTester.create(System2.INSTANCE); private final DbSession dbSession = db.getSession(); - private final GroupPermissionDao underTest = new GroupPermissionDao(); + private final GroupPermissionDao underTest = new GroupPermissionDao(new NoOpAuditPersister()); @Test public void group_count_by_permission_and_component_id_on_private_projects() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoWithPersisterTest.java index f633f63febd..78c4a49eb26 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoWithPersisterTest.java @@ -33,24 +33,22 @@ 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 { private final AuditPersister auditPersister = mock(AuditPersister.class); @Rule - public DbTester db = DbTester.create(System2.INSTANCE, auditPersister); + public DbTester db = DbTester.create(System2.INSTANCE); private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory(); private final ArgumentCaptor<GroupPermissionNewValue> newValueCaptor = ArgumentCaptor.forClass(GroupPermissionNewValue.class); private final DbSession dbSession = db.getSession(); - private final GroupPermissionDao underTest = db.getDbClient().groupPermissionDao(); + private final GroupPermissionDao underTest = new GroupPermissionDao(auditPersister); private GroupDto group; private ComponentDto project; @@ -87,7 +85,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); GroupPermissionNewValue newValue = newValueCaptor.getValue(); assertNewValue(newValue, dto.getUuid(), group.getUuid(), group.getName(), project.uuid(), dto.getRole(), project.getKey(), - project.name(), "project"); + project.name(), "TRK"); assertThat(newValue).hasToString("{\"permissionUuid\": \"1\", \"permission\": \"admin\", \"groupUuid\": \"guuid\", \"groupName\": \"gname\"," + " \"componentUuid\": \"cuuid\", \"componentKey\": \"cKey\", \"componentName\": \"cname\", \"qualifier\": \"project\" }"); @@ -95,7 +93,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); newValue = newValueCaptor.getValue(); - assertNewValue(newValue, null, null, null, project.uuid(), null, project.getKey(), project.name(), "project"); + assertNewValue(newValue, null, null, null, project.uuid(), null, project.getKey(), project.name(), "TRK"); assertThat(newValue).hasToString("{\"componentUuid\": \"cuuid\", \"componentKey\": \"cKey\", \"componentName\": \"cname\", \"qualifier\": \"project\" }"); } @@ -105,8 +103,7 @@ public class GroupPermissionDaoWithPersisterTest { underTest.deleteByRootComponentUuid(dbSession, project); - verify(auditPersister).addComponent(any(), any(), any()); - verifyNoMoreInteractions(auditPersister); + verifyNoInteractions(auditPersister); } @Test @@ -115,7 +112,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); GroupPermissionNewValue newValue = newValueCaptor.getValue(); - assertNewValue(newValue, dto.getUuid(), null, null, project.uuid(), dto.getRole(), project.getKey(), project.name(), "project"); + assertNewValue(newValue, dto.getUuid(), null, null, project.uuid(), dto.getRole(), project.getKey(), project.name(), "TRK"); assertThat(newValue).hasToString("{\"permissionUuid\": \"1\", \"permission\": \"admin\", \"componentUuid\": \"cuuid\", " + "\"componentKey\": \"cKey\", \"componentName\": \"cname\", \"qualifier\": \"project\" }"); @@ -123,7 +120,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); newValue = newValueCaptor.getValue(); - assertNewValue(newValue, null, null, null, project.uuid(), null, project.getKey(), project.name(), "project"); + assertNewValue(newValue, null, null, null, project.uuid(), null, project.getKey(), project.name(), "TRK"); assertThat(newValue).hasToString("{\"componentUuid\": \"cuuid\", \"componentKey\": \"cKey\", " + "\"componentName\": \"cname\", \"qualifier\": \"project\" }"); } @@ -134,8 +131,7 @@ public class GroupPermissionDaoWithPersisterTest { underTest.deleteByRootComponentUuidForAnyOne(dbSession, project); - verify(auditPersister).addComponent(any(), any(), any()); - verifyNoMoreInteractions(auditPersister); + verifyNoInteractions(auditPersister); } @Test @@ -144,7 +140,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); GroupPermissionNewValue newValue = newValueCaptor.getValue(); - assertNewValue(newValue, dto.getUuid(), group.getUuid(), group.getName(), project.uuid(), dto.getRole(), project.getKey(), project.name(), "project"); + assertNewValue(newValue, dto.getUuid(), group.getUuid(), group.getName(), project.uuid(), dto.getRole(), project.getKey(), project.name(), "TRK"); assertThat(newValue).hasToString("{\"permissionUuid\": \"1\", \"permission\": \"admin\", \"groupUuid\": \"guuid\", \"groupName\": \"gname\", " + "\"componentUuid\": \"cuuid\", \"componentKey\": \"cKey\", \"componentName\": \"cname\", \"qualifier\": \"project\" }"); @@ -152,7 +148,7 @@ public class GroupPermissionDaoWithPersisterTest { verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); newValue = newValueCaptor.getValue(); - assertNewValue(newValue, null, null, null, project.uuid(), ADMIN, project.getKey(), project.name(), "project"); + assertNewValue(newValue, null, null, null, project.uuid(), ADMIN, project.getKey(), project.name(), "TRK"); assertThat(newValue).hasToString("{\"permission\": \"admin\", \"componentUuid\": \"cuuid\", \"componentKey\": \"cKey\"," + " \"componentName\": \"cname\", \"qualifier\": \"project\" }"); } @@ -165,17 +161,14 @@ public class GroupPermissionDaoWithPersisterTest { underTest.deleteByRootComponentUuidAndPermission(dbSession, dto.getRole(), project); - verify(auditPersister).addComponent(any(), any(), any()); - verifyNoMoreInteractions(auditPersister); + verifyNoInteractions(auditPersister); } private void assertNewValue(GroupPermissionNewValue newValue, String uuid, String groupUuid, String groupName, String cUuid, String permission, String componentKey, String cName, String qualifier) { assertThat(newValue) - .extracting("permissionUuid", "groupUuid", "groupName", "componentUuid", "permission", - "componentKey", "componentName", "qualifier") - .containsExactly(uuid, groupUuid, groupName, cUuid, permission, componentKey, - cName, qualifier); + .extracting("permissionUuid", "groupUuid", "groupName", "componentUuid", "permission", "componentKey", "componentName", "qualifier") + .containsExactly(uuid, groupUuid, groupName, cUuid, permission, componentKey, cName, qualifier); } private void addGroupPermission() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index 97b28dbf552..3fa1803a26b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -34,6 +34,7 @@ import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.component.ComponentDto; import org.sonar.db.user.UserDto; @@ -62,7 +63,7 @@ public class UserPermissionDaoTest { public DbTester db = DbTester.create(System2.INSTANCE); private DbSession dbSession = db.getSession(); - private UserPermissionDao underTest = new UserPermissionDao(); + private UserPermissionDao underTest = new UserPermissionDao(new NoOpAuditPersister()); @Test public void select_global_permissions() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoWithPersisterTest.java index b1c8f862f50..a8b767d942b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoWithPersisterTest.java @@ -93,16 +93,6 @@ public class UserPermissionDaoWithPersisterTest { } @Test - public void userGlobalPermissionDeleteWithoutAffectedRowsIsNotPersisted() { - UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com")); - - underTest.deleteGlobalPermission(dbSession, user, 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")); ComponentDto project = db.components().insertPrivateProject(); @@ -112,7 +102,7 @@ public class UserPermissionDaoWithPersisterTest { verify(auditPersister).addUserPermission(eq(dbSession), newValueCaptor.capture()); UserPermissionNewValue newValue = newValueCaptor.getValue(); assertNewValue(newValue, dto.getUuid(), user.getUuid(), user.getLogin(), project.uuid(), dto.getPermission(), - project.getKey(), project.name(), "project"); + project.getKey(), project.name(), "TRK"); assertThat(newValue.toString()).contains("componentUuid"); underTest.deleteProjectPermission(dbSession, user, SYSTEM_ADMIN, project); @@ -120,7 +110,7 @@ public class UserPermissionDaoWithPersisterTest { verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); newValue = newValueCaptor.getValue(); assertNewValue(newValue, null, user.getUuid(), user.getLogin(), project.uuid(), dto.getPermission(), - project.getKey(), project.name(), "project"); + project.getKey(), project.name(), "TRK"); assertThat(newValue.toString()).doesNotContain("permissionUuid"); } @@ -132,7 +122,7 @@ public class UserPermissionDaoWithPersisterTest { underTest.deleteProjectPermission(dbSession, user, SYSTEM_ADMIN, project); verify(auditPersister).addUser(any(), any()); - verify(auditPersister).addComponent(any(), any(), any()); + verify(auditPersister).addComponent(any(), any()); verifyNoMoreInteractions(auditPersister); } @@ -146,7 +136,7 @@ public class UserPermissionDaoWithPersisterTest { verify(auditPersister).addUserPermission(eq(dbSession), newValueCaptor.capture()); UserPermissionNewValue newValue = newValueCaptor.getValue(); assertNewValue(newValue, dto.getUuid(), user.getUuid(), user.getLogin(), portfolio.uuid(), dto.getPermission(), - portfolio.getKey(), portfolio.name(), "portfolio"); + portfolio.getKey(), portfolio.name(), "VW"); assertThat(newValue.toString()).contains("componentUuid"); } @@ -160,7 +150,7 @@ public class UserPermissionDaoWithPersisterTest { verify(auditPersister).addUserPermission(eq(dbSession), newValueCaptor.capture()); UserPermissionNewValue newValue = newValueCaptor.getValue(); assertNewValue(newValue, dto.getUuid(), user.getUuid(), user.getLogin(), application.uuid(), dto.getPermission(), - application.getKey(), application.name(), "application"); + application.getKey(), application.name(), "APP"); assertThat(newValue.toString()).contains("componentUuid"); } @@ -175,7 +165,7 @@ public class UserPermissionDaoWithPersisterTest { verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); UserPermissionNewValue newValue = newValueCaptor.getValue(); assertNewValue(newValue, null, null, null, project.uuid(), dto.getPermission(), - project.getKey(), project.name(), "project"); + project.getKey(), project.name(), "TRK"); assertThat(newValue.toString()).doesNotContain("userUuid"); } @@ -185,7 +175,7 @@ public class UserPermissionDaoWithPersisterTest { underTest.deleteProjectPermissionOfAnyUser(dbSession, SCAN_EXECUTION, project); - verify(auditPersister).addComponent(any(), any(), any()); + verify(auditPersister).addComponent(any(), any()); verifyNoMoreInteractions(auditPersister); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java index 8cdf7c6d698..4a399f69880 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java @@ -27,6 +27,7 @@ import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import static com.google.common.collect.Lists.newArrayList; import static java.util.Arrays.asList; @@ -39,7 +40,7 @@ public class PermissionTemplateCharacteristicDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); private final DbSession dbSession = db.getSession(); - private final PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao(); + private final PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao(new NoOpAuditPersister()); @Test public void selectByTemplateId_filter_by_template_uuid() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java index 980a2f32087..3cfe40e6d75 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java @@ -32,6 +32,7 @@ import org.sonar.core.util.UuidFactory; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; @@ -63,7 +64,7 @@ public class PermissionTemplateDaoTest { private PermissionTemplateDbTester templateDb = db.permissionTemplates(); private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); - private PermissionTemplateDao underTest = new PermissionTemplateDao(uuidFactory, system2); + private PermissionTemplateDao underTest = new PermissionTemplateDao(uuidFactory, system2, new NoOpAuditPersister()); @Before public void setUp() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java index 62a3d921d5b..9bb9a1e00c8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java @@ -32,6 +32,7 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; @@ -50,7 +51,7 @@ public class ProjectDaoTest { private final AuditPersister auditPersister = mock(AuditPersister.class); - private final ProjectDao projectDao = new ProjectDao(system2); + private final ProjectDao projectDao = new ProjectDao(system2, new NoOpAuditPersister()); private final ProjectDao projectDaoWithAuditPersister = new ProjectDao(system2, auditPersister); @Test @@ -201,7 +202,7 @@ public class ProjectDaoTest { projectDaoWithAuditPersister.insert(db.getSession(), dto1, true); - verify(auditPersister, times(1)).addComponent(any(), any(), any()); + verify(auditPersister, times(1)).addComponent(any(), any()); } @Test @@ -210,7 +211,7 @@ public class ProjectDaoTest { projectDaoWithAuditPersister.update(db.getSession(), dto1); - verify(auditPersister, times(1)).updateComponent(any(), any(), any()); + verify(auditPersister, times(1)).updateComponent(any(), any()); } private void assertProject(ProjectDto dto, String name, String kee, String uuid, String desc, @Nullable String tags, boolean isPrivate) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java index 7e83aa2bdc2..1ba0912b70b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java @@ -45,7 +45,6 @@ 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; @RunWith(DataProviderRunner.class) @@ -81,7 +80,6 @@ public class PropertiesDaoWithPersisterTest { when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); underTest.saveProperty(new PropertyDto().setKey(KEY).setValue(VALUE)); - verify(auditPersister).isTrackedProperty(KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); @@ -94,22 +92,11 @@ public class PropertiesDaoWithPersisterTest { } @Test - public void saveGlobalNotTrackedPropertyIsNotPersisted() { - when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); - - underTest.saveProperty(new PropertyDto().setKey(KEY).setValue(VALUE)); - - verify(auditPersister).isTrackedProperty(KEY); - verifyNoMoreInteractions(auditPersister); - } - - @Test public void saveGlobalTrackedAndSecuredPropertyIsPersisted() { when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true); underTest.saveProperty(new PropertyDto().setKey(SECURED_KEY).setValue(VALUE)); - verify(auditPersister).isTrackedProperty(SECURED_KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -120,13 +107,12 @@ public class PropertiesDaoWithPersisterTest { } @Test - public void saveProjectTrackedPropertyIsPersisted() { + public void saveProjectPropertyIsPersisted() { when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID); underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - verify(auditPersister).isTrackedProperty(KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -135,7 +121,7 @@ public class PropertiesDaoWithPersisterTest { PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey, PropertyNewValue::getComponentName, PropertyNewValue::getQualifier) .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(), - USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_KEY, PROJECT_NAME, "project"); + USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_KEY, PROJECT_NAME, "TRK"); assertThat(newValue.toString()).contains("componentUuid"); } @@ -146,7 +132,6 @@ public class PropertiesDaoWithPersisterTest { PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID); underTest.saveProperty(session, propertyDto, USER_LOGIN, "app-key", "app-name", Qualifiers.APP); - verify(auditPersister).isTrackedProperty(KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -155,7 +140,7 @@ public class PropertiesDaoWithPersisterTest { PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey, PropertyNewValue::getComponentName, PropertyNewValue::getQualifier) .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(), - USER_LOGIN, propertyDto.getComponentUuid(), "app-key", "app-name", "application"); + USER_LOGIN, propertyDto.getComponentUuid(), "app-key", "app-name", "APP"); assertThat(newValue.toString()) .contains("componentUuid"); } @@ -167,7 +152,6 @@ public class PropertiesDaoWithPersisterTest { PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID); underTest.saveProperty(session, propertyDto, USER_LOGIN, "portfolio-key", "portfolio-name", Qualifiers.VIEW); - verify(auditPersister).isTrackedProperty(KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -176,7 +160,7 @@ public class PropertiesDaoWithPersisterTest { PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey, PropertyNewValue::getComponentName, PropertyNewValue::getQualifier) .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(), - USER_LOGIN, propertyDto.getComponentUuid(), "portfolio-key", "portfolio-name", "portfolio"); + USER_LOGIN, propertyDto.getComponentUuid(), "portfolio-key", "portfolio-name", "VW"); assertThat(newValue.toString()) .contains("componentUuid"); } @@ -188,7 +172,6 @@ public class PropertiesDaoWithPersisterTest { PropertyDto propertyDto = getPropertyDto(SECURED_KEY, PROJECT_UUID, USER_UUID); underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - verify(auditPersister).isTrackedProperty(SECURED_KEY); verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -197,7 +180,7 @@ public class PropertiesDaoWithPersisterTest { PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey, PropertyNewValue::getComponentName, PropertyNewValue::getQualifier) .containsExactly(propertyDto.getKey(), null, propertyDto.getUserUuid(), - USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_KEY, PROJECT_NAME, "project"); + USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_KEY, PROJECT_NAME, "TRK"); assertThat(newValue.toString()).contains("componentUuid"); } @@ -210,7 +193,6 @@ public class PropertiesDaoWithPersisterTest { underTest.deleteByQuery(session, query); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -233,19 +215,6 @@ public class PropertiesDaoWithPersisterTest { } @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_KEY, PROJECT_NAME, Qualifiers.PROJECT); - - underTest.deleteByQuery(session, query); - - 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); @@ -253,7 +222,6 @@ public class PropertiesDaoWithPersisterTest { underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -276,18 +244,6 @@ public class PropertiesDaoWithPersisterTest { } @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_KEY, PROJECT_NAME, Qualifiers.PROJECT); - - underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - - 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); @@ -295,7 +251,6 @@ public class PropertiesDaoWithPersisterTest { underTest.deleteProjectProperty(KEY, PROJECT_UUID, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -316,18 +271,6 @@ public class PropertiesDaoWithPersisterTest { } @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_KEY, PROJECT_NAME, Qualifiers.PROJECT); - - underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, Qualifiers.PROJECT); - - 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); @@ -335,7 +278,6 @@ public class PropertiesDaoWithPersisterTest { underTest.deleteProjectProperties(KEY, VALUE); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -356,18 +298,6 @@ public class PropertiesDaoWithPersisterTest { } @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, times(2)).isTrackedProperty(KEY); - verifyNoMoreInteractions(auditPersister); - } - - @Test public void deleteTrackedGlobalPropertyIsPersisted() { when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); PropertyDto propertyDto = getPropertyDto(KEY, null, null); @@ -375,7 +305,6 @@ public class PropertiesDaoWithPersisterTest { underTest.deleteGlobalProperty(KEY, session); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -396,25 +325,10 @@ public class PropertiesDaoWithPersisterTest { } @Test - public void deleteNotTrackedGlobalPropertyIsNotPersisted() { - when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); - PropertyDto propertyDto = getPropertyDto(KEY, null, null); - underTest.saveProperty(session, propertyDto, null, null, null, null); - - underTest.deleteGlobalProperty(KEY, session); - - verify(auditPersister, times(2)).isTrackedProperty(KEY); - verifyNoMoreInteractions(auditPersister); - } - - @Test public void deletePropertyByUserIsPersisted() { UserDto user = setUserProperties(VALUE); underTest.deleteByUser(session, user.getUuid(), user.getLogin()); - verify(auditPersister, times(2)).isTrackedProperty(KEY); - verify(auditPersister, times(2)).isTrackedProperty(ANOTHER_KEY); - verify(auditPersister, times(2)).isTrackedProperty(SECURED_KEY); verify(auditPersister, times(2)).deleteProperty(any(), newValueCaptor.capture(), eq(false)); List<PropertyNewValue> newValues = newValueCaptor.getAllValues(); assertThat(newValues.get(0)) @@ -446,9 +360,6 @@ public class PropertiesDaoWithPersisterTest { UserDto user = setUserProperties(null); underTest.deleteByMatchingLogin(session, user.getLogin(), newArrayList(KEY, ANOTHER_KEY, SECURED_KEY)); - verify(auditPersister, times(2)).isTrackedProperty(KEY); - verify(auditPersister, times(2)).isTrackedProperty(ANOTHER_KEY); - verify(auditPersister, times(2)).isTrackedProperty(SECURED_KEY); verify(auditPersister, times(2)).deleteProperty(any(), newValueCaptor.capture(), eq(false)); List<PropertyNewValue> newValues = newValueCaptor.getAllValues(); assertThat(newValues.get(0)) @@ -484,7 +395,6 @@ public class PropertiesDaoWithPersisterTest { underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null, null); underTest.deleteByKeyAndValue(session, KEY, VALUE); - verify(auditPersister, times(2)).isTrackedProperty(KEY); verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -504,18 +414,6 @@ public class PropertiesDaoWithPersisterTest { 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, null); - - underTest.deleteByKeyAndValue(session, KEY, VALUE); - - verify(auditPersister, times(2)).isTrackedProperty(KEY); - verifyNoMoreInteractions(auditPersister); - } - private PropertyDto getPropertyDto(String key, @Nullable String projectUuid, @Nullable String userUuid) { return new PropertyDto() .setKey(key) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java index 0b37df85ebc..70c27f914ea 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java @@ -59,12 +59,11 @@ public class PurgeDaoWithAuditTest { underTestWithPersister.deleteProject(dbSession, project.uuid(), project.qualifier(), project.name(), project.getKey()); - verify(auditPersister).deleteComponent(any(DbSession.class), newValueCaptor.capture(), eq(project.qualifier())); + verify(auditPersister).deleteComponent(any(DbSession.class), newValueCaptor.capture()); ComponentNewValue componentNewValue = newValueCaptor.getValue(); assertThat(componentNewValue) - .extracting(ComponentNewValue::getComponentUuid, ComponentNewValue::getComponentName, ComponentNewValue::getComponentKey, - ComponentNewValue::getQualifier) - .containsExactly(project.uuid(), project.name(), project.getKey(), "project"); + .extracting(ComponentNewValue::getComponentUuid, ComponentNewValue::getComponentName, ComponentNewValue::getComponentKey, ComponentNewValue::getQualifier) + .containsExactly(project.uuid(), project.name(), project.getKey(), "TRK"); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java index e18a167d6ad..fdcda4f0ad3 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java @@ -92,7 +92,8 @@ public class UserGroupDaoWithPersisterTest { underTest.deleteByGroupUuid(db.getSession(), group1.getUuid(), group1.getName()); db.getSession().commit(); - verifyNoInteractions(auditPersister); + verify(auditPersister).addUserGroup(any(), any()); + verifyNoMoreInteractions(auditPersister); } @Test @@ -145,13 +146,14 @@ public class UserGroupDaoWithPersisterTest { } @Test - public void deletByUserAndGroupWithoutAffectedRowsIsNotPersisted() { + public void deleteByUserAndGroupWithoutAffectedRowsIsNotPersisted() { UserDto user1 = db.users().insertUser(); GroupDto group1 = db.users().insertGroup(); underTest.delete(db.getSession(), group1, user1); db.getSession().commit(); verify(auditPersister).addUser(any(), any()); + verify(auditPersister).addUserGroup(any(), any()); verifyNoMoreInteractions(auditPersister); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserPropertiesDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserPropertiesDaoWithPersisterTest.java index 5992048b027..86bc3f1941d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserPropertiesDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserPropertiesDaoWithPersisterTest.java @@ -53,8 +53,6 @@ public class UserPropertiesDaoWithPersisterTest { @Test public void insertTrackedUserPropertyIsPersisted() { - when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(true); - UserDto user = db.users().insertUser(); verify(auditPersister).addUser(eq(db.getSession()), any()); @@ -66,7 +64,6 @@ public class UserPropertiesDaoWithPersisterTest { user.getLogin()); verify(auditPersister).addProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); - verify(auditPersister).isTrackedProperty(PROPERTY_KEY); assertThat(newValueCaptor.getValue()) .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin) @@ -76,8 +73,6 @@ public class UserPropertiesDaoWithPersisterTest { @Test public void insertTrackedAndSecuredUserPropertyIsPersisted() { - when(auditPersister.isTrackedProperty(SECURED_PROPERTY_KEY)).thenReturn(true); - UserDto user = db.users().insertUser(); verify(auditPersister).addUser(eq(db.getSession()), any()); @@ -88,7 +83,6 @@ public class UserPropertiesDaoWithPersisterTest { .setValue("a_value"), user.getLogin()); - verify(auditPersister).isTrackedProperty(SECURED_PROPERTY_KEY); verify(auditPersister).addProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); PropertyNewValue newValue = newValueCaptor.getValue(); assertThat(newValue) @@ -99,25 +93,7 @@ public class UserPropertiesDaoWithPersisterTest { } @Test - public void insertNotTrackedUserPropertyIsNotPersisted() { - when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(false); - - UserDto user = db.users().insertUser(); - underTest.insertOrUpdate(db.getSession(), new UserPropertyDto() - .setUserUuid(user.getUuid()) - .setKey(PROPERTY_KEY) - .setValue("a_value"), - user.getLogin()); - - verify(auditPersister).addUser(eq(db.getSession()), any()); - verify(auditPersister).isTrackedProperty(PROPERTY_KEY); - verifyNoMoreInteractions(auditPersister); - } - - @Test public void updateTrackedUserPropertyIsPersisted() { - when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(true); - UserDto user = db.users().insertUser(); underTest.insertOrUpdate(db.getSession(), new UserPropertyDto() .setUserUuid(user.getUuid()) @@ -159,10 +135,9 @@ public class UserPropertiesDaoWithPersisterTest { underTest.deleteByUser(db.getSession(), user); verify(auditPersister).addUser(eq(db.getSession()), any()); - verify(auditPersister).addProperty(eq(db.getSession()), any(), eq(true)); - verify(auditPersister).addProperty(eq(db.getSession()), any(), eq(true)); - verify(auditPersister, times(2)).isTrackedProperty(PROPERTY_KEY); - verify(auditPersister, times(2)).isTrackedProperty(SECURED_PROPERTY_KEY); + verify(auditPersister, times(2)).addProperty(eq(db.getSession()), any(), eq(true)); + verify(auditPersister).isTrackedProperty(PROPERTY_KEY); + verify(auditPersister).isTrackedProperty(SECURED_PROPERTY_KEY); verify(auditPersister).deleteProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); verifyNoMoreInteractions(auditPersister); assertThat(newValueCaptor.getValue()) 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 e258b5e55ca..8d640e17fd0 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 @@ -63,7 +63,7 @@ public class UserTokenDaoTest { UserTokenDto userToken2 = db.users().insertToken(user1); assertThat(underTest.selectByTokenHash(dbSession, userToken1.getTokenHash()).getLastConnectionDate()).isNull(); - underTest.update(dbSession, userToken1.setLastConnectionDate(10_000_000_000L), false, null); + underTest.updateWithoutAudit(dbSession, userToken1.setLastConnectionDate(10_000_000_000L)); UserTokenDto userTokenReloaded = underTest.selectByTokenHash(dbSession, userToken1.getTokenHash()); assertThat(userTokenReloaded.getLastConnectionDate()).isEqualTo(10_000_000_000L); 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 79940eeaf34..4aca0d5f93e 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 @@ -78,7 +78,7 @@ public class UserTokenDaoWithPersisterTest { assertThat(underTest.selectByTokenHash(dbSession, userToken1.getTokenHash()).getLastConnectionDate()).isNull(); - underTest.update(dbSession, userToken1.setLastConnectionDate(10_000_000_000L), false, null); + underTest.updateWithoutAudit(dbSession, userToken1.setLastConnectionDate(10_000_000_000L)); underTest.update(dbSession, userToken1.setName("new_name"), user1.getLogin()); verify(auditPersister).updateUserToken(eq(db.getSession()), newValueCaptor.capture()); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoWithPersisterTest.java index cb3429d319f..dd178be2cd5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDaoWithPersisterTest.java @@ -37,7 +37,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -180,7 +179,7 @@ public class WebhookDaoWithPersisterTest { underTest.deleteByProject(dbSession, projectDto); - verify(auditPersister).addComponent(any(), any(), any()); + verify(auditPersister).addComponent(any(), any()); verifyNoMoreInteractions(auditPersister); } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index 5c2a73191e3..e4b05ec5585 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -36,6 +36,7 @@ import org.sonar.db.alm.integration.pat.AlmPatsDbTester; import org.sonar.db.almsettings.AlmSettingsDbTester; import org.sonar.db.audit.AuditDbTester; import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ProjectLinkDbTester; import org.sonar.db.event.EventDbTester; @@ -65,6 +66,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { private final UuidFactory uuidFactory = new SequenceUuidFactory(); private final System2 system2; + private final AuditPersister auditPersister; private DbClient client; private DbSession session = null; private final UserDbTester userTester; @@ -94,8 +96,9 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { private DbTester(System2 system2, @Nullable String schemaPath, AuditPersister auditPersister, MyBatisConfExtension... confExtensions) { super(TestDbImpl.create(schemaPath, confExtensions)); this.system2 = system2; + this.auditPersister = auditPersister; - initDbClient(auditPersister); + initDbClient(); this.userTester = new UserDbTester(this); this.componentTester = new ComponentDbTester(this); this.componentLinkTester = new ProjectLinkDbTester(this); @@ -122,7 +125,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { } public static DbTester create() { - return new DbTester(System2.INSTANCE, null, null); + return new DbTester(System2.INSTANCE, null, new NoOpAuditPersister()); } public static DbTester create(AuditPersister auditPersister) { @@ -134,18 +137,16 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { } public static DbTester create(System2 system2) { - return new DbTester(system2, null, null); + return new DbTester(system2, null, new NoOpAuditPersister()); } public static DbTester createWithExtensionMappers(System2 system2, Class<?> firstMapperClass, Class<?>... otherMapperClasses) { - return new DbTester(system2, null, null, new DbTesterMyBatisConfExtension(firstMapperClass, otherMapperClasses)); + return new DbTester(system2, null, new NoOpAuditPersister(), new DbTesterMyBatisConfExtension(firstMapperClass, otherMapperClasses)); } - private void initDbClient(AuditPersister auditPersister) { + private void initDbClient() { TransientPicoContainer ioc = new TransientPicoContainer(); - if (auditPersister != null) { - ioc.addComponent(auditPersister); - } + ioc.addComponent(auditPersister); ioc.addComponent(db.getMyBatis()); ioc.addComponent(system2); ioc.addComponent(uuidFactory); @@ -160,7 +161,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { protected void before() { db.start(); db.truncateTables(); - initDbClient(null); + initDbClient(); } public UserDbTester users() { diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java index 33e3b98d273..cc555763c2a 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java @@ -194,6 +194,7 @@ public class ComponentTesting { return new ComponentDto() .setUuid(uuid) .setUuidPath(formatUuidPathFromParent(parent)) + .setDbKey(uuid) .setProjectUuid(moduleOrProject.projectUuid()) .setRootUuid(moduleOrProject.uuid()) .setModuleUuid(moduleOrProject.uuid()) diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImpl.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImpl.java index 20b0e23ead8..77373951147 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImpl.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImpl.java @@ -59,7 +59,7 @@ public class UserLastConnectionDatesUpdaterImpl implements UserLastConnectionDat return; } try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.userTokenDao().update(dbSession, userToken.setLastConnectionDate(now), false, null); + dbClient.userTokenDao().updateWithoutAudit(dbSession, userToken.setLastConnectionDate(now)); userToken.setLastConnectionDate(now); dbSession.commit(); } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImplTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImplTest.java index e6bb550e9ae..8d5f0f4edab 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImplTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserLastConnectionDatesUpdaterImplTest.java @@ -21,8 +21,8 @@ package org.sonar.server.authentication; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.utils.System2; import org.sonar.api.impl.utils.TestSystem2; +import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserTokenDto; @@ -79,7 +79,7 @@ public class UserLastConnectionDatesUpdaterImplTest { public void update_last_connection_date_from_user_token_when_last_connection_was_more_than_one_hour() { UserDto user = db.users().insertUser(); UserTokenDto userToken = db.users().insertToken(user); - db.getDbClient().userTokenDao().update(db.getSession(), userToken.setLastConnectionDate(NOW - TWO_HOUR), false, null); + db.getDbClient().userTokenDao().updateWithoutAudit(db.getSession(), userToken.setLastConnectionDate(NOW - TWO_HOUR)); db.commit(); underTest.updateLastConnectionDateIfNeeded(userToken); @@ -103,7 +103,7 @@ public class UserLastConnectionDatesUpdaterImplTest { public void do_not_update_when_last_connection_from_user_token_was_less_than_one_hour() { UserDto user = db.users().insertUser(); UserTokenDto userToken = db.users().insertToken(user); - db.getDbClient().userTokenDao().update(db.getSession(), userToken.setLastConnectionDate(NOW - ONE_MINUTE), false, null); + db.getDbClient().userTokenDao().updateWithoutAudit(db.getSession(), userToken.setLastConnectionDate(NOW - ONE_MINUTE)); db.commit(); underTest.updateLastConnectionDateIfNeeded(userToken); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java index 12dad7b7e1a..7011f0ad668 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java @@ -26,8 +26,6 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.UuidFactory; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; @@ -135,8 +133,7 @@ public class UpdateVisibilityAction implements ProjectsWsAction { private void setPrivateForRootComponentUuid(DbSession dbSession, ComponentDto component, boolean isPrivate) { String uuid = component.uuid(); - dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, uuid, isPrivate, component.getKey(), - component.qualifier(), component.name(), true); + dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, uuid, isPrivate, component.getKey(), component.qualifier(), component.name()); if (component.qualifier().equals(Qualifiers.PROJECT) || component.qualifier().equals(Qualifiers.APP)) { dbClient.projectDao().updateVisibility(dbSession, uuid, isPrivate); @@ -165,7 +162,6 @@ public class UpdateVisibilityAction implements ProjectsWsAction { }); } - private void insertProjectPermissionOnUser(DbSession dbSession, ComponentDto component, String permission, UserId userId) { dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(Uuids.create(), permission, userId.getUuid(), component.uuid()), component, userId, null); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/GenerateSecretKeyAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/GenerateSecretKeyAction.java index 9bd81ab4219..2b56d343548 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/GenerateSecretKeyAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/GenerateSecretKeyAction.java @@ -35,13 +35,7 @@ public class GenerateSecretKeyAction implements SettingsWsAction { private final DbClient dbClient; private final Settings settings; private final UserSession userSession; - private AuditPersister auditPersister; - - public GenerateSecretKeyAction(DbClient dbClient, Settings settings, UserSession userSession) { - this.dbClient = dbClient; - this.settings = settings; - this.userSession = userSession; - } + private final AuditPersister auditPersister; public GenerateSecretKeyAction(DbClient dbClient, Settings settings, UserSession userSession, AuditPersister auditPersister) { this.dbClient = dbClient; @@ -67,11 +61,9 @@ public class GenerateSecretKeyAction implements SettingsWsAction { writeProtobuf(GenerateSecretKeyWsResponse.newBuilder().setSecretKey(settings.getEncryption().generateRandomSecretKey()).build(), request, response); - if (auditPersister != null) { - try (DbSession dbSession = dbClient.openSession(false)) { - auditPersister.generateSecretKey(dbSession); - dbSession.commit(); - } + try (DbSession dbSession = dbClient.openSession(false)) { + auditPersister.generateSecretKey(dbSession); + dbSession.commit(); } } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/GenerateSecretKeyActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/GenerateSecretKeyActionTest.java index 4f41d85c399..fdcd9597c1a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/GenerateSecretKeyActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/GenerateSecretKeyActionTest.java @@ -33,6 +33,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -53,7 +54,7 @@ public class GenerateSecretKeyActionTest { private DbClient dbClient = db.getDbClient(); private MapSettings settings = new MapSettings(); private Encryption encryption = settings.getEncryption(); - private GenerateSecretKeyAction underTest = new GenerateSecretKeyAction(dbClient, settings, userSession); + private GenerateSecretKeyAction underTest = new GenerateSecretKeyAction(dbClient, settings, userSession, new NoOpAuditPersister()); private WsActionTester ws = new WsActionTester(underTest); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java index 09dc785b3f2..2cbec0cee39 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java @@ -28,6 +28,7 @@ import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDao; @@ -62,7 +63,7 @@ public class LinesActionTest { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); - private final ComponentDao componentDao = new ComponentDao(); + private final ComponentDao componentDao = new ComponentDao(new NoOpAuditPersister()); private final SnapshotDao snapshotDao = new SnapshotDao(); private final HtmlSourceDecorator htmlSourceDecorator = mock(HtmlSourceDecorator.class); private final SourceService sourceService = new SourceService(db.getDbClient(), htmlSourceDecorator); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java index 861523e4e21..98da7f5ea21 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java @@ -100,7 +100,7 @@ public class SearchActionTest { UserDto user = db.users().insertUser(); UserTokenDto token1 = db.users().insertToken(user); UserTokenDto token2 = db.users().insertToken(user); - db.getDbClient().userTokenDao().update(db.getSession(), token1.setLastConnectionDate(10_000_000_000L), false, user.getLogin()); + db.getDbClient().userTokenDao().updateWithoutAudit(db.getSession(), token1.setLastConnectionDate(10_000_000_000L)); db.commit(); logInAsSystemAdministrator(); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java index d16a642d366..4bd1c1d4c69 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java @@ -29,6 +29,8 @@ import org.sonar.api.internal.MetadataLoader; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; +import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.server.issue.index.IssueIndexSyncProgressChecker; import org.sonar.server.platform.DockerSupportImpl; import org.sonar.server.util.GlobalLockManagerImpl; @@ -162,6 +164,9 @@ public class PlatformLevel1 extends PlatformLevel { .load(); get(WebCoreExtensionsInstaller.class) .install(getContainer(), hasPlatformLevel(1), noAdditionalSideFilter()); + if (getOptional(AuditPersister.class).isEmpty()) { + add(NoOpAuditPersister.class); + } return super.start(); } |