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.permtemplatesusers;
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 PopulatePermTemplatesUsersUserUuidTest {
34 public CoreDbTester db = CoreDbTester.createForSchema(PopulatePermTemplatesUsersUserUuidTest.class, "schema.sql");
36 private DataChange underTest = new PopulatePermTemplatesUsersUserUuid(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 permTemplatesUserUuid_1 = Uuids.createFast();
57 insertPermTemplatesUser(permTemplatesUserUuid_1, userId_1);
58 String permTemplatesUserUuid_2 = Uuids.createFast();
59 insertPermTemplatesUser(permTemplatesUserUuid_2, userId_2);
60 String permTemplatesUserUuid_3 = Uuids.createFast();
61 insertPermTemplatesUser(permTemplatesUserUuid_3, userId_3);
62 String permTemplatesUserUuid_4 = Uuids.createFast();
63 insertPermTemplatesUser(permTemplatesUserUuid_4, userId_4);
64 String permTemplatesUserUuid_5 = Uuids.createFast();
65 insertPermTemplatesUser(permTemplatesUserUuid_5, userId_1);
69 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_1, userUuid_1);
70 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_2, userUuid_2);
71 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_3, userUuid_3);
72 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_4, userUuid_4);
73 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_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 permTemplatesUserUuid_1 = Uuids.createFast();
95 insertPermTemplatesUser(permTemplatesUserUuid_1, userId_1);
96 String permTemplatesUserUuid_2 = Uuids.createFast();
97 insertPermTemplatesUser(permTemplatesUserUuid_2, userId_2);
98 String permTemplatesUserUuid_3 = Uuids.createFast();
99 insertPermTemplatesUser(permTemplatesUserUuid_3, userId_3);
103 String permTemplatesUserUuid_4 = Uuids.createFast();
104 insertPermTemplatesUser(permTemplatesUserUuid_4, userId_4);
105 String permTemplatesUserUuid_5 = Uuids.createFast();
106 insertPermTemplatesUser(permTemplatesUserUuid_5, userId_1);
111 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_1, userUuid_1);
112 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_2, userUuid_2);
113 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_3, userUuid_3);
114 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_4, userUuid_4);
115 assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_5, userUuid_1);
118 private void assertThatPermTemplatesUsersUserUuidIsEqualTo(String permTemplatesUserUuid, String expectedUuid) {
119 assertThat(db.select(String.format("select user_uuid from perm_templates_users where uuid = '%s'", permTemplatesUserUuid))
121 .map(row -> row.get("USER_UUID"))
123 .hasValue(expectedUuid);
126 private void insertPermTemplatesUser(String uuid, long userId) {
127 db.executeInsert("perm_templates_users",
130 "template_id", userId + 100,
131 "permission_reference", Uuids.createFast());
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,