3 * Copyright (C) 2009-2020 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.v84.users.fk.qprofileeditusers;
22 import java.sql.SQLException;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.core.util.Uuids;
26 import org.sonar.db.CoreDbTester;
27 import org.sonar.server.platform.db.migration.step.DataChange;
29 import static org.assertj.core.api.Assertions.assertThat;
31 public class PopulateQProfileEditUsersUserUuidTest {
34 public CoreDbTester db = CoreDbTester.createForSchema(PopulateQProfileEditUsersUserUuidTest.class, "schema.sql");
36 private DataChange underTest = new PopulateQProfileEditUsersUserUuid(db.database());
39 public void populate_uuids() throws SQLException {
41 String userUuid_1 = "uuid-1";
42 insertUser(userId_1, userUuid_1);
45 String userUuid_2 = "uuid-2";
46 insertUser(userId_2, userUuid_2);
49 String userUuid_3 = "uuid-3";
50 insertUser(userId_3, userUuid_3);
53 String userUuid_4 = "uuid-4";
54 insertUser(userId_4, userUuid_4);
56 String qprofileEditUserUuid_1 = Uuids.createFast();
57 insertQProfileEditUser(qprofileEditUserUuid_1, userId_1);
58 String qprofileEditUserUuid_2 = Uuids.createFast();
59 insertQProfileEditUser(qprofileEditUserUuid_2, userId_2);
60 String qprofileEditUserUuid_3 = Uuids.createFast();
61 insertQProfileEditUser(qprofileEditUserUuid_3, userId_3);
62 String qprofileEditUserUuid_4 = Uuids.createFast();
63 insertQProfileEditUser(qprofileEditUserUuid_4, userId_4);
64 String qprofileEditUserUuid_5 = Uuids.createFast();
65 insertQProfileEditUser(qprofileEditUserUuid_5, userId_1);
69 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_1, userUuid_1);
70 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_2, userUuid_2);
71 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_3, userUuid_3);
72 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_4, userUuid_4);
73 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_5, userUuid_1);
77 public void migration_is_reentrant() throws SQLException {
79 String userUuid_1 = "uuid-1";
80 insertUser(userId_1, userUuid_1);
83 String userUuid_2 = "uuid-2";
84 insertUser(userId_2, userUuid_2);
87 String userUuid_3 = "uuid-3";
88 insertUser(userId_3, userUuid_3);
91 String userUuid_4 = "uuid-4";
92 insertUser(userId_4, userUuid_4);
94 String qprofileEditUserUuid_1 = Uuids.createFast();
95 insertQProfileEditUser(qprofileEditUserUuid_1, userId_1);
96 String qprofileEditUserUuid_2 = Uuids.createFast();
97 insertQProfileEditUser(qprofileEditUserUuid_2, userId_2);
98 String qprofileEditUserUuid_3 = Uuids.createFast();
99 insertQProfileEditUser(qprofileEditUserUuid_3, userId_3);
103 String qprofileEditUserUuid_4 = Uuids.createFast();
104 insertQProfileEditUser(qprofileEditUserUuid_4, userId_4);
105 String qprofileEditUserUuid_5 = Uuids.createFast();
106 insertQProfileEditUser(qprofileEditUserUuid_5, userId_1);
111 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_1, userUuid_1);
112 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_2, userUuid_2);
113 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_3, userUuid_3);
114 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_4, userUuid_4);
115 assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_5, userUuid_1);
118 private void assertThatQProfileEditUserUserUuidIsEqualTo(String qprofileEditUserUuid, String expectedUuid) {
119 assertThat(db.select(String.format("select user_uuid from qprofile_edit_users where uuid = '%s'", qprofileEditUserUuid))
121 .map(row -> row.get("USER_UUID"))
123 .hasValue(expectedUuid);
126 private void insertQProfileEditUser(String uuid, long userId) {
127 db.executeInsert("qprofile_edit_users",
130 "qprofile_uuid", Uuids.createFast(),
131 "created_at", System.currentTimeMillis());
134 private void insertUser(Long id, String uuid) {
135 db.executeInsert("users",
138 "login", "login" + id,
139 "external_login", "ex-login" + id,
140 "external_identity_provider", "ex-provider" + id,
141 "external_id", id + 1,