You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateUserLocalValueInUsersTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.db.migration.version.v100;
  21. import java.sql.SQLException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.core.util.UuidFactory;
  28. import org.sonar.core.util.UuidFactoryFast;
  29. import org.sonar.db.CoreDbTester;
  30. import org.sonar.server.platform.db.migration.step.DataChange;
  31. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  32. import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. public class UpdateUserLocalValueInUsersTest {
  35. private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
  36. @Rule
  37. public CoreDbTester db = CoreDbTester.createForSchema(UpdateUserLocalValueInUsersTest.class, "schema.sql");
  38. private final DataChange underTest = new UpdateUserLocalValueInUsers(db.database());
  39. @Test
  40. public void migration_updates_user_local_if_null() throws SQLException {
  41. String userUuid1 = insertUser(true);
  42. String userUuid2 = insertUser(false);
  43. String userUuid3 = insertUser(null);
  44. underTest.execute();
  45. assertUserLocalIsUpdatedCorrectly(userUuid1, true);
  46. assertUserLocalIsUpdatedCorrectly(userUuid2, false);
  47. assertUserLocalIsUpdatedCorrectly(userUuid3, true);
  48. }
  49. @Test
  50. public void migration_should_be_reentrant() throws SQLException {
  51. String userUuid1 = insertUser(true);
  52. String userUuid2 = insertUser(false);
  53. String userUuid3 = insertUser(null);
  54. underTest.execute();
  55. // re-entrant
  56. underTest.execute();
  57. assertUserLocalIsUpdatedCorrectly(userUuid1, true);
  58. assertUserLocalIsUpdatedCorrectly(userUuid2, false);
  59. assertUserLocalIsUpdatedCorrectly(userUuid3, true);
  60. }
  61. private void assertUserLocalIsUpdatedCorrectly(String userUuid, boolean expected) {
  62. String selectSql = String.format("select USER_LOCAL from users where uuid='%s'", userUuid);
  63. assertThat(db.select(selectSql).stream()
  64. .map(row -> row.get("USER_LOCAL"))
  65. .toList())
  66. .containsExactlyInAnyOrder(expected);
  67. }
  68. private String insertUser(Boolean userLocal) {
  69. Map<String, Object> map = new HashMap<>();
  70. String uuid = uuidFactory.create();
  71. map.put("UUID", uuid);
  72. map.put("LOGIN", randomAlphabetic(20));
  73. map.put("EXTERNAL_LOGIN", randomAlphabetic(20));
  74. map.put("EXTERNAL_IDENTITY_PROVIDER", "sonarqube");
  75. map.put("EXTERNAL_ID", randomNumeric(5));
  76. map.put("CREATED_AT", System.currentTimeMillis());
  77. map.put("USER_LOCAL", userLocal);
  78. map.put("RESET_PASSWORD", false);
  79. db.executeInsert("users", map);
  80. return uuid;
  81. }
  82. }