3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.version.v72;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.assertj.core.groups.Tuple;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.api.utils.internal.TestSystem2;
30 import org.sonar.api.utils.log.LogTester;
31 import org.sonar.api.utils.log.LoggerLevel;
32 import org.sonar.core.util.SequenceUuidFactory;
33 import org.sonar.db.CoreDbTester;
35 import static java.lang.String.format;
36 import static java.util.stream.Collectors.toList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.tuple;
40 public class UpdateNullValuesFromExternalColumnsAndLoginOfUsersTest {
42 private static final long PAST = 5_000_000_000L;
43 private static final long NOW = 10_000_000_000L;
46 public ExpectedException expectedException = ExpectedException.none();
49 public CoreDbTester db = CoreDbTester.createForSchema(UpdateNullValuesFromExternalColumnsAndLoginOfUsersTest.class, "users.sql");
52 public LogTester logTester = new LogTester();
54 private System2 system2 = new TestSystem2().setNow(NOW);
56 private UpdateNullValuesFromExternalColumnsAndLoginOfUsers underTest = new UpdateNullValuesFromExternalColumnsAndLoginOfUsers(db.database(), system2, new SequenceUuidFactory());
59 public void update_users() throws SQLException {
60 insertUser("USER_1", "user1", "github");
61 insertUser("USER_2", null, null);
62 insertUser("USER_3", "user", null);
63 insertUser("USER_4", null, "github");
64 insertUser(null, "user", "bitbucket");
65 insertUser(null, null, null);
70 tuple("USER_1", "user1", "github", PAST),
71 tuple("USER_2", "USER_2", "sonarqube", NOW),
72 tuple("USER_3", "USER_3", "sonarqube", NOW),
73 tuple("USER_4", "USER_4", "sonarqube", NOW),
74 tuple("1", "1", "sonarqube", NOW),
75 tuple("2", "2", "sonarqube", NOW));
79 public void log_warning_when_login_is_null() throws SQLException {
80 insertUser(null, "user", "bitbucket");
81 long id = (long) db.selectFirst("SELECT ID FROM USERS").get("ID");
85 assertThat(logTester.logs(LoggerLevel.WARN))
86 .containsExactlyInAnyOrder(format("No login has been found for user id '%s'. A UUID has been generated to not have null value.", id));
90 public void is_reentrant() throws SQLException {
91 insertUser("USER_1", null, null);
96 assertUsers(tuple("USER_1", "USER_1", "sonarqube", NOW));
99 private void assertUsers(Tuple... expectedTuples) {
100 assertThat(db.select("SELECT LOGIN, EXTERNAL_LOGIN, EXTERNAL_IDENTITY_PROVIDER, UPDATED_AT FROM USERS")
102 .map(map -> new Tuple(map.get("LOGIN"), map.get("EXTERNAL_LOGIN"), map.get("EXTERNAL_IDENTITY_PROVIDER"), map.get("UPDATED_AT")))
104 .containsExactlyInAnyOrder(expectedTuples);
107 private void insertUser(@Nullable String login, @Nullable String externalLogin, @Nullable String externalIdentityProvider) {
108 db.executeInsert("USERS",
110 "EXTERNAL_LOGIN", externalLogin,
111 "EXTERNAL_IDENTITY_PROVIDER", externalIdentityProvider,