From c250b15a9a3d57d9d05bf8503f0c24972175cef1 Mon Sep 17 00:00:00 2001 From: lukasz-jarocki-sonarsource Date: Fri, 2 Aug 2024 11:19:39 +0200 Subject: [PATCH] SONAR-22649 adding users to dump --- .../org/sonar/db/createdb/PopulateDb.java | 48 +++++++++++++++++-- .../java/org/sonar/db/user/UserDbTester.java | 4 ++ .../java/org/sonar/db/user/UserTesting.java | 32 +++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java index 884bdfc1aeb..6993dd9dd11 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -34,6 +35,7 @@ import org.sonar.api.rules.RuleType; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.ce.CeTaskMessageType; import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; @@ -43,13 +45,20 @@ import org.sonar.db.issue.IssueDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.rule.RuleDto; +import org.sonar.db.user.TokenType; +import org.sonar.db.user.UserDto; import static org.sonar.db.component.BranchType.BRANCH; public class PopulateDb { - public static void main(String[] args) { - final DbTester dbTester = createDbTester(); + private static final DbTester dbTester = createDbTester(); + + private static final Random random = new Random(); + + private static final long NUMBER_OF_USERS = 100_000L; + + public static void main(String[] args) { // read base data final Map metricDtosByKey; final List allProjects; @@ -62,13 +71,44 @@ public class PopulateDb { allProjects = dbTester.getDbClient().projectDao().selectProjects(initSession); enabledRules = new HashSet<>(dbTester.getDbClient().ruleDao().selectEnabled(dbTester.getSession())); - generateProject(new SqContext(enabledRules, metricDtosByKey, dbTester), + ProjectDto projectDto = generateProject(new SqContext(enabledRules, metricDtosByKey, dbTester), new ProjectStructure("project " + (allProjects.size() + 1), 10, 10, 10, 2, 5)); + allProjects.add(projectDto); - // close database connexion + createUsers(allProjects); + // close database connection dbTester.getDbClient().getDatabase().stop(); } + private static List createUsers(List allProjects) { + List allUsers = new ArrayList<>(); + for (int i = 0; i < NUMBER_OF_USERS; i++) { + UserDto userDto = dbTester.users().insertUserRealistic(); + allUsers.add(userDto); + ProjectDto projectDto = random.nextBoolean() ? null : allProjects.get(random.nextInt(allProjects.size())); + if (i % 60 == 0 && projectDto != null) { + createUserTokensDto(userDto, projectDto); + } + if (i % 50 == 5 && projectDto != null) { + createUserDismissedMessages(userDto, projectDto); + } + } + return allUsers; + } + + private static void createUserDismissedMessages(UserDto userDto, ProjectDto projectDto) { + CeTaskMessageType type = random.nextBoolean() ? CeTaskMessageType.GENERIC : CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE; + dbTester.users().insertUserDismissedMessage(userDto, projectDto, type); + } + + private static void createUserTokensDto(UserDto userDto, ProjectDto randomProject) { + long now = System.currentTimeMillis(); + Long expirationDate = random.nextBoolean() ? now + 123_123 : null; + dbTester.users().insertToken(userDto, a -> a.setCreatedAt(now).setExpirationDate(expirationDate).setProjectKey(randomProject.getKey()) + .setLastConnectionDate(now).setType(randomProject.getKey() != null ? TokenType.PROJECT_ANALYSIS_TOKEN.name() : + TokenType.USER_TOKEN.name())); + } + private record SqContext(Set rules, Map metricDtosByKey, DbTester dbTester) { public RuleDto findNotSecurityHotspotRule() { return rules.stream().filter(r -> r.getType() != RuleType.SECURITY_HOTSPOT.getDbConstant()).findAny().orElseThrow(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java index e9815ac5b1b..a899bbc043e 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java @@ -63,6 +63,10 @@ public class UserDbTester { return insertUser(UserTesting.newUserDto()); } + public UserDto insertUserRealistic() { + return insertUser(UserTesting.newUserDtoRealistic()); + } + public UserDto insertUser(String login) { UserDto dto = UserTesting.newUserDto().setLogin(login).setActive(true); return insertUser(dto); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserTesting.java index d38b73d036c..d328f16842e 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserTesting.java @@ -22,12 +22,16 @@ package org.sonar.db.user; import javax.annotation.Nullable; import static java.util.Collections.singletonList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.apache.commons.lang.math.RandomUtils.nextBoolean; +import static org.apache.commons.lang.math.RandomUtils.nextInt; import static org.apache.commons.lang.math.RandomUtils.nextLong; public class UserTesting { + private static final String[] realisticIdentityProviders = {"github", "google", "microsoft"}; + public static UserDto newUserDto() { return new UserDto() .setUuid(randomAlphanumeric(40)) @@ -46,6 +50,34 @@ public class UserTesting { .setUpdatedAt(nextLong()); } + public static UserDto newUserDtoRealistic() { + long timeNow = System.currentTimeMillis(); + String loginAndAndId = randomAlphanumeric(30); + String realisticIdentityProvider = realisticIdentityProviders[nextInt(realisticIdentityProviders.length)]; + boolean isExternal = nextBoolean(); + String externalIdAndLogin = isExternal ? loginAndAndId + "_" + realisticIdentityProvider : loginAndAndId; + return new UserDto().setUuid(randomAlphanumeric(40)) + .setActive(nextBoolean()) + .setLocal(!isExternal) + .setLogin(loginAndAndId) + .setName(loginAndAndId + " " + loginAndAndId) + .setEmail(loginAndAndId + "@" + loginAndAndId + ".com") + .setScmAccounts(loginAndAndId + "@github") + .setExternalId(externalIdAndLogin) + .setExternalLogin(externalIdAndLogin) + .setExternalIdentityProvider(isExternal ? realisticIdentityProvider : "sonarqube") + .setSalt("ZLqSawNE/T7QNk+FLsSWiJ7D9qM=") + .setHashMethod("PBKDF2") + // password is "admin2" + .setCryptedPassword("100000$arHk2+TbNYyFeUgAsDBz7O5M+W0Y3NKJGgvz0KsURHzfXaTXlLT0WYI3DWwXOgHLgyFidVJ4HF22h7zbJoaa8g==") + .setCreatedAt(timeNow) + .setUpdatedAt(timeNow) + .setLastConnectionDate(nextBoolean() ? timeNow : null) + .setResetPassword(nextBoolean() && nextBoolean() && nextBoolean()) + .setHomepageParameter(nextInt(10) + "") + .setHomepageType("projects"); + } + public static UserDto newUserDto(String login, String name, @Nullable String email) { return newUserDto() .setName(name) -- 2.39.5