@@ -52,7 +52,7 @@ public class ProjectConfigurationFactoryTest { | |||
@Test | |||
public void return_project_settings() { | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
db.properties().insertProperties( | |||
db.properties().insertProperties(null, project.name(), | |||
newComponentPropertyDto(project).setKey("1").setValue("val1"), | |||
newComponentPropertyDto(project).setKey("2").setValue("val2"), | |||
newComponentPropertyDto(project).setKey("3").setValue("val3")); | |||
@@ -68,7 +68,7 @@ public class ProjectConfigurationFactoryTest { | |||
public void project_settings_override_global_settings() { | |||
settings.setProperty("key", "value"); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value2")); | |||
db.properties().insertProperties(null, project.name(), newComponentPropertyDto(project).setKey("key").setValue("value2")); | |||
Configuration projectConfig = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl()); | |||
@@ -139,6 +139,7 @@ public class ConfigurationRepositoryTest { | |||
} | |||
private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) { | |||
db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setComponentUuid(project.uuid())); | |||
db.properties().insertProperties(null, project.name(), | |||
new PropertyDto().setKey(propertyKey).setValue(propertyValue).setComponentUuid(project.uuid())); | |||
} | |||
} |
@@ -20,6 +20,7 @@ | |||
package org.sonar.ce.db; | |||
import java.util.Map; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.DbSession; | |||
@@ -42,7 +43,8 @@ public class ReadOnlyPropertiesDao extends PropertiesDao { | |||
} | |||
@Override | |||
public void saveProperty(DbSession session, PropertyDto property) { | |||
public void saveProperty(DbSession session, PropertyDto property, @Nullable String userLogin, | |||
@Nullable String projectName) { | |||
// do nothing | |||
} | |||
@@ -52,12 +54,12 @@ public class ReadOnlyPropertiesDao extends PropertiesDao { | |||
} | |||
@Override | |||
public void deleteProjectProperty(String key, String projectUuid) { | |||
public void deleteProjectProperty(String key, String projectUuid, String projectName) { | |||
// do nothing | |||
} | |||
@Override | |||
public void deleteProjectProperty(String key, String projectUuid, DbSession session) { | |||
public void deleteProjectProperty(String key, String projectUuid, DbSession session, String projectName) { | |||
// do nothing | |||
} | |||
@@ -144,7 +144,7 @@ public class ComputeEngineContainerImplTest { | |||
private void insertProperty(String key, String value) { | |||
PropertyDto dto = new PropertyDto().setKey(key).setValue(value); | |||
db.getDbClient().propertiesDao().saveProperty(db.getSession(), dto); | |||
db.getDbClient().propertiesDao().saveProperty(db.getSession(), dto, null, null); | |||
db.commit(); | |||
} | |||
@@ -40,7 +40,7 @@ public class ReadOnlyPropertiesDaoTest { | |||
@Test | |||
public void insertProperty() { | |||
underTest.saveProperty(dbSession, propertyDto); | |||
underTest.saveProperty(dbSession, propertyDto, null, null); | |||
assertNoInteraction(); | |||
} | |||
@@ -54,7 +54,7 @@ public class ReadOnlyPropertiesDaoTest { | |||
@Test | |||
public void deleteProjectProperty() { | |||
underTest.deleteProjectProperty(null, null); | |||
underTest.deleteProjectProperty(null, null, null); | |||
assertNoInteraction(); | |||
@@ -62,7 +62,7 @@ public class ReadOnlyPropertiesDaoTest { | |||
@Test | |||
public void deleteProjectProperty1() { | |||
underTest.deleteProjectProperty(null, null, dbSession); | |||
underTest.deleteProjectProperty(null, null, dbSession, null); | |||
assertNoInteraction(); | |||
@@ -20,23 +20,33 @@ | |||
package org.sonar.db.alm.pat; | |||
import java.util.Optional; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PersonalAccessTokenNewValue; | |||
import org.sonar.db.user.UserDto; | |||
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; | |||
} | |||
public AlmPatDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { | |||
this.system2 = system2; | |||
this.uuidFactory = uuidFactory; | |||
this.auditPersister = auditPersister; | |||
} | |||
private static AlmPatMapper getMapper(DbSession dbSession) { | |||
return dbSession.getMapper(AlmPatMapper.class); | |||
} | |||
@@ -49,31 +59,46 @@ public class AlmPatDao implements Dao { | |||
return Optional.ofNullable(getMapper(dbSession).selectByUserAndAlmSetting(userUuid, almSettingDto.getUuid())); | |||
} | |||
public void insert(DbSession dbSession, AlmPatDto almPatDto) { | |||
public void insert(DbSession dbSession, AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) { | |||
String uuid = uuidFactory.create(); | |||
long now = system2.now(); | |||
almPatDto.setUuid(uuid); | |||
almPatDto.setCreatedAt(now); | |||
almPatDto.setUpdatedAt(now); | |||
getMapper(dbSession).insert(almPatDto); | |||
if (auditPersister != null) { | |||
auditPersister.addPersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); | |||
} | |||
} | |||
public void update(DbSession dbSession, AlmPatDto almPatDto) { | |||
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)); | |||
} | |||
} | |||
public void delete(DbSession dbSession, AlmPatDto almPatDto) { | |||
public void delete(DbSession dbSession, AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) { | |||
getMapper(dbSession).deleteByUuid(almPatDto.getUuid()); | |||
if (auditPersister != null) { | |||
auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almPatDto, userLogin, almSettingKey)); | |||
} | |||
} | |||
public void deleteByUser(DbSession dbSession, UserDto user) { | |||
getMapper(dbSession).deleteByUser(user.getUuid()); | |||
if (auditPersister != null) { | |||
auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(user)); | |||
} | |||
} | |||
public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) { | |||
getMapper(dbSession).deleteByAlmSetting(almSetting.getUuid()); | |||
if (auditPersister != null) { | |||
auditPersister.deletePersonalAccessToken(dbSession, new PersonalAccessTokenNewValue(almSetting)); | |||
} | |||
} | |||
} |
@@ -25,17 +25,26 @@ import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; | |||
public class AlmSettingDao implements Dao { | |||
private final System2 system2; | |||
private final UuidFactory uuidFactory; | |||
private AuditPersister auditPersister; | |||
public AlmSettingDao(System2 system2, UuidFactory uuidFactory) { | |||
this.system2 = system2; | |||
this.uuidFactory = uuidFactory; | |||
} | |||
public AlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { | |||
this(system2, uuidFactory); | |||
this.auditPersister = auditPersister; | |||
} | |||
private static AlmSettingMapper getMapper(DbSession dbSession) { | |||
return dbSession.getMapper(AlmSettingMapper.class); | |||
} | |||
@@ -47,6 +56,11 @@ public class AlmSettingDao implements Dao { | |||
almSettingDto.setCreatedAt(now); | |||
almSettingDto.setUpdatedAt(now); | |||
getMapper(dbSession).insert(almSettingDto); | |||
if (auditPersister != null) { | |||
auditPersister.addDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto.getUuid(), | |||
almSettingDto.getKey())); | |||
} | |||
} | |||
public Optional<AlmSettingDto> selectByUuid(DbSession dbSession, String uuid) { | |||
@@ -67,11 +81,20 @@ public class AlmSettingDao implements Dao { | |||
public void delete(DbSession dbSession, AlmSettingDto almSettingDto) { | |||
getMapper(dbSession).deleteByKey(almSettingDto.getKey()); | |||
if (auditPersister != null) { | |||
auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto.getUuid(), | |||
almSettingDto.getKey())); | |||
} | |||
} | |||
public void update(DbSession dbSession, AlmSettingDto almSettingDto) { | |||
long now = system2.now(); | |||
almSettingDto.setUpdatedAt(now); | |||
getMapper(dbSession).update(almSettingDto); | |||
if (auditPersister != null) { | |||
auditPersister.updateDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSettingDto)); | |||
} | |||
} | |||
} |
@@ -26,6 +26,8 @@ import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; | |||
import org.sonar.db.project.ProjectDto; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputs; | |||
@@ -34,31 +36,62 @@ public class ProjectAlmSettingDao implements Dao { | |||
private final System2 system2; | |||
private final UuidFactory uuidFactory; | |||
private AuditPersister auditPersister; | |||
public ProjectAlmSettingDao(System2 system2, UuidFactory uuidFactory) { | |||
this.system2 = system2; | |||
this.uuidFactory = uuidFactory; | |||
} | |||
public void insertOrUpdate(DbSession dbSession, ProjectAlmSettingDto projectAlmSettingDto) { | |||
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 uuid = uuidFactory.create(); | |||
long now = system2.now(); | |||
ProjectAlmSettingMapper mapper = getMapper(dbSession); | |||
boolean isUpdate = true; | |||
if (mapper.update(projectAlmSettingDto, now) == 0) { | |||
mapper.insert(projectAlmSettingDto, uuid, now); | |||
projectAlmSettingDto.setUuid(uuid); | |||
projectAlmSettingDto.setCreatedAt(now); | |||
isUpdate = false; | |||
} | |||
projectAlmSettingDto.setUpdatedAt(now); | |||
if (auditPersister != null) { | |||
if (isUpdate) { | |||
auditPersister.updateDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(projectAlmSettingDto, key, projectName)); | |||
} else { | |||
auditPersister.addDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(projectAlmSettingDto.getAlmSettingUuid(), | |||
projectAlmSettingDto.getProjectUuid(), key, projectName)); | |||
} | |||
} | |||
} | |||
public void deleteByProject(DbSession dbSession, ProjectDto project) { | |||
getMapper(dbSession).deleteByProjectUuid(project.getUuid()); | |||
if (auditPersister != null) { | |||
auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(project)); | |||
} | |||
} | |||
public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) { | |||
deleteByAlmSetting(dbSession, almSetting, false); | |||
} | |||
public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting, boolean track) { | |||
getMapper(dbSession).deleteByAlmSettingUuid(almSetting.getUuid()); | |||
if (track && auditPersister != null) { | |||
auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(almSetting.getUuid(), | |||
almSetting.getKey())); | |||
} | |||
} | |||
public int countByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) { |
@@ -42,11 +42,11 @@ public interface AuditPersister { | |||
void deleteUserFromGroup(DbSession dbSession, NewValue newValue); | |||
void addUserProperty(DbSession dbSession, NewValue newValue); | |||
void addProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); | |||
void updateUserProperty(DbSession dbSession, NewValue newValue); | |||
void updateProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); | |||
void deleteUserProperty(DbSession dbSession, NewValue newValue); | |||
void deleteProperty(DbSession dbSession, NewValue newValue, boolean isUserProperty); | |||
void addUserToken(DbSession dbSession, NewValue newValue); | |||
@@ -54,5 +54,57 @@ public interface AuditPersister { | |||
void deleteUserToken(DbSession dbSession, NewValue newValue); | |||
void addGroupPermission(DbSession dbSession, NewValue newValue); | |||
void deleteGroupPermission(DbSession dbSession, NewValue newValue); | |||
void addUserPermission(DbSession dbSession, NewValue newValue); | |||
void deleteUserPermission(DbSession dbSession, NewValue newValue); | |||
void addPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void updatePermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void deletePermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void addUserToPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void deleteUserFromPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void addGroupToPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void deleteGroupFromPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void addCharacteristicToPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void updateCharacteristicInPermissionTemplate(DbSession dbSession, NewValue newValue); | |||
void addPlugin(DbSession dbSession, NewValue newValue); | |||
void updatePlugin(DbSession dbSession, NewValue newValue); | |||
void generateSecretKey(DbSession dbSession); | |||
void setLicense(DbSession dbSession, boolean isSet, NewValue newValue); | |||
void addWebhook(DbSession dbSession, NewValue newValue); | |||
void updateWebhook(DbSession dbSession, NewValue newValue); | |||
void deleteWebhook(DbSession dbSession, NewValue newValue); | |||
void addDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); | |||
void updateDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); | |||
void deleteDevOpsPlatformSetting(DbSession dbSession, NewValue newValue); | |||
void addPersonalAccessToken(DbSession dbSession, NewValue newValue); | |||
void updatePersonalAccessToken(DbSession dbSession, NewValue newValue); | |||
void deletePersonalAccessToken(DbSession dbSession, NewValue newValue); | |||
boolean isTrackedProperty(String propertyKey); | |||
} |
@@ -0,0 +1,181 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.ObjectUtils; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.db.alm.setting.ProjectAlmSettingDto; | |||
import org.sonar.db.project.ProjectDto; | |||
public class DevOpsPlatformSettingNewValue implements NewValue { | |||
@Nullable | |||
private String devOpsPlatformSettingUuid; | |||
@Nullable | |||
private String key; | |||
@Nullable | |||
private String devOpsPlatformName; | |||
@Nullable | |||
private String url; | |||
@Nullable | |||
private String appId; | |||
@Nullable | |||
private String clientId; | |||
@Nullable | |||
private String projectUuid; | |||
@Nullable | |||
private String projectName; | |||
@Nullable | |||
private String almRepo; | |||
@Nullable | |||
private String almSlug; | |||
@Nullable | |||
private Boolean isSummaryCommentEnabled; | |||
@Nullable | |||
private Boolean isMonorepo; | |||
public DevOpsPlatformSettingNewValue(String devOpsPlatformSettingUuid, String key) { | |||
this.devOpsPlatformSettingUuid = devOpsPlatformSettingUuid; | |||
this.key = key; | |||
} | |||
public DevOpsPlatformSettingNewValue(AlmSettingDto dto) { | |||
this.devOpsPlatformSettingUuid = dto.getUuid(); | |||
this.key = dto.getKey(); | |||
this.devOpsPlatformName = dto.getAppId(); | |||
this.url = dto.getUrl(); | |||
this.appId = dto.getAppId(); | |||
this.clientId = dto.getClientId(); | |||
} | |||
public DevOpsPlatformSettingNewValue(ProjectAlmSettingDto dto, String key, String projectName) { | |||
this.devOpsPlatformSettingUuid = dto.getAlmSettingUuid(); | |||
this.key = key; | |||
this.projectUuid = dto.getProjectUuid(); | |||
this.projectName = projectName; | |||
this.almRepo = dto.getAlmRepo(); | |||
this.almSlug = dto.getAlmSlug(); | |||
this.isSummaryCommentEnabled = dto.getSummaryCommentEnabled(); | |||
this.isMonorepo = dto.getMonorepo(); | |||
} | |||
public DevOpsPlatformSettingNewValue(String devOpsPlatformSettingUuid, String projectUuid, String key, String projectName) { | |||
this.devOpsPlatformSettingUuid = devOpsPlatformSettingUuid; | |||
this.key = key; | |||
this.projectUuid = projectUuid; | |||
this.projectName = projectName; | |||
} | |||
public DevOpsPlatformSettingNewValue(ProjectDto projectDto) { | |||
this.projectUuid = projectDto.getUuid(); | |||
this.projectName = projectDto.getName(); | |||
} | |||
@CheckForNull | |||
public String getDevOpsPlatformSettingUuid() { | |||
return this.devOpsPlatformSettingUuid; | |||
} | |||
@CheckForNull | |||
public String getKey() { | |||
return this.key; | |||
} | |||
@CheckForNull | |||
public String getDevOpsPlatformName() { | |||
return this.devOpsPlatformName; | |||
} | |||
@CheckForNull | |||
public String getUrl() { | |||
return this.url; | |||
} | |||
@CheckForNull | |||
public String getAppId() { | |||
return this.appId; | |||
} | |||
@CheckForNull | |||
public String getClientId() { | |||
return this.clientId; | |||
} | |||
@CheckForNull | |||
public String getProjectUuid() { | |||
return this.projectUuid; | |||
} | |||
@CheckForNull | |||
public String getProjectName() { | |||
return this.projectName; | |||
} | |||
@CheckForNull | |||
public String getAlmRepo() { | |||
return this.almRepo; | |||
} | |||
@CheckForNull | |||
public String getAlmSlug() { | |||
return this.almSlug; | |||
} | |||
@CheckForNull | |||
public Boolean isSummaryCommentEnabled() { | |||
return this.isSummaryCommentEnabled; | |||
} | |||
@CheckForNull | |||
public Boolean isMonorepo() { | |||
return this.isMonorepo; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"devOpsPlatformSettingUuid\": ", this.devOpsPlatformSettingUuid, true); | |||
addField(sb, "\"key\": ", this.key, true); | |||
addField(sb, "\"devOpsPlatformName\": ", this.devOpsPlatformName, true); | |||
addField(sb, "\"url\": ", this.url, true); | |||
addField(sb, "\"appId\": ", this.appId, true); | |||
addField(sb, "\"clientId\": ", this.clientId, true); | |||
addField(sb, "\"projectUuid\": ", this.projectUuid, true); | |||
addField(sb, "\"projectName\": ", this.projectName, true); | |||
addField(sb, "\"almRepo\": ", this.almRepo, true); | |||
addField(sb, "\"almSlug\": ", this.almSlug, true); | |||
addField(sb, "\"isSummaryCommentEnabled\": ", ObjectUtils.toString(this.isSummaryCommentEnabled), false); | |||
addField(sb, "\"isMonorepo\": ", ObjectUtils.toString(this.isMonorepo), false); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -0,0 +1,45 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
public class LicenseNewValue implements NewValue { | |||
@Nullable | |||
private String edition; | |||
public LicenseNewValue(@Nullable String edition) { | |||
this.edition = edition; | |||
} | |||
@CheckForNull | |||
public String getEdition() { | |||
return this.edition; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"edition\": ", this.edition, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -29,13 +29,21 @@ public interface NewValue { | |||
addQuote(sb, isString); | |||
sb.append(value); | |||
addQuote(sb, isString); | |||
sb.append(","); | |||
sb.append(", "); | |||
} | |||
} | |||
default void endString(StringBuilder sb) { | |||
int length = sb.length(); | |||
if(sb.length() > 1) { | |||
sb.delete(length - 2, length - 1); | |||
} | |||
sb.append("}"); | |||
} | |||
private static void addQuote(StringBuilder sb, boolean isString) { | |||
if(isString) { | |||
sb.append("'"); | |||
sb.append("\""); | |||
} | |||
} | |||
} |
@@ -0,0 +1,113 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.permission.GroupPermissionDto; | |||
import org.sonar.db.permission.UserPermissionDto; | |||
public class PermissionNewValue implements NewValue { | |||
@Nullable | |||
private String permissionUuid; | |||
@Nullable | |||
private String groupUuid; | |||
@Nullable | |||
private String userUuid; | |||
@Nullable | |||
private String projectUuid; | |||
@Nullable | |||
private String projectName; | |||
@Nullable | |||
private String role; | |||
public PermissionNewValue(GroupPermissionDto groupPermissionDto, String projectName) { | |||
this.permissionUuid = groupPermissionDto.getUuid(); | |||
this.groupUuid = groupPermissionDto.getGroupUuid(); | |||
this.projectUuid = groupPermissionDto.getComponentUuid(); | |||
this.role = groupPermissionDto.getRole(); | |||
this.projectName = projectName; | |||
} | |||
public PermissionNewValue(UserPermissionDto permissionDto, @Nullable String projectName) { | |||
this.permissionUuid = permissionDto.getUuid(); | |||
this.userUuid = permissionDto.getUserUuid(); | |||
this.projectUuid = permissionDto.getComponentUuid(); | |||
this.role = permissionDto.getPermission(); | |||
this.projectName = projectName; | |||
} | |||
public PermissionNewValue(@Nullable String role, @Nullable String groupUuid, @Nullable String rootComponentUuid, | |||
@Nullable String projectName, @Nullable String userUuid) { | |||
this.role = role; | |||
this.groupUuid = groupUuid; | |||
this.projectUuid = rootComponentUuid; | |||
this.projectName = projectName; | |||
this.userUuid = userUuid; | |||
} | |||
@CheckForNull | |||
public String getPermissionUuid() { | |||
return this.permissionUuid; | |||
} | |||
@CheckForNull | |||
public String getGroupUuid() { | |||
return this.groupUuid; | |||
} | |||
@CheckForNull | |||
public String getProjectUuid() { | |||
return this.projectUuid; | |||
} | |||
@CheckForNull | |||
public String getRole() { | |||
return this.role; | |||
} | |||
@CheckForNull | |||
public String getProjectName() { | |||
return this.projectName; | |||
} | |||
@CheckForNull | |||
public String getUserUuid() { | |||
return this.userUuid; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"permissionUuid\": ", this.permissionUuid, true); | |||
addField(sb, "\"groupUuid\": ", this.groupUuid, true); | |||
addField(sb, "\"projectUuid\": ", this.projectUuid, true); | |||
addField(sb, "\"role\": ", this.role, true); | |||
addField(sb, "\"projectName\": ", this.projectName, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -0,0 +1,149 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.ObjectUtils; | |||
import org.sonar.db.permission.template.PermissionTemplateDto; | |||
public class PermissionTemplateNewValue implements NewValue { | |||
private String templateUuid; | |||
private String name; | |||
@Nullable | |||
private String keyPattern; | |||
@Nullable | |||
private String description; | |||
@Nullable | |||
private String role; | |||
@Nullable | |||
private String userUuid; | |||
@Nullable | |||
private String userLogin; | |||
@Nullable | |||
private String groupUuid; | |||
@Nullable | |||
private String groupName; | |||
@Nullable | |||
private Boolean withProjectCreator; | |||
public PermissionTemplateNewValue(String templateUuid, String name) { | |||
this.templateUuid = templateUuid; | |||
this.name = name; | |||
} | |||
public PermissionTemplateNewValue(PermissionTemplateDto permissionTemplateDto) { | |||
this.templateUuid = permissionTemplateDto.getUuid(); | |||
this.name = permissionTemplateDto.getName(); | |||
this.keyPattern = permissionTemplateDto.getKeyPattern(); | |||
this.description = permissionTemplateDto.getDescription(); | |||
} | |||
public PermissionTemplateNewValue(@Nullable String templateUuid, @Nullable String name, @Nullable String role, | |||
@Nullable String userUuid, @Nullable String userLogin, @Nullable String groupUuid, @Nullable String groupName) { | |||
this.templateUuid = templateUuid; | |||
this.name = name; | |||
this.role = role; | |||
this.userUuid = userUuid; | |||
this.userLogin = userLogin; | |||
this.groupUuid = groupUuid; | |||
this.groupName = groupName; | |||
} | |||
public PermissionTemplateNewValue(String templateUuid, String role, String name, boolean withProjectCreator) { | |||
this.templateUuid = templateUuid; | |||
this.name = name; | |||
this.role = role; | |||
this.withProjectCreator = withProjectCreator; | |||
} | |||
public String getTemplateUuid() { | |||
return this.templateUuid; | |||
} | |||
public String getName() { | |||
return this.name; | |||
} | |||
@CheckForNull | |||
public String getKeyPattern() { | |||
return this.keyPattern; | |||
} | |||
@CheckForNull | |||
public String getDescription() { | |||
return this.description; | |||
} | |||
@CheckForNull | |||
public String getRole() { | |||
return this.role; | |||
} | |||
@CheckForNull | |||
public String getUserUuid() { | |||
return this.userUuid; | |||
} | |||
@CheckForNull | |||
public String getUserLogin() { | |||
return this.userLogin; | |||
} | |||
@CheckForNull | |||
public String getGroupUuid() { | |||
return this.groupUuid; | |||
} | |||
@CheckForNull | |||
public String getGroupName() { | |||
return this.groupName; | |||
} | |||
@CheckForNull | |||
public Boolean isWithProjectCreator() { | |||
return this.withProjectCreator; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"templateUuid\": ", this.templateUuid, true); | |||
addField(sb, "\"name\": ", this.name, true); | |||
addField(sb, "\"keyPattern\": ", this.keyPattern, true); | |||
addField(sb, "\"description\": ", this.description, true); | |||
addField(sb, "\"role\": ", this.role, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"userLogin\": ", this.userLogin, true); | |||
addField(sb, "\"groupUuid\": ", this.groupUuid, true); | |||
addField(sb, "\"groupName\": ", this.groupName, true); | |||
addField(sb, "\"withProjectCreator\": ", ObjectUtils.toString(this.withProjectCreator), false); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -0,0 +1,98 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.alm.pat.AlmPatDto; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.db.user.UserDto; | |||
public class PersonalAccessTokenNewValue implements NewValue { | |||
@Nullable | |||
private String patUuid; | |||
@Nullable | |||
private String userUuid; | |||
@Nullable | |||
private String userLogin; | |||
@Nullable | |||
private String almSettingUuid; | |||
@Nullable | |||
private String almSettingKey; | |||
public PersonalAccessTokenNewValue(AlmPatDto almPatDto, @Nullable String userLogin, @Nullable String almSettingKey) { | |||
this.patUuid = almPatDto.getUuid(); | |||
this.userUuid = almPatDto.getUserUuid(); | |||
this.almSettingUuid = almPatDto.getAlmSettingUuid(); | |||
this.userLogin = userLogin; | |||
this.almSettingKey = almSettingKey; | |||
} | |||
public PersonalAccessTokenNewValue(UserDto userDto) { | |||
this.userUuid = userDto.getUuid(); | |||
this.userLogin = userDto.getLogin(); | |||
} | |||
public PersonalAccessTokenNewValue(AlmSettingDto almSettingDto) { | |||
this.almSettingUuid = almSettingDto.getUuid(); | |||
this.almSettingKey = almSettingDto.getKey(); | |||
} | |||
@CheckForNull | |||
public String getPatUuid() { | |||
return this.patUuid; | |||
} | |||
@CheckForNull | |||
public String getUserUuid() { | |||
return this.userUuid; | |||
} | |||
@CheckForNull | |||
public String getUserLogin() { | |||
return this.userLogin; | |||
} | |||
@CheckForNull | |||
public String getAlmSettingUuid() { | |||
return this.almSettingUuid; | |||
} | |||
@CheckForNull | |||
public String getAlmSettingKey() { | |||
return this.almSettingKey; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"patUuid\": ", this.patUuid, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"userLogin\": ", this.userLogin, true); | |||
addField(sb, "\"almSettingUuid\": ", this.almSettingUuid, true); | |||
addField(sb, "\"almSettingKey\": ", this.almSettingKey, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -0,0 +1,63 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import org.sonar.db.plugin.PluginDto; | |||
public class PluginNewValue implements NewValue { | |||
private String pluginUuid; | |||
private String kee; | |||
private String basePluginKey; | |||
private String type; | |||
public PluginNewValue(PluginDto pluginDto) { | |||
this.pluginUuid = pluginDto.getUuid(); | |||
this.kee = pluginDto.getKee(); | |||
this.basePluginKey = pluginDto.getBasePluginKey(); | |||
this.type = pluginDto.getType().name(); | |||
} | |||
public String getPluginUuid() { | |||
return this.pluginUuid; | |||
} | |||
public String getKee() { | |||
return this.kee; | |||
} | |||
public String getBasePluginKey() { | |||
return this.basePluginKey; | |||
} | |||
public String getType() { | |||
return this.type; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"pluginUuid\": ", this.pluginUuid, true); | |||
addField(sb, "\"kee\": ", this.kee, true); | |||
addField(sb, "\"basePluginKey\": ", this.basePluginKey, true); | |||
addField(sb, "\"type\": ", this.type, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -19,49 +19,118 @@ | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.property.PropertyDto; | |||
import org.sonar.db.user.UserPropertyDto; | |||
public class PropertyNewValue implements NewValue { | |||
@Nullable | |||
private String propertyKey; | |||
@Nullable | |||
private String propertyValue; | |||
@Nullable | |||
private String userUuid; | |||
@Nullable | |||
private String userLogin; | |||
@Nullable | |||
private String projectUuid; | |||
@Nullable | |||
private String projectName; | |||
public PropertyNewValue(UserPropertyDto userPropertyDto, String login) { | |||
this.propertyKey = userPropertyDto.getKey(); | |||
this.userUuid = userPropertyDto.getUserUuid(); | |||
this.userLogin = login; | |||
if(!propertyKey.contains(".secured")) { | |||
this.propertyValue = userPropertyDto.getValue(); | |||
} | |||
setValue(propertyKey, userPropertyDto.getValue()); | |||
} | |||
public PropertyNewValue(PropertyDto propertyDto, @Nullable String userLogin, @Nullable String projectName) { | |||
this.propertyKey = propertyDto.getKey(); | |||
this.userUuid = propertyDto.getUserUuid(); | |||
this.userLogin = userLogin; | |||
this.projectUuid = propertyDto.getComponentUuid(); | |||
this.projectName = projectName; | |||
setValue(propertyKey, propertyDto.getValue()); | |||
} | |||
public PropertyNewValue(String propertyKey) { | |||
this.propertyKey = propertyKey; | |||
} | |||
public PropertyNewValue(String propertyKey, @Nullable String userUuid, String userLogin) { | |||
this.propertyKey = propertyKey; | |||
this.userUuid = userUuid; | |||
this.userLogin = userLogin; | |||
} | |||
public PropertyNewValue(String propertyKey, String propertyValue) { | |||
this.propertyKey = propertyKey; | |||
setValue(propertyKey, propertyValue); | |||
} | |||
public PropertyNewValue(@Nullable String propertyKey, @Nullable String projectUuid, | |||
@Nullable String projectName, @Nullable String userUuid) { | |||
this.propertyKey = propertyKey; | |||
this.projectUuid = projectUuid; | |||
this.projectName = projectName; | |||
this.userUuid = userUuid; | |||
} | |||
@CheckForNull | |||
public String getPropertyKey() { | |||
return this.propertyKey; | |||
} | |||
@CheckForNull | |||
public String getPropertyValue() { | |||
return this.propertyValue; | |||
} | |||
@CheckForNull | |||
public String getUserUuid() { | |||
return this.userUuid; | |||
} | |||
@CheckForNull | |||
public String getUserLogin() { | |||
return this.userLogin; | |||
} | |||
@CheckForNull | |||
public String getProjectUuid() { | |||
return this.projectUuid; | |||
} | |||
@CheckForNull | |||
public String getProjectName() { | |||
return this.projectName; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "'propertyKey':", this.propertyKey, true); | |||
addField(sb, "'propertyValue':", this.propertyValue, true); | |||
addField(sb, "'userUuid':", this.userUuid, true); | |||
addField(sb, "'userLogin':", this.userLogin, true); | |||
sb.append("}"); | |||
addField(sb, "\"propertyKey\": ", this.propertyKey, true); | |||
addField(sb, "\"propertyValue\": ", this.propertyValue, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"userLogin\": ", this.userLogin, true); | |||
addField(sb, "\"projectUuid\": ", this.projectUuid, true); | |||
addField(sb, "\"projectName\": ", this.projectName, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
private void setValue(String propertyKey, String value) { | |||
if (!propertyKey.contains(".secured")) { | |||
this.propertyValue = value; | |||
} | |||
} | |||
} |
@@ -19,15 +19,27 @@ | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.user.GroupDto; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.db.user.UserGroupDto; | |||
public class UserGroupNewValue implements NewValue { | |||
@Nullable | |||
private String groupUuid; | |||
@Nullable | |||
private String name; | |||
@Nullable | |||
private String description; | |||
@Nullable | |||
private String userUuid; | |||
@Nullable | |||
private String userLogin; | |||
public UserGroupNewValue(String groupUuid, String name) { | |||
@@ -60,22 +72,27 @@ public class UserGroupNewValue implements NewValue { | |||
this.userLogin = userLogin; | |||
} | |||
@CheckForNull | |||
public String getGroupUuid() { | |||
return this.groupUuid; | |||
} | |||
@CheckForNull | |||
public String getName() { | |||
return this.name; | |||
} | |||
@CheckForNull | |||
public String getDescription() { | |||
return this.description; | |||
} | |||
@CheckForNull | |||
public String getUserUuid() { | |||
return this.userUuid; | |||
} | |||
@CheckForNull | |||
public String getUserLogin() { | |||
return this.userLogin; | |||
} | |||
@@ -83,13 +100,12 @@ public class UserGroupNewValue implements NewValue { | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "'groupUuid':", this.groupUuid, true); | |||
addField(sb, "'name':", this.name, true); | |||
addField(sb, "'description':", this.description, true); | |||
addField(sb, "'userUuid':", this.userUuid, true); | |||
addField(sb, "'userLogin':", this.userLogin, true); | |||
sb.append("}"); | |||
addField(sb, "\"groupUuid\": ", this.groupUuid, true); | |||
addField(sb, "\"name\": ", this.name, true); | |||
addField(sb, "\"description\": ", this.description, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"userLogin\": ", this.userLogin, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -19,22 +19,46 @@ | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.ObjectUtils; | |||
import org.sonar.db.user.UserDto; | |||
public class UserNewValue implements NewValue { | |||
private String userUuid; | |||
private String login; | |||
@Nullable | |||
private String name; | |||
@Nullable | |||
private String email; | |||
@Nullable | |||
private Boolean isActive; | |||
@Nullable | |||
private String scmAccounts; | |||
@Nullable | |||
private String externalId; | |||
@Nullable | |||
private String externalLogin; | |||
@Nullable | |||
private String externalIdentityProvider; | |||
@Nullable | |||
private Boolean local; | |||
@Nullable | |||
private Boolean onboarded; | |||
@Nullable | |||
private Boolean root; | |||
@Nullable | |||
private Long lastConnectionDate; | |||
public UserNewValue(String userUuid, String userLogin) { | |||
@@ -66,46 +90,57 @@ public class UserNewValue implements NewValue { | |||
return this.login; | |||
} | |||
@CheckForNull | |||
public String getName() { | |||
return this.name; | |||
} | |||
@CheckForNull | |||
public String getEmail() { | |||
return this.email; | |||
} | |||
@CheckForNull | |||
public boolean isActive() { | |||
return this.isActive; | |||
} | |||
@CheckForNull | |||
public String getScmAccounts() { | |||
return this.scmAccounts; | |||
} | |||
@CheckForNull | |||
public String getExternalId() { | |||
return this.externalId; | |||
} | |||
@CheckForNull | |||
public String getExternalLogin() { | |||
return this.externalLogin; | |||
} | |||
@CheckForNull | |||
public String getExternalIdentityProvider() { | |||
return this.externalIdentityProvider; | |||
} | |||
@CheckForNull | |||
public boolean isLocal() { | |||
return this.local; | |||
} | |||
@CheckForNull | |||
public boolean isOnboarded() { | |||
return this.onboarded; | |||
} | |||
@CheckForNull | |||
public boolean isRoot() { | |||
return this.root; | |||
} | |||
@CheckForNull | |||
public Long getLastConnectionDate() { | |||
return this.lastConnectionDate; | |||
} | |||
@@ -113,21 +148,20 @@ public class UserNewValue implements NewValue { | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "'userUuid':", this.userUuid, true); | |||
addField(sb, "'login':", this.login, true); | |||
addField(sb, "'name':", this.name, true); | |||
addField(sb, "'email':", this.email, true); | |||
addField(sb, "'isActive':", ObjectUtils.toString(this.isActive), false); | |||
addField(sb, "'scmAccounts':", this.scmAccounts, true); | |||
addField(sb, "'externalId':", this.externalId, true); | |||
addField(sb, "'externalLogin':", this.externalLogin, true); | |||
addField(sb, "'externalIdentityProvider':", this.externalIdentityProvider, true); | |||
addField(sb, "'local':", ObjectUtils.toString(this.local), false); | |||
addField(sb, "'onboarded':", ObjectUtils.toString(this.onboarded), false); | |||
addField(sb, "'root':", ObjectUtils.toString(this.root), false); | |||
addField(sb, "'lastConnectionDate':", ObjectUtils.toString(this.lastConnectionDate), false); | |||
sb.append("}"); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"login\": ", this.login, true); | |||
addField(sb, "\"name\": ", this.name, true); | |||
addField(sb, "\"email\": ", this.email, true); | |||
addField(sb, "\"isActive\": ", ObjectUtils.toString(this.isActive), false); | |||
addField(sb, "\"scmAccounts\": ", this.scmAccounts, true); | |||
addField(sb, "\"externalId\": ", this.externalId, true); | |||
addField(sb, "\"externalLogin\": ", this.externalLogin, true); | |||
addField(sb, "\"externalIdentityProvider\": ", this.externalIdentityProvider, true); | |||
addField(sb, "\"local\": ", ObjectUtils.toString(this.local), false); | |||
addField(sb, "\"onboarded\": ", ObjectUtils.toString(this.onboarded), false); | |||
addField(sb, "\"root\": ", ObjectUtils.toString(this.root), false); | |||
addField(sb, "\"lastConnectionDate\": ", ObjectUtils.toString(this.lastConnectionDate), false); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -19,15 +19,25 @@ | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.ObjectUtils; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.db.user.UserTokenDto; | |||
public class UserTokenNewValue implements NewValue { | |||
@Nullable | |||
private String tokenUuid; | |||
private String userUuid; | |||
@Nullable | |||
private String userLogin; | |||
@Nullable | |||
private String tokenName; | |||
@Nullable | |||
private Long lastConnectionDate; | |||
public UserTokenNewValue(UserTokenDto userTokenDto, @Nullable String userLogin) { | |||
@@ -48,6 +58,7 @@ public class UserTokenNewValue implements NewValue { | |||
this.tokenName = tokenName; | |||
} | |||
@CheckForNull | |||
public String getTokenUuid() { | |||
return this.tokenUuid; | |||
} | |||
@@ -56,14 +67,17 @@ public class UserTokenNewValue implements NewValue { | |||
return this.userUuid; | |||
} | |||
@CheckForNull | |||
public String getUserLogin() { | |||
return this.userLogin; | |||
} | |||
@CheckForNull | |||
public String getTokenName() { | |||
return this.tokenName; | |||
} | |||
@CheckForNull | |||
public Long getLastConnectionDate() { | |||
return this.lastConnectionDate; | |||
} | |||
@@ -71,13 +85,12 @@ public class UserTokenNewValue implements NewValue { | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "'tokenUuid':", this.tokenUuid, true); | |||
addField(sb, "'userUuid':", this.userUuid, true); | |||
addField(sb, "'userLogin':", this.userLogin, true); | |||
addField(sb, "'tokenName':", this.tokenName, true); | |||
addField(sb, "'lastConnectionDate':", ObjectUtils.toString(this.lastConnectionDate), false); | |||
sb.append("}"); | |||
addField(sb, "\"tokenUuid\": ", this.tokenUuid, true); | |||
addField(sb, "\"userUuid\": ", this.userUuid, true); | |||
addField(sb, "\"userLogin\": ", this.userLogin, true); | |||
addField(sb, "\"tokenName\": ", this.tokenName, true); | |||
addField(sb, "\"lastConnectionDate\": ", ObjectUtils.toString(this.lastConnectionDate), false); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -0,0 +1,103 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.project.ProjectDto; | |||
import org.sonar.db.webhook.WebhookDto; | |||
public class WebhookNewValue implements NewValue { | |||
@Nullable | |||
private String webhookUuid; | |||
@Nullable | |||
private String name; | |||
@Nullable | |||
private String url; | |||
@Nullable | |||
private String projectUuid; | |||
@Nullable | |||
private String projectName; | |||
public WebhookNewValue(WebhookDto dto, @Nullable String projectName) { | |||
this.webhookUuid = dto.getUuid(); | |||
this.name = dto.getName(); | |||
this.url = dto.getUrl(); | |||
this.projectUuid = dto.getProjectUuid(); | |||
this.projectName = projectName; | |||
} | |||
public WebhookNewValue(String webhookUuid, String webhookName) { | |||
this.webhookUuid = webhookUuid; | |||
this.name = webhookName; | |||
} | |||
public WebhookNewValue(String webhookUuid, String webhookName, @Nullable String projectUuid, @Nullable String projectName) { | |||
this(webhookUuid, webhookName); | |||
this.projectUuid = projectUuid; | |||
this.projectName = projectName; | |||
} | |||
public WebhookNewValue(ProjectDto projectDto) { | |||
this.projectUuid = projectDto.getUuid(); | |||
this.projectName = projectDto.getName(); | |||
} | |||
@CheckForNull | |||
public String getWebhookUuid() { | |||
return this.webhookUuid; | |||
} | |||
@CheckForNull | |||
public String getName() { | |||
return this.name; | |||
} | |||
@CheckForNull | |||
public String getUrl() { | |||
return this.url; | |||
} | |||
@CheckForNull | |||
public String getProjectUuid() { | |||
return this.projectUuid; | |||
} | |||
@CheckForNull | |||
public String getProjectName() { | |||
return this.projectName; | |||
} | |||
@Override | |||
public String toString() { | |||
StringBuilder sb = new StringBuilder("{"); | |||
addField(sb, "\"webhookUuid\": ", this.webhookUuid, true); | |||
addField(sb, "\"name\": ", this.name, true); | |||
addField(sb, "\"url\": ", this.url, true); | |||
addField(sb, "\"projectUuid\": ", this.projectUuid, true); | |||
addField(sb, "\"projectName\": ", this.projectName, true); | |||
endString(sb); | |||
return sb.toString(); | |||
} | |||
} |
@@ -29,6 +29,9 @@ import org.apache.ibatis.session.RowBounds; | |||
import org.sonar.api.security.DefaultGroups; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionNewValue; | |||
import org.sonar.db.component.ComponentDto; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputs; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; | |||
@@ -37,6 +40,15 @@ public class GroupPermissionDao implements Dao { | |||
private static final String ANYONE_GROUP_PARAMETER = "anyoneGroup"; | |||
private AuditPersister auditPersister; | |||
public GroupPermissionDao() { | |||
} | |||
public GroupPermissionDao(AuditPersister auditPersister) { | |||
this.auditPersister = auditPersister; | |||
} | |||
/** | |||
* Returns the names of the groups that match the given query. | |||
* The virtual group "Anyone" may be returned as the value {@link DefaultGroups#ANYONE}. | |||
@@ -110,30 +122,51 @@ public class GroupPermissionDao implements Dao { | |||
return mapper(session).selectGroupUuidsWithPermissionOnProjectBut(projectUuid, permission); | |||
} | |||
public void insert(DbSession dbSession, GroupPermissionDto dto) { | |||
public void insert(DbSession dbSession, GroupPermissionDto dto, @Nullable ComponentDto componentDto) { | |||
mapper(dbSession).insert(dto); | |||
if (auditPersister != null) { | |||
String projectName = (componentDto != null) ? componentDto.name() : null; | |||
auditPersister.addGroupPermission(dbSession, new PermissionNewValue(dto, projectName)); | |||
} | |||
} | |||
/** | |||
* Delete all the permissions associated to a root component (project) | |||
*/ | |||
public void deleteByRootComponentUuid(DbSession dbSession, String rootComponentUuid) { | |||
public void deleteByRootComponentUuid(DbSession dbSession, String rootComponentUuid, String projectName) { | |||
mapper(dbSession).deleteByRootComponentUuid(rootComponentUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(null, null, rootComponentUuid, projectName, null)); | |||
} | |||
} | |||
/** | |||
* Delete all permissions of the specified group (group "AnyOne" if {@code groupUuid} is {@code null}) for the specified | |||
* component. | |||
*/ | |||
public int deleteByRootComponentUuidAndGroupUuid(DbSession dbSession, String rootComponentUuid, @Nullable String groupUuid) { | |||
return mapper(dbSession).deleteByRootComponentUuidAndGroupUuid(rootComponentUuid, groupUuid); | |||
public int deleteByRootComponentUuidAndGroupUuid(DbSession dbSession, String rootComponentUuid, @Nullable String groupUuid, String projectName) { | |||
int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndGroupUuid(rootComponentUuid, groupUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(null, groupUuid, rootComponentUuid, projectName, null)); | |||
} | |||
return deletedRecords; | |||
} | |||
/** | |||
* Delete the specified permission for the specified component for any group (including group AnyOne). | |||
*/ | |||
public int deleteByRootComponentUuidAndPermission(DbSession dbSession, String rootComponentUuid, String permission) { | |||
return mapper(dbSession).deleteByRootComponentUuidAndPermission(rootComponentUuid, permission); | |||
public int deleteByRootComponentUuidAndPermission(DbSession dbSession, String rootComponentUuid, String permission, String projectName) { | |||
int deletedRecords = mapper(dbSession).deleteByRootComponentUuidAndPermission(rootComponentUuid, permission); | |||
if (auditPersister != null) { | |||
auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(permission, null, rootComponentUuid, projectName, null)); | |||
} | |||
return deletedRecords; | |||
} | |||
/** | |||
@@ -149,8 +182,13 @@ public class GroupPermissionDao implements Dao { | |||
* @param groupUuid if null, then anyone, else uuid of group | |||
* @param rootComponentUuid if null, then global permission, otherwise the uuid of root component (project) | |||
*/ | |||
public void delete(DbSession dbSession, String permission, @Nullable String groupUuid, @Nullable String rootComponentUuid) { | |||
public void delete(DbSession dbSession, String permission, @Nullable String groupUuid, @Nullable String rootComponentUuid, @Nullable ComponentDto componentDto) { | |||
mapper(dbSession).delete(permission, groupUuid, rootComponentUuid); | |||
if (auditPersister != null) { | |||
String projectName = (componentDto != null) ? componentDto.name() : null; | |||
auditPersister.deleteGroupPermission(dbSession, new PermissionNewValue(permission, groupUuid, rootComponentUuid, projectName, null)); | |||
} | |||
} | |||
private static GroupPermissionMapper mapper(DbSession session) { |
@@ -22,16 +22,28 @@ package org.sonar.db.permission; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Set; | |||
import javax.annotation.Nullable; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DatabaseUtils; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionNewValue; | |||
import org.sonar.db.component.ComponentDto; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.util.Collections.emptyList; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputs; | |||
public class UserPermissionDao implements Dao { | |||
private AuditPersister auditPersister; | |||
public UserPermissionDao() { | |||
} | |||
public UserPermissionDao(AuditPersister auditPersister) { | |||
this.auditPersister = auditPersister; | |||
} | |||
/** | |||
* List of user permissions ordered by alphabetical order of user names. | |||
@@ -101,8 +113,13 @@ public class UserPermissionDao implements Dao { | |||
return mapper(session).selectUserUuidsWithPermissionOnProjectBut(projectUuid, permission); | |||
} | |||
public void insert(DbSession dbSession, UserPermissionDto dto) { | |||
public void insert(DbSession dbSession, UserPermissionDto dto, @Nullable ComponentDto componentDto) { | |||
mapper(dbSession).insert(dto); | |||
if (auditPersister != null) { | |||
String projectName = (componentDto != null) ? componentDto.name() : null; | |||
auditPersister.addUserPermission(dbSession, new PermissionNewValue(dto, projectName)); | |||
} | |||
} | |||
/** | |||
@@ -110,31 +127,54 @@ public class UserPermissionDao implements Dao { | |||
*/ | |||
public void deleteGlobalPermission(DbSession dbSession, String userUuid, String permission) { | |||
mapper(dbSession).deleteGlobalPermission(userUuid, permission); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, null, | |||
null, userUuid)); | |||
} | |||
} | |||
/** | |||
* Removes a single project permission from user | |||
*/ | |||
public void deleteProjectPermission(DbSession dbSession, String userUuid, String permission, String projectUuid) { | |||
public void deleteProjectPermission(DbSession dbSession, String userUuid, String permission, String projectUuid, @Nullable String projectName) { | |||
mapper(dbSession).deleteProjectPermission(userUuid, permission, projectUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, projectUuid, projectName, userUuid)); | |||
} | |||
} | |||
/** | |||
* Deletes all the permissions defined on a project | |||
*/ | |||
public void deleteProjectPermissions(DbSession dbSession, String projectUuid) { | |||
public void deleteProjectPermissions(DbSession dbSession, String projectUuid, String projectName) { | |||
mapper(dbSession).deleteProjectPermissions(projectUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(null, null, projectUuid, projectName, null)); | |||
} | |||
} | |||
/** | |||
* Deletes the specified permission on the specified project for any user. | |||
*/ | |||
public int deleteProjectPermissionOfAnyUser(DbSession dbSession, String projectUuid, String permission) { | |||
return mapper(dbSession).deleteProjectPermissionOfAnyUser(projectUuid, permission); | |||
public int deleteProjectPermissionOfAnyUser(DbSession dbSession, String projectUuid, String permission, String projectName) { | |||
int deletedRows = mapper(dbSession).deleteProjectPermissionOfAnyUser(projectUuid, permission); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(permission, null, projectUuid, projectName, null)); | |||
} | |||
return deletedRows; | |||
} | |||
public void deleteByUserUuid(DbSession dbSession, String userUuid) { | |||
mapper(dbSession).deleteByUserUuid(userUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserPermission(dbSession, new PermissionNewValue(null, null, null, null, userUuid)); | |||
} | |||
} | |||
private static UserPermissionMapper mapper(DbSession dbSession) { |
@@ -23,12 +23,22 @@ import java.util.List; | |||
import java.util.Optional; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionTemplateNewValue; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.util.Objects.requireNonNull; | |||
import static org.sonar.db.DatabaseUtils.executeLargeInputs; | |||
public class PermissionTemplateCharacteristicDao implements Dao { | |||
private AuditPersister auditPersister; | |||
public PermissionTemplateCharacteristicDao() { | |||
} | |||
public PermissionTemplateCharacteristicDao(AuditPersister auditPersister) { | |||
this.auditPersister = auditPersister; | |||
} | |||
public List<PermissionTemplateCharacteristicDto> selectByTemplateUuids(DbSession dbSession, List<String> templateUuids) { | |||
return executeLargeInputs(templateUuids, partitionOfTemplateUuids -> mapper(dbSession).selectByTemplateUuids(partitionOfTemplateUuids)); | |||
@@ -39,15 +49,28 @@ public class PermissionTemplateCharacteristicDao implements Dao { | |||
return Optional.ofNullable(dto); | |||
} | |||
public PermissionTemplateCharacteristicDto insert(DbSession dbSession, PermissionTemplateCharacteristicDto dto) { | |||
public PermissionTemplateCharacteristicDto insert(DbSession dbSession, PermissionTemplateCharacteristicDto dto, String templateName) { | |||
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())); | |||
} | |||
return dto; | |||
} | |||
public PermissionTemplateCharacteristicDto update(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermissionDto) { | |||
public PermissionTemplateCharacteristicDto update(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermissionDto, | |||
String templateName) { | |||
requireNonNull(templatePermissionDto.getUuid()); | |||
mapper(dbSession).update(templatePermissionDto); | |||
if (auditPersister != null) { | |||
auditPersister.updateCharacteristicInPermissionTemplate(dbSession, new PermissionTemplateNewValue(templatePermissionDto.getTemplateUuid(), | |||
templatePermissionDto.getPermission(), templateName, templatePermissionDto.getWithProjectCreator())); | |||
} | |||
return templatePermissionDto; | |||
} | |||
@@ -33,6 +33,8 @@ import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionTemplateNewValue; | |||
import org.sonar.db.permission.CountPerProjectPermission; | |||
import org.sonar.db.permission.PermissionQuery; | |||
@@ -48,11 +50,18 @@ public class PermissionTemplateDao implements Dao { | |||
private final System2 system; | |||
private final UuidFactory uuidFactory; | |||
private AuditPersister auditPersister; | |||
public PermissionTemplateDao(UuidFactory uuidFactory, System2 system) { | |||
this.uuidFactory = uuidFactory; | |||
this.system = system; | |||
} | |||
public PermissionTemplateDao(UuidFactory uuidFactory, System2 system, AuditPersister auditPersister) { | |||
this(uuidFactory, system); | |||
this.auditPersister = auditPersister; | |||
} | |||
/** | |||
* @return a paginated list of user logins. | |||
*/ | |||
@@ -115,6 +124,11 @@ 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())); | |||
} | |||
return dto; | |||
} | |||
@@ -151,20 +165,28 @@ public class PermissionTemplateDao implements Dao { | |||
return mapper(dbSession).selectAllGroupPermissionTemplatesByGroupUuid(groupUuid); | |||
} | |||
public void deleteByUuid(DbSession session, String templateUuid) { | |||
public void deleteByUuid(DbSession session, String templateUuid, String templateName) { | |||
PermissionTemplateMapper mapper = mapper(session); | |||
mapper.deleteUserPermissionsByTemplateUuid(templateUuid); | |||
mapper.deleteGroupPermissionsByTemplateUuid(templateUuid); | |||
session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateUuid(templateUuid); | |||
mapper.deleteByUuid(templateUuid); | |||
if (auditPersister != null) { | |||
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)); | |||
} | |||
return permissionTemplate; | |||
} | |||
public void insertUserPermission(DbSession session, String templateUuid, String userUuid, String permission) { | |||
public void insertUserPermission(DbSession session, String templateUuid, String userUuid, String permission, | |||
String templateName, String userLogin) { | |||
PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto() | |||
.setUuid(uuidFactory.create()) | |||
.setTemplateUuid(templateUuid) | |||
@@ -174,23 +196,42 @@ public class PermissionTemplateDao implements Dao { | |||
.setUpdatedAt(now()); | |||
mapper(session).insertUserPermission(permissionTemplateUser); | |||
if (auditPersister != null) { | |||
auditPersister.addUserToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, | |||
permission, userUuid, userLogin, null, null)); | |||
} | |||
session.commit(); | |||
} | |||
public void deleteUserPermission(DbSession session, String templateUuid, String userUuid, String permission) { | |||
public void deleteUserPermission(DbSession session, String templateUuid, String userUuid, String permission, | |||
String templateName, String userLogin) { | |||
PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto() | |||
.setTemplateUuid(templateUuid) | |||
.setPermission(permission) | |||
.setUserUuid(userUuid); | |||
mapper(session).deleteUserPermission(permissionTemplateUser); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserFromPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, | |||
permission, userUuid, userLogin, null, null)); | |||
} | |||
session.commit(); | |||
} | |||
public void deleteUserPermissionsByUserUuid(DbSession dbSession, String userUuid) { | |||
public void deleteUserPermissionsByUserUuid(DbSession dbSession, String userUuid, String userLogin) { | |||
mapper(dbSession).deleteUserPermissionsByUserUuid(userUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteUserFromPermissionTemplate(dbSession, new PermissionTemplateNewValue(null, null, | |||
null, userUuid, userLogin, null, null)); | |||
} | |||
} | |||
public void insertGroupPermission(DbSession session, String templateUuid, @Nullable String groupUuid, String permission) { | |||
public void insertGroupPermission(DbSession session, String templateUuid, @Nullable String groupUuid, String permission, | |||
String templateName, @Nullable String groupName) { | |||
PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto() | |||
.setUuid(uuidFactory.create()) | |||
.setTemplateUuid(templateUuid) | |||
@@ -199,18 +240,35 @@ public class PermissionTemplateDao implements Dao { | |||
.setCreatedAt(now()) | |||
.setUpdatedAt(now()); | |||
mapper(session).insertGroupPermission(permissionTemplateGroup); | |||
if (auditPersister != null) { | |||
auditPersister.addGroupToPermissionTemplate(session, new PermissionTemplateNewValue(templateUuid, templateName, | |||
permission, null, null, groupUuid, groupName)); | |||
} | |||
} | |||
public void insertGroupPermission(DbSession session, PermissionTemplateGroupDto permissionTemplateGroup) { | |||
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())); | |||
} | |||
} | |||
public void deleteGroupPermission(DbSession session, String templateUuid, @Nullable String groupUuid, String permission) { | |||
public void deleteGroupPermission(DbSession session, String templateUuid, @Nullable String groupUuid, String permission, String templateName, | |||
@Nullable String groupName) { | |||
PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto() | |||
.setTemplateUuid(templateUuid) | |||
.setPermission(permission) | |||
.setGroupUuid(groupUuid); | |||
mapper(session).deleteGroupPermission(permissionTemplateGroup); | |||
if (auditPersister != null) { | |||
auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(permissionTemplateGroup.getTemplateUuid(), templateName, | |||
permissionTemplateGroup.getPermission(), null, null, permissionTemplateGroup.getGroupUuid(), groupName)); | |||
} | |||
session.commit(); | |||
} | |||
@@ -225,8 +283,13 @@ public class PermissionTemplateDao implements Dao { | |||
/** | |||
* Remove a group from all templates (used when removing a group) | |||
*/ | |||
public void deleteByGroup(DbSession session, String groupUuid) { | |||
public void deleteByGroup(DbSession session, String groupUuid, String groupName) { | |||
session.getMapper(PermissionTemplateMapper.class).deleteByGroupUuid(groupUuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteGroupFromPermissionTemplate(session, new PermissionTemplateNewValue(null, null, | |||
null, null, null, groupUuid, groupName)); | |||
} | |||
} | |||
private Date now() { |
@@ -23,9 +23,21 @@ import java.util.List; | |||
import java.util.Optional; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PluginNewValue; | |||
public class PluginDao implements Dao { | |||
private AuditPersister auditPersister; | |||
public PluginDao() { | |||
} | |||
public PluginDao(AuditPersister auditPersister) { | |||
this.auditPersister = auditPersister; | |||
} | |||
public List<PluginDto> selectAll(DbSession dbSession) { | |||
return mapper(dbSession).selectAll(); | |||
} | |||
@@ -36,10 +48,18 @@ 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)); | |||
} | |||
} | |||
public void update(DbSession dbSession, PluginDto dto) { | |||
mapper(dbSession).update(dto); | |||
if (auditPersister != null) { | |||
auditPersister.updatePlugin(dbSession, new PluginNewValue(dto)); | |||
} | |||
} | |||
private static PluginMapper mapper(DbSession dbSession) { |
@@ -38,6 +38,8 @@ import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.EmailSubscriberDto; | |||
import org.sonar.db.MyBatis; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PropertyNewValue; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static org.apache.commons.lang.StringUtils.repeat; | |||
@@ -52,7 +54,8 @@ public class PropertiesDao implements Dao { | |||
private final MyBatis mybatis; | |||
private final System2 system2; | |||
private UuidFactory uuidFactory; | |||
private final UuidFactory uuidFactory; | |||
private AuditPersister auditPersister; | |||
public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory) { | |||
this.mybatis = mybatis; | |||
@@ -60,6 +63,11 @@ public class PropertiesDao implements Dao { | |||
this.uuidFactory = uuidFactory; | |||
} | |||
public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) { | |||
this(mybatis, system2, uuidFactory); | |||
this.auditPersister = auditPersister; | |||
} | |||
/** | |||
* Returns the logins of users who have subscribed to the given notification dispatcher with the given notification channel. | |||
* If a resource ID is passed, the search is made on users who have specifically subscribed for the given resource. | |||
@@ -101,9 +109,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); | |||
@@ -202,8 +210,12 @@ public class PropertiesDao implements Dao { | |||
* | |||
* @throws IllegalArgumentException if {@link PropertyDto#getKey()} is {@code null} or empty | |||
*/ | |||
public void saveProperty(DbSession session, PropertyDto property) { | |||
public void saveProperty(DbSession session, PropertyDto property, @Nullable String userLogin, @Nullable String projectName) { | |||
save(getMapper(session), property.getKey(), property.getUserUuid(), property.getComponentUuid(), property.getValue()); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(property.getKey())) { | |||
auditPersister.addProperty(session, new PropertyNewValue(property, userLogin, projectName), false); | |||
} | |||
} | |||
private void save(PropertiesMapper mapper, String key, @Nullable String userUuid, @Nullable String componentUuid, @Nullable String value) { | |||
@@ -235,7 +247,7 @@ public class PropertiesDao implements Dao { | |||
public void saveProperty(PropertyDto property) { | |||
try (DbSession session = mybatis.openSession(false)) { | |||
saveProperty(session, property); | |||
saveProperty(session, property, null, null); | |||
session.commit(); | |||
} | |||
} | |||
@@ -248,26 +260,46 @@ public class PropertiesDao implements Dao { | |||
* Used by Governance. | |||
*/ | |||
public int deleteByQuery(DbSession dbSession, PropertyQuery query) { | |||
return getMapper(dbSession).deleteByQuery(query); | |||
int deletedRows = getMapper(dbSession).deleteByQuery(query); | |||
if (auditPersister != null && query.key() != null && auditPersister.isTrackedProperty(query.key())) { | |||
auditPersister.deleteProperty(dbSession, new PropertyNewValue(query.key(), query.componentUuid(), | |||
null, query.userUuid()), false); | |||
} | |||
return deletedRows; | |||
} | |||
public int delete(DbSession dbSession, PropertyDto dto) { | |||
return getMapper(dbSession).delete(dto.getKey(), dto.getUserUuid(), dto.getComponentUuid()); | |||
public int delete(DbSession dbSession, PropertyDto dto, @Nullable String userLogin, @Nullable String projectName) { | |||
int deletedRows = getMapper(dbSession).delete(dto.getKey(), dto.getUserUuid(), dto.getComponentUuid()); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) { | |||
auditPersister.deleteProperty(dbSession, new PropertyNewValue(dto, userLogin, projectName), false); | |||
} | |||
return deletedRows; | |||
} | |||
public void deleteProjectProperty(String key, String projectUuid) { | |||
public void deleteProjectProperty(String key, String projectUuid, String projectName) { | |||
try (DbSession session = mybatis.openSession(false)) { | |||
deleteProjectProperty(key, projectUuid, session); | |||
deleteProjectProperty(key, projectUuid, session, projectName); | |||
session.commit(); | |||
} | |||
} | |||
public void deleteProjectProperty(String key, String projectUuid, DbSession session) { | |||
public void deleteProjectProperty(String key, String projectUuid, DbSession session, String projectName) { | |||
getMapper(session).deleteProjectProperty(key, projectUuid); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(key)) { | |||
auditPersister.deleteProperty(session, new PropertyNewValue(key, projectUuid, projectName, null), false); | |||
} | |||
} | |||
public void deleteProjectProperties(String key, String value, DbSession session) { | |||
getMapper(session).deleteProjectProperties(key, value); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(key)) { | |||
auditPersister.deleteProperty(session, new PropertyNewValue(key, value), false); | |||
} | |||
} | |||
public void deleteProjectProperties(String key, String value) { | |||
@@ -279,6 +311,10 @@ public class PropertiesDao implements Dao { | |||
public void deleteGlobalProperty(String key, DbSession session) { | |||
getMapper(session).deleteGlobalProperty(key); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(key)) { | |||
auditPersister.deleteProperty(session, new PropertyNewValue(key), false); | |||
} | |||
} | |||
public void deleteGlobalProperty(String key) { | |||
@@ -288,18 +324,28 @@ public class PropertiesDao implements Dao { | |||
} | |||
} | |||
public void deleteByUser(DbSession dbSession, String userUuid) { | |||
public void deleteByUser(DbSession dbSession, String userUuid, String userLogin) { | |||
List<String> uuids = getMapper(dbSession).selectUuidsByUser(userUuid); | |||
persistDeletedProperties(dbSession, userUuid, userLogin, uuids); | |||
executeLargeInputsWithoutOutput(uuids, subList -> getMapper(dbSession).deleteByUuids(subList)); | |||
} | |||
public void deleteByMatchingLogin(DbSession dbSession, String login, List<String> propertyKeys) { | |||
List<String> uuids = getMapper(dbSession).selectIdsByMatchingLogin(login, propertyKeys); | |||
persistDeletedProperties(dbSession, null, login, uuids); | |||
executeLargeInputsWithoutOutput(uuids, list -> getMapper(dbSession).deleteByUuids(list)); | |||
} | |||
public void deleteByKeyAndValue(DbSession dbSession, String key, String value) { | |||
getMapper(dbSession).deleteByKeyAndValue(key, value); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(key)) { | |||
auditPersister.deleteProperty(dbSession, new PropertyNewValue(key, value), false); | |||
} | |||
} | |||
public void saveGlobalProperties(Map<String, String> properties) { | |||
@@ -308,6 +354,10 @@ public class PropertiesDao implements Dao { | |||
properties.forEach((key, value) -> { | |||
mapper.deleteGlobalProperty(key); | |||
save(mapper, key, null, null, value); | |||
if (auditPersister != null && auditPersister.isTrackedProperty(key)) { | |||
auditPersister.addProperty(session, new PropertyNewValue(key, value), false); | |||
} | |||
}); | |||
session.commit(); | |||
} | |||
@@ -329,4 +379,15 @@ public class PropertiesDao implements Dao { | |||
return session.getMapper(PropertiesMapper.class); | |||
} | |||
private void persistDeletedProperties(DbSession dbSession, @Nullable String userUuid, String userLogin, List<String> uuids) { | |||
if (auditPersister != null) { | |||
List<PropertyDto> properties = executeLargeInputs(uuids, subList -> getMapper(dbSession).selectByUuids(subList)); | |||
properties | |||
.stream() | |||
.filter(p -> auditPersister.isTrackedProperty(p.getKey())) | |||
.forEach(p -> auditPersister.deleteProperty(dbSession, | |||
new PropertyNewValue(p.getKey(), userUuid, userLogin), false)); | |||
} | |||
} | |||
} |
@@ -77,6 +77,8 @@ public interface PropertiesMapper { | |||
void deleteByKeyAndValue(@Param("key") String key, @Param("value") String value); | |||
List<PropertyDto> selectByUuids(@Param("uuids") List<String> uuids); | |||
int renamePropertyKey(@Param("oldKey") String oldKey, @Param("newKey") String newKey); | |||
} |
@@ -59,9 +59,9 @@ public class UserPropertiesDao implements Dao { | |||
if (auditPersister != null && auditPersister.isTrackedProperty(dto.getKey())) { | |||
if (isUpdate) { | |||
auditPersister.updateUserProperty(session, new PropertyNewValue(dto, login)); | |||
auditPersister.updateProperty(session, new PropertyNewValue(dto, login), true); | |||
} else { | |||
auditPersister.addUserProperty(session, new PropertyNewValue(dto, login)); | |||
auditPersister.addProperty(session, new PropertyNewValue(dto, login), true); | |||
} | |||
} | |||
@@ -75,7 +75,7 @@ public class UserPropertiesDao implements Dao { | |||
if (auditPersister != null) { | |||
userProperties.stream() | |||
.filter(p -> auditPersister.isTrackedProperty(p.getKey())) | |||
.forEach(p -> auditPersister.deleteUserProperty(session, new PropertyNewValue(p, user.getLogin()))); | |||
.forEach(p -> auditPersister.deleteProperty(session, new PropertyNewValue(p, user.getLogin()), true)); | |||
} | |||
} | |||
@@ -21,19 +21,29 @@ package org.sonar.db.webhook; | |||
import java.util.List; | |||
import java.util.Optional; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.WebhookNewValue; | |||
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; | |||
} | |||
public WebhookDao(System2 system2, AuditPersister auditPersister) { | |||
this.system2 = system2; | |||
this.auditPersister = auditPersister; | |||
} | |||
public List<WebhookDto> selectGlobalWebhooks(DbSession dbSession) { | |||
return mapper(dbSession).selectGlobalWebhooksOrderedByName(); | |||
} | |||
@@ -46,20 +56,37 @@ public class WebhookDao implements Dao { | |||
return mapper(dbSession).selectForProjectUuidOrderedByName(projectDto.getUuid()); | |||
} | |||
public void insert(DbSession dbSession, WebhookDto dto) { | |||
public void insert(DbSession dbSession, WebhookDto dto, @Nullable String projectName) { | |||
mapper(dbSession).insert(dto.setCreatedAt(system2.now()).setUpdatedAt(system2.now())); | |||
if (auditPersister != null) { | |||
auditPersister.addWebhook(dbSession, new WebhookNewValue(dto.getUuid(), dto.getName(), | |||
dto.getProjectUuid(), projectName)); | |||
} | |||
} | |||
public void update(DbSession dbSession, WebhookDto dto) { | |||
public void update(DbSession dbSession, WebhookDto dto, @Nullable String projectName) { | |||
mapper(dbSession).update(dto.setUpdatedAt(system2.now())); | |||
if (auditPersister != null) { | |||
auditPersister.updateWebhook(dbSession, new WebhookNewValue(dto, projectName)); | |||
} | |||
} | |||
public void delete(DbSession dbSession, String uuid) { | |||
public void delete(DbSession dbSession, String uuid, String webhookName) { | |||
mapper(dbSession).delete(uuid); | |||
if (auditPersister != null) { | |||
auditPersister.deleteWebhook(dbSession, new WebhookNewValue(uuid, webhookName)); | |||
} | |||
} | |||
public void deleteByProject(DbSession dbSession, ProjectDto projectDto) { | |||
mapper(dbSession).deleteForProjectUuid(projectDto.getUuid()); | |||
if (auditPersister != null) { | |||
auditPersister.deleteWebhook(dbSession, new WebhookNewValue(projectDto)); | |||
} | |||
} | |||
private static WebhookMapper mapper(DbSession dbSession) { |
@@ -381,6 +381,18 @@ | |||
</foreach> | |||
</delete> | |||
<select id="selectByUuids" parameterType="map" resultType="ScrapProperty"> | |||
select | |||
<include refid="columnsToScrapPropertyDto"/> | |||
from | |||
properties p | |||
where | |||
uuid in | |||
<foreach collection="uuids" open="(" close=")" item="uuid" separator=","> | |||
#{uuid} | |||
</foreach> | |||
</select> | |||
<update id="renamePropertyKey" parameterType="map"> | |||
update properties set | |||
prop_key=#{newKey} |
@@ -104,7 +104,7 @@ CREATE TABLE "AUDITS"( | |||
"UUID" VARCHAR(40) NOT NULL, | |||
"USER_UUID" VARCHAR(40) NOT NULL, | |||
"USER_LOGIN" VARCHAR(255) NOT NULL, | |||
"CATEGORY" VARCHAR(20) NOT NULL, | |||
"CATEGORY" VARCHAR(25) NOT NULL, | |||
"OPERATION" VARCHAR(50) NOT NULL, | |||
"NEW_VALUE" VARCHAR(4000), | |||
"CREATED_AT" BIGINT NOT NULL |
@@ -55,7 +55,7 @@ public class AlmPatDaoTest { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmPatDto almPatDto = newAlmPatDto(); | |||
underTest.insert(dbSession, almPatDto); | |||
underTest.insert(dbSession, almPatDto, null, null); | |||
assertThat(underTest.selectByUuid(dbSession, A_UUID).get()) | |||
.extracting(AlmPatDto::getUuid, AlmPatDto::getPersonalAccessToken, | |||
@@ -79,7 +79,7 @@ public class AlmPatDaoTest { | |||
String userUuid = randomAlphanumeric(40); | |||
almPatDto.setUserUuid(userUuid); | |||
underTest.insert(dbSession, almPatDto); | |||
underTest.insert(dbSession, almPatDto, null, null); | |||
assertThat(underTest.selectByUserAndAlmSetting(dbSession, userUuid, almSetting).get()) | |||
.extracting(AlmPatDto::getUuid, AlmPatDto::getPersonalAccessToken, | |||
@@ -95,13 +95,13 @@ public class AlmPatDaoTest { | |||
public void update() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmPatDto almPatDto = newAlmPatDto(); | |||
underTest.insert(dbSession, almPatDto); | |||
underTest.insert(dbSession, almPatDto, null, null); | |||
String updated_pat = "updated pat"; | |||
almPatDto.setPersonalAccessToken(updated_pat); | |||
system2.setNow(NOW + 1); | |||
underTest.update(dbSession, almPatDto); | |||
underTest.update(dbSession, almPatDto, null, null); | |||
AlmPatDto result = underTest.selectByUuid(dbSession, A_UUID).get(); | |||
assertThat(result) | |||
@@ -117,9 +117,9 @@ public class AlmPatDaoTest { | |||
public void delete() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
underTest.insert(dbSession, almPat); | |||
underTest.insert(dbSession, almPat, null, null); | |||
underTest.delete(dbSession, almPat); | |||
underTest.delete(dbSession, almPat, null, null); | |||
assertThat(underTest.selectByUuid(dbSession, almPat.getUuid())).isNotPresent(); | |||
} | |||
@@ -130,7 +130,7 @@ public class AlmPatDaoTest { | |||
UserDto userDto = db.users().insertUser(); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
almPat.setUserUuid(userDto.getUuid()); | |||
underTest.insert(dbSession, almPat); | |||
underTest.insert(dbSession, almPat, userDto.getLogin(), null); | |||
underTest.deleteByUser(dbSession, userDto); | |||
@@ -143,7 +143,7 @@ public class AlmPatDaoTest { | |||
AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
almPat.setAlmSettingUuid(almSettingDto.getUuid()); | |||
underTest.insert(dbSession, almPat); | |||
underTest.insert(dbSession, almPat, null, null); | |||
underTest.deleteByAlmSetting(dbSession, almSettingDto); | |||
@@ -0,0 +1,142 @@ | |||
/* | |||
* 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.alm.pat; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
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.alm.setting.AlmSettingDto; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PersonalAccessTokenNewValue; | |||
import org.sonar.db.user.UserDto; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto; | |||
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); | |||
@Rule | |||
public DbTester db = DbTester.create(system2, auditPersister); | |||
private DbSession dbSession = db.getSession(); | |||
private UuidFactory uuidFactory = mock(UuidFactory.class); | |||
private AlmPatDao underTest = db.getDbClient().almPatDao(); | |||
@Test | |||
public void insertAndUpdateArePersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmPatDto almPatDto = newAlmPatDto(); | |||
underTest.insert(dbSession, almPatDto, "user-login", "alm-key"); | |||
verify(auditPersister).addPersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
PersonalAccessTokenNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PersonalAccessTokenNewValue::getPatUuid, PersonalAccessTokenNewValue::getUserUuid, | |||
PersonalAccessTokenNewValue::getAlmSettingUuid, PersonalAccessTokenNewValue::getUserLogin, | |||
PersonalAccessTokenNewValue::getAlmSettingKey) | |||
.containsExactly(almPatDto.getUuid(), almPatDto.getUserUuid(), almPatDto.getAlmSettingUuid(), | |||
"user-login", "alm-key"); | |||
assertThat(newValue.toString()).contains("userLogin"); | |||
String updated_pat = "updated pat"; | |||
almPatDto.setPersonalAccessToken(updated_pat); | |||
system2.setNow(NOW + 1); | |||
underTest.update(dbSession, almPatDto, null, null); | |||
verify(auditPersister).updatePersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
} | |||
@Test | |||
public void deleteIsPersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
underTest.insert(dbSession, almPat, null, null); | |||
verify(auditPersister).addPersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
underTest.delete(dbSession, almPat, null, null); | |||
verify(auditPersister).deletePersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
PersonalAccessTokenNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PersonalAccessTokenNewValue::getPatUuid, PersonalAccessTokenNewValue::getUserUuid, | |||
PersonalAccessTokenNewValue::getAlmSettingUuid, PersonalAccessTokenNewValue::getUserLogin, | |||
PersonalAccessTokenNewValue::getAlmSettingKey) | |||
.containsExactly(almPat.getUuid(), almPat.getUserUuid(), almPat.getAlmSettingUuid(), | |||
null, null); | |||
assertThat(newValue.toString()).doesNotContain("userLogin"); | |||
} | |||
@Test | |||
public void deleteByUserIsPersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
UserDto userDto = db.users().insertUser(); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
almPat.setUserUuid(userDto.getUuid()); | |||
underTest.insert(dbSession, almPat, userDto.getLogin(), null); | |||
verify(auditPersister).addPersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
underTest.deleteByUser(dbSession, userDto); | |||
verify(auditPersister).deletePersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
PersonalAccessTokenNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PersonalAccessTokenNewValue::getPatUuid, PersonalAccessTokenNewValue::getUserUuid, | |||
PersonalAccessTokenNewValue::getUserLogin, PersonalAccessTokenNewValue::getAlmSettingKey) | |||
.containsExactly(null, userDto.getUuid(), userDto.getLogin(), null); | |||
assertThat(newValue.toString()).doesNotContain("patUuid"); | |||
} | |||
@Test | |||
public void deleteByAlmSettingIsPersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); | |||
AlmPatDto almPat = newAlmPatDto(); | |||
almPat.setAlmSettingUuid(almSettingDto.getUuid()); | |||
underTest.insert(dbSession, almPat, null, almSettingDto.getKey()); | |||
verify(auditPersister).addPersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
underTest.deleteByAlmSetting(dbSession, almSettingDto); | |||
verify(auditPersister).deletePersonalAccessToken(eq(dbSession), newValueCaptor.capture()); | |||
PersonalAccessTokenNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PersonalAccessTokenNewValue::getPatUuid, PersonalAccessTokenNewValue::getAlmSettingUuid, | |||
PersonalAccessTokenNewValue::getAlmSettingKey) | |||
.containsExactly(null, almPat.getAlmSettingUuid(), almSettingDto.getKey()); | |||
assertThat(newValue.toString()).doesNotContain("userUuid"); | |||
} | |||
} |
@@ -0,0 +1,103 @@ | |||
/* | |||
* 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.alm.setting; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
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.AuditPersister; | |||
import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto; | |||
public class AlmSettingDaoWithPersisterTest { | |||
private static final long NOW = 1000000L; | |||
private static final String A_UUID = "SOME_UUID"; | |||
private final ArgumentCaptor<DevOpsPlatformSettingNewValue> newValueCaptor = ArgumentCaptor.forClass(DevOpsPlatformSettingNewValue.class); | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
private final TestSystem2 system2 = new TestSystem2().setNow(NOW); | |||
@Rule | |||
public final DbTester db = DbTester.create(system2, auditPersister); | |||
private final DbSession dbSession = db.getSession(); | |||
private final UuidFactory uuidFactory = mock(UuidFactory.class); | |||
private final AlmSettingDao underTest = db.getDbClient().almSettingDao(); | |||
@Test | |||
public void insertAndUpdateArePersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmSettingDto almSettingDto = newGithubAlmSettingDto(); | |||
underTest.insert(dbSession, almSettingDto); | |||
verify(auditPersister).addDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey) | |||
.containsExactly(almSettingDto.getUuid(), almSettingDto.getKey()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
almSettingDto.setPrivateKey("updated private key"); | |||
almSettingDto.setAppId("updated app id"); | |||
almSettingDto.setUrl("updated url"); | |||
almSettingDto.setPersonalAccessToken("updated pat"); | |||
almSettingDto.setKey("updated key"); | |||
system2.setNow(NOW + 1); | |||
underTest.update(dbSession, almSettingDto); | |||
verify(auditPersister).updateDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey, | |||
DevOpsPlatformSettingNewValue::getAppId, DevOpsPlatformSettingNewValue::getDevOpsPlatformName, | |||
DevOpsPlatformSettingNewValue::getUrl, DevOpsPlatformSettingNewValue::getClientId) | |||
.containsExactly(almSettingDto.getUuid(), almSettingDto.getKey(), almSettingDto.getAppId(), almSettingDto.getAppId(), | |||
almSettingDto.getUrl(), almSettingDto.getClientId()); | |||
} | |||
@Test | |||
public void deleteIsPersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmSettingDto almSettingDto = newGithubAlmSettingDto(); | |||
underTest.insert(dbSession, almSettingDto); | |||
underTest.delete(dbSession, almSettingDto); | |||
verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey) | |||
.containsExactly(almSettingDto.getUuid(), almSettingDto.getKey()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
} |
@@ -57,7 +57,7 @@ public class ProjectAlmSettingDaoTest { | |||
ProjectDto project = db.components().insertPrivateProjectDto(); | |||
ProjectDto anotherProject = db.components().insertPrivateProjectDto(); | |||
ProjectAlmSettingDto githubProjectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSettingDto, project); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto, githubAlmSettingDto.getKey(), anotherProject.getName()); | |||
assertThat(underTest.selectByProject(dbSession, project).get()) | |||
.extracting(ProjectAlmSettingDto::getUuid, ProjectAlmSettingDto::getAlmSettingUuid, ProjectAlmSettingDto::getProjectUuid, | |||
@@ -78,11 +78,11 @@ public class ProjectAlmSettingDaoTest { | |||
ProjectDto project = db.components().insertPrivateProjectDto(); | |||
ProjectAlmSettingDto bitbucketProjectAlmSettingDto = newBitbucketProjectAlmSettingDto(almSettingsDto, project); | |||
bitbucketProjectAlmSettingDto.setAlmSlug("slug1"); | |||
underTest.insertOrUpdate(dbSession, bitbucketProjectAlmSettingDto); | |||
underTest.insertOrUpdate(dbSession, bitbucketProjectAlmSettingDto, almSettingsDto.getKey(), project.getName()); | |||
ProjectAlmSettingDto bitbucketProjectAlmSettingDto2 = newBitbucketProjectAlmSettingDto(almSettingsDto, db.components().insertPrivateProjectDto()); | |||
bitbucketProjectAlmSettingDto2.setAlmSlug("slug2"); | |||
when(uuidFactory.create()).thenReturn(A_UUID + 1); | |||
underTest.insertOrUpdate(dbSession, bitbucketProjectAlmSettingDto2); | |||
underTest.insertOrUpdate(dbSession, bitbucketProjectAlmSettingDto2, almSettingsDto.getKey(), project.getName()); | |||
Set<String> slugs = new HashSet<>(); | |||
slugs.add("slug1"); | |||
@@ -106,11 +106,11 @@ public class ProjectAlmSettingDaoTest { | |||
ProjectDto project = db.components().insertPrivateProjectDto(); | |||
ProjectAlmSettingDto githubProjectAlmSettingDto = newGithubProjectAlmSettingDto(almSettingsDto, project); | |||
githubProjectAlmSettingDto.setAlmRepo("repo1"); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto, almSettingsDto.getKey(), project.getName()); | |||
ProjectAlmSettingDto githubProjectAlmSettingDto2 = newGithubProjectAlmSettingDto(almSettingsDto, db.components().insertPrivateProjectDto()); | |||
githubProjectAlmSettingDto2.setAlmRepo("repo2"); | |||
when(uuidFactory.create()).thenReturn(A_UUID + 1); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto2); | |||
underTest.insertOrUpdate(dbSession, githubProjectAlmSettingDto2, almSettingsDto.getKey(), project.getName()); | |||
Set<String> repos = new HashSet<>(); | |||
repos.add("repo1"); | |||
@@ -138,7 +138,7 @@ public class ProjectAlmSettingDaoTest { | |||
system2.setNow(A_DATE_LATER); | |||
ProjectAlmSettingDto newProjectAlmSettingDto = newGithubProjectAlmSettingDto(anotherGithubAlmSetting, project) | |||
.setSummaryCommentEnabled(false); | |||
underTest.insertOrUpdate(dbSession, newProjectAlmSettingDto); | |||
underTest.insertOrUpdate(dbSession, newProjectAlmSettingDto, githubAlmSetting.getKey(), project.getName()); | |||
assertThat(underTest.selectByProject(dbSession, project).get()) | |||
.extracting(ProjectAlmSettingDto::getUuid, ProjectAlmSettingDto::getAlmSettingUuid, ProjectAlmSettingDto::getProjectUuid, |
@@ -0,0 +1,132 @@ | |||
/* | |||
* 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.alm.setting; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
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.AuditPersister; | |||
import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue; | |||
import org.sonar.db.project.ProjectDto; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoInteractions; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto; | |||
public class ProjectAlmSettingDaoWithPersisterTest { | |||
private static final long A_DATE = 1_000_000_000_000L; | |||
private static final long A_DATE_LATER = 1_700_000_000_000L; | |||
private static final String A_UUID = "SOME_UUID"; | |||
private final ArgumentCaptor<DevOpsPlatformSettingNewValue> newValueCaptor = ArgumentCaptor.forClass(DevOpsPlatformSettingNewValue.class); | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
private final TestSystem2 system2 = new TestSystem2().setNow(A_DATE); | |||
@Rule | |||
public final DbTester db = DbTester.create(system2, auditPersister); | |||
private final DbSession dbSession = db.getSession(); | |||
private final UuidFactory uuidFactory = mock(UuidFactory.class); | |||
private final ProjectAlmSettingDao underTest = db.getDbClient().projectAlmSettingDao(); | |||
@Test | |||
public void insertAndUpdateExistingBindingArePersisted() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); | |||
ProjectDto project = db.components().insertPrivateProjectDto(); | |||
ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project) | |||
.setSummaryCommentEnabled(false); | |||
underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName()); | |||
verify(auditPersister).addDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey, | |||
DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName) | |||
.containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey(), project.getUuid(), project.getName()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
AlmSettingDto anotherGithubAlmSetting = db.almSettings().insertGitHubAlmSetting(); | |||
system2.setNow(A_DATE_LATER); | |||
ProjectAlmSettingDto newProjectAlmSettingDto = newGithubProjectAlmSettingDto(anotherGithubAlmSetting, project) | |||
.setSummaryCommentEnabled(false); | |||
underTest.insertOrUpdate(dbSession, newProjectAlmSettingDto, anotherGithubAlmSetting.getKey(), project.getName()); | |||
verify(auditPersister).updateDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey, | |||
DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName, | |||
DevOpsPlatformSettingNewValue::getAlmRepo, DevOpsPlatformSettingNewValue::getAlmSlug, | |||
DevOpsPlatformSettingNewValue::isSummaryCommentEnabled, DevOpsPlatformSettingNewValue::isMonorepo) | |||
.containsExactly(anotherGithubAlmSetting.getUuid(), anotherGithubAlmSetting.getKey(), project.getUuid(), project.getName(), | |||
newProjectAlmSettingDto.getAlmRepo(), newProjectAlmSettingDto.getAlmSlug(), | |||
newProjectAlmSettingDto.getSummaryCommentEnabled(), newProjectAlmSettingDto.getMonorepo()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
@Test | |||
public void deleteByProject() { | |||
when(uuidFactory.create()).thenReturn(A_UUID); | |||
AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); | |||
ProjectDto project = db.components().insertPrivateProjectDto(); | |||
ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project) | |||
.setSummaryCommentEnabled(false); | |||
underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName()); | |||
underTest.deleteByProject(dbSession, project); | |||
verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName) | |||
.containsExactly(project.getUuid(), project.getName()); | |||
assertThat(newValue.toString()).doesNotContain("devOpsPlatformSettingUuid"); | |||
} | |||
@Test | |||
public void deleteByAlmSettingWhenNotTracked() { | |||
AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); | |||
underTest.deleteByAlmSetting(dbSession, githubAlmSetting); | |||
verifyNoInteractions(auditPersister); | |||
} | |||
@Test | |||
public void deleteByAlmSettingWhenTracked() { | |||
AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); | |||
underTest.deleteByAlmSetting(dbSession, githubAlmSetting, true); | |||
verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture()); | |||
DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey) | |||
.containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
} |
@@ -0,0 +1,39 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2021 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.audit.model; | |||
import org.junit.Test; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
public class LicenseNewValueTest { | |||
@Test | |||
public void toStringIsEmptyForNullEdition(){ | |||
LicenseNewValue newValue = new LicenseNewValue(null); | |||
assertThat(newValue.toString()).isEqualTo("{}"); | |||
} | |||
@Test | |||
public void toStringHasEdition(){ | |||
LicenseNewValue newValue = new LicenseNewValue("Developer"); | |||
assertThat(newValue.toString()).contains("edition"); | |||
} | |||
} |
@@ -34,7 +34,6 @@ import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.component.ComponentDto; | |||
import org.sonar.db.component.ComponentTesting; | |||
import org.sonar.db.user.GroupDto; | |||
import static java.util.Arrays.asList; | |||
@@ -562,7 +561,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group1, "perm2", project1); | |||
db.users().insertProjectPermissionOnGroup(group2, "perm3", project2); | |||
underTest.deleteByRootComponentUuid(dbSession, project1.uuid()); | |||
underTest.deleteByRootComponentUuid(dbSession, project1.uuid(), project1.name()); | |||
dbSession.commit(); | |||
assertThat(db.countSql("select count(uuid) from group_roles where component_uuid='" + project1.uuid() + "'")).isZero(); | |||
@@ -581,7 +580,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnAnyone("perm4", project1); | |||
db.users().insertProjectPermissionOnAnyone("perm5", project2); | |||
underTest.deleteByRootComponentUuid(dbSession, project1.uuid()); | |||
underTest.deleteByRootComponentUuid(dbSession, project1.uuid(), project1.name()); | |||
dbSession.commit(); | |||
assertThat(db.countSql("select count(uuid) from group_roles where component_uuid='" + project1.uuid() + "'")).isZero(); | |||
@@ -597,7 +596,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
db.users().insertProjectPermissionOnAnyone("perm4", project1); | |||
underTest.delete(dbSession, "perm2", group1.getUuid(), null); | |||
underTest.delete(dbSession, "perm2", group1.getUuid(), null, project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm2"); | |||
@@ -612,7 +611,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnGroup(group1, "perm2"); | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
underTest.delete(dbSession, "perm2", group1.getUuid(), null); | |||
underTest.delete(dbSession, "perm2", group1.getUuid(), null, project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm2"); | |||
@@ -628,7 +627,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
db.users().insertProjectPermissionOnAnyone("perm4", project1); | |||
underTest.delete(dbSession, "perm1", null, null); | |||
underTest.delete(dbSession, "perm1", null, null, project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm1"); | |||
@@ -643,7 +642,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnGroup(group1, "perm2"); | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
underTest.delete(dbSession, "perm3", group1.getUuid(), project1.uuid()); | |||
underTest.delete(dbSession, "perm3", group1.getUuid(), project1.uuid(), project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm3"); | |||
@@ -659,7 +658,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
db.users().insertProjectPermissionOnAnyone("perm4", project1); | |||
underTest.delete(dbSession, "perm3", group1.getUuid(), project1.uuid()); | |||
underTest.delete(dbSession, "perm3", group1.getUuid(), project1.uuid(), project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm3"); | |||
@@ -675,7 +674,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group1, "perm3", project1); | |||
db.users().insertProjectPermissionOnAnyone("perm4", project1); | |||
underTest.delete(dbSession, "perm4", null, project1.uuid()); | |||
underTest.delete(dbSession, "perm4", null, project1.uuid(), project1); | |||
dbSession.commit(); | |||
assertThatNoPermission("perm4"); | |||
@@ -699,7 +698,7 @@ public class GroupPermissionDaoTest { | |||
assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, group.getUuid())) | |||
.containsOnly("p4"); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null, project.name()); | |||
assertThat(deletedCount).isEqualTo(1); | |||
assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, null, project.uuid())) | |||
@@ -737,7 +736,7 @@ public class GroupPermissionDaoTest { | |||
assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, group2.getUuid())) | |||
.containsOnly("p7"); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group1.getUuid()); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group1.getUuid(), project.name()); | |||
assertThat(deletedCount).isEqualTo(1); | |||
assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, null, project.uuid())) | |||
@@ -751,7 +750,7 @@ public class GroupPermissionDaoTest { | |||
assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, group2.getUuid())) | |||
.containsOnly("p7"); | |||
deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group2.getUuid()); | |||
deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group2.getUuid(), project.name()); | |||
assertThat(deletedCount).isEqualTo(2); | |||
assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, null, project.uuid())) | |||
@@ -770,8 +769,8 @@ public class GroupPermissionDaoTest { | |||
public void deleteByRootComponentUuidAndGroupUuid_has_no_effect_if_component_does_not_exist() { | |||
GroupDto group = db.users().insertGroup(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, "1234", null)).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, "1234", group.getUuid())).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, "1234", null, "")).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, "1234", group.getUuid(), "")).isZero(); | |||
} | |||
@Test | |||
@@ -779,15 +778,15 @@ public class GroupPermissionDaoTest { | |||
ComponentDto project = randomPublicOrPrivateProject(); | |||
GroupDto group = db.users().insertGroup(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null)).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group.getUuid())).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null, project.name())).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group.getUuid(), project.name())).isZero(); | |||
} | |||
@Test | |||
public void deleteByRootComponentUuidAndGroupUuid_has_no_effect_if_group_does_not_exist() { | |||
ComponentDto project = randomPublicOrPrivateProject(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), "5678")).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), "5678", project.name())).isZero(); | |||
} | |||
@Test | |||
@@ -802,7 +801,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnAnyone("p2"); | |||
db.users().insertPermissionOnGroup(group1, "p3"); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), null, project.name()); | |||
assertThat(deletedCount).isZero(); | |||
assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, null, project.uuid())) | |||
@@ -824,7 +823,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnAnyone("p2"); | |||
db.users().insertPermissionOnGroup(group1, "p3"); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group2.getUuid()); | |||
int deletedCount = underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group2.getUuid(), project.name()); | |||
assertThat(deletedCount).isZero(); | |||
assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, group1.getUuid(), project.uuid())) | |||
@@ -852,7 +851,7 @@ public class GroupPermissionDaoTest { | |||
assertThat(getProjectPermissionsForAnyOne(project)).containsOnly("p1", "p2"); | |||
assertThat(getProjectPermissionsForGroup(project, group)).containsOnly("p1", "p2"); | |||
int deletedRows = underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1"); | |||
int deletedRows = underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1", project.name()); | |||
assertThat(deletedRows).isEqualTo(2); | |||
assertThat(getGlobalPermissionsForAnyone()).containsOnly("p1", "p2"); | |||
@@ -860,7 +859,7 @@ public class GroupPermissionDaoTest { | |||
assertThat(getProjectPermissionsForAnyOne(project)).containsOnly("p2"); | |||
assertThat(getProjectPermissionsForGroup(project, group)).containsOnly("p2"); | |||
deletedRows = underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p2"); | |||
deletedRows = underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p2", project.name()); | |||
assertThat(deletedRows).isEqualTo(2); | |||
assertThat(getGlobalPermissionsForAnyone()).containsOnly("p1", "p2"); | |||
@@ -876,7 +875,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnAnyone("p1"); | |||
db.users().insertPermissionOnGroup(group, "p1"); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1")).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1", project.name())).isZero(); | |||
assertThat(getGlobalPermissionsForAnyone()).containsOnly("p1"); | |||
assertThat(getGlobalPermissionsForGroup(group)).containsOnly("p1"); | |||
@@ -893,7 +892,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertProjectPermissionOnGroup(group, "p1", project); | |||
db.users().insertProjectPermissionOnAnyone("p1", project); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, "1324", "p1")).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, "1324", "p1", "")).isZero(); | |||
assertThat(getGlobalPermissionsForAnyone()).containsOnly("p1"); | |||
assertThat(getGlobalPermissionsForGroup(group)).containsOnly("p1"); | |||
@@ -908,7 +907,7 @@ public class GroupPermissionDaoTest { | |||
db.users().insertPermissionOnAnyone("p1"); | |||
db.users().insertPermissionOnGroup(group, "p1"); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1")).isZero(); | |||
assertThat(underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), "p1", project.name())).isZero(); | |||
} | |||
private Collection<String> getGlobalPermissionsForAnyone() { |
@@ -0,0 +1,163 @@ | |||
/* | |||
* 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.permission; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.Uuids; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionNewValue; | |||
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.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
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); | |||
private final ArgumentCaptor<PermissionNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionNewValue.class); | |||
private final DbSession dbSession = db.getSession(); | |||
private final GroupPermissionDao underTest = db.getDbClient().groupPermissionDao(); | |||
@Test | |||
public void groupGlobalPermissionInsertAndDeleteArePersisted() { | |||
GroupDto group = db.users().insertGroup(); | |||
GroupPermissionDto dto = new GroupPermissionDto() | |||
.setUuid(Uuids.createFast()) | |||
.setGroupUuid(group.getUuid()) | |||
.setRole(ADMIN); | |||
underTest.insert(dbSession, dto, null); | |||
verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), group.getUuid(), null, dto.getRole(), null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
underTest.delete(dbSession, ADMIN, group.getUuid(), null, null); | |||
verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, group.getUuid(), null, ADMIN, null); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
@Test | |||
public void groupProjectPermissionDeleteByComponentIsPersisted() { | |||
GroupDto group = db.users().insertGroup(); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
GroupPermissionDto dto = getGroupPermission(group, project); | |||
underTest.insert(dbSession, dto, project); | |||
verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), group.getUuid(), project.uuid(), dto.getRole(), project.name()); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
underTest.deleteByRootComponentUuid(dbSession, project.uuid(), project.name()); | |||
verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, null, project.uuid(), null, project.name()); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
@Test | |||
public void groupProjectPermissionDeleteByComponentAndGroupIsPersisted() { | |||
GroupDto group = db.users().insertGroup(); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
GroupPermissionDto dto = getGroupPermission(group, project); | |||
underTest.insert(dbSession, dto, project); | |||
verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), group.getUuid(), project.uuid(), dto.getRole(), project.name()); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
underTest.deleteByRootComponentUuidAndGroupUuid(dbSession, project.uuid(), group.getUuid(), project.name()); | |||
verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, group.getUuid(), project.uuid(), null, project.name()); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
@Test | |||
public void groupProjectPermissionDeleteByComponentAndPermissionIsPersisted() { | |||
GroupDto group = db.users().insertGroup(); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
GroupPermissionDto dto = getGroupPermission(group, project); | |||
underTest.insert(dbSession, dto, project); | |||
verify(auditPersister).addGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), group.getUuid(), project.uuid(), dto.getRole(), project.name()); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
underTest.deleteByRootComponentUuidAndPermission(dbSession, project.uuid(), dto.getRole(), project.name()); | |||
verify(auditPersister).deleteGroupPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getGroupUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, null, project.uuid(), ADMIN, project.name()); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
private GroupPermissionDto getGroupPermission(GroupDto group, ComponentDto project) { | |||
return new GroupPermissionDto() | |||
.setUuid(Uuids.createFast()) | |||
.setGroupUuid(group.getUuid()) | |||
.setRole(ADMIN) | |||
.setComponentUuid(project.uuid()); | |||
} | |||
} |
@@ -397,10 +397,10 @@ public class UserPermissionDaoTest { | |||
addProjectPermission("perm", user2, project1); | |||
// no such provision -> ignore | |||
underTest.deleteProjectPermission(dbSession, user1.getUuid(), "anotherPerm", project1.uuid()); | |||
underTest.deleteProjectPermission(dbSession, user1.getUuid(), "anotherPerm", project1.uuid(), project1.name()); | |||
assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); | |||
underTest.deleteProjectPermission(dbSession, user1.getUuid(), "perm", project1.uuid()); | |||
underTest.deleteProjectPermission(dbSession, user1.getUuid(), "perm", project1.uuid(), project1.name()); | |||
assertThatProjectPermissionDoesNotExist(user1, "perm", project1); | |||
assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(3); | |||
} | |||
@@ -416,7 +416,7 @@ public class UserPermissionDaoTest { | |||
addProjectPermission("perm", user2, project1); | |||
addProjectPermission("perm", user1, project2); | |||
underTest.deleteProjectPermissions(dbSession, project1.uuid()); | |||
underTest.deleteProjectPermissions(dbSession, project1.uuid(), project1.name()); | |||
assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(2); | |||
assertThatProjectHasNoPermissions(project1); | |||
} | |||
@@ -521,7 +521,7 @@ public class UserPermissionDaoTest { | |||
UserDto user = insertUser(); | |||
db.users().insertPermissionOnUser(user, SCAN); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, "124", SCAN.getKey()); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, "124", SCAN.getKey(), ""); | |||
assertThat(deletedCount).isZero(); | |||
assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid())).containsOnly(SCAN.getKey()); | |||
@@ -533,7 +533,7 @@ public class UserPermissionDaoTest { | |||
db.users().insertPermissionOnUser(user, SCAN); | |||
ComponentDto project = randomPublicOrPrivateProject(); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), SCAN.getKey()); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), SCAN.getKey(), ""); | |||
assertThat(deletedCount).isZero(); | |||
assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid())).containsOnly(SCAN.getKey()); | |||
@@ -546,7 +546,7 @@ public class UserPermissionDaoTest { | |||
ComponentDto project = randomPublicOrPrivateProject(); | |||
db.users().insertProjectPermissionOnUser(user, SCAN.getKey(), project); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), "p1"); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), "p1", project.name()); | |||
assertThat(deletedCount).isZero(); | |||
assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid())).containsOnly(SCAN.getKey()); | |||
@@ -567,7 +567,7 @@ public class UserPermissionDaoTest { | |||
db.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project2); | |||
db.users().insertProjectPermissionOnUser(user2, PROVISION_PROJECTS.getKey(), project2); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project1.uuid(), SCAN.getKey()); | |||
int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project1.uuid(), SCAN.getKey(), project1.name()); | |||
assertThat(deletedCount).isEqualTo(2); | |||
assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid())).containsOnly(SCAN.getKey()); | |||
@@ -577,7 +577,7 @@ public class UserPermissionDaoTest { | |||
assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project2.uuid())).containsOnly(SCAN.getKey()); | |||
assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project2.uuid())).containsOnly(SCAN.getKey(), PROVISION_PROJECTS.getKey()); | |||
deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project2.uuid(), SCAN.getKey()); | |||
deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project2.uuid(), SCAN.getKey(), project2.name()); | |||
assertThat(deletedCount).isEqualTo(2); | |||
assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid())).containsOnly(SCAN.getKey()); | |||
@@ -632,14 +632,14 @@ public class UserPermissionDaoTest { | |||
private UserPermissionDto addGlobalPermission(String permission, UserDto user) { | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), permission, user.getUuid(), null); | |||
underTest.insert(dbSession, dto); | |||
underTest.insert(dbSession, dto, null); | |||
db.commit(); | |||
return dto; | |||
} | |||
private UserPermissionDto addProjectPermission(String permission, UserDto user, ComponentDto project) { | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), permission, user.getUuid(), project.uuid()); | |||
underTest.insert(dbSession, dto); | |||
underTest.insert(dbSession, dto, project); | |||
db.commit(); | |||
return dto; | |||
} |
@@ -0,0 +1,141 @@ | |||
/* | |||
* 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.permission; | |||
import java.util.function.Consumer; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.Uuids; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionNewValue; | |||
import org.sonar.db.component.ComponentDto; | |||
import org.sonar.db.user.UserDto; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; | |||
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; | |||
public class UserPermissionDaoWithPersisterTest { | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
@Rule | |||
public final DbTester db = DbTester.create(System2.INSTANCE, auditPersister); | |||
private final ArgumentCaptor<PermissionNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionNewValue.class); | |||
private final DbSession dbSession = db.getSession(); | |||
private final UserPermissionDao underTest = db.getDbClient().userPermissionDao(); | |||
@Test | |||
public void userGlobalPermissionInsertAndDeleteArePersisted() { | |||
UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com")); | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), SYSTEM_ADMIN, user.getUuid(), null); | |||
underTest.insert(dbSession, dto, null); | |||
verify(auditPersister).addUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), user.getUuid(), null, dto.getPermission(), null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
underTest.deleteGlobalPermission(dbSession, user.getUuid(), SYSTEM_ADMIN); | |||
verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, user.getUuid(), null, dto.getPermission(), null); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
@Test | |||
public void userProjectPermissionInsertAndDeleteArePersisted() { | |||
UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com")); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), SYSTEM_ADMIN, user.getUuid(), project.uuid()); | |||
underTest.insert(dbSession, dto, project); | |||
verify(auditPersister).addUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), user.getUuid(), project.uuid(), dto.getPermission(), project.name()); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
underTest.deleteProjectPermission(dbSession, user.getUuid(), SYSTEM_ADMIN, project.projectUuid(), project.name()); | |||
verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, user.getUuid(), project.uuid(), dto.getPermission(), project.name()); | |||
assertThat(newValue.toString()).doesNotContain("permissionUuid"); | |||
} | |||
@Test | |||
public void deleteUserPermissionOfAnyUserIsPersisted() { | |||
UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com")); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), SCAN_EXECUTION, user.getUuid(), project.uuid()); | |||
underTest.insert(dbSession, dto, project); | |||
underTest.deleteProjectPermissionOfAnyUser(dbSession, project.projectUuid(), SCAN_EXECUTION, project.name()); | |||
verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, null, project.uuid(), SCAN_EXECUTION, project.name()); | |||
assertThat(newValue.toString()).doesNotContain("userUuid"); | |||
} | |||
@Test | |||
public void deleteUserPermissionByUserUuidIsPersisted() { | |||
UserDto user = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com")); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), SYSTEM_ADMIN, user.getUuid(), project.uuid()); | |||
underTest.insert(dbSession, dto, project); | |||
underTest.deleteByUserUuid(dbSession, user.getUuid()); | |||
verify(auditPersister).deleteUserPermission(eq(dbSession), newValueCaptor.capture()); | |||
PermissionNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionNewValue::getPermissionUuid, PermissionNewValue::getUserUuid, PermissionNewValue::getProjectUuid, | |||
PermissionNewValue::getRole, PermissionNewValue::getProjectName) | |||
.containsExactly(null, user.getUuid(), null, null, null); | |||
assertThat(newValue.toString()).contains("userUuid"); | |||
} | |||
private UserDto insertUser(Consumer<UserDto> populateUserDto) { | |||
UserDto user = db.users().insertUser(populateUserDto); | |||
return user; | |||
} | |||
} |
@@ -57,13 +57,13 @@ public class GroupWithPermissionTemplateDaoTest { | |||
GroupDto group3 = db.users().insertGroup("Group-3"); | |||
PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName()); | |||
PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName()); | |||
assertThat(selectGroupNamesByQueryAndTemplate(builder(), template)) | |||
.containsOnly("Group-1", "Group-2", "Group-3", "Anyone"); | |||
@@ -152,7 +152,7 @@ public class GroupWithPermissionTemplateDaoTest { | |||
GroupDto group = db.users().insertGroup("Group"); | |||
PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getUuid(), group.getUuid(), USER); | |||
permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getUuid(), group.getUuid(), USER, otherTemplate.getName(), group.getName()); | |||
assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), template)) | |||
.containsExactly("Anyone"); | |||
@@ -165,13 +165,13 @@ public class GroupWithPermissionTemplateDaoTest { | |||
GroupDto group3 = db.users().insertGroup("Group-3"); | |||
PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName()); | |||
PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName()); | |||
assertThat(countGroupNamesByQueryAndTemplate(builder(), template)) | |||
.isEqualTo(4); | |||
@@ -200,13 +200,13 @@ public class GroupWithPermissionTemplateDaoTest { | |||
GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3")); | |||
PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName()); | |||
PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName()); | |||
assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), asList("Group-1"))) | |||
.extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission) | |||
@@ -236,13 +236,13 @@ public class GroupWithPermissionTemplateDaoTest { | |||
GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3")); | |||
PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName()); | |||
permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName()); | |||
PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null); | |||
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName()); | |||
assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, template.getUuid())) | |||
.extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission) |
@@ -44,33 +44,37 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
@Test | |||
public void selectByTemplateId_filter_by_template_uuid() { | |||
PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
PermissionTemplateCharacteristicDto templatePermission2 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("2") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("2") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
PermissionTemplateCharacteristicDto templatePermission3 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid3") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("3") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_001L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid3") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("3") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_001L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
PermissionTemplateCharacteristicDto templatePermissionForAnotherTemplate = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid4") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("42") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid4") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("42") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateUuids(dbSession, newArrayList("1", "2", "3")); | |||
assertThat(result) | |||
@@ -92,26 +96,29 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
@Test | |||
public void selectByPermissionAndTemplateId() { | |||
PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(false) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid3") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("42") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid3") | |||
.setPermission(UserRole.ADMIN) | |||
.setTemplateUuid("42") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(1_000_000_000L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, "1"); | |||
@@ -122,12 +129,13 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
@Test | |||
public void insert() { | |||
PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
PermissionTemplateCharacteristicDto result = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).selectByUuid(expectedResult.getUuid()); | |||
assertThat(result.getUuid()).isEqualTo("uuid"); | |||
@@ -137,20 +145,22 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
@Test | |||
public void update_only_change_with_project_creator_and_updated_at() { | |||
PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
underTest.update(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid") | |||
.setPermission("PERMISSION_ARE_NOT_UPDATABLE") | |||
.setTemplateUuid("42") | |||
.setCreatedAt(42L) | |||
.setWithProjectCreator(false) | |||
.setUpdatedAt(3_000_000_000L)); | |||
.setUuid("uuid") | |||
.setPermission("PERMISSION_ARE_NOT_UPDATABLE") | |||
.setTemplateUuid("42") | |||
.setCreatedAt(42L) | |||
.setWithProjectCreator(false) | |||
.setUpdatedAt(3_000_000_000L), | |||
"template"); | |||
PermissionTemplateCharacteristicDto result = underTest.selectByPermissionAndTemplateId(dbSession, insertedDto.getPermission(), insertedDto.getTemplateUuid()).get(); | |||
assertThat(result).extracting("uuid", "permission", "templateUuid", "createdAt") | |||
@@ -167,7 +177,7 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setUpdatedAt(2_000_000_000L); | |||
assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto)) | |||
assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto, "template")) | |||
.isInstanceOf(IllegalArgumentException.class); | |||
} | |||
@@ -179,7 +189,7 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(2_000_000_000L); | |||
assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto)) | |||
assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto, "template")) | |||
.isInstanceOf(IllegalArgumentException.class); | |||
} | |||
@@ -192,26 +202,28 @@ public class PermissionTemplateCharacteristicDaoTest { | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L); | |||
assertThatThrownBy(() -> underTest.update(dbSession, characteristicDto)) | |||
assertThatThrownBy(() -> underTest.update(dbSession, characteristicDto, "template")) | |||
.isInstanceOf(NullPointerException.class); | |||
} | |||
@Test | |||
public void delete_by_permission_template_uuid() { | |||
underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid1") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("2") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L)); | |||
.setUuid("uuid2") | |||
.setPermission(UserRole.USER) | |||
.setTemplateUuid("2") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L), | |||
"template"); | |||
assertThat(underTest.selectByTemplateUuids(dbSession, singletonList("1"))).hasSize(1); | |||
assertThat(underTest.selectByTemplateUuids(dbSession, asList("1", "2"))).hasSize(2); |
@@ -0,0 +1,85 @@ | |||
/* | |||
* 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.permission.template; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
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.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionTemplateNewValue; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
public class PermissionTemplateCharacteristicDaoWithPersisterTest { | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
@Rule | |||
public final DbTester db = DbTester.create(System2.INSTANCE, auditPersister); | |||
private final DbSession session = db.getSession(); | |||
private final PermissionTemplateCharacteristicDao underTest = db.getDbClient().permissionTemplateCharacteristicDao(); | |||
private final ArgumentCaptor<PermissionTemplateNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionTemplateNewValue.class); | |||
@Test | |||
public void insertPermissionTemplateCharacteristicIsPersisted() { | |||
PermissionTemplateCharacteristicDto dto = getPermissionTemplateCharacteristic(UserRole.USER); | |||
underTest.insert(session, dto, "template"); | |||
verify(auditPersister).addCharacteristicToPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::isWithProjectCreator) | |||
.containsExactly(dto.getTemplateUuid(), "template", dto.getPermission(), dto.getWithProjectCreator()); | |||
assertThat(newValue.toString()).contains("withProjectCreator"); | |||
} | |||
@Test | |||
public void updatePermissionTemplateCharacteristicIsPersisted() { | |||
underTest.insert(session, getPermissionTemplateCharacteristic(UserRole.USER), | |||
"template"); | |||
PermissionTemplateCharacteristicDto updated = getPermissionTemplateCharacteristic(UserRole.ADMIN); | |||
underTest.update(session, updated, "template"); | |||
verify(auditPersister).updateCharacteristicInPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::isWithProjectCreator) | |||
.containsExactly(updated.getTemplateUuid(), "template", updated.getPermission(), updated.getWithProjectCreator()); | |||
assertThat(newValue.toString()).contains("withProjectCreator"); | |||
} | |||
private PermissionTemplateCharacteristicDto getPermissionTemplateCharacteristic(String role) { | |||
return new PermissionTemplateCharacteristicDto() | |||
.setUuid("uuid") | |||
.setPermission(role) | |||
.setTemplateUuid("1") | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(123_456_789L) | |||
.setUpdatedAt(2_000_000_000L); | |||
} | |||
} |
@@ -175,10 +175,10 @@ public class PermissionTemplateDaoTest { | |||
templateDb.addGroupToTemplate(permissionTemplate1, group2, "user"); | |||
templateDb.addAnyoneToTemplate(permissionTemplate1, "admin"); | |||
templateDb.addAnyoneToTemplate(permissionTemplate2, "admin"); | |||
templateDb.addProjectCreatorToTemplate(permissionTemplate1.getUuid(), "user"); | |||
templateDb.addProjectCreatorToTemplate(permissionTemplate2.getUuid(), "user"); | |||
templateDb.addProjectCreatorToTemplate(permissionTemplate1.getUuid(), "user", permissionTemplate1.getName()); | |||
templateDb.addProjectCreatorToTemplate(permissionTemplate2.getUuid(), "user", permissionTemplate2.getName()); | |||
underTest.deleteByUuid(dbSession, permissionTemplate1.getUuid()); | |||
underTest.deleteByUuid(dbSession, permissionTemplate1.getUuid(), permissionTemplate1.getName()); | |||
dbSession.commit(); | |||
assertThat(underTest.selectAll(db.getSession(), null)) | |||
@@ -198,7 +198,7 @@ public class PermissionTemplateDaoTest { | |||
PermissionTemplateDto permissionTemplate = templateDb.insertTemplate(); | |||
UserDto user = db.users().insertUser(); | |||
underTest.insertUserPermission(dbSession, permissionTemplate.getUuid(), user.getUuid(), "user"); | |||
underTest.insertUserPermission(dbSession, permissionTemplate.getUuid(), user.getUuid(), "user", permissionTemplate.getName(), user.getLogin()); | |||
assertThat(db.getDbClient().permissionTemplateDao().selectUserPermissionsByTemplateId(db.getSession(), permissionTemplate.getUuid())) | |||
.extracting(PermissionTemplateUserDto::getTemplateUuid, PermissionTemplateUserDto::getUserUuid, PermissionTemplateUserDto::getPermission, | |||
@@ -216,7 +216,7 @@ public class PermissionTemplateDaoTest { | |||
templateDb.addUserToTemplate(permissionTemplate, user1, "admin"); | |||
templateDb.addUserToTemplate(permissionTemplate, user2, "user"); | |||
underTest.deleteUserPermission(dbSession, permissionTemplate.getUuid(), user1.getUuid(), "user"); | |||
underTest.deleteUserPermission(dbSession, permissionTemplate.getUuid(), user1.getUuid(), "user", permissionTemplate.getName(), user1.getLogin()); | |||
assertThat(db.getDbClient().permissionTemplateDao().selectUserPermissionsByTemplateId(db.getSession(), permissionTemplate.getUuid())) | |||
.extracting(PermissionTemplateUserDto::getUserUuid, PermissionTemplateUserDto::getPermission) | |||
@@ -228,7 +228,7 @@ public class PermissionTemplateDaoTest { | |||
PermissionTemplateDto permissionTemplate = templateDb.insertTemplate(); | |||
GroupDto group = db.users().insertGroup(); | |||
underTest.insertGroupPermission(dbSession, permissionTemplate.getUuid(), group.getUuid(), "user"); | |||
underTest.insertGroupPermission(dbSession, permissionTemplate.getUuid(), group.getUuid(), "user", permissionTemplate.getName(), group.getName()); | |||
dbSession.commit(); | |||
assertThat(db.getDbClient().permissionTemplateDao().selectGroupPermissionsByTemplateUuid(db.getSession(), permissionTemplate.getUuid())) | |||
@@ -247,7 +247,7 @@ public class PermissionTemplateDaoTest { | |||
templateDb.addGroupToTemplate(permissionTemplate, group1, "admin"); | |||
templateDb.addGroupToTemplate(permissionTemplate, group2, "user"); | |||
underTest.deleteByGroup(db.getSession(), group1.getUuid()); | |||
underTest.deleteByGroup(db.getSession(), group1.getUuid(), group1.getName()); | |||
db.getSession().commit(); | |||
assertThat(db.getDbClient().permissionTemplateDao().selectGroupPermissionsByTemplateUuid(db.getSession(), permissionTemplate.getUuid())) | |||
@@ -259,7 +259,7 @@ public class PermissionTemplateDaoTest { | |||
public void add_group_permission_to_anyone() { | |||
PermissionTemplateDto permissionTemplate = templateDb.insertTemplate(); | |||
underTest.insertGroupPermission(dbSession, permissionTemplate.getUuid(), null, "user"); | |||
underTest.insertGroupPermission(dbSession, permissionTemplate.getUuid(), null, "user", permissionTemplate.getName(), null); | |||
dbSession.commit(); | |||
assertThat(db.getDbClient().permissionTemplateDao().selectGroupPermissionsByTemplateUuid(db.getSession(), permissionTemplate.getUuid())) | |||
@@ -277,13 +277,13 @@ public class PermissionTemplateDaoTest { | |||
GroupDto group1 = db.users().insertGroup(newGroupDto()); | |||
GroupDto group2 = db.users().insertGroup(newGroupDto()); | |||
GroupDto group3 = db.users().insertGroup(newGroupDto()); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group1.getUuid(), CODEVIEWER); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group2.getUuid(), CODEVIEWER); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group3.getUuid(), CODEVIEWER); | |||
templateDb.addGroupToTemplate(template1.getUuid(), null, CODEVIEWER); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group1.getUuid(), ADMIN); | |||
templateDb.addGroupToTemplate(template2.getUuid(), group1.getUuid(), ADMIN); | |||
templateDb.addGroupToTemplate(template4.getUuid(), group1.getUuid(), ISSUE_ADMIN); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group1.getUuid(), CODEVIEWER, template1.getName(), group1.getName()); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group2.getUuid(), CODEVIEWER, template1.getName(), group2.getName()); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group3.getUuid(), CODEVIEWER, template1.getName(), group3.getName()); | |||
templateDb.addGroupToTemplate(template1.getUuid(), null, CODEVIEWER, template1.getName(), null); | |||
templateDb.addGroupToTemplate(template1.getUuid(), group1.getUuid(), ADMIN, template1.getName(), group1.getName()); | |||
templateDb.addGroupToTemplate(template2.getUuid(), group1.getUuid(), ADMIN, template2.getName(), group1.getName()); | |||
templateDb.addGroupToTemplate(template4.getUuid(), group1.getUuid(), ISSUE_ADMIN, template4.getName(), group1.getName()); | |||
final List<CountByTemplateAndPermissionDto> result = new ArrayList<>(); | |||
underTest.groupsCountByTemplateUuidAndPermission(dbSession, asList(template1.getUuid(), template2.getUuid(), template3.getUuid()), | |||
@@ -304,12 +304,12 @@ public class PermissionTemplateDaoTest { | |||
UserDto user2 = db.users().insertUser(); | |||
UserDto user3 = db.users().insertUser(); | |||
templateDb.addUserToTemplate(template1.getUuid(), user1.getUuid(), ADMIN); | |||
templateDb.addUserToTemplate(template1.getUuid(), user2.getUuid(), ADMIN); | |||
templateDb.addUserToTemplate(template1.getUuid(), user3.getUuid(), ADMIN); | |||
templateDb.addUserToTemplate(template1.getUuid(), user1.getUuid(), USER); | |||
templateDb.addUserToTemplate(template2.getUuid(), user1.getUuid(), USER); | |||
templateDb.addUserToTemplate(anotherTemplate.getUuid(), user1.getUuid(), ISSUE_ADMIN); | |||
templateDb.addUserToTemplate(template1.getUuid(), user1.getUuid(), ADMIN, template1.getName(), user1.getLogin()); | |||
templateDb.addUserToTemplate(template1.getUuid(), user2.getUuid(), ADMIN, template1.getName(), user2.getLogin()); | |||
templateDb.addUserToTemplate(template1.getUuid(), user3.getUuid(), ADMIN, template1.getName(), user3.getLogin()); | |||
templateDb.addUserToTemplate(template1.getUuid(), user1.getUuid(), USER, template1.getName(), user1.getLogin()); | |||
templateDb.addUserToTemplate(template2.getUuid(), user1.getUuid(), USER, template2.getName(), user1.getLogin()); | |||
templateDb.addUserToTemplate(anotherTemplate.getUuid(), user1.getUuid(), ISSUE_ADMIN, anotherTemplate.getName(), user1.getLogin()); | |||
final List<CountByTemplateAndPermissionDto> result = new ArrayList<>(); | |||
underTest.usersCountByTemplateUuidAndPermission(dbSession, asList(template1.getUuid(), template2.getUuid(), template3.getUuid()), | |||
@@ -345,13 +345,13 @@ public class PermissionTemplateDaoTest { | |||
GroupDto group = db.users().insertGroup(newGroupDto()); | |||
db.users().insertMember(group, user); | |||
PermissionTemplateDto template = templateDb.insertTemplate(); | |||
templateDb.addProjectCreatorToTemplate(template.getUuid(), SCAN_EXECUTION); | |||
templateDb.addProjectCreatorToTemplate(template.getUuid(), UserRole.ADMIN); | |||
templateDb.addUserToTemplate(template.getUuid(), user.getUuid(), UserRole.USER); | |||
templateDb.addUserToTemplate(template.getUuid(), user.getUuid(), UserRole.ADMIN); | |||
templateDb.addGroupToTemplate(template.getUuid(), group.getUuid(), UserRole.CODEVIEWER); | |||
templateDb.addGroupToTemplate(template.getUuid(), group.getUuid(), UserRole.ADMIN); | |||
templateDb.addGroupToTemplate(template.getUuid(), null, UserRole.ISSUE_ADMIN); | |||
templateDb.addProjectCreatorToTemplate(template.getUuid(), SCAN_EXECUTION, template.getName()); | |||
templateDb.addProjectCreatorToTemplate(template.getUuid(), UserRole.ADMIN, template.getName()); | |||
templateDb.addUserToTemplate(template.getUuid(), user.getUuid(), UserRole.USER, template.getName(), user.getLogin()); | |||
templateDb.addUserToTemplate(template.getUuid(), user.getUuid(), UserRole.ADMIN, template.getName(), user.getLogin()); | |||
templateDb.addGroupToTemplate(template.getUuid(), group.getUuid(), UserRole.CODEVIEWER, template.getName(), group.getName()); | |||
templateDb.addGroupToTemplate(template.getUuid(), group.getUuid(), UserRole.ADMIN, template.getName(), group.getName()); | |||
templateDb.addGroupToTemplate(template.getUuid(), null, UserRole.ISSUE_ADMIN, template.getName(), null); | |||
List<String> resultWithUser = underTest.selectPotentialPermissionsByUserUuidAndTemplateUuid(dbSession, user.getUuid(), template.getUuid()); | |||
List<String> resultWithoutUser = underTest.selectPotentialPermissionsByUserUuidAndTemplateUuid(dbSession, null, template.getUuid()); | |||
@@ -381,10 +381,10 @@ public class PermissionTemplateDaoTest { | |||
UserDto anotherUser = db.users().insertUser(); | |||
PermissionTemplateDto template = db.permissionTemplates().insertTemplate(); | |||
String permission = "PERMISSION"; | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), user.getUuid(), permission); | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), anotherUser.getUuid(), permission); | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), user.getUuid(), permission, template.getName(), user.getLogin()); | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), anotherUser.getUuid(), permission, template.getName(), anotherUser.getLogin()); | |||
underTest.deleteUserPermissionsByUserUuid(dbSession, user.getUuid()); | |||
underTest.deleteUserPermissionsByUserUuid(dbSession, user.getUuid(), user.getLogin()); | |||
db.commit(); | |||
assertThat(db.select("select template_uuid as \"templateUuid\", user_uuid as \"userUuid\", permission_reference as \"permission\" from perm_templates_users")) |
@@ -0,0 +1,200 @@ | |||
/* | |||
* 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.permission.template; | |||
import java.util.Date; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.core.util.Uuids; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PermissionTemplateNewValue; | |||
import org.sonar.db.user.GroupDto; | |||
import org.sonar.db.user.UserDto; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.sonar.api.web.UserRole.ADMIN; | |||
import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto; | |||
import static org.sonar.db.user.GroupTesting.newGroupDto; | |||
public class PermissionTemplateDaoWithPersisterTest { | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
private static final Date PAST = new Date(100_000_000_000L); | |||
private static final Date NOW = new Date(500_000_000_000L); | |||
@Rule | |||
public final DbTester db = DbTester.create(auditPersister); | |||
private final DbSession session = db.getSession(); | |||
private final ArgumentCaptor<PermissionTemplateNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionTemplateNewValue.class); | |||
private final PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao(); | |||
@Test | |||
public void insertPermissionTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
verify(auditPersister).addPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName) | |||
.containsExactly(dto.getUuid(), dto.getName()); | |||
assertThat(newValue.toString()).doesNotContain("keyPattern"); | |||
} | |||
@Test | |||
public void updatePermissionTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
underTest.update(session, dto); | |||
verify(auditPersister).updatePermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getDescription, PermissionTemplateNewValue::getKeyPattern) | |||
.containsExactly(dto.getUuid(), dto.getName(), dto.getDescription(), dto.getKeyPattern()); | |||
assertThat(newValue.toString()).contains("keyPattern"); | |||
} | |||
@Test | |||
public void deletePermissionTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
underTest.deleteByUuid(session, dto.getUuid(), dto.getName()); | |||
verify(auditPersister).deletePermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName) | |||
.containsExactly(dto.getUuid(), dto.getName()); | |||
assertThat(newValue.toString()).doesNotContain("keyPattern"); | |||
} | |||
@Test | |||
public void insertAndDeleteUserPermissionToTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
UserDto user = db.users().insertUser(); | |||
underTest.insertUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin()); | |||
verify(auditPersister).addUserToPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin) | |||
.containsExactly(dto.getUuid(), dto.getName(), ADMIN, user.getUuid(), user.getLogin()); | |||
assertThat(newValue.toString()).doesNotContain("groupUuid"); | |||
underTest.deleteUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin()); | |||
verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin) | |||
.containsExactly(dto.getUuid(), dto.getName(), ADMIN, user.getUuid(), user.getLogin()); | |||
assertThat(newValue.toString()).doesNotContain("groupUuid"); | |||
} | |||
@Test | |||
public void insertAndDeleteUserPermissionByUserUuidToTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
UserDto user = db.users().insertUser(); | |||
underTest.insertUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin()); | |||
underTest.deleteUserPermissionsByUserUuid(session, user.getUuid(), user.getLogin()); | |||
verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin) | |||
.containsExactly(null, null, null, user.getUuid(), user.getLogin()); | |||
assertThat(newValue.toString()).doesNotContain("groupUuid"); | |||
} | |||
@Test | |||
public void insertAndDeleteGroupPermissionToTemplateIsPersisted() { | |||
PermissionTemplateDto dto = insertPermissionTemplate(); | |||
GroupDto group = db.users().insertGroup(newGroupDto()); | |||
underTest.insertGroupPermission(session, dto.getUuid(), group.getUuid(), ADMIN, dto.getName(), group.getName()); | |||
verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName) | |||
.containsExactly(dto.getUuid(), dto.getName(), ADMIN, group.getUuid(), group.getName()); | |||
assertThat(newValue.toString()).contains("groupUuid"); | |||
underTest.deleteGroupPermission(session, dto.getUuid(), group.getUuid(), ADMIN, dto.getName(), group.getName()); | |||
verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName) | |||
.containsExactly(dto.getUuid(), dto.getName(), ADMIN, group.getUuid(), group.getName()); | |||
assertThat(newValue.toString()).contains("groupUuid"); | |||
} | |||
@Test | |||
public void insertAndDeleteGroupPermissionByGroupUuidToTemplateIsPersisted() { | |||
PermissionTemplateDto templateDto = insertPermissionTemplate(); | |||
PermissionTemplateGroupDto templateGroupDto = new PermissionTemplateGroupDto() | |||
.setUuid(Uuids.createFast()) | |||
.setGroupName("group") | |||
.setPermission(ADMIN) | |||
.setTemplateUuid(templateDto.getUuid()) | |||
.setCreatedAt(new Date()) | |||
.setUpdatedAt(new Date()); | |||
underTest.insertGroupPermission(session, templateGroupDto, templateDto.getName()); | |||
verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
PermissionTemplateNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName) | |||
.containsExactly(templateDto.getUuid(), templateDto.getName(), ADMIN, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName()); | |||
assertThat(newValue.toString()).doesNotContain("userUuid"); | |||
underTest.deleteByGroup(session, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName()); | |||
verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture()); | |||
newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName, | |||
PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName) | |||
.containsExactly(null, null, null, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName()); | |||
assertThat(newValue.toString()).doesNotContain("userUuid"); | |||
} | |||
private PermissionTemplateDto insertPermissionTemplate() { | |||
return underTest.insert(session, newPermissionTemplateDto() | |||
.setUuid("ABCD") | |||
.setName("my template") | |||
.setDescription("my description") | |||
.setKeyPattern("myregexp") | |||
.setCreatedAt(PAST) | |||
.setUpdatedAt(NOW)); | |||
} | |||
} |
@@ -140,7 +140,7 @@ public class UserWithPermissionTemplateDaoTest { | |||
UserDto user1 = db.users().insertUser(u -> u.setName("A")); | |||
UserDto user2 = db.users().insertUser(u -> u.setName("B")); | |||
UserDto user3 = db.users().insertUser(u -> u.setName("C")); | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), user3.getUuid(), UserRole.USER); | |||
db.permissionTemplates().addUserToTemplate(template.getUuid(), user3.getUuid(), UserRole.USER, template.getName(), user3.getLogin()); | |||
PermissionQuery query = PermissionQuery.builder().build(); | |||
assertThat(underTest.selectUserLoginsByQueryAndTemplate(db.getSession(), query, template.getUuid())) |
@@ -0,0 +1,89 @@ | |||
/* | |||
* 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.plugin; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PluginNewValue; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.sonar.db.plugin.PluginDto.Type.EXTERNAL; | |||
public class PluginDaoWithPersisterTest { | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
@Rule | |||
public final ExpectedException thrown = ExpectedException.none(); | |||
@Rule | |||
public final DbTester db = DbTester.create(System2.INSTANCE, auditPersister); | |||
private final ArgumentCaptor<PluginNewValue> newValueCaptor = ArgumentCaptor.forClass(PluginNewValue.class); | |||
private final DbSession session = db.getSession(); | |||
private PluginDao underTest = db.getDbClient().pluginDao(); | |||
@Test | |||
public void insert() { | |||
PluginDto dto = getPluginDto(); | |||
underTest.insert(session, dto); | |||
verify(auditPersister).addPlugin(eq(session), newValueCaptor.capture()); | |||
PluginNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PluginNewValue::getPluginUuid, PluginNewValue::getKee, | |||
PluginNewValue::getBasePluginKey, PluginNewValue::getType) | |||
.containsExactly(dto.getUuid(), dto.getKee(), dto.getBasePluginKey(), dto.getType().name()); | |||
assertThat(newValue.toString()).contains("pluginUuid"); | |||
} | |||
@Test | |||
public void update() { | |||
PluginDto dto = getPluginDto(); | |||
underTest.update(session, dto); | |||
verify(auditPersister).updatePlugin(eq(session), newValueCaptor.capture()); | |||
PluginNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PluginNewValue::getPluginUuid, PluginNewValue::getKee, | |||
PluginNewValue::getBasePluginKey, PluginNewValue::getType) | |||
.containsExactly(dto.getUuid(), dto.getKee(), dto.getBasePluginKey(), dto.getType().name()); | |||
assertThat(newValue.toString()).contains("pluginUuid"); | |||
} | |||
private PluginDto getPluginDto() { | |||
return new PluginDto() | |||
.setUuid("plugin_uuid") | |||
.setKee("plugin_kee") | |||
.setBasePluginKey("plugin_base_key") | |||
.setFileHash("plugin_file_hash") | |||
.setType(EXTERNAL) | |||
.setCreatedAt(1L) | |||
.setUpdatedAt(2L); | |||
} | |||
} |
@@ -81,11 +81,11 @@ public class PropertiesDaoTest { | |||
UserDto user1 = db.users().insertUser(u -> u.setLogin("user1")); | |||
UserDto user2 = db.users().insertUser(u -> u.setLogin("user2")); | |||
UserDto user3 = db.users().insertUser(u -> u.setLogin("user3")); | |||
insertProperty("notification.NewViolations.Email", "true", project1.uuid(), user2.getUuid()); | |||
insertProperty("notification.NewViolations.Twitter", "true", null, user3.getUuid()); | |||
insertProperty("notification.NewViolations.Twitter", "true", project2.uuid(), user1.getUuid()); | |||
insertProperty("notification.NewViolations.Twitter", "true", project1.uuid(), user2.getUuid()); | |||
insertProperty("notification.NewViolations.Twitter", "true", project2.uuid(), user3.getUuid()); | |||
insertProperty("notification.NewViolations.Email", "true", project1.uuid(), user2.getUuid(), user2.getLogin(), project1.name()); | |||
insertProperty("notification.NewViolations.Twitter", "true", null, user3.getUuid(), user3.getLogin(), null); | |||
insertProperty("notification.NewViolations.Twitter", "true", project2.uuid(), user1.getUuid(), user1.getLogin(), project2.name()); | |||
insertProperty("notification.NewViolations.Twitter", "true", project1.uuid(), user2.getUuid(), user2.getLogin(), project1.name()); | |||
insertProperty("notification.NewViolations.Twitter", "true", project2.uuid(), user3.getUuid(), user3.getLogin(), project2.name()); | |||
db.users().insertProjectPermissionOnUser(user2, UserRole.USER, project1); | |||
db.users().insertProjectPermissionOnUser(user3, UserRole.USER, project2); | |||
db.users().insertProjectPermissionOnUser(user1, UserRole.USER, project2); | |||
@@ -117,19 +117,25 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void hasNotificationSubscribers() { | |||
String userUuid1 = db.users().insertUser(u -> u.setLogin("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(u -> u.setLogin("user2")).getUuid(); | |||
UserDto user1 = db.users().insertUser(u -> u.setLogin("user1")); | |||
UserDto user2 = db.users().insertUser(u -> u.setLogin("user2")); | |||
String projectUuid = randomAlphabetic(8); | |||
String projectName = randomAlphabetic(4); | |||
db.components().insertPrivateProject(projectUuid); | |||
// global subscription | |||
insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userUuid2); | |||
insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, | |||
user2.getUuid(), user2.getLogin(), null); | |||
// project subscription | |||
insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectUuid, userUuid1); | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", "uuid56", userUuid1); | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectUuid, userUuid1); | |||
insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectUuid, user1.getUuid(), | |||
user1.getLogin(), projectName); | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", "uuid56", user1.getUuid(), | |||
user1.getLogin(), projectName); | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectUuid, user1.getUuid(), | |||
user1.getLogin(), projectName); | |||
// global subscription | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, userUuid2); | |||
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, user2.getUuid(), | |||
user2.getLogin(), null); | |||
// Nobody is subscribed | |||
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers(projectUuid, singletonList("NotSexyDispatcher"))) | |||
@@ -179,24 +185,24 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_finds_only_globally_subscribed_users_if_projectKey_is_null() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(withEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(withEmail("user4")).getUuid(); | |||
String projectUuid = insertPrivateProject("PROJECT_A").uuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(withEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(withEmail("user4")); | |||
ComponentDto project = insertPrivateProject("PROJECT_A"); | |||
String dispatcherKey = randomAlphabetic(5); | |||
String otherDispatcherKey = randomAlphabetic(6); | |||
String channelKey = randomAlphabetic(7); | |||
String otherChannelKey = randomAlphabetic(8); | |||
// user1 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
// user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 subscribed on project only | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user3.getUuid(), user3.getLogin(), project.name()); | |||
// user4 did not subscribe | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectUuid, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", project.uuid(), user4.getUuid(), user4.getLogin(), project.name()); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null)) | |||
.containsOnly(EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user2", true, emailOf("user2"))); | |||
@@ -211,24 +217,24 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_with_logins_finds_only_globally_subscribed_specified_users_if_projectKey_is_null() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(withEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(withEmail("user4")).getUuid(); | |||
String projectUuid = insertPrivateProject("PROJECT_A").uuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(withEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(withEmail("user4")); | |||
ComponentDto project = insertPrivateProject("PROJECT_A"); | |||
String dispatcherKey = randomAlphabetic(5); | |||
String otherDispatcherKey = randomAlphabetic(6); | |||
String channelKey = randomAlphabetic(7); | |||
String otherChannelKey = randomAlphabetic(8); | |||
// user1 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
// user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 subscribed on project only | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user3.getUuid(), user3.getLogin(), project.name()); | |||
// user4 did not subscribe | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectUuid, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", project.uuid(), user4.getUuid(), user4.getLogin(), project.name()); | |||
Set<String> allLogins = of("user1", "user2", "user3", "user4"); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, allLogins)) | |||
@@ -252,26 +258,26 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_finds_global_and_project_subscribed_users_when_projectKey_is_non_null() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(withEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(withEmail("user4")).getUuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(withEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(withEmail("user4")); | |||
String projectKey = randomAlphabetic(3); | |||
String otherProjectKey = randomAlphabetic(4); | |||
String projectUuid = insertPrivateProject(projectKey).uuid(); | |||
ComponentDto project = insertPrivateProject(projectKey); | |||
String dispatcherKey = randomAlphabetic(5); | |||
String otherDispatcherKey = randomAlphabetic(6); | |||
String channelKey = randomAlphabetic(7); | |||
String otherChannelKey = randomAlphabetic(8); | |||
// user1 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
// user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 subscribed on project only | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user3.getUuid(), user3.getLogin(), project.name()); | |||
// user4 did not subscribe | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectUuid, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", project.uuid(), user4.getUuid(), user4.getLogin(), project.name()); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey)) | |||
.containsOnly( | |||
@@ -291,26 +297,26 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_with_logins_finds_global_and_project_subscribed_specified_users_when_projectKey_is_non_null() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(withEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(withEmail("user4")).getUuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(withEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(withEmail("user4")); | |||
String projectKey = randomAlphabetic(3); | |||
String otherProjectKey = randomAlphabetic(4); | |||
String projectUuid = insertPrivateProject(projectKey).uuid(); | |||
ComponentDto project = insertPrivateProject(projectKey); | |||
String dispatcherKey = randomAlphabetic(5); | |||
String otherDispatcherKey = randomAlphabetic(6); | |||
String channelKey = randomAlphabetic(7); | |||
String otherChannelKey = randomAlphabetic(8); | |||
// user1 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
// user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 subscribed on project only | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user3.getUuid(), user3.getLogin(), project.name()); | |||
// user4 did not subscribe | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectUuid, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", project.uuid(), user4.getUuid(), user4.getLogin(), project.name()); | |||
Set<String> allLogins = of("user1", "user2", "user3", "user4"); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, allLogins)) | |||
@@ -341,22 +347,22 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_ignores_subscribed_users_without_email() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(noEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(noEmail("user4")).getUuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(noEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(noEmail("user4")); | |||
String projectKey = randomAlphabetic(3); | |||
String projectUuid = insertPrivateProject(projectKey).uuid(); | |||
ComponentDto project = insertPrivateProject(projectKey); | |||
String dispatcherKey = randomAlphabetic(4); | |||
String channelKey = randomAlphabetic(5); | |||
// user1 and user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user1.getUuid(), user1.getLogin(), project.name()); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 and user4 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user3.getUuid(), user3.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user4.getUuid(), user4.getLogin(), null); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey)) | |||
.containsOnly( | |||
@@ -370,23 +376,23 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void findEmailRecipientsForNotification_with_logins_ignores_subscribed_users_without_email() { | |||
String userUuid1 = db.users().insertUser(withEmail("user1")).getUuid(); | |||
String userUuid2 = db.users().insertUser(noEmail("user2")).getUuid(); | |||
String userUuid3 = db.users().insertUser(withEmail("user3")).getUuid(); | |||
String userUuid4 = db.users().insertUser(noEmail("user4")).getUuid(); | |||
UserDto user1 = db.users().insertUser(withEmail("user1")); | |||
UserDto user2 = db.users().insertUser(noEmail("user2")); | |||
UserDto user3 = db.users().insertUser(withEmail("user3")); | |||
UserDto user4 = db.users().insertUser(noEmail("user4")); | |||
Set<String> allLogins = of("user1", "user2", "user3"); | |||
String projectKey = randomAlphabetic(3); | |||
String projectUuid = insertPrivateProject(projectKey).uuid(); | |||
ComponentDto project = insertPrivateProject(projectKey); | |||
String dispatcherKey = randomAlphabetic(4); | |||
String channelKey = randomAlphabetic(5); | |||
// user1 and user2 subscribed on project and globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid1); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectUuid, userUuid2); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user1.getUuid(), user1.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user1.getUuid(), user1.getLogin(), project.name()); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user2.getUuid(), user2.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", project.uuid(), user2.getUuid(), user2.getLogin(), project.name()); | |||
// user3 and user4 subscribed only globally | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid3); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userUuid4); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user3.getUuid(), user3.getLogin(), null); | |||
insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, user4.getUuid(), user4.getLogin(), null); | |||
assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, allLogins)) | |||
.containsOnly( | |||
@@ -401,8 +407,8 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void selectGlobalProperties() { | |||
// global | |||
insertProperty("global.one", "one", null, null); | |||
insertProperty("global.two", "two", null, null); | |||
insertProperty("global.one", "one", null, null, null, null); | |||
insertProperty("global.two", "two", null, null, null, null); | |||
List<PropertyDto> properties = underTest.selectGlobalProperties(); | |||
assertThat(properties.size()) | |||
@@ -420,7 +426,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
@UseDataProvider("allValuesForSelect") | |||
public void selectGlobalProperties_supports_all_values(String dbValue, String expected) { | |||
insertProperty("global.one", dbValue, null, null); | |||
insertProperty("global.one", dbValue, null, null, null, null); | |||
List<PropertyDto> dtos = underTest.selectGlobalProperties(); | |||
assertThat(dtos) | |||
@@ -434,12 +440,12 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void selectGlobalProperty() { | |||
// global | |||
insertProperty("global.one", "one", null, null); | |||
insertProperty("global.two", "two", null, null); | |||
insertProperty("global.one", "one", null, null, null, null); | |||
insertProperty("global.two", "two", null, null, null, null); | |||
// project | |||
insertProperty("project.one", "one", "uuid10", null); | |||
insertProperty("project.one", "one", "uuid10", null, null, "component"); | |||
// user | |||
insertProperty("user.one", "one", null, "100"); | |||
insertProperty("user.one", "one", null, "100", "login", null); | |||
assertThat(underTest.selectGlobalProperty("global.one")) | |||
.extracting(PropertyDto::getComponentUuid, PropertyDto::getUserUuid, PropertyDto::getValue) | |||
@@ -453,7 +459,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
@UseDataProvider("allValuesForSelect") | |||
public void selectGlobalProperty_supports_all_values(String dbValue, String expected) { | |||
insertProperty("global.one", dbValue, null, null); | |||
insertProperty("global.one", dbValue, null, null, null, null); | |||
assertThat(underTest.selectGlobalProperty("global.one")) | |||
.extracting(PropertyDto::getComponentUuid, PropertyDto::getUserUuid, PropertyDto::getValue) | |||
@@ -465,11 +471,11 @@ public class PropertiesDaoTest { | |||
ComponentDto projectDto = insertPrivateProject("A"); | |||
String projectUuid = projectDto.uuid(); | |||
// global | |||
insertProperty("global.one", "one", null, null); | |||
insertProperty("global.two", "two", null, null); | |||
insertProperty("global.one", "one", null, null, null, null); | |||
insertProperty("global.two", "two", null, null, null, null); | |||
// project | |||
insertProperty("project.one", "Pone", projectUuid, null); | |||
insertProperty("project.two", "Ptwo", projectUuid, null); | |||
insertProperty("project.one", "Pone", projectUuid, null, null, projectDto.name()); | |||
insertProperty("project.two", "Ptwo", projectUuid, null, null, projectDto.name()); | |||
List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey()); | |||
assertThat(dtos) | |||
@@ -487,7 +493,7 @@ public class PropertiesDaoTest { | |||
@UseDataProvider("allValuesForSelect") | |||
public void selectProjectProperties_supports_all_values(String dbValue, String expected) { | |||
ComponentDto projectDto = insertPrivateProject("A"); | |||
insertProperty("project.one", dbValue, projectDto.uuid(), null); | |||
insertProperty("project.one", dbValue, projectDto.uuid(), null, null, projectDto.name()); | |||
List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey()); | |||
assertThat(dtos).hasSize(1); | |||
@@ -499,7 +505,7 @@ public class PropertiesDaoTest { | |||
@DataProvider | |||
public static Object[][] allValuesForSelect() { | |||
return new Object[][] { | |||
return new Object[][]{ | |||
{null, ""}, | |||
{"", ""}, | |||
{"some value", "some value"}, | |||
@@ -510,7 +516,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void selectProjectProperty() { | |||
insertProperty("project.one", "one", "uuid10", null); | |||
insertProperty("project.one", "one", "uuid10", null, null, "component"); | |||
PropertyDto property = underTest.selectProjectProperty("uuid10", "project.one"); | |||
@@ -522,17 +528,17 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void select_by_query() { | |||
// global | |||
insertProperty("global.one", "one", null, null); | |||
insertProperty("global.two", "two", null, null); | |||
insertProperty("global.one", "one", null, null, null, null); | |||
insertProperty("global.two", "two", null, null, null, null); | |||
// struts | |||
insertProperty("struts.one", "one", "uuid10", null); | |||
insertProperty("struts.one", "one", "uuid10", null, null, "component"); | |||
// commons | |||
insertProperty("commonslang.one", "one", "uuid11", null); | |||
insertProperty("commonslang.one", "one", "uuid11", null, null, "component"); | |||
// user | |||
insertProperty("user.one", "one", null, "100"); | |||
insertProperty("user.two", "two", "uuid10", "100"); | |||
insertProperty("user.one", "one", null, "100", "login", null); | |||
insertProperty("user.two", "two", "uuid10", "100", "login", "component"); | |||
// other | |||
insertProperty("other.one", "one", "uuid12", null); | |||
insertProperty("other.one", "one", "uuid12", null, null, "component"); | |||
List<PropertyDto> results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentUuid("uuid10") | |||
.setUserUuid("100").build(), db.getSession()); | |||
@@ -547,18 +553,18 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void select_global_properties_by_keys() { | |||
insertPrivateProject("A"); | |||
String userUuid = db.users().insertUser(u -> u.setLogin("B")).getUuid(); | |||
UserDto user = db.users().insertUser(u -> u.setLogin("B")); | |||
String key = "key"; | |||
String anotherKey = "anotherKey"; | |||
insertProperty(key, "value", null, null); | |||
insertProperty(key, "value", "uuid10", null); | |||
insertProperty(key, "value", null, userUuid); | |||
insertProperty(anotherKey, "value", null, null); | |||
insertProperty(key, "value", null, null, null, null); | |||
insertProperty(key, "value", "uuid10", null, null, "component"); | |||
insertProperty(key, "value", null, user.getUuid(), user.getLogin(), null); | |||
insertProperty(anotherKey, "value", null, null, null, null); | |||
insertProperty("key1", "value", null, null); | |||
insertProperty("key2", "value", null, null); | |||
insertProperty("key3", "value", null, null); | |||
insertProperty("key1", "value", null, null, null, null); | |||
insertProperty("key2", "value", null, null, null, null); | |||
insertProperty("key3", "value", null, null, null, null); | |||
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key))) | |||
.extracting("key") | |||
@@ -586,20 +592,21 @@ public class PropertiesDaoTest { | |||
String key = "key"; | |||
String anotherKey = "anotherKey"; | |||
insertProperties( | |||
newGlobalPropertyDto().setKey(key), | |||
newComponentPropertyDto(project).setKey(key), | |||
newComponentPropertyDto(project2).setKey(key), | |||
newComponentPropertyDto(project2).setKey(anotherKey), | |||
newUserPropertyDto(user).setKey(key)); | |||
insertProperties(null, null, newGlobalPropertyDto().setKey(key)); | |||
insertProperties(null, project.name(), newComponentPropertyDto(project).setKey(key)); | |||
insertProperties(null, project2.name(), newComponentPropertyDto(project2).setKey(key), | |||
newComponentPropertyDto(project2).setKey(anotherKey)); | |||
insertProperties(user.getLogin(), null, newUserPropertyDto(user).setKey(key)); | |||
newUserPropertyDto(user).setKey(key); | |||
assertThat(underTest.selectPropertiesByComponentUuids(session, newHashSet(project.uuid()))) | |||
.extracting("key", "componentUuid").containsOnly(tuple(key, project.uuid())); | |||
assertThat(underTest.selectPropertiesByComponentUuids(session, newHashSet(project.uuid(), project2.uuid()))) | |||
.extracting("key", "componentUuid").containsOnly( | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid()), | |||
tuple(anotherKey, project2.uuid())); | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid()), | |||
tuple(anotherKey, project2.uuid())); | |||
assertThat(underTest.selectPropertiesByComponentUuids(session, newHashSet("uuid123456789"))).isEmpty(); | |||
} | |||
@@ -612,24 +619,23 @@ public class PropertiesDaoTest { | |||
String key = "key"; | |||
String anotherKey = "anotherKey"; | |||
insertProperties( | |||
newGlobalPropertyDto().setKey(key), | |||
newComponentPropertyDto(project).setKey(key), | |||
newComponentPropertyDto(project2).setKey(key), | |||
newComponentPropertyDto(project2).setKey(anotherKey), | |||
newUserPropertyDto(user).setKey(key)); | |||
insertProperties(null, null, newGlobalPropertyDto().setKey(key)); | |||
insertProperties(null, project.name(), newComponentPropertyDto(project).setKey(key)); | |||
insertProperties(null, project2.name(), newComponentPropertyDto(project2).setKey(key), | |||
newComponentPropertyDto(project2).setKey(anotherKey)); | |||
insertProperties(user.getLogin(), null, newUserPropertyDto(user).setKey(key)); | |||
assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet(key), newHashSet(project.uuid()))) | |||
.extracting("key", "componentUuid").containsOnly(tuple(key, project.uuid())); | |||
assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet(key), newHashSet(project.uuid(), project2.uuid()))) | |||
.extracting("key", "componentUuid").containsOnly( | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid())); | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid())); | |||
assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet(key, anotherKey), newHashSet(project.uuid(), project2.uuid()))) | |||
.extracting("key", "componentUuid").containsOnly( | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid()), | |||
tuple(anotherKey, project2.uuid())); | |||
tuple(key, project.uuid()), | |||
tuple(key, project2.uuid()), | |||
tuple(anotherKey, project2.uuid())); | |||
assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet("unknown"), newHashSet(project.uuid()))).isEmpty(); | |||
assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet("key"), newHashSet("uuid123456789"))).isEmpty(); | |||
@@ -640,11 +646,10 @@ public class PropertiesDaoTest { | |||
public void select_by_key_and_matching_value() { | |||
ComponentDto project1 = db.components().insertPrivateProject(); | |||
ComponentDto project2 = db.components().insertPrivateProject(); | |||
db.properties().insertProperties( | |||
newComponentPropertyDto("key", "value", project1), | |||
newComponentPropertyDto("key", "value", project2), | |||
newGlobalPropertyDto("key", "value"), | |||
newComponentPropertyDto("another key", "value", project1)); | |||
db.properties().insertProperties(null, project1.name(), newComponentPropertyDto("key", "value", project1)); | |||
db.properties().insertProperties(null, project2.name(), newComponentPropertyDto("key", "value", project2)); | |||
db.properties().insertProperties(null, null, newGlobalPropertyDto("key", "value")); | |||
db.properties().insertProperties(null, project1.name(), newComponentPropertyDto("another key", "value", project1)); | |||
assertThat(underTest.selectByKeyAndMatchingValue(db.getSession(), "key", "value")) | |||
.extracting(PropertyDto::getValue, PropertyDto::getComponentUuid) | |||
@@ -661,13 +666,12 @@ public class PropertiesDaoTest { | |||
ComponentDto project1 = db.components().insertPrivateProject(); | |||
ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project1)); | |||
ComponentDto project2 = db.components().insertPrivateProject(); | |||
db.properties().insertProperties( | |||
newPropertyDto("key", "1", project1, user1), | |||
newPropertyDto("key", "2", project2, user1), | |||
newPropertyDto("key", "3", file1, user1), | |||
newPropertyDto("another key", "4", project1, user1), | |||
newPropertyDto("key", "5", project1, user2), | |||
newGlobalPropertyDto("key", "global")); | |||
db.properties().insertProperties(user1.getLogin(), project1.name(), newPropertyDto("key", "1", project1, user1)); | |||
db.properties().insertProperties(user1.getLogin(), project2.name(), newPropertyDto("key", "2", project2, user1)); | |||
db.properties().insertProperties(user1.getLogin(), file1.name(), newPropertyDto("key", "3", file1, user1)); | |||
db.properties().insertProperties(user1.getLogin(), project1.name(), newPropertyDto("another key", "4", project1, user1)); | |||
db.properties().insertProperties(user2.getLogin(), project1.name(), newPropertyDto("key", "5", project1, user2)); | |||
db.properties().insertProperties(null, null, newGlobalPropertyDto("key", "global")); | |||
assertThat(underTest.selectByKeyAndUserUuidAndComponentQualifier(db.getSession(), "key", user1.getUuid(), "TRK")) | |||
.extracting(PropertyDto::getValue).containsExactlyInAnyOrder("1", "2"); | |||
@@ -786,7 +790,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
@UseDataProvider("valueUpdatesDataProvider") | |||
public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { | |||
String uuid = insertProperty("global", oldValue, null, null); | |||
String uuid = insertProperty("global", oldValue, null, null, null, null); | |||
underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue)); | |||
@@ -810,7 +814,7 @@ public class PropertiesDaoTest { | |||
@UseDataProvider("valueUpdatesDataProvider") | |||
public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { | |||
String componentUuid = "uuid999"; | |||
String uuid = insertProperty("global", oldValue, componentUuid, null); | |||
String uuid = insertProperty("global", oldValue, componentUuid, null, null, "component"); | |||
underTest.saveProperty(new PropertyDto().setKey("global").setComponentUuid(componentUuid).setValue(newValue)); | |||
@@ -833,7 +837,7 @@ public class PropertiesDaoTest { | |||
@UseDataProvider("valueUpdatesDataProvider") | |||
public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { | |||
String userUuid = "uuid-90"; | |||
String uuid = insertProperty("global", oldValue, null, userUuid); | |||
String uuid = insertProperty("global", oldValue, null, userUuid, "login", null); | |||
underTest.saveProperty(new PropertyDto().setKey("global").setUserUuid(userUuid).setValue(newValue)); | |||
@@ -855,7 +859,7 @@ public class PropertiesDaoTest { | |||
@DataProvider | |||
public static Object[][] valueUpdatesDataProvider() { | |||
return new Object[][] { | |||
return new Object[][]{ | |||
{null, null}, | |||
{null, ""}, | |||
{null, "some value"}, | |||
@@ -892,15 +896,15 @@ public class PropertiesDaoTest { | |||
insertPrivateProject("A"); | |||
insertPrivateProject("B"); | |||
insertPrivateProject("C"); | |||
String uuid1 = insertProperty("global.one", "one", null, null); | |||
String uuid2 = insertProperty("global.two", "two", null, null); | |||
String uuid3 = insertProperty("struts.one", "one", "project1", null); | |||
String uuid4 = insertProperty("commonslang.one", "one", "project2", null); | |||
String uuid5 = insertProperty("user.one", "one", null, "100"); | |||
String uuid6 = insertProperty("user.two", "two", null, "100"); | |||
String uuid7 = insertProperty("other.one", "one", "project3", null); | |||
String uuid1 = insertProperty("global.one", "one", null, null, null, null); | |||
String uuid2 = insertProperty("global.two", "two", null, null, null, null); | |||
String uuid3 = insertProperty("struts.one", "one", "project1", null, null, "project1"); | |||
String uuid4 = insertProperty("commonslang.one", "one", "project2", null, null, "project2"); | |||
String uuid5 = insertProperty("user.one", "one", null, "100", "login", null); | |||
String uuid6 = insertProperty("user.two", "two", null, "100", "login", null); | |||
String uuid7 = insertProperty("other.one", "one", "project3", null, null, "project3"); | |||
underTest.deleteProjectProperty("struts.one", "project1"); | |||
underTest.deleteProjectProperty("struts.one", "project1", "project1"); | |||
assertThatPropertiesRowByUuid(uuid1) | |||
.hasKey("global.one") | |||
@@ -938,14 +942,14 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void delete_project_properties() { | |||
String uuid1 = insertProperty("sonar.profile.java", "Sonar Way", "uuid1", null); | |||
String uuid2 = insertProperty("sonar.profile.java", "Sonar Way", "uuid2", null); | |||
String uuid1 = insertProperty("sonar.profile.java", "Sonar Way", "uuid1", null, null, "component"); | |||
String uuid2 = insertProperty("sonar.profile.java", "Sonar Way", "uuid2", null, null, "component"); | |||
String uuid3 = insertProperty("sonar.profile.java", "Sonar Way", null, null); | |||
String uuid3 = insertProperty("sonar.profile.java", "Sonar Way", null, null, null, "component"); | |||
String uuid4 = insertProperty("sonar.profile.js", "Sonar Way", "uuid1", null); | |||
String uuid5 = insertProperty("sonar.profile.js", "Sonar Way", "uuid2", null); | |||
String uuid6 = insertProperty("sonar.profile.js", "Sonar Way", null, null); | |||
String uuid4 = insertProperty("sonar.profile.js", "Sonar Way", "uuid1", null, null, "component"); | |||
String uuid5 = insertProperty("sonar.profile.js", "Sonar Way", "uuid2", null, null, "component"); | |||
String uuid6 = insertProperty("sonar.profile.js", "Sonar Way", null, null, null, "component"); | |||
underTest.deleteProjectProperties("sonar.profile.java", "Sonar Way"); | |||
@@ -978,12 +982,12 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void deleteGlobalProperty() { | |||
// global | |||
String uuid1 = insertProperty("global.key", "new_global", null, null); | |||
String uuid2 = insertProperty("to_be_deleted", "xxx", null, null); | |||
String uuid1 = insertProperty("global.key", "new_global", null, null, null, null); | |||
String uuid2 = insertProperty("to_be_deleted", "xxx", null, null, null, null); | |||
// project - do not delete this project property that has the same key | |||
String uuid3 = insertProperty("to_be_deleted", "new_project", "to_be_deleted", null); | |||
String uuid3 = insertProperty("to_be_deleted", "new_project", "to_be_deleted", null, null, "component"); | |||
// user | |||
String uuid4 = insertProperty("user.key", "new_user", null, "100"); | |||
String uuid4 = insertProperty("user.key", "new_user", null, "100", "login", null); | |||
underTest.deleteGlobalProperty("to_be_deleted"); | |||
@@ -1014,12 +1018,12 @@ public class PropertiesDaoTest { | |||
ComponentDto anotherProject = db.components().insertPrivateProject(); | |||
UserDto user = db.users().insertUser(); | |||
UserDto anotherUser = db.users().insertUser(); | |||
insertProperty("KEY_11", "VALUE", project.uuid(), user.getUuid()); | |||
insertProperty("KEY_12", "VALUE", project.uuid(), user.getUuid()); | |||
insertProperty("KEY_11", "VALUE", project.uuid(), anotherUser.getUuid()); | |||
insertProperty("KEY_11", "VALUE", anotherProject.uuid(), user.getUuid()); | |||
insertProperty("KEY_11", "VALUE", project.uuid(), user.getUuid(), user.getLogin(), project.name()); | |||
insertProperty("KEY_12", "VALUE", project.uuid(), user.getUuid(), user.getLogin(), project.name()); | |||
insertProperty("KEY_11", "VALUE", project.uuid(), anotherUser.getUuid(), anotherUser.getLogin(), project.name()); | |||
insertProperty("KEY_11", "VALUE", anotherProject.uuid(), user.getUuid(), user.getLogin(), anotherProject.name()); | |||
underTest.deleteByUser(session, user.getUuid()); | |||
underTest.deleteByUser(session, user.getUuid(), user.getLogin()); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentUuid(project.uuid()).build(), session)) | |||
.hasSize(1) | |||
@@ -1034,10 +1038,10 @@ public class PropertiesDaoTest { | |||
ComponentDto anotherProject = db.components().insertPrivateProject(); | |||
UserDto user = db.users().insertUser(); | |||
UserDto anotherUser = db.users().insertUser(); | |||
insertProperty("KEY_11", user.getLogin(), project.uuid(), null); | |||
insertProperty("KEY_12", user.getLogin(), project.uuid(), null); | |||
insertProperty("KEY_11", anotherUser.getLogin(), project.uuid(), null); | |||
insertProperty("KEY_11", user.getLogin(), anotherProject.uuid(), null); | |||
insertProperty("KEY_11", user.getLogin(), project.uuid(), null, null, project.name()); | |||
insertProperty("KEY_12", user.getLogin(), project.uuid(), null, null, project.name()); | |||
insertProperty("KEY_11", anotherUser.getLogin(), project.uuid(), null, null, project.name()); | |||
insertProperty("KEY_11", user.getLogin(), anotherProject.uuid(), null, null, anotherProject.name()); | |||
underTest.deleteByMatchingLogin(session, user.getLogin(), newArrayList("KEY_11", "KEY_12")); | |||
@@ -1052,14 +1056,14 @@ public class PropertiesDaoTest { | |||
public void delete_by_key_and_value() { | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
ComponentDto anotherProject = db.components().insertPrivateProject(); | |||
insertProperty("KEY", "VALUE", null, null); | |||
insertProperty("KEY", "VALUE", project.uuid(), null); | |||
insertProperty("KEY", "VALUE", null, "100"); | |||
insertProperty("KEY", "VALUE", project.uuid(), "100"); | |||
insertProperty("KEY", "VALUE", anotherProject.uuid(), null); | |||
insertProperty("KEY", "VALUE", null, null, null, null); | |||
insertProperty("KEY", "VALUE", project.uuid(), null, null, project.name()); | |||
insertProperty("KEY", "VALUE", null, "100", "login", null); | |||
insertProperty("KEY", "VALUE", project.uuid(), "100", "login", project.name()); | |||
insertProperty("KEY", "VALUE", anotherProject.uuid(), null, null, anotherProject.name()); | |||
// Should not be removed | |||
insertProperty("KEY", "ANOTHER_VALUE", null, null); | |||
insertProperty("ANOTHER_KEY", "VALUE", project.uuid(), "100"); | |||
insertProperty("KEY", "ANOTHER_VALUE", null, null, null, null); | |||
insertProperty("ANOTHER_KEY", "VALUE", project.uuid(), "100", "login", project.name()); | |||
underTest.deleteByKeyAndValue(session, "KEY", "VALUE"); | |||
db.commit(); | |||
@@ -1107,7 +1111,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() { | |||
String uuid = insertProperty("to_be_updated", "old_value", null, null); | |||
String uuid = insertProperty("to_be_updated", "old_value", null, null, null, null); | |||
underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value")); | |||
@@ -1133,12 +1137,12 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void renamePropertyKey_updates_global_component_and_user_properties() { | |||
String uuid1 = insertProperty("foo", "bar", null, null); | |||
String uuid2 = insertProperty("old_name", "doc1", null, null); | |||
String uuid3 = insertProperty("old_name", "doc2", "15", null); | |||
String uuid4 = insertProperty("old_name", "doc3", "16", null); | |||
String uuid5 = insertProperty("old_name", "doc4", null, "100"); | |||
String uuid6 = insertProperty("old_name", "doc5", null, "101"); | |||
String uuid1 = insertProperty("foo", "bar", null, null, null, null); | |||
String uuid2 = insertProperty("old_name", "doc1", null, null, null, null); | |||
String uuid3 = insertProperty("old_name", "doc2", "15", null, null, "component"); | |||
String uuid4 = insertProperty("old_name", "doc3", "16", null, null, "component"); | |||
String uuid5 = insertProperty("old_name", "doc4", null, "100", "login", null); | |||
String uuid6 = insertProperty("old_name", "doc5", null, "101", "login", null); | |||
underTest.renamePropertyKey("old_name", "new_name"); | |||
@@ -1182,7 +1186,7 @@ public class PropertiesDaoTest { | |||
@Test | |||
public void rename_to_same_key_has_no_effect() { | |||
String uuid = insertProperty("foo", "bar", null, null); | |||
String uuid = insertProperty("foo", "bar", null, null, null, null); | |||
assertThatPropertiesRowByUuid(uuid) | |||
.hasCreatedAt(INITIAL_DATE); | |||
@@ -1218,19 +1222,20 @@ public class PropertiesDaoTest { | |||
return null; | |||
} | |||
private void insertProperties(PropertyDto... properties) { | |||
private void insertProperties(@Nullable String userLogin, @Nullable String projectName, PropertyDto... properties) { | |||
for (PropertyDto propertyDto : properties) { | |||
underTest.saveProperty(session, propertyDto); | |||
underTest.saveProperty(session, propertyDto, userLogin, projectName); | |||
} | |||
session.commit(); | |||
} | |||
private String insertProperty(String key, @Nullable String value, @Nullable String componentUuid, @Nullable String userUuid) { | |||
private String insertProperty(String key, @Nullable String value, @Nullable String componentUuid, @Nullable String userUuid, | |||
@Nullable String userLogin, @Nullable String projectName) { | |||
PropertyDto dto = new PropertyDto().setKey(key) | |||
.setComponentUuid(componentUuid) | |||
.setUserUuid(userUuid) | |||
.setValue(value); | |||
db.properties().insertProperty(dto); | |||
db.properties().insertProperty(dto, projectName, userLogin); | |||
return (String) db.selectFirst(session, "select uuid as \"uuid\" from properties" + | |||
" where prop_key='" + key + "'" + |
@@ -0,0 +1,429 @@ | |||
/* | |||
* 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.property; | |||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | |||
import java.util.List; | |||
import javax.annotation.Nullable; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.junit.runner.RunWith; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.PropertyNewValue; | |||
import org.sonar.db.component.ComponentDto; | |||
import org.sonar.db.user.UserDto; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
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.times; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoMoreInteractions; | |||
import static org.mockito.Mockito.when; | |||
@RunWith(DataProviderRunner.class) | |||
public class PropertiesDaoWithPersisterTest { | |||
private static final String KEY = "key"; | |||
private static final String ANOTHER_KEY = "another_key"; | |||
private static final String PROJECT_NAME = "project_name"; | |||
private static final String PROJECT_UUID = "project_uuid"; | |||
private static final String SECURED_KEY = "key.secured"; | |||
private static final String USER_LOGIN = "user_login"; | |||
private static final String USER_UUID = "user_uuid"; | |||
private static final String VALUE = "value"; | |||
private static final long INITIAL_DATE = 1_444_000L; | |||
private final AlwaysIncreasingSystem2 system2 = new AlwaysIncreasingSystem2(INITIAL_DATE, 1); | |||
private final AuditPersister auditPersister = mock(AuditPersister.class); | |||
private final ArgumentCaptor<PropertyNewValue> newValueCaptor = ArgumentCaptor.forClass(PropertyNewValue.class); | |||
@Rule | |||
public ExpectedException thrown = ExpectedException.none(); | |||
@Rule | |||
public DbTester db = DbTester.create(system2, auditPersister); | |||
private final DbClient dbClient = db.getDbClient(); | |||
private final DbSession session = db.getSession(); | |||
private final PropertiesDao underTest = dbClient.propertiesDao(); | |||
@Test | |||
public void saveGlobalTrackedPropertyIsPersisted() { | |||
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(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin) | |||
.containsExactly(KEY, VALUE, null, null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@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) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin) | |||
.containsExactly(SECURED_KEY, null, null, null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@Test | |||
public void saveProjectTrackedPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
PropertyDto propertyDto = getPropertyDto(KEY); | |||
underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(), | |||
USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_NAME); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
} | |||
@Test | |||
public void saveProjectTrackedAndSecuredPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true); | |||
PropertyDto propertyDto = getPropertyDto(SECURED_KEY); | |||
underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(SECURED_KEY); | |||
verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(propertyDto.getKey(), null, propertyDto.getUserUuid(), | |||
USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_NAME); | |||
assertThat(newValue.toString()).contains("projectUuid"); | |||
} | |||
@Test | |||
public void deleteTrackedPropertyByQueryIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
PropertyQuery query = getPropertyQuery(KEY); | |||
underTest.deleteByQuery(session, query); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(query.key(), null, query.userUuid(), | |||
null, query.componentUuid(), null); | |||
assertThat(newValue.toString()).doesNotContain("userLogin"); | |||
} | |||
@Test | |||
public void deleteNotTrackedPropertyByQueryIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
PropertyQuery query = getPropertyQuery(KEY); | |||
underTest.deleteByQuery(session, query); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verifyNoMoreInteractions(auditPersister); | |||
} | |||
@Test | |||
public void deleteTrackedPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
PropertyDto propertyDto = getPropertyDto(KEY); | |||
underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(), | |||
USER_LOGIN, propertyDto.getComponentUuid(), PROJECT_NAME); | |||
assertThat(newValue.toString()).contains("userLogin"); | |||
} | |||
@Test | |||
public void deleteNotTrackedPropertyIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
PropertyDto propertyDto = getPropertyDto(KEY); | |||
underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verifyNoMoreInteractions(auditPersister); | |||
} | |||
@Test | |||
public void deleteTrackedProjectPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
underTest.deleteProjectProperty(KEY, PROJECT_UUID, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, null, null, | |||
null, PROJECT_UUID, PROJECT_NAME); | |||
assertThat(newValue.toString()).doesNotContain("userLogin"); | |||
} | |||
@Test | |||
public void deleteNotTrackedProjectPropertyIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
PropertyDto propertyDto = getPropertyDto(KEY); | |||
underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_NAME); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verifyNoMoreInteractions(auditPersister); | |||
} | |||
@Test | |||
public void deleteTrackedProjectPropertiesIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
underTest.deleteProjectProperties(KEY, VALUE); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, VALUE, null, | |||
null, null, null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@Test | |||
public void deleteNotTrackedProjectPropertiesIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
underTest.deleteProjectProperties(KEY, VALUE); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verifyNoMoreInteractions(auditPersister); | |||
} | |||
@Test | |||
public void deleteTrackedGlobalPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
underTest.deleteGlobalProperty(KEY, session); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, null, null, | |||
null, null, null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@Test | |||
public void deleteNotTrackedGlobalPropertyIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
underTest.deleteGlobalProperty(KEY, session); | |||
verify(auditPersister).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)) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, null, user.getUuid(), | |||
user.getLogin(), null, null); | |||
assertThat(newValues.get(0).toString()).contains("userUuid"); | |||
assertThat(newValues.get(1)) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(SECURED_KEY, null, user.getUuid(), | |||
user.getLogin(), null, null); | |||
assertThat(newValues.get(1).toString()).doesNotContain("value"); | |||
} | |||
@Test | |||
public void deletePropertyByUserLoginIsPersisted() { | |||
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)) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, null, null, | |||
user.getLogin(), null, null); | |||
assertThat(newValues.get(0).toString()).contains("userLogin"); | |||
assertThat(newValues.get(1)) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(SECURED_KEY, null, null, | |||
user.getLogin(), null, null); | |||
assertThat(newValues.get(1).toString()).doesNotContain("value"); | |||
} | |||
@Test | |||
public void deleteTrackedPropertyByKeyAndValueIsPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
underTest.deleteByKeyAndValue(session, KEY, VALUE); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, | |||
PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin, | |||
PropertyNewValue::getProjectUuid, PropertyNewValue::getProjectName) | |||
.containsExactly(KEY, VALUE, null, | |||
null, null, null); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@Test | |||
public void deleteNotTrackedPropertyByKeyAndValueIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(false); | |||
underTest.deleteByKeyAndValue(session, KEY, VALUE); | |||
verify(auditPersister).isTrackedProperty(KEY); | |||
verifyNoMoreInteractions(auditPersister); | |||
} | |||
private PropertyDto getPropertyDto(String key) { | |||
return new PropertyDto() | |||
.setKey(key) | |||
.setValue(VALUE) | |||
.setUserUuid(USER_UUID) | |||
.setComponentUuid(PROJECT_UUID); | |||
} | |||
private PropertyQuery getPropertyQuery(String key) { | |||
return PropertyQuery.builder() | |||
.setKey(key) | |||
.setComponentUuid(PROJECT_UUID) | |||
.setUserUuid(USER_UUID) | |||
.build(); | |||
} | |||
private UserDto setUserProperties(@Nullable String value) { | |||
when(auditPersister.isTrackedProperty(KEY)).thenReturn(true); | |||
when(auditPersister.isTrackedProperty(ANOTHER_KEY)).thenReturn(false); | |||
when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
UserDto user = db.users().insertUser(); | |||
if (value == null) { | |||
value = user.getLogin(); | |||
} | |||
PropertyDto dto1 = new PropertyDto().setKey(KEY) | |||
.setComponentUuid(project.uuid()) | |||
.setUserUuid(user.getUuid()) | |||
.setValue(value); | |||
PropertyDto dto2 = new PropertyDto().setKey(ANOTHER_KEY) | |||
.setComponentUuid(project.uuid()) | |||
.setUserUuid(user.getUuid()) | |||
.setValue(value); | |||
PropertyDto dto3 = new PropertyDto().setKey(SECURED_KEY) | |||
.setComponentUuid(project.uuid()) | |||
.setUserUuid(user.getUuid()) | |||
.setValue(value); | |||
db.properties().insertProperty(dto1, project.name(), user.getLogin()); | |||
db.properties().insertProperty(dto2, project.name(), user.getLogin()); | |||
db.properties().insertProperty(dto3, project.name(), user.getLogin()); | |||
List<PropertyDto> list = db.getDbClient().propertiesDao().selectByKeyAndMatchingValue(session, KEY, VALUE); | |||
list = db.getDbClient().propertiesDao().selectByKeyAndMatchingValue(session, ANOTHER_KEY, VALUE); | |||
list = db.getDbClient().propertiesDao().selectByKeyAndMatchingValue(session, SECURED_KEY, VALUE); | |||
return user; | |||
} | |||
} |
@@ -391,7 +391,7 @@ public class PurgeDaoTest { | |||
@Test | |||
public void selectPurgeableAnalyses() { | |||
SnapshotDto[] analyses = new SnapshotDto[] { | |||
SnapshotDto[] analyses = new SnapshotDto[]{ | |||
newSnapshot() | |||
.setUuid("u1") | |||
.setComponentUuid(PROJECT_UUID) | |||
@@ -1618,9 +1618,10 @@ public class PurgeDaoTest { | |||
private void insertPropertyFor(ComponentDto... components) { | |||
Stream.of(components).forEach(componentDto -> db.properties().insertProperty(new PropertyDto() | |||
.setKey(randomAlphabetic(3)) | |||
.setValue(randomAlphabetic(3)) | |||
.setComponentUuid(componentDto.uuid()))); | |||
.setKey(randomAlphabetic(3)) | |||
.setValue(randomAlphabetic(3)) | |||
.setComponentUuid(componentDto.uuid()), | |||
componentDto.name(), null)); | |||
} | |||
private Stream<String> getComponentUuidsOfMeasures() { |
@@ -62,7 +62,7 @@ public class GroupDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void insert_and_update() { | |||
public void insertAndUpdateGroupIsPersisted() { | |||
dbClient.groupDao().insert(db.getSession(), aGroup); | |||
verify(auditPersister).addUserGroup(eq(db.getSession()), newValueCaptor.capture()); | |||
@@ -72,7 +72,7 @@ public class GroupDaoWithPersisterTest { | |||
assertThat(newValue) | |||
.extracting(UserGroupNewValue::getGroupUuid, UserGroupNewValue::getName) | |||
.containsExactly(aGroup.getUuid(), aGroup.getName()); | |||
assertThat(newValue.toString()).doesNotContain("'description':"); | |||
assertThat(newValue.toString()).doesNotContain("description"); | |||
GroupDto dto = new GroupDto() | |||
.setUuid(aGroup.getUuid()) | |||
@@ -87,11 +87,11 @@ public class GroupDaoWithPersisterTest { | |||
assertThat(newValue) | |||
.extracting(UserGroupNewValue::getGroupUuid, UserGroupNewValue::getName, UserGroupNewValue::getDescription) | |||
.containsExactly(dto.getUuid(), dto.getName(), dto.getDescription()); | |||
assertThat(newValue.toString()).contains("'description':"); | |||
assertThat(newValue.toString()).contains("description"); | |||
} | |||
@Test | |||
public void deleteByUuid() { | |||
public void deleteGroupIsPersisted() { | |||
dbClient.groupDao().insert(db.getSession(), aGroup); | |||
verify(auditPersister).addUserGroup(eq(db.getSession()), any()); |
@@ -54,7 +54,7 @@ public class UserDaoWithPersisterTest { | |||
private final UserDao underTest = db.getDbClient().userDao(); | |||
@Test | |||
public void insert_user_with_default_values() { | |||
public void insertUserIsPersisted() { | |||
UserDto userDto = new UserDto() | |||
.setLogin("john") | |||
.setName("John") | |||
@@ -71,11 +71,11 @@ public class UserDaoWithPersisterTest { | |||
assertThat(newValue) | |||
.extracting(UserNewValue::getUserUuid, UserNewValue::getLogin) | |||
.containsExactly(user.getUuid(), user.getLogin()); | |||
assertThat(newValue.toString()).doesNotContain("'name':"); | |||
assertThat(newValue.toString()).doesNotContain("name"); | |||
} | |||
@Test | |||
public void update_user() { | |||
public void updateUserIsPersisted() { | |||
UserDto user = db.users().insertUser(u -> u | |||
.setLogin("john") | |||
.setName("John") | |||
@@ -114,11 +114,11 @@ public class UserDaoWithPersisterTest { | |||
.containsExactly(updatedUser.getUuid(), updatedUser.getLogin(), updatedUser.getName(), updatedUser.getEmail(), updatedUser.isActive(), | |||
updatedUser.getScmAccounts(), updatedUser.getExternalId(), updatedUser.getExternalLogin(), updatedUser.getExternalIdentityProvider(), | |||
updatedUser.isLocal(), updatedUser.isOnboarded(), updatedUser.isRoot(), updatedUser.getLastConnectionDate()); | |||
assertThat(newValue.toString()).contains("'name':"); | |||
assertThat(newValue.toString()).contains("name"); | |||
} | |||
@Test | |||
public void update_user_without_track() { | |||
public void updateUserWithoutTrackIsNotPersisted() { | |||
UserDto user = db.users().insertUser(u -> u | |||
.setLogin("john") | |||
.setName("John") | |||
@@ -139,7 +139,7 @@ public class UserDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void deactivate_user() { | |||
public void deactivateUserIsPersisted() { | |||
UserDto user = insertActiveUser(); | |||
insertUserGroup(user); | |||
underTest.update(db.getSession(), user.setLastConnectionDate(10_000_000_000L)); |
@@ -46,7 +46,7 @@ public class UserGroupDaoWithPersisterTest { | |||
private final UserGroupDao underTest = dbClient.userGroupDao(); | |||
@Test | |||
public void insert() { | |||
public void insertUserGroupIsPersisted() { | |||
UserDto user = db.users().insertUser(); | |||
verify(auditPersister).addUser(eq(db.getSession()), any()); | |||
@@ -64,7 +64,7 @@ public class UserGroupDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void delete_members_by_group_uuid() { | |||
public void deleteUserGroupByGroupIsPersisted() { | |||
UserDto user1 = db.users().insertUser(); | |||
UserDto user2 = db.users().insertUser(); | |||
GroupDto group1 = db.users().insertGroup(); | |||
@@ -85,7 +85,7 @@ public class UserGroupDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void delete_by_user() { | |||
public void deleteUserGroupByUserIsPersisted() { | |||
UserDto user1 = db.users().insertUser(); | |||
UserDto user2 = db.users().insertUser(); | |||
GroupDto group1 = db.users().insertGroup(); |
@@ -52,7 +52,7 @@ public class UserPropertiesDaoWithPersisterTest { | |||
private UserPropertiesDao underTest = db.getDbClient().userPropertiesDao(); | |||
@Test | |||
public void insert_tracked_property() { | |||
public void insertTrackedUserPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(true); | |||
UserDto user = db.users().insertUser(); | |||
@@ -65,7 +65,7 @@ public class UserPropertiesDaoWithPersisterTest { | |||
.setValue("a_value"), | |||
user.getLogin()); | |||
verify(auditPersister).addUserProperty(eq(db.getSession()), newValueCaptor.capture()); | |||
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, | |||
@@ -75,7 +75,7 @@ public class UserPropertiesDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void insert_tracked_secured_property() { | |||
public void insertTrackedAndSecuredUserPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(SECURED_PROPERTY_KEY)).thenReturn(true); | |||
UserDto user = db.users().insertUser(); | |||
@@ -89,17 +89,17 @@ public class UserPropertiesDaoWithPersisterTest { | |||
user.getLogin()); | |||
verify(auditPersister).isTrackedProperty(SECURED_PROPERTY_KEY); | |||
verify(auditPersister).addUserProperty(eq(db.getSession()), newValueCaptor.capture()); | |||
verify(auditPersister).addProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); | |||
PropertyNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, PropertyNewValue::getUserUuid, | |||
PropertyNewValue::getUserLogin) | |||
.containsExactly(userSetting.getKey(), null, user.getUuid(), user.getLogin()); | |||
assertThat(newValue.toString()).doesNotContain("'propertyValue':"); | |||
assertThat(newValue.toString()).doesNotContain("propertyValue"); | |||
} | |||
@Test | |||
public void insert_not_tracked_property() { | |||
public void insertNotTrackedUserPropertyIsNotPersisted() { | |||
when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(false); | |||
UserDto user = db.users().insertUser(); | |||
@@ -115,7 +115,7 @@ public class UserPropertiesDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void update() { | |||
public void updateTrackedUserPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(true); | |||
UserDto user = db.users().insertUser(); | |||
@@ -132,8 +132,8 @@ public class UserPropertiesDaoWithPersisterTest { | |||
user.getLogin()); | |||
verify(auditPersister).addUser(eq(db.getSession()), any()); | |||
verify(auditPersister).addUserProperty(eq(db.getSession()), any()); | |||
verify(auditPersister).updateUserProperty(eq(db.getSession()), newValueCaptor.capture()); | |||
verify(auditPersister).addProperty(eq(db.getSession()), any(), eq(true)); | |||
verify(auditPersister).updateProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); | |||
assertThat(newValueCaptor.getValue()) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, PropertyNewValue::getUserUuid, | |||
PropertyNewValue::getUserLogin) | |||
@@ -141,7 +141,7 @@ public class UserPropertiesDaoWithPersisterTest { | |||
} | |||
@Test | |||
public void delete_by_user() { | |||
public void deleteTrackedUserPropertyIsPersisted() { | |||
when(auditPersister.isTrackedProperty(PROPERTY_KEY)).thenReturn(true); | |||
when(auditPersister.isTrackedProperty(SECURED_PROPERTY_KEY)).thenReturn(false); | |||
@@ -159,11 +159,11 @@ public class UserPropertiesDaoWithPersisterTest { | |||
underTest.deleteByUser(db.getSession(), user); | |||
verify(auditPersister).addUser(eq(db.getSession()), any()); | |||
verify(auditPersister).addUserProperty(eq(db.getSession()), any()); | |||
verify(auditPersister).addUserProperty(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).deleteUserProperty(eq(db.getSession()), newValueCaptor.capture()); | |||
verify(auditPersister).deleteProperty(eq(db.getSession()), newValueCaptor.capture(), eq(true)); | |||
verifyNoMoreInteractions(auditPersister); | |||
assertThat(newValueCaptor.getValue()) | |||
.extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue, PropertyNewValue::getUserUuid, |
@@ -66,7 +66,7 @@ public class UserTokenDaoWithPersisterTest { | |||
assertThat(newValue) | |||
.extracting(UserTokenNewValue::getTokenUuid, UserTokenNewValue::getTokenName, UserTokenNewValue::getUserUuid, UserTokenNewValue::getLastConnectionDate) | |||
.containsExactly(userToken.getUuid(), userToken.getName(), userToken.getUserUuid(), userToken.getLastConnectionDate()); | |||
assertThat(newValue.toString()).contains("'tokenUuid':"); | |||
assertThat(newValue.toString()).contains("tokenUuid"); | |||
} | |||
@Test |
@@ -82,7 +82,7 @@ public class WebhookDaoTest { | |||
.setUrl("URL_1") | |||
.setSecret("a_secret"); | |||
underTest.insert(dbSession, dto); | |||
underTest.insert(dbSession, dto, null); | |||
WebhookDto stored = selectByUuid(dto.getUuid()); | |||
@@ -104,7 +104,7 @@ public class WebhookDaoTest { | |||
.setProjectUuid("UUID_2") | |||
.setSecret("a_secret"); | |||
underTest.insert(dbSession, dto); | |||
underTest.insert(dbSession, dto, "project_name"); | |||
WebhookDto reloaded = selectByUuid(dto.getUuid()); | |||
@@ -122,9 +122,10 @@ public class WebhookDaoTest { | |||
WebhookDto dto = webhookDbTester.insertGlobalWebhook(); | |||
underTest.update(dbSession, dto | |||
.setName("a-fancy-webhook") | |||
.setUrl("http://www.fancy-webhook.io") | |||
.setSecret(null)); | |||
.setName("a-fancy-webhook") | |||
.setUrl("http://www.fancy-webhook.io") | |||
.setSecret(null), | |||
null); | |||
Optional<WebhookDto> optionalResult = underTest.selectByUuid(dbSession, dto.getUuid()); | |||
assertThat(optionalResult).isPresent(); | |||
@@ -143,9 +144,10 @@ public class WebhookDaoTest { | |||
WebhookDto dto = webhookDbTester.insertGlobalWebhook(); | |||
underTest.update(dbSession, dto | |||
.setName("a-fancy-webhook") | |||
.setUrl("http://www.fancy-webhook.io") | |||
.setSecret("a_new_secret")); | |||
.setName("a-fancy-webhook") | |||
.setUrl("http://www.fancy-webhook.io") | |||
.setSecret("a_new_secret"), | |||
null); | |||
Optional<WebhookDto> optionalResult = underTest.selectByUuid(dbSession, dto.getUuid()); | |||
assertThat(optionalResult).isPresent(); | |||
@@ -177,7 +179,7 @@ public class WebhookDaoTest { | |||
public void delete() { | |||
WebhookDto dto = webhookDbTester.insertGlobalWebhook(); | |||
underTest.delete(dbSession, dto.getUuid()); | |||
underTest.delete(dbSession, dto.getUuid(), dto.getName()); | |||
Optional<WebhookDto> reloaded = underTest.selectByUuid(dbSession, dto.getUuid()); | |||
assertThat(reloaded).isEmpty(); | |||
@@ -190,7 +192,7 @@ public class WebhookDaoTest { | |||
.setName("NAME_1") | |||
.setUrl("URL_1"); | |||
underTest.insert(dbSession, dto); | |||
underTest.insert(dbSession, dto, null); | |||
Optional<WebhookDto> reloaded = underTest.selectByUuid(dbSession, dto.getUuid()); | |||
assertThat(reloaded).isPresent(); |
@@ -0,0 +1,137 @@ | |||
/* | |||
* 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.webhook; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.audit.AuditPersister; | |||
import org.sonar.db.audit.model.WebhookNewValue; | |||
import org.sonar.db.component.ComponentDbTester; | |||
import org.sonar.db.project.ProjectDto; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
public class WebhookDaoWithPersisterTest { | |||
private AuditPersister auditPersister = mock(AuditPersister.class); | |||
@Rule | |||
public final DbTester dbTester = DbTester.create(System2.INSTANCE, auditPersister); | |||
private final DbClient dbClient = dbTester.getDbClient(); | |||
private final DbSession dbSession = dbTester.getSession(); | |||
private final WebhookDao underTest = dbClient.webhookDao(); | |||
private final WebhookDbTester webhookDbTester = dbTester.webhooks(); | |||
private final ComponentDbTester componentDbTester = dbTester.components(); | |||
private final ArgumentCaptor<WebhookNewValue> newValueCaptor = ArgumentCaptor.forClass(WebhookNewValue.class); | |||
@Test | |||
public void insertGlobalWebhookIsPersisted() { | |||
WebhookDto dto = new WebhookDto() | |||
.setUuid("UUID_1") | |||
.setName("NAME_1") | |||
.setUrl("URL_1") | |||
.setSecret("a_secret"); | |||
underTest.insert(dbSession, dto, null); | |||
verify(auditPersister).addWebhook(eq(dbSession), newValueCaptor.capture()); | |||
WebhookNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(WebhookNewValue::getWebhookUuid, WebhookNewValue::getName) | |||
.containsExactly(dto.getUuid(), dto.getName()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
@Test | |||
public void insertProjectWebhookIsPersisted() { | |||
WebhookDto dto = new WebhookDto() | |||
.setUuid("UUID_1") | |||
.setName("NAME_1") | |||
.setUrl("URL_1") | |||
.setProjectUuid("UUID_2") | |||
.setSecret("a_secret"); | |||
underTest.insert(dbSession, dto, "project_name"); | |||
verify(auditPersister).addWebhook(eq(dbSession), newValueCaptor.capture()); | |||
WebhookNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(WebhookNewValue::getWebhookUuid, WebhookNewValue::getName, WebhookNewValue::getProjectUuid, | |||
WebhookNewValue::getProjectName) | |||
.containsExactly(dto.getUuid(), dto.getName(), dto.getProjectUuid(), "project_name"); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
@Test | |||
public void updateWebhookIsPersisted() { | |||
WebhookDto dto = webhookDbTester.insertGlobalWebhook(); | |||
dto = dto | |||
.setName("a-fancy-webhook") | |||
.setUrl("http://www.fancy-webhook.io") | |||
.setSecret(null); | |||
underTest.update(dbSession, dto, null); | |||
verify(auditPersister).updateWebhook(eq(dbSession), newValueCaptor.capture()); | |||
WebhookNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(WebhookNewValue::getWebhookUuid, WebhookNewValue::getName, WebhookNewValue::getUrl) | |||
.containsExactly(dto.getUuid(), dto.getName(), dto.getUrl()); | |||
assertThat(newValue.toString()).doesNotContain("projectUuid"); | |||
} | |||
@Test | |||
public void deleteProjectWebhooksIsPersisted() { | |||
ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(); | |||
webhookDbTester.insertWebhook(projectDto); | |||
underTest.deleteByProject(dbSession, projectDto); | |||
verify(auditPersister).deleteWebhook(eq(dbSession), newValueCaptor.capture()); | |||
WebhookNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(WebhookNewValue::getProjectUuid, WebhookNewValue::getProjectName) | |||
.containsExactly(projectDto.getUuid(), projectDto.getName()); | |||
assertThat(newValue.toString()).doesNotContain("webhookUuid"); | |||
} | |||
@Test | |||
public void deleteWebhookIsPersisted() { | |||
WebhookDto dto = webhookDbTester.insertGlobalWebhook(); | |||
underTest.delete(dbSession, dto.getUuid(), dto.getName()); | |||
verify(auditPersister).deleteWebhook(eq(dbSession), newValueCaptor.capture()); | |||
WebhookNewValue newValue = newValueCaptor.getValue(); | |||
assertThat(newValue) | |||
.extracting(WebhookNewValue::getWebhookUuid, WebhookNewValue::getName) | |||
.containsExactly(dto.getUuid(), dto.getName()); | |||
assertThat(newValue.toString()).doesNotContain("url"); | |||
} | |||
} |
@@ -114,7 +114,7 @@ public class WebhookDeliveryDaoTest { | |||
@Test | |||
public void selectByWebhookUuid_returns_records_ordered_by_date() { | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1")); | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_NAME"); | |||
WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); | |||
WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW); | |||
WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "fake-webhook-uuid", "COMPONENT_2", "TASK_1").setCreatedAt(NOW); | |||
@@ -129,7 +129,7 @@ public class WebhookDeliveryDaoTest { | |||
@Test | |||
public void selectByWebhookUuid_returns_records_according_to_pagination() { | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1")); | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_NAME"); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 5_000L)); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 4_000L)); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D3", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 3_000L)); | |||
@@ -144,11 +144,11 @@ public class WebhookDeliveryDaoTest { | |||
@Test | |||
public void selectLatestDelivery_of_a_webhook() { | |||
WebhookDto webhook1 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1")); | |||
WebhookDto webhook1 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"), "COMPONENT_NAME"); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE)); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW)); | |||
WebhookDto webhook2 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1")); | |||
WebhookDto webhook2 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"), "COMPONENT_NAME"); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE)); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW)); | |||
@@ -197,7 +197,7 @@ public class WebhookDeliveryDaoTest { | |||
@Test | |||
public void deleteByWebhook() { | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1")); | |||
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_NAME"); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); | |||
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L)); |
@@ -125,6 +125,10 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { | |||
return new DbTester(System2.INSTANCE, null, null); | |||
} | |||
public static DbTester create(AuditPersister auditPersister) { | |||
return new DbTester(System2.INSTANCE, null, auditPersister); | |||
} | |||
public static DbTester create(System2 system2, AuditPersister auditPersister) { | |||
return new DbTester(system2, null, auditPersister); | |||
} |
@@ -41,7 +41,7 @@ public class AlmPatsDbTester { | |||
private AlmPatDto insert(AlmPatDto dto, Consumer<AlmPatDto>[] populators) { | |||
stream(populators).forEach(p -> p.accept(dto)); | |||
db.getDbClient().almPatDao().insert(db.getSession(), dto); | |||
db.getDbClient().almPatDao().insert(db.getSession(), dto, null, null); | |||
db.commit(); | |||
return dto; | |||
} |
@@ -71,46 +71,60 @@ public class AlmSettingsDbTester { | |||
} | |||
@SafeVarargs | |||
public final ProjectAlmSettingDto insertGitHubProjectAlmSetting(AlmSettingDto githubAlmSetting, ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newGithubProjectAlmSettingDto(githubAlmSetting, project), populators); | |||
public final ProjectAlmSettingDto insertGitHubProjectAlmSetting(AlmSettingDto githubAlmSetting, ProjectDto project, | |||
Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newGithubProjectAlmSettingDto(githubAlmSetting, project), githubAlmSetting.getKey(), | |||
project.getName(), populators); | |||
} | |||
public ProjectAlmSettingDto insertAzureProjectAlmSetting(AlmSettingDto azureAlmSetting, ProjectDto project) { | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project)); | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project), azureAlmSetting.getKey(), | |||
project.getName()); | |||
} | |||
public ProjectAlmSettingDto insertAzureMonoRepoProjectAlmSetting(AlmSettingDto azureAlmSetting, ProjectDto project) { | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project), d -> d.setMonorepo(true)); | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project), azureAlmSetting.getKey(), | |||
project.getName(), d -> d.setMonorepo(true)); | |||
} | |||
public ProjectAlmSettingDto insertGitlabProjectAlmSetting(AlmSettingDto gitlabAlmSetting, ProjectDto project) { | |||
return insertProjectAlmSetting(newGitlabProjectAlmSettingDto(gitlabAlmSetting, project)); | |||
return insertProjectAlmSetting(newGitlabProjectAlmSettingDto(gitlabAlmSetting, project), gitlabAlmSetting.getKey(), | |||
project.getName()); | |||
} | |||
@SafeVarargs | |||
public final ProjectAlmSettingDto insertAzureProjectAlmSetting(AlmSettingDto azureAlmSetting, ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project), populators); | |||
public final ProjectAlmSettingDto insertAzureProjectAlmSetting(AlmSettingDto azureAlmSetting, ProjectDto project, | |||
Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project), azureAlmSetting.getKey(), | |||
project.getName(), populators); | |||
} | |||
@SafeVarargs | |||
public final ProjectAlmSettingDto insertGitlabProjectAlmSetting(AlmSettingDto gitlabAlmSetting, ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newGitlabProjectAlmSettingDto(gitlabAlmSetting, project), populators); | |||
public final ProjectAlmSettingDto insertGitlabProjectAlmSetting(AlmSettingDto gitlabAlmSetting, ProjectDto project, | |||
Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newGitlabProjectAlmSettingDto(gitlabAlmSetting, project), gitlabAlmSetting.getKey(), | |||
project.getName(), populators); | |||
} | |||
@SafeVarargs | |||
public final ProjectAlmSettingDto insertBitbucketCloudProjectAlmSetting(AlmSettingDto bbCloudAlmSetting, ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newBitbucketCloudProjectAlmSettingDto(bbCloudAlmSetting, project), populators); | |||
public final ProjectAlmSettingDto insertBitbucketCloudProjectAlmSetting(AlmSettingDto bbCloudAlmSetting, ProjectDto project, | |||
Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newBitbucketCloudProjectAlmSettingDto(bbCloudAlmSetting, project), bbCloudAlmSetting.getKey(), | |||
project.getName(), populators); | |||
} | |||
@SafeVarargs | |||
public final ProjectAlmSettingDto insertBitbucketProjectAlmSetting(AlmSettingDto bitbucketAlmSetting, ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newBitbucketProjectAlmSettingDto(bitbucketAlmSetting, project), populators); | |||
public final ProjectAlmSettingDto insertBitbucketProjectAlmSetting(AlmSettingDto bitbucketAlmSetting, | |||
ProjectDto project, Consumer<ProjectAlmSettingDto>... populators) { | |||
return insertProjectAlmSetting(newBitbucketProjectAlmSettingDto(bitbucketAlmSetting, project), | |||
bitbucketAlmSetting.getKey(), project.getName(), populators); | |||
} | |||
@SafeVarargs | |||
private final ProjectAlmSettingDto insertProjectAlmSetting(ProjectAlmSettingDto dto, Consumer<ProjectAlmSettingDto>... populators) { | |||
private final ProjectAlmSettingDto insertProjectAlmSetting(ProjectAlmSettingDto dto, String key, String projectName, | |||
Consumer<ProjectAlmSettingDto>... populators) { | |||
stream(populators).forEach(p -> p.accept(dto)); | |||
db.getDbClient().projectAlmSettingDao().insertOrUpdate(db.getSession(), dto); | |||
db.getDbClient().projectAlmSettingDao().insertOrUpdate(db.getSession(), dto, key, projectName); | |||
db.commit(); | |||
return dto; | |||
} |
@@ -38,11 +38,12 @@ public class FavoriteDbTester { | |||
this.dbSession = db.getSession(); | |||
} | |||
public void add(ComponentDto componentDto, String userUuid) { | |||
public void add(ComponentDto componentDto, String userUuid, String userLogin) { | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setUserUuid(userUuid) | |||
.setComponentUuid(componentDto.uuid())); | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setUserUuid(userUuid) | |||
.setComponentUuid(componentDto.uuid()), | |||
userLogin, componentDto.name()); | |||
dbSession.commit(); | |||
} | |||
@@ -70,36 +70,37 @@ public class PermissionTemplateDbTester { | |||
} | |||
public void addGroupToTemplate(PermissionTemplateDto permissionTemplate, GroupDto group, String permission) { | |||
addGroupToTemplate(permissionTemplate.getUuid(), group.getUuid(), permission); | |||
addGroupToTemplate(permissionTemplate.getUuid(), group.getUuid(), permission, permissionTemplate.getName(), group.getName()); | |||
} | |||
public void addGroupToTemplate(String templateUuid, @Nullable String groupUuid, String permission) { | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateUuid, groupUuid, permission); | |||
public void addGroupToTemplate(String templateUuid, @Nullable String groupUuid, String permission, String templateName, @Nullable String groupName) { | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateUuid, groupUuid, permission, templateName, groupName); | |||
db.commit(); | |||
} | |||
public void addAnyoneToTemplate(PermissionTemplateDto permissionTemplate, String permission) { | |||
addGroupToTemplate(permissionTemplate.getUuid(), null, permission); | |||
addGroupToTemplate(permissionTemplate.getUuid(), null, permission, permissionTemplate.getName(), null); | |||
} | |||
public void addUserToTemplate(PermissionTemplateDto permissionTemplate, UserDto user, String permission) { | |||
addUserToTemplate(permissionTemplate.getUuid(), user.getUuid(), permission); | |||
addUserToTemplate(permissionTemplate.getUuid(), user.getUuid(), permission, permissionTemplate.getName(), user.getName()); | |||
} | |||
public void addUserToTemplate(String templateUuid, String userUuid, String permission) { | |||
dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateUuid, userUuid, permission); | |||
public void addUserToTemplate(String templateUuid, String userUuid, String permission, String templateName, String userLogin) { | |||
dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateUuid, userUuid, permission, templateName, userLogin); | |||
db.commit(); | |||
} | |||
public void addProjectCreatorToTemplate(PermissionTemplateDto permissionTemplate, String permission) { | |||
addProjectCreatorToTemplate(permissionTemplate.getUuid(), permission); | |||
addProjectCreatorToTemplate(permissionTemplate.getUuid(), permission, permissionTemplate.getName()); | |||
} | |||
public void addProjectCreatorToTemplate(String templateUuid, String permission) { | |||
public void addProjectCreatorToTemplate(String templateUuid, String permission, String templateName) { | |||
dbClient.permissionTemplateCharacteristicDao().insert(dbSession, newPermissionTemplateCharacteristicDto() | |||
.setWithProjectCreator(true) | |||
.setTemplateUuid(templateUuid) | |||
.setPermission(permission)); | |||
.setWithProjectCreator(true) | |||
.setTemplateUuid(templateUuid) | |||
.setPermission(permission), | |||
templateName); | |||
db.commit(); | |||
} | |||
} |
@@ -47,20 +47,20 @@ public class PropertyDbTester { | |||
this.dbSession = db.getSession(); | |||
} | |||
public PropertyDto insertProperty(PropertyDto property) { | |||
dbClient.propertiesDao().saveProperty(dbSession, property); | |||
public PropertyDto insertProperty(PropertyDto property, @Nullable String componentName, @Nullable String userLogin) { | |||
dbClient.propertiesDao().saveProperty(dbSession, property, userLogin, componentName); | |||
db.commit(); | |||
return property; | |||
} | |||
public void insertProperties(PropertyDto... properties) { | |||
insertProperties(asList(properties)); | |||
public void insertProperties(@Nullable String userLogin, @Nullable String projectName, PropertyDto... properties) { | |||
insertProperties(asList(properties), userLogin, projectName); | |||
} | |||
public void insertProperties(List<PropertyDto> properties) { | |||
public void insertProperties(List<PropertyDto> properties, @Nullable String userLogin, @Nullable String projectName) { | |||
for (PropertyDto propertyDto : properties) { | |||
dbClient.propertiesDao().saveProperty(dbSession, propertyDto); | |||
dbClient.propertiesDao().saveProperty(dbSession, propertyDto, userLogin, projectName); | |||
} | |||
dbSession.commit(); | |||
} | |||
@@ -87,7 +87,8 @@ public class PropertyDbTester { | |||
} else { | |||
propertyDtos.add(newGlobalPropertyDto().setKey(settingBaseKey).setValue(idsValue)); | |||
} | |||
insertProperties(propertyDtos); | |||
String componentName = componentDto == null ? null : componentDto.name(); | |||
insertProperties(propertyDtos, null, componentName); | |||
} | |||
public PropertyDbTester verifyInternal(String key, @Nullable String expectedValue) { |
@@ -200,7 +200,7 @@ public class UserDbTester { | |||
.setUuid(Uuids.createFast()) | |||
.setGroupUuid(null) | |||
.setRole(permission); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto, null); | |||
db.commit(); | |||
return dto; | |||
} | |||
@@ -214,7 +214,7 @@ public class UserDbTester { | |||
.setUuid(Uuids.createFast()) | |||
.setGroupUuid(group.getUuid()) | |||
.setRole(permission); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto, null); | |||
db.commit(); | |||
return dto; | |||
} | |||
@@ -224,7 +224,7 @@ public class UserDbTester { | |||
} | |||
public void deletePermissionFromGroup(GroupDto group, String permission) { | |||
db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, group.getUuid(), null); | |||
db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, group.getUuid(), null, null); | |||
db.commit(); | |||
} | |||
@@ -238,13 +238,13 @@ public class UserDbTester { | |||
.setGroupUuid(null) | |||
.setRole(permission) | |||
.setComponentUuid(project.uuid()); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto, project); | |||
db.commit(); | |||
return dto; | |||
} | |||
public void deleteProjectPermissionFromAnyone(ComponentDto project, String permission) { | |||
db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, project.uuid()); | |||
db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, project.uuid(), project); | |||
db.commit(); | |||
} | |||
@@ -257,7 +257,7 @@ public class UserDbTester { | |||
.setGroupUuid(group.getUuid()) | |||
.setRole(permission) | |||
.setComponentUuid(project.uuid()); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().groupPermissionDao().insert(db.getSession(), dto, project); | |||
db.commit(); | |||
return dto; | |||
} | |||
@@ -292,7 +292,7 @@ public class UserDbTester { | |||
@Deprecated | |||
public UserPermissionDto insertPermissionOnUser(UserDto user, String permission) { | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), permission, user.getUuid(), null); | |||
db.getDbClient().userPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().userPermissionDao().insert(db.getSession(), dto, null); | |||
db.commit(); | |||
return dto; | |||
} | |||
@@ -303,7 +303,7 @@ public class UserDbTester { | |||
} | |||
public void deletePermissionFromUser(ComponentDto project, UserDto user, String permission) { | |||
db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user.getUuid(), permission, project.uuid()); | |||
db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user.getUuid(), permission, project.uuid(), project.name()); | |||
db.commit(); | |||
} | |||
@@ -315,7 +315,7 @@ public class UserDbTester { | |||
"%s can't be granted on a public project", permission); | |||
checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); | |||
UserPermissionDto dto = new UserPermissionDto(Uuids.create(), permission, user.getUuid(), project.uuid()); | |||
db.getDbClient().userPermissionDao().insert(db.getSession(), dto); | |||
db.getDbClient().userPermissionDao().insert(db.getSession(), dto, project); | |||
db.commit(); | |||
return dto; | |||
} |
@@ -20,6 +20,7 @@ | |||
package org.sonar.db.webhook; | |||
import java.util.Optional; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.project.ProjectDto; | |||
@@ -36,16 +37,16 @@ public class WebhookDbTester { | |||
} | |||
public WebhookDto insertGlobalWebhook() { | |||
return insert(newGlobalWebhook()); | |||
return insert(newGlobalWebhook(), null); | |||
} | |||
public WebhookDto insertWebhook(ProjectDto project) { | |||
return insert(newWebhook(project)); | |||
return insert(newWebhook(project), project.getName()); | |||
} | |||
public WebhookDto insert(WebhookDto dto) { | |||
public WebhookDto insert(WebhookDto dto, @Nullable String projectName) { | |||
DbSession dbSession = dbTester.getSession(); | |||
dbTester.getDbClient().webhookDao().insert(dbSession, dto); | |||
dbTester.getDbClient().webhookDao().insert(dbSession, dto, projectName); | |||
dbSession.commit(); | |||
return dto; | |||
} |
@@ -49,7 +49,7 @@ public class CreateAuditTable extends DdlChange { | |||
.addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(UUID_SIZE).build()) | |||
.addColumn(newVarcharColumnBuilder("user_uuid").setIsNullable(false).setLimit(UUID_SIZE).build()) | |||
.addColumn(newVarcharColumnBuilder("user_login").setIsNullable(false).setLimit(255).build()) | |||
.addColumn(newVarcharColumnBuilder("category").setIsNullable(false).setLimit(20).build()) | |||
.addColumn(newVarcharColumnBuilder("category").setIsNullable(false).setLimit(25).build()) | |||
.addColumn(newVarcharColumnBuilder("operation").setIsNullable(false).setLimit(50).build()) | |||
.addColumn(newVarcharColumnBuilder("new_value").setIsNullable(true).setLimit(4000).build()) | |||
.addColumn(createdAtColumn) |
@@ -41,7 +41,7 @@ public class FavoriteUpdater { | |||
/** | |||
* Set favorite to the logged in user. If no user, no action is done | |||
*/ | |||
public void add(DbSession dbSession, ComponentDto componentDto, @Nullable String userUuid, boolean failIfTooManyFavorites) { | |||
public void add(DbSession dbSession, ComponentDto componentDto, @Nullable String userUuid, @Nullable String userLogin, boolean failIfTooManyFavorites) { | |||
if (userUuid == null) { | |||
return; | |||
} | |||
@@ -59,24 +59,28 @@ public class FavoriteUpdater { | |||
return; | |||
} | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setComponentUuid(componentDto.uuid()) | |||
.setUserUuid(userUuid)); | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setComponentUuid(componentDto.uuid()) | |||
.setUserUuid(userUuid), | |||
userLogin, | |||
componentDto.name()); | |||
} | |||
/** | |||
* Remove a favorite to the user. | |||
* | |||
* @throws IllegalArgumentException if the component is not a favorite | |||
*/ | |||
public void remove(DbSession dbSession, ComponentDto component, @Nullable String userUuid) { | |||
public void remove(DbSession dbSession, ComponentDto component, @Nullable String userUuid, @Nullable String userLogin) { | |||
if (userUuid == null) { | |||
return; | |||
} | |||
int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto() | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setComponentUuid(component.uuid()) | |||
.setUserUuid(userUuid)); | |||
.setKey(PROP_FAVORITE_KEY) | |||
.setComponentUuid(component.uuid()) | |||
.setUserUuid(userUuid), | |||
userLogin, component.name()); | |||
checkArgument(result == 1, "Component '%s' is not a favorite", component.getDbKey()); | |||
} | |||
} |
@@ -50,7 +50,7 @@ public class FavoriteUpdaterTest { | |||
UserDto user = db.users().insertUser(); | |||
assertNoFavorite(project, user); | |||
underTest.add(dbSession, project, user.getUuid(), true); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), true); | |||
assertFavorite(project, user); | |||
} | |||
@@ -59,7 +59,7 @@ public class FavoriteUpdaterTest { | |||
public void do_nothing_when_no_user() { | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
underTest.add(dbSession, project, null, true); | |||
underTest.add(dbSession, project, null, null,true); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() | |||
.setComponentUuid(project.uuid()) | |||
@@ -69,13 +69,13 @@ public class FavoriteUpdaterTest { | |||
@Test | |||
public void do_not_add_favorite_when_already_100_favorite_projects() { | |||
UserDto user = db.users().insertUser(); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getUuid())); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getUuid(), user.getName())); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() | |||
.setUserUuid(user.getUuid()) | |||
.build(), dbSession)).hasSize(100); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
underTest.add(dbSession, project, user.getUuid(), false); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), false); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() | |||
.setUserUuid(user.getUuid()) | |||
@@ -85,13 +85,14 @@ public class FavoriteUpdaterTest { | |||
@Test | |||
public void do_not_add_favorite_when_already_100_favorite_portfolios() { | |||
UserDto user = db.users().insertUser(); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getUuid())); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), | |||
user.getUuid(), user.getLogin())); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() | |||
.setUserUuid(user.getUuid()) | |||
.build(), dbSession)).hasSize(100); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
underTest.add(dbSession, project, user.getUuid(), false); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), false); | |||
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() | |||
.setUserUuid(user.getUuid()) | |||
@@ -101,26 +102,27 @@ public class FavoriteUpdaterTest { | |||
@Test | |||
public void fail_when_more_than_100_projects_favorites() { | |||
UserDto user = db.users().insertUser(); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getUuid())); | |||
IntStream.rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), | |||
user.getUuid(), user.getLogin())); | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage("You cannot have more than 100 favorites on components with qualifier 'TRK'"); | |||
underTest.add(dbSession, project, user.getUuid(), true); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), true); | |||
} | |||
@Test | |||
public void fail_when_adding_existing_favorite() { | |||
ComponentDto project = db.components().insertPrivateProject(); | |||
UserDto user = db.users().insertUser(); | |||
underTest.add(dbSession, project, user.getUuid(), true); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), true); | |||
assertFavorite(project, user); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage(String.format("Component '%s' is already a favorite", project.getKey())); | |||
underTest.add(dbSession, project, user.getUuid(), true); | |||
underTest.add(dbSession, project, user.getUuid(), user.getLogin(), true); | |||
} | |||
private void assertFavorite(ComponentDto project, UserDto user) { |
@@ -101,7 +101,8 @@ public class StartupMetadataProviderTest { | |||
} | |||
private void testLoadingFromDatabase(SonarRuntime runtime, boolean isStartupLeader) { | |||
dbTester.properties().insertProperty(new PropertyDto().setKey(CoreProperties.SERVER_STARTTIME).setValue(formatDateTime(A_DATE))); | |||
dbTester.properties().insertProperty(new PropertyDto().setKey(CoreProperties.SERVER_STARTTIME).setValue(formatDateTime(A_DATE)), | |||
null, null); | |||
when(webServer.isStartupLeader()).thenReturn(isStartupLeader); | |||
StartupMetadata metadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); |
@@ -63,8 +63,8 @@ public class AsynchronousWebHooksImplTest { | |||
@Test | |||
public void send_global_webhooks() { | |||
ComponentDto project = componentDbTester.insertPrivateProject(); | |||
webhookDbTester.insert(newGlobalWebhook().setName("First").setUrl("http://url1")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("Second").setUrl("http://url2")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("First").setUrl("http://url1"), null); | |||
webhookDbTester.insert(newGlobalWebhook().setName("Second").setUrl("http://url2"), null); | |||
caller.enqueueSuccess(NOW, 200, 1_234); | |||
caller.enqueueFailure(NOW, new IOException("Fail to connect")); |
@@ -74,7 +74,7 @@ public class SynchronousWebHooksImplTest { | |||
@Test | |||
public void isEnabled_returns_true_if_one_valid_global_webhook() { | |||
ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1")); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1"), projectDto.getName()); | |||
assertThat(underTest.isEnabled(projectDto)).isTrue(); | |||
} | |||
@@ -82,7 +82,7 @@ public class SynchronousWebHooksImplTest { | |||
@Test | |||
public void isEnabled_returns_true_if_one_valid_project_webhook() { | |||
ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1")); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1"), projectDto.getName()); | |||
assertThat(underTest.isEnabled(projectDto)).isTrue(); | |||
} | |||
@@ -113,8 +113,8 @@ public class SynchronousWebHooksImplTest { | |||
@Test | |||
public void send_global_webhooks() { | |||
ComponentDto componentDto = componentDbTester.insertPrivateProject(); | |||
webhookDbTester.insert(newGlobalWebhook().setName("First").setUrl("http://url1")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("Second").setUrl("http://url2")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("First").setUrl("http://url1"), null); | |||
webhookDbTester.insert(newGlobalWebhook().setName("Second").setUrl("http://url2"), null); | |||
caller.enqueueSuccess(NOW, 200, 1_234); | |||
caller.enqueueFailure(NOW, new IOException("Fail to connect")); | |||
@@ -131,7 +131,7 @@ public class SynchronousWebHooksImplTest { | |||
@Test | |||
public void send_project_webhooks() { | |||
ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1")); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("First").setUrl("http://url1"), projectDto.getName()); | |||
caller.enqueueSuccess(NOW, 200, 1_234); | |||
underTest.sendProjectAnalysisUpdate(new WebHooks.Analysis(projectDto.getUuid(), "1", "#1"), () -> mock, taskStatistics); | |||
@@ -146,11 +146,11 @@ public class SynchronousWebHooksImplTest { | |||
@Test | |||
public void send_global_and_project_webhooks() { | |||
ProjectDto projectDto = componentDbTester.insertPrivateProjectDto(); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("1First").setUrl("http://url1")); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("2Second").setUrl("http://url2")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("3Third").setUrl("http://url3")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("4Fourth").setUrl("http://url4")); | |||
webhookDbTester.insert(newGlobalWebhook().setName("5Fifth").setUrl("http://url5")); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("1First").setUrl("http://url1"), projectDto.getName()); | |||
webhookDbTester.insert(newWebhook(projectDto).setName("2Second").setUrl("http://url2"), projectDto.getName()); | |||
webhookDbTester.insert(newGlobalWebhook().setName("3Third").setUrl("http://url3"), null); | |||
webhookDbTester.insert(newGlobalWebhook().setName("4Fourth").setUrl("http://url4"), null); | |||
webhookDbTester.insert(newGlobalWebhook().setName("5Fifth").setUrl("http://url5"), null); | |||
caller.enqueueSuccess(NOW, 200, 1_234); | |||
caller.enqueueFailure(NOW, new IOException("Fail to connect 1")); | |||
caller.enqueueFailure(NOW, new IOException("Fail to connect 2")); |
@@ -54,7 +54,7 @@ public class PluginConsentVerifier implements Startable { | |||
if (hasExternalPlugins && NOT_ACCEPTED == PluginRiskConsent.valueOf(property.getValue())) { | |||
addWarningInSonarDotLog(); | |||
property.setValue(REQUIRED.name()); | |||
dbClient.propertiesDao().saveProperty(session, property); | |||
dbClient.propertiesDao().saveProperty(session, property, null, null); | |||
session.commit(); | |||
} else if (!hasExternalPlugins && REQUIRED == PluginRiskConsent.valueOf(property.getValue())) { | |||
dbClient.propertiesDao().deleteGlobalProperty(PLUGINS_RISK_CONSENT, session); |
@@ -46,6 +46,6 @@ public class ProjectDefaultVisibility { | |||
public void set(DbSession dbSession, Visibility visibility) { | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() | |||
.setKey(PROJECTS_DEFAULT_VISIBILITY_PROPERTY_NAME) | |||
.setValue(visibility.getLabel())); | |||
.setValue(visibility.getLabel()), null, null); | |||
} | |||
} |
@@ -88,7 +88,7 @@ public class UserUpdaterUpdateTest { | |||
.setName("Marius2") | |||
.setEmail("marius2@mail.com") | |||
.setScmAccounts(singletonList("ma2")), u -> { | |||
}); | |||
}); | |||
UserDto updatedUser = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(updatedUser.isActive()).isTrue(); | |||
@@ -116,7 +116,7 @@ public class UserUpdaterUpdateTest { | |||
.setName("Marius2") | |||
.setEmail("marius2@email.com") | |||
.setExternalIdentity(new ExternalIdentity("github", "john", "ABCD")), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getExternalId()).isEqualTo("ABCD"); | |||
@@ -134,7 +134,7 @@ public class UserUpdaterUpdateTest { | |||
.setName("Marius2") | |||
.setEmail("marius2@email.com") | |||
.setExternalIdentity(new ExternalIdentity("github", "john", "ABCD")), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getExternalId()).isEqualTo("ABCD"); | |||
@@ -157,7 +157,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail("marius2@mail.com") | |||
.setPassword("password2") | |||
.setScmAccounts(asList("ma2", "", null)), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getScmAccountsAsList()).containsOnly("ma2"); | |||
@@ -170,7 +170,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setLogin("new_login"), u -> { | |||
}); | |||
}); | |||
assertThat(dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN)).isNull(); | |||
UserDto userReloaded = dbClient.userDao().selectByUuid(session, user.getUuid()); | |||
@@ -194,7 +194,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setLogin("new_login"), u -> { | |||
}); | |||
}); | |||
assertThat(dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN)).isNull(); | |||
UserDto userReloaded = dbClient.userDao().selectByUuid(session, user.getUuid()); | |||
@@ -218,7 +218,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, oldUser, new UpdateUser() | |||
.setLogin("new_login"), u -> { | |||
}); | |||
}); | |||
List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.TYPE_USER); | |||
assertThat(indexUsers).hasSize(1); | |||
@@ -233,16 +233,18 @@ public class UserUpdaterUpdateTest { | |||
ComponentDto project1 = db.components().insertPrivateProject(); | |||
ComponentDto project2 = db.components().insertPrivateProject(); | |||
ComponentDto anotherProject = db.components().insertPrivateProject(); | |||
db.properties().insertProperties( | |||
db.properties().insertProperties(oldUser.getLogin(), project1.name(), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setComponentUuid(project1.uuid()), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setComponentUuid(project2.uuid()), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setComponentUuid(project1.uuid())); | |||
db.properties().insertProperties(oldUser.getLogin(), project2.name(), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setComponentUuid(project2.uuid())); | |||
db.properties().insertProperties(oldUser.getLogin(), anotherProject.name(), | |||
new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue("another login").setComponentUuid(anotherProject.uuid())); | |||
userIndexer.indexAll(); | |||
underTest.updateAndCommit(session, oldUser, new UpdateUser() | |||
.setLogin("new_login"), u -> { | |||
}); | |||
}); | |||
assertThat(db.getDbClient().propertiesDao().selectByQuery(PropertyQuery.builder().setKey(DEFAULT_ISSUE_ASSIGNEE).build(), db.getSession())) | |||
.extracting(PropertyDto::getValue, PropertyDto::getComponentUuid) | |||
@@ -263,7 +265,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setName("Marius2"), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getName()).isEqualTo("Marius2"); | |||
@@ -285,7 +287,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setEmail("marius2@mail.com"), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getEmail()).isEqualTo("marius2@mail.com"); | |||
@@ -307,7 +309,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setScmAccounts(asList("ma2")), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getScmAccountsAsList()).containsOnly("ma2"); | |||
@@ -327,7 +329,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setScmAccounts(asList("ma", "marius33")), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getScmAccountsAsList()).containsOnly("ma", "marius33"); | |||
@@ -341,7 +343,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setScmAccounts(null), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getScmAccounts()).isNull(); | |||
@@ -357,7 +359,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setPassword("password2"), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getSalt()).isNotEqualTo("salt"); | |||
@@ -380,7 +382,7 @@ public class UserUpdaterUpdateTest { | |||
underTest.updateAndCommit(session, user, new UpdateUser() | |||
.setPassword("password2"), u -> { | |||
}); | |||
}); | |||
UserDto dto = dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN); | |||
assertThat(dto.getSalt()).isNotEqualTo("salt"); | |||
@@ -453,7 +455,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail(user.getEmail()) | |||
.setScmAccounts(user.getScmAccountsAsList()) | |||
.setExternalIdentity(new ExternalIdentity(user.getExternalIdentityProvider(), user.getExternalLogin(), user.getExternalId())), u -> { | |||
}); | |||
}); | |||
assertThat(dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN).getUpdatedAt()).isEqualTo(user.getUpdatedAt()); | |||
} | |||
@@ -470,7 +472,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail(user.getEmail()) | |||
.setScmAccounts(asList("ma2", "ma1")) | |||
.setExternalIdentity(new ExternalIdentity(user.getExternalIdentityProvider(), user.getExternalLogin(), user.getExternalId())), u -> { | |||
}); | |||
}); | |||
assertThat(dbClient.userDao().selectByLogin(session, DEFAULT_LOGIN).getUpdatedAt()).isEqualTo(user.getUpdatedAt()); | |||
} | |||
@@ -487,7 +489,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail("marius2@mail.com") | |||
.setPassword("password2") | |||
.setScmAccounts(asList("ma2")), u -> { | |||
}, otherUser); | |||
}, otherUser); | |||
assertThat(es.getIds(UserIndexDefinition.TYPE_USER)).containsExactlyInAnyOrder(user.getUuid(), otherUser.getUuid()); | |||
} | |||
@@ -527,7 +529,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail("marius2@mail.com") | |||
.setPassword("password2") | |||
.setScmAccounts(asList("ma2")), u -> { | |||
}); | |||
}); | |||
Multimap<String, String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); | |||
assertThat(groups.get(DEFAULT_LOGIN).stream().anyMatch(g -> g.equals(defaultGroup.getName()))).isFalse(); | |||
@@ -548,7 +550,7 @@ public class UserUpdaterUpdateTest { | |||
.setEmail("marius2@mail.com") | |||
.setPassword("password2") | |||
.setScmAccounts(asList("ma2")), u -> { | |||
}); | |||
}); | |||
// Nothing as changed | |||
groups = dbClient.groupMembershipDao().selectGroupsByLogins(session, asList(DEFAULT_LOGIN)); |
@@ -72,7 +72,7 @@ public class PersistentSettings { | |||
if (value == null) { | |||
dbClient.propertiesDao().deleteGlobalProperty(key, dbSession); | |||
} else { | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(key).setValue(value)); | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(key).setValue(value), null, null); | |||
} | |||
// refresh the cache of settings | |||
delegate.setProperty(key, value); |
@@ -139,7 +139,7 @@ public class ServerIdManager implements Startable { | |||
} | |||
private void persistServerId(DbSession dbSession, ServerId serverId) { | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(SERVER_ID).setValue(serverId.toString())); | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(SERVER_ID).setValue(serverId.toString()), null, null); | |||
} | |||
private void persistChecksum(DbSession dbSession, String checksump) { |
@@ -114,7 +114,7 @@ public class RegisterPermissionTemplates implements Startable { | |||
} | |||
private void insertGroupPermission(DbSession dbSession, PermissionTemplateDto template, String permission, GroupDto group) { | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getUuid(), group.getUuid(), permission); | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getUuid(), group.getUuid(), permission, template.getName(), group.getName()); | |||
} | |||
} |
@@ -236,8 +236,7 @@ public class ServerIdManagerTest { | |||
try { | |||
test(SERVER); | |||
fail("An ISE should have been raised"); | |||
} | |||
catch (IllegalStateException e) { | |||
} catch (IllegalStateException e) { | |||
assertThat(e.getMessage()).isEqualTo("Server ID is invalid"); | |||
// no changes | |||
verifyDb(serverId, dbChecksum); | |||
@@ -284,8 +283,7 @@ public class ServerIdManagerTest { | |||
try { | |||
test(SERVER); | |||
fail("An ISE should have been raised"); | |||
} | |||
catch (IllegalStateException e) { | |||
} catch (IllegalStateException e) { | |||
assertThat(e.getMessage()).isEqualTo("Server ID is invalid"); | |||
// no changes | |||
verifyDb(serverId, dbChecksum); | |||
@@ -294,10 +292,10 @@ public class ServerIdManagerTest { | |||
@DataProvider | |||
public static Object[][] allFormatsOfServerId() { | |||
return new Object[][] { | |||
{OLD_FORMAT_SERVER_ID}, | |||
{NO_DATABASE_ID_SERVER_ID}, | |||
{WITH_DATABASE_ID_SERVER_ID} | |||
return new Object[][]{ | |||
{OLD_FORMAT_SERVER_ID}, | |||
{NO_DATABASE_ID_SERVER_ID}, | |||
{WITH_DATABASE_ID_SERVER_ID} | |||
}; | |||
} | |||
@@ -346,7 +344,8 @@ public class ServerIdManagerTest { | |||
} | |||
private void insertServerId(String serverId) { | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(CoreProperties.SERVER_ID).setValue(serverId)); | |||
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(CoreProperties.SERVER_ID).setValue(serverId), | |||
null, null); | |||
dbSession.commit(); | |||
} | |||
@@ -171,7 +171,7 @@ public class PermissionIndexerDaoTest { | |||
.setGroupUuid(group.getUuid()) | |||
.setRole(USER) | |||
.setComponentUuid(project.uuid()); | |||
dbClient.groupPermissionDao().insert(dbSession, dto); | |||
dbClient.groupPermissionDao().insert(dbSession, dto, project); | |||
} | |||
dbSession.commit(); | |||
@@ -111,13 +111,13 @@ public class SetPatAction implements AlmIntegrationsWsAction { | |||
if (almPatDto.isPresent()) { | |||
AlmPatDto almPat = almPatDto.get(); | |||
almPat.setPersonalAccessToken(resultingPat); | |||
dbClient.almPatDao().update(dbSession, almPat); | |||
dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSetting.getKey()); | |||
} else { | |||
AlmPatDto almPat = new AlmPatDto() | |||
.setPersonalAccessToken(resultingPat) | |||
.setAlmSettingUuid(almSetting.getUuid()) | |||
.setUserUuid(userUuid); | |||
dbClient.almPatDao().insert(dbSession, almPat); | |||
dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSetting.getKey()); | |||
} | |||
dbSession.commit(); | |||
} |
@@ -20,9 +20,7 @@ | |||
package org.sonar.server.almintegration.ws.azure; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import java.util.Optional; | |||
import org.sonar.alm.client.azure.AzureDevOpsHttpClient; | |||
import org.sonar.alm.client.azure.GsonAzureRepo; | |||
import org.sonar.api.server.ws.Request; | |||
@@ -137,6 +135,7 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction { | |||
.setQualifier(PROJECT) | |||
.build(), | |||
userSession.isLoggedIn() ? userSession.getUuid() : null, | |||
userSession.isLoggedIn() ? userSession.getLogin() : null, | |||
repo.getDefaultBranchName(), | |||
s -> { | |||
}); | |||
@@ -149,7 +148,7 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction { | |||
.setAlmSlug(repo.getProject().getName()) | |||
.setProjectUuid(componentDto.uuid()) | |||
.setMonorepo(false); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), componentDto.name()); | |||
} | |||
@VisibleForTesting |
@@ -131,8 +131,9 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction { | |||
.setQualifier(PROJECT) | |||
.build(); | |||
String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null; | |||
String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null; | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, defaultBranchName, p -> { | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userLogin, defaultBranchName, p -> { | |||
}); | |||
} | |||
@@ -143,7 +144,7 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction { | |||
.setAlmRepo(repo.getSlug()) | |||
.setProjectUuid(componentDto.uuid()) | |||
.setMonorepo(false); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), componentDto.name()); | |||
} | |||
} |
@@ -148,8 +148,9 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi | |||
.setQualifier(PROJECT) | |||
.build(); | |||
String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null; | |||
String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null; | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, defaultBranchName, p -> {}); | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userLogin, defaultBranchName, p -> {}); | |||
} | |||
private void populatePRSetting(DbSession dbSession, Repository repo, ComponentDto componentDto, AlmSettingDto almSettingDto) { | |||
@@ -159,7 +160,7 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi | |||
.setAlmSlug(repo.getSlug()) | |||
.setProjectUuid(componentDto.uuid()) | |||
.setMonorepo(false); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), componentDto.name()); | |||
} | |||
} |
@@ -137,7 +137,7 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { | |||
.setPrivate(visibility) | |||
.setQualifier(PROJECT) | |||
.build(), | |||
userSession.getUuid(), mainBranchName, s -> {}); | |||
userSession.getUuid(), userSession.getLogin(), mainBranchName, s -> {}); | |||
} | |||
static String getProjectKeyFromRepository(Repository repo) { | |||
@@ -152,6 +152,6 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { | |||
.setProjectUuid(componentDto.uuid()) | |||
.setSummaryCommentEnabled(true) | |||
.setMonorepo(false); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto); | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), componentDto.name()); | |||
} | |||
} |
@@ -122,13 +122,13 @@ public class ListGithubOrganizationsAction implements AlmIntegrationsWsAction { | |||
if (almPatDto.isPresent()) { | |||
AlmPatDto almPat = almPatDto.get(); | |||
almPat.setPersonalAccessToken(accessToken.getValue()); | |||
dbClient.almPatDao().update(dbSession, almPat); | |||
dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey()); | |||
} else { | |||
AlmPatDto almPat = new AlmPatDto() | |||
.setPersonalAccessToken(accessToken.getValue()) | |||
.setAlmSettingUuid(almSettingDto.getUuid()) | |||
.setUserUuid(userUuid); | |||
dbClient.almPatDao().insert(dbSession, almPat); | |||
dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey()); | |||
} | |||
dbSession.commit(); | |||
} else { |
@@ -124,11 +124,13 @@ public class ImportGitLabProjectAction implements AlmIntegrationsWsAction { | |||
private void populateMRSetting(DbSession dbSession, Long gitlabProjectId, ComponentDto componentDto, AlmSettingDto almSettingDto) { | |||
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, new ProjectAlmSettingDto() | |||
.setProjectUuid(componentDto.projectUuid()) | |||
.setAlmSettingUuid(almSettingDto.getUuid()) | |||
.setAlmRepo(gitlabProjectId.toString()) | |||
.setAlmSlug(null) | |||
.setMonorepo(false)); | |||
.setProjectUuid(componentDto.projectUuid()) | |||
.setAlmSettingUuid(almSettingDto.getUuid()) | |||
.setAlmRepo(gitlabProjectId.toString()) | |||
.setAlmSlug(null) | |||
.setMonorepo(false), | |||
almSettingDto.getKey(), | |||
componentDto.name()); | |||
} | |||
private ComponentDto createProject(DbSession dbSession, Project gitlabProject, @Nullable String mainBranchName) { | |||
@@ -136,12 +138,12 @@ public class ImportGitLabProjectAction implements AlmIntegrationsWsAction { | |||
String sqProjectKey = generateProjectKey(gitlabProject.getPathWithNamespace(), uuidFactory.create()); | |||
return componentUpdater.createWithoutCommit(dbSession, newComponentBuilder() | |||
.setKey(sqProjectKey) | |||
.setName(gitlabProject.getName()) | |||
.setPrivate(visibility) | |||
.setQualifier(PROJECT) | |||
.build(), | |||
userSession.getUuid(), mainBranchName, s -> { | |||
.setKey(sqProjectKey) | |||
.setName(gitlabProject.getName()) | |||
.setPrivate(visibility) | |||
.setQualifier(PROJECT) | |||
.build(), | |||
userSession.getUuid(), userSession.getLogin(), mainBranchName, s -> { | |||
}); | |||
} | |||
@@ -152,6 +152,7 @@ public class ReportSubmitter { | |||
private ComponentDto createProject(DbSession dbSession, BranchSupport.ComponentKey componentKey, @Nullable String projectName) { | |||
userSession.checkPermission(GlobalPermission.PROVISION_PROJECTS); | |||
String userUuid = userSession.getUuid(); | |||
String userName = userSession.getLogin(); | |||
boolean wouldCurrentUserHaveScanPermission = permissionTemplateService.wouldUserHaveScanPermissionWithDefaultTemplate( | |||
dbSession, userUuid, componentKey.getDbKey()); | |||
@@ -165,7 +166,7 @@ public class ReportSubmitter { | |||
.setQualifier(Qualifiers.PROJECT) | |||
.setPrivate(getDefaultVisibility(dbSession).isPrivate()) | |||
.build(); | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, c -> { | |||
return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userName, c -> { | |||
}); | |||
} | |||
@@ -78,8 +78,8 @@ public class ComponentUpdater { | |||
* - Add component to favorite if the component has the 'Project Creators' permission | |||
* - Index component in es indexes | |||
*/ | |||
public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable String userUuid) { | |||
ComponentDto componentDto = createWithoutCommit(dbSession, newComponent, userUuid, c -> { | |||
public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin) { | |||
ComponentDto componentDto = createWithoutCommit(dbSession, newComponent, userUuid, userLogin, c -> { | |||
}); | |||
commitAndIndex(dbSession, componentDto); | |||
return componentDto; | |||
@@ -90,8 +90,8 @@ public class ComponentUpdater { | |||
* Don't forget to call commitAndIndex(...) when ready to commit. | |||
*/ | |||
public ComponentDto createWithoutCommit(DbSession dbSession, NewComponent newComponent, | |||
@Nullable String userUuid, Consumer<ComponentDto> componentModifier) { | |||
return createWithoutCommit(dbSession, newComponent, userUuid, null, componentModifier); | |||
@Nullable String userUuid, @Nullable String userLogin, Consumer<ComponentDto> componentModifier) { | |||
return createWithoutCommit(dbSession, newComponent, userUuid, userLogin, null, componentModifier); | |||
} | |||
/** | |||
@@ -99,14 +99,14 @@ public class ComponentUpdater { | |||
* Don't forget to call commitAndIndex(...) when ready to commit. | |||
*/ | |||
public ComponentDto createWithoutCommit(DbSession dbSession, NewComponent newComponent, | |||
@Nullable String userUuid, @Nullable String mainBranchName, | |||
@Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName, | |||
Consumer<ComponentDto> componentModifier) { | |||
checkKeyFormat(newComponent.qualifier(), newComponent.key()); | |||
ComponentDto componentDto = createRootComponent(dbSession, newComponent, componentModifier); | |||
if (isRootProject(componentDto)) { | |||
createMainBranch(dbSession, componentDto.uuid(), mainBranchName); | |||
} | |||
handlePermissionTemplate(dbSession, componentDto, userUuid); | |||
handlePermissionTemplate(dbSession, componentDto, userUuid, userLogin); | |||
return componentDto; | |||
} | |||
@@ -175,11 +175,11 @@ public class ComponentUpdater { | |||
dbClient.branchDao().upsert(session, branch); | |||
} | |||
private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable String userUuid) { | |||
private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable String userUuid, @Nullable String userLogin) { | |||
permissionTemplateService.applyDefault(dbSession, componentDto, userUuid); | |||
if (componentDto.qualifier().equals(PROJECT) | |||
&& permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, componentDto)) { | |||
favoriteUpdater.add(dbSession, componentDto, userUuid, false); | |||
favoriteUpdater.add(dbSession, componentDto, userUuid, userLogin, false); | |||
} | |||
} | |||
@@ -96,7 +96,9 @@ public class AddAction implements FavoritesWsAction { | |||
userSession | |||
.checkLoggedIn() | |||
.checkComponentPermission(USER, componentDto); | |||
favoriteUpdater.add(dbSession, componentDto, userSession.isLoggedIn() ? userSession.getUuid() : null, true); | |||
String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null; | |||
String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null; | |||
favoriteUpdater.add(dbSession, componentDto, userUuid, userLogin, true); | |||
dbSession.commit(); | |||
} | |||
}; |
@@ -74,7 +74,9 @@ public class RemoveAction implements FavoritesWsAction { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
ComponentDto component = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_COMPONENT)); | |||
userSession.checkLoggedIn(); | |||
favoriteUpdater.remove(dbSession, component, userSession.isLoggedIn() ? userSession.getUuid() : null); | |||
favoriteUpdater.remove(dbSession, component, | |||
userSession.isLoggedIn() ? userSession.getUuid() : null, | |||
userSession.isLoggedIn() ? userSession.getLogin() : null); | |||
dbSession.commit(); | |||
} | |||
}; |
@@ -48,6 +48,7 @@ public class NotificationUpdater { | |||
public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { | |||
String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); | |||
String projectUuid = project == null ? null : project.uuid(); | |||
String projectName = project == null ? null : project.name(); | |||
List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( | |||
PropertyQuery.builder() | |||
@@ -65,7 +66,8 @@ public class NotificationUpdater { | |||
.setKey(key) | |||
.setUserUuid(user.getUuid()) | |||
.setValue(PROP_NOTIFICATION_VALUE) | |||
.setComponentUuid(projectUuid)); | |||
.setComponentUuid(projectUuid), | |||
user.getLogin(), projectName); | |||
} | |||
/** | |||
@@ -74,6 +76,7 @@ public class NotificationUpdater { | |||
public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { | |||
String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); | |||
String projectUuid = project == null ? null : project.uuid(); | |||
String projectName = project == null ? null : project.name(); | |||
List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( | |||
PropertyQuery.builder() | |||
@@ -90,7 +93,7 @@ public class NotificationUpdater { | |||
.setKey(key) | |||
.setUserUuid(user.getUuid()) | |||
.setValue(PROP_NOTIFICATION_VALUE) | |||
.setComponentUuid(projectUuid)); | |||
.setComponentUuid(projectUuid), user.getLogin(), projectName); | |||
} | |||
private static Predicate<PropertyDto> notificationScope(@Nullable ComponentDto project) { |
@@ -120,7 +120,7 @@ public class GroupPermissionChanger { | |||
.setRole(change.getPermission()) | |||
.setGroupUuid(change.getGroupUuidOrAnyone().getUuid()) | |||
.setComponentUuid(change.getProjectUuid()); | |||
dbClient.groupPermissionDao().insert(dbSession, addedDto); | |||
dbClient.groupPermissionDao().insert(dbSession, addedDto, change.getProject()); | |||
return true; | |||
} | |||
@@ -137,7 +137,8 @@ public class GroupPermissionChanger { | |||
dbClient.groupPermissionDao().delete(dbSession, | |||
change.getPermission(), | |||
change.getGroupUuidOrAnyone().getUuid(), | |||
change.getProjectUuid()); | |||
change.getProjectUuid(), | |||
change.getProject()); | |||
return true; | |||
} | |||
@@ -126,8 +126,8 @@ public class PermissionTemplateService { | |||
} | |||
private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable String projectCreatorUserUuid) { | |||
dbClient.groupPermissionDao().deleteByRootComponentUuid(dbSession, project.uuid()); | |||
dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project.uuid()); | |||
dbClient.groupPermissionDao().deleteByRootComponentUuid(dbSession, project.uuid(), project.name()); | |||
dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project.uuid(), project.name()); | |||
List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid()); | |||
Map<String, String> userDtoMap = dbClient.userDao().selectByUuids(dbSession, usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet())) | |||
@@ -137,7 +137,7 @@ public class PermissionTemplateService { | |||
.filter(up -> permissionValidForProject(project, up.getPermission())) | |||
.forEach(up -> { | |||
UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), up.getPermission(), userDtoMap.get(up.getUserUuid()), project.uuid()); | |||
dbClient.userPermissionDao().insert(dbSession, dto); | |||
dbClient.userPermissionDao().insert(dbSession, dto, project); | |||
}); | |||
List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateUuid(dbSession, template.getUuid()); | |||
@@ -151,7 +151,7 @@ public class PermissionTemplateService { | |||
.setGroupUuid(isAnyone(gp.getGroupName()) ? null : gp.getGroupUuid()) | |||
.setRole(gp.getPermission()) | |||
.setComponentUuid(project.uuid()); | |||
dbClient.groupPermissionDao().insert(dbSession, dto); | |||
dbClient.groupPermissionDao().insert(dbSession, dto, project); | |||
}); | |||
List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid())); | |||
@@ -168,7 +168,7 @@ public class PermissionTemplateService { | |||
.filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())) | |||
.forEach(c -> { | |||
UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), c.getPermission(), userDto.getUuid(), project.uuid()); | |||
dbClient.userPermissionDao().insert(dbSession, dto); | |||
dbClient.userPermissionDao().insert(dbSession, dto, project); | |||
}); | |||
} | |||
} |
@@ -98,7 +98,7 @@ public class UserPermissionChanger { | |||
} | |||
UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), change.getPermission(), change.getUserId().getUuid(), | |||
change.getProjectUuid()); | |||
dbClient.userPermissionDao().insert(dbSession, dto); | |||
dbClient.userPermissionDao().insert(dbSession, dto, change.getProject()); | |||
return true; | |||
} | |||
@@ -108,8 +108,10 @@ public class UserPermissionChanger { | |||
} | |||
checkOtherAdminsExist(dbSession, change); | |||
String projectUuid = change.getProjectUuid(); | |||
String projectName = change.getProject() == null ? null : change.getProject().name(); | |||
if (projectUuid != null) { | |||
dbClient.userPermissionDao().deleteProjectPermission(dbSession, change.getUserId().getUuid(), change.getPermission(), projectUuid); | |||
dbClient.userPermissionDao().deleteProjectPermission(dbSession, change.getUserId().getUuid(), change.getPermission(), | |||
projectUuid, projectName); | |||
} else { | |||
dbClient.userPermissionDao().deleteGlobalPermission(dbSession, change.getUserId().getUuid(), change.getPermission()); | |||
} |
@@ -40,6 +40,7 @@ import static org.sonar.server.permission.ws.WsParameters.createGroupIdParameter | |||
import static org.sonar.server.permission.ws.WsParameters.createGroupNameParameter; | |||
import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters; | |||
import static org.sonar.server.permission.ws.template.WsTemplateRef.fromRequest; | |||
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME; | |||
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; | |||
public class AddGroupToTemplateAction implements PermissionsWsAction { | |||
@@ -86,7 +87,8 @@ public class AddGroupToTemplateAction implements PermissionsWsAction { | |||
checkGlobalAdmin(userSession); | |||
if (!groupAlreadyAdded(dbSession, template.getUuid(), permission, group)) { | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getUuid(), group.getUuid(), permission); | |||
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getUuid(), group.getUuid(), permission, | |||
template.getName(), request.param(PARAM_GROUP_NAME)); | |||
dbSession.commit(); | |||
} | |||
} |
@@ -99,7 +99,7 @@ public class AddProjectCreatorToTemplateAction implements PermissionsWsAction { | |||
Optional<PermissionTemplateCharacteristicDto> templatePermission = dbClient.permissionTemplateCharacteristicDao() | |||
.selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getUuid()); | |||
if (templatePermission.isPresent()) { | |||
updateTemplatePermission(dbSession, templatePermission.get()); | |||
updateTemplatePermission(dbSession, templatePermission.get(), template.getName()); | |||
} else { | |||
addTemplatePermission(dbSession, request, template); | |||
} | |||
@@ -109,20 +109,21 @@ public class AddProjectCreatorToTemplateAction implements PermissionsWsAction { | |||
private void addTemplatePermission(DbSession dbSession, AddProjectCreatorToTemplateRequest request, PermissionTemplateDto template) { | |||
long now = system.now(); | |||
dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto() | |||
.setUuid(Uuids.create()) | |||
.setPermission(request.getPermission()) | |||
.setTemplateUuid(template.getUuid()) | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(now) | |||
.setUpdatedAt(now)); | |||
.setUuid(Uuids.create()) | |||
.setPermission(request.getPermission()) | |||
.setTemplateUuid(template.getUuid()) | |||
.setWithProjectCreator(true) | |||
.setCreatedAt(now) | |||
.setUpdatedAt(now), | |||
template.getName()); | |||
dbSession.commit(); | |||
} | |||
private void updateTemplatePermission(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission) { | |||
private void updateTemplatePermission(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission, String templateName) { | |||
PermissionTemplateCharacteristicDto targetTemplatePermission = templatePermission | |||
.setUpdatedAt(system.now()) | |||
.setWithProjectCreator(true); | |||
dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission); | |||
dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission, templateName); | |||
dbSession.commit(); | |||
} | |||