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.

UpdateNullValuesFromExternalColumnsAndLoginOfUsersTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.v72;
  21. import java.sql.SQLException;
  22. import javax.annotation.Nullable;
  23. import org.assertj.core.groups.Tuple;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.impl.utils.TestSystem2;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.api.utils.log.LogTester;
  30. import org.sonar.api.utils.log.LoggerLevel;
  31. import org.sonar.core.util.SequenceUuidFactory;
  32. import org.sonar.db.CoreDbTester;
  33. import static java.lang.String.format;
  34. import static java.util.stream.Collectors.toList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.api.Assertions.tuple;
  37. public class UpdateNullValuesFromExternalColumnsAndLoginOfUsersTest {
  38. private static final long PAST = 5_000_000_000L;
  39. private static final long NOW = 10_000_000_000L;
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. @Rule
  43. public CoreDbTester db = CoreDbTester.createForSchema(UpdateNullValuesFromExternalColumnsAndLoginOfUsersTest.class, "users.sql");
  44. @Rule
  45. public LogTester logTester = new LogTester();
  46. private System2 system2 = new TestSystem2().setNow(NOW);
  47. private UpdateNullValuesFromExternalColumnsAndLoginOfUsers underTest = new UpdateNullValuesFromExternalColumnsAndLoginOfUsers(db.database(), system2, new SequenceUuidFactory());
  48. @Test
  49. public void update_users() throws SQLException {
  50. insertUser("USER_1", "user1", "user1", "github");
  51. insertUser("USER_2", null, null, null);
  52. insertUser("USER_3", "user", null, null);
  53. insertUser("USER_4", null, "user", null);
  54. insertUser("USER_5", null, null, "github");
  55. insertUser(null, "user", "user", "bitbucket");
  56. insertUser(null, null, null, null);
  57. underTest.execute();
  58. assertUsers(
  59. tuple("USER_1", "user1", "user1", "github", PAST),
  60. tuple("USER_2", "USER_2", "USER_2", "sonarqube", NOW),
  61. tuple("USER_3", "user", "user", "sonarqube", NOW),
  62. tuple("USER_4", "USER_4", "USER_4", "sonarqube", NOW),
  63. tuple("USER_5", "USER_5", "USER_5", "github", NOW),
  64. tuple("1", "user", "user", "bitbucket", NOW),
  65. tuple("2", "2", "2", "sonarqube", NOW));
  66. }
  67. @Test
  68. public void log_warning_when_login_is_null() throws SQLException {
  69. insertUser(null, "user", "user", "bitbucket");
  70. long id = (long) db.selectFirst("SELECT ID FROM USERS").get("ID");
  71. underTest.execute();
  72. assertThat(logTester.logs(LoggerLevel.WARN))
  73. .containsExactlyInAnyOrder(format("No login has been found for user id '%s'. A UUID has been generated to not have null value.", id));
  74. }
  75. @Test
  76. public void is_reentrant() throws SQLException {
  77. insertUser("USER_1", null, null, null);
  78. underTest.execute();
  79. underTest.execute();
  80. assertUsers(tuple("USER_1", "USER_1", "USER_1", "sonarqube", NOW));
  81. }
  82. private void assertUsers(Tuple... expectedTuples) {
  83. assertThat(db.select("SELECT LOGIN, EXTERNAL_LOGIN, EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, UPDATED_AT FROM USERS")
  84. .stream()
  85. .map(map -> new Tuple(map.get("LOGIN"), map.get("EXTERNAL_LOGIN"), map.get("EXTERNAL_ID"), map.get("EXTERNAL_IDENTITY_PROVIDER"), map.get("UPDATED_AT")))
  86. .collect(toList()))
  87. .containsExactlyInAnyOrder(expectedTuples);
  88. }
  89. private void insertUser(@Nullable String login, @Nullable String externalLogin, @Nullable String externalId, @Nullable String externalIdentityProvider) {
  90. db.executeInsert("USERS",
  91. "LOGIN", login,
  92. "EXTERNAL_LOGIN", externalLogin,
  93. "EXTERNAL_ID", externalLogin,
  94. "EXTERNAL_IDENTITY_PROVIDER", externalIdentityProvider,
  95. "CREATED_AT", PAST,
  96. "UPDATED_AT", PAST,
  97. "IS_ROOT", false,
  98. "ONBOARDED", false);
  99. }
  100. }