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 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.junit.rules.ExpectedException;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.utils.internal.TestSystem2;
31 import org.sonar.db.CoreDbTester;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.tuple;
36 public class PopulateExternalIdOnUsersTest {
38 private static final long PAST = 5_000_000_000L;
39 private static final long NOW = 10_000_000_000L;
42 public ExpectedException expectedException = ExpectedException.none();
45 public CoreDbTester db = CoreDbTester.createForSchema(PopulateExternalIdOnUsersTest.class, "users.sql");
47 private System2 system2 = new TestSystem2().setNow(NOW);
49 private PopulateExternalIdOnUsers underTest = new PopulateExternalIdOnUsers(db.database(), system2);
52 public void update_users() throws SQLException {
53 insertUser("USER_1", "user1", null);
54 insertUser("USER_2", "user2", "1234");
59 tuple("USER_1", "user1", NOW),
60 tuple("USER_2", "1234", PAST));
64 public void migration_is_reentrant() throws SQLException {
65 insertUser("USER_1", "user1", null);
66 insertUser("USER_2", "user2", "1234");
72 tuple("USER_1", "user1", NOW),
73 tuple("USER_2", "1234", PAST));
76 private void assertUsers(Tuple... expectedTuples) {
77 assertThat(db.select("SELECT LOGIN, EXTERNAL_ID, UPDATED_AT FROM USERS")
79 .map(map -> new Tuple(map.get("LOGIN"), map.get("EXTERNAL_ID"), map.get("UPDATED_AT")))
80 .collect(Collectors.toList()))
81 .containsExactlyInAnyOrder(expectedTuples);
84 private void insertUser(String login, String externalLogin, @Nullable String externalId) {
85 db.executeInsert("USERS",
87 "EXTERNAL_ID", externalId,
88 "EXTERNAL_LOGIN", externalLogin,