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.

FixEmptyIdentityProviderInUsersTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.v66;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.sql.SQLException;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.Nullable;
  25. import org.assertj.core.groups.Tuple;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.db.CoreDbTester;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.tuple;
  33. public class FixEmptyIdentityProviderInUsersTest {
  34. private final static long PAST = 100_000_000_000l;
  35. private final static long NOW = 500_000_000_000l;
  36. private System2 system2 = new TestSystem2().setNow(NOW);
  37. @Rule
  38. public CoreDbTester db = CoreDbTester.createForSchema(FixEmptyIdentityProviderInUsersTest.class, "users.sql");
  39. private FixEmptyIdentityProviderInUsers underTest = new FixEmptyIdentityProviderInUsers(db.database(), system2);
  40. @Test
  41. public void execute_has_no_effect_if_tables_are_empty() throws SQLException {
  42. underTest.execute();
  43. }
  44. @Test
  45. public void migrate_user_without_external_identity_info() throws SQLException {
  46. insertUser("userWithoutExternalIdentityInfo", null, null);
  47. underTest.execute();
  48. assertUsers(tuple("userWithoutExternalIdentityInfo", "userWithoutExternalIdentityInfo", "sonarqube", NOW));
  49. }
  50. @Test
  51. public void migrate_user_with_partial_external_identity_info() throws SQLException {
  52. insertUser("userWithoutExternalIdentity", "user", null);
  53. insertUser("userWithoutExternalIdentityProvider", null, "github");
  54. underTest.execute();
  55. assertUsers(tuple("userWithoutExternalIdentity", "userWithoutExternalIdentity", "sonarqube", NOW),
  56. tuple("userWithoutExternalIdentityProvider", "userWithoutExternalIdentityProvider", "sonarqube", NOW));
  57. }
  58. @Test
  59. public void does_not_migrate_user_with_external_identity_info() throws SQLException {
  60. insertUser("userWithIdentityInfo", "user", "sonarqube");
  61. underTest.execute();
  62. assertUsers(tuple("userWithIdentityInfo", "user", "sonarqube", PAST));
  63. }
  64. private void insertUser(String login, @Nullable String externalIdentity, @Nullable String externalIdentityProvider) {
  65. ImmutableMap.Builder<String, Object> map = ImmutableMap.<String, Object>builder()
  66. .put("LOGIN", login)
  67. .put("IS_ROOT", true)
  68. .put("ONBOARDED", true)
  69. .put("CREATED_AT", PAST)
  70. .put("UPDATED_AT", PAST);
  71. if (externalIdentity != null) {
  72. map.put("EXTERNAL_IDENTITY", externalIdentity);
  73. }
  74. if (externalIdentityProvider != null) {
  75. map.put("EXTERNAL_IDENTITY_PROVIDER", externalIdentityProvider);
  76. }
  77. db.executeInsert("USERS", map.build());
  78. }
  79. private void assertUsers(Tuple... expectedTuples) {
  80. assertThat(db.select("SELECT LOGIN, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, UPDATED_AT FROM USERS")
  81. .stream()
  82. .map(map -> new Tuple(map.get("LOGIN"), map.get("EXTERNAL_IDENTITY"), map.get("EXTERNAL_IDENTITY_PROVIDER"), map.get("UPDATED_AT")))
  83. .collect(Collectors.toList()))
  84. .containsExactlyInAnyOrder(expectedTuples);
  85. }
  86. }