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.groupsusers;
22 import java.sql.SQLException;
23 import java.util.Objects;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.core.util.Uuids;
27 import org.sonar.db.CoreDbTester;
28 import org.sonar.server.platform.db.migration.step.DataChange;
30 import static org.assertj.core.api.Assertions.assertThat;
32 public class PopulateGroupsUsersUserUuidTest {
35 public CoreDbTester db = CoreDbTester.createForSchema(PopulateGroupsUsersUserUuidTest.class, "schema.sql");
37 private DataChange underTest = new PopulateGroupsUsersUserUuid(db.database());
40 public void populate_uuids() throws SQLException {
42 String userUuid_1 = "uuid-1";
43 insertUser(userId_1, userUuid_1);
46 String userUuid_2 = "uuid-2";
47 insertUser(userId_2, userUuid_2);
50 String userUuid_3 = "uuid-3";
51 insertUser(userId_3, userUuid_3);
54 String userUuid_4 = "uuid-4";
55 insertUser(userId_4, userUuid_4);
57 String groupUuid_1 = Uuids.createFast();
58 insertGroupUser(userId_1, groupUuid_1);
59 String groupUuid_2 = Uuids.createFast();
60 insertGroupUser(userId_2, groupUuid_2);
61 String groupUuid_3 = Uuids.createFast();
62 insertGroupUser(userId_3, groupUuid_3);
63 String groupUuid_4 = Uuids.createFast();
64 insertGroupUser(userId_1, groupUuid_4);
68 assertThatGroupsUserUserUuidIsEqualTo(userId_1, groupUuid_1, userUuid_1);
69 assertThatGroupsUserUserUuidIsEqualTo(userId_2, groupUuid_2, userUuid_2);
70 assertThatGroupsUserUserUuidIsEqualTo(userId_3, groupUuid_3, userUuid_3);
71 assertThatGroupsUserUserUuidIsEqualTo(userId_1, groupUuid_4, userUuid_1);
75 public void migration_is_reentrant() throws SQLException {
77 String userUuid_1 = "uuid-1";
78 insertUser(userId_1, userUuid_1);
81 String userUuid_2 = "uuid-2";
82 insertUser(userId_2, userUuid_2);
85 String userUuid_3 = "uuid-3";
86 insertUser(userId_3, userUuid_3);
89 String userUuid_4 = "uuid-4";
90 insertUser(userId_4, userUuid_4);
92 String groupUuid_1 = Uuids.createFast();
93 insertGroupUser(userId_1, groupUuid_1);
94 String groupUuid_2 = Uuids.createFast();
95 insertGroupUser(userId_2, groupUuid_2);
96 String groupUuid_3 = Uuids.createFast();
97 insertGroupUser(userId_3, groupUuid_3);
101 String groupUuid_4 = Uuids.createFast();
102 insertGroupUser(userId_1, groupUuid_4);
107 assertThatGroupsUserUserUuidIsEqualTo(userId_1, groupUuid_1, userUuid_1);
108 assertThatGroupsUserUserUuidIsEqualTo(userId_2, groupUuid_2, userUuid_2);
109 assertThatGroupsUserUserUuidIsEqualTo(userId_3, groupUuid_3, userUuid_3);
110 assertThatGroupsUserUserUuidIsEqualTo(userId_1, groupUuid_4, userUuid_1);
113 private void assertThatGroupsUserUserUuidIsEqualTo(Long userId, String groupUuid, String expectedUuid) {
114 assertThat(db.select(String.format("select user_uuid from groups_users where user_id = %d and group_uuid = '%s'", userId, groupUuid))
116 .map(row -> row.get("USER_UUID"))
117 .filter(Objects::nonNull)
119 .hasValue(expectedUuid);
122 private void insertGroupUser(Long userId, String groupUuid) {
123 db.executeInsert("groups_users",
125 "group_uuid", groupUuid);
128 private void insertUser(Long id, String uuid) {
129 db.executeInsert("users",
132 "login", "login" + id,
133 "external_login", "ex-login" + id,
134 "external_identity_provider", "ex-provider" + id,
135 "external_id", id + 1,