]> source.dussan.org Git - sonarqube.git/blob
aa1217b9387e5adaa846b78a4d21d5fc2f79c234
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.version.v84.users.fk.groupsusers;
21
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;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31
32 public class PopulateGroupsUsersUserUuidTest {
33
34   @Rule
35   public CoreDbTester db = CoreDbTester.createForSchema(PopulateGroupsUsersUserUuidTest.class, "schema.sql");
36
37   private DataChange underTest = new PopulateGroupsUsersUserUuid(db.database());
38
39   @Test
40   public void populate_uuids() throws SQLException {
41     long userId_1 = 1L;
42     String userUuid_1 = "uuid-1";
43     insertUser(userId_1, userUuid_1);
44
45     long userId_2 = 2L;
46     String userUuid_2 = "uuid-2";
47     insertUser(userId_2, userUuid_2);
48
49     long userId_3 = 3L;
50     String userUuid_3 = "uuid-3";
51     insertUser(userId_3, userUuid_3);
52
53     long userId_4 = 4L;
54     String userUuid_4 = "uuid-4";
55     insertUser(userId_4, userUuid_4);
56
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);
65
66     underTest.execute();
67
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);
72   }
73
74   @Test
75   public void migration_is_reentrant() throws SQLException {
76     long userId_1 = 1L;
77     String userUuid_1 = "uuid-1";
78     insertUser(userId_1, userUuid_1);
79
80     long userId_2 = 2L;
81     String userUuid_2 = "uuid-2";
82     insertUser(userId_2, userUuid_2);
83
84     long userId_3 = 3L;
85     String userUuid_3 = "uuid-3";
86     insertUser(userId_3, userUuid_3);
87
88     long userId_4 = 4L;
89     String userUuid_4 = "uuid-4";
90     insertUser(userId_4, userUuid_4);
91
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);
98
99     underTest.execute();
100
101     String groupUuid_4 = Uuids.createFast();
102     insertGroupUser(userId_1, groupUuid_4);
103
104     // re-entrant
105     underTest.execute();
106
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);
111   }
112
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))
115       .stream()
116       .map(row -> row.get("USER_UUID"))
117       .filter(Objects::nonNull)
118       .findFirst())
119         .hasValue(expectedUuid);
120   }
121
122   private void insertGroupUser(Long userId, String groupUuid) {
123     db.executeInsert("groups_users",
124       "user_id", userId,
125       "group_uuid", groupUuid);
126   }
127
128   private void insertUser(Long id, String uuid) {
129     db.executeInsert("users",
130       "id", id,
131       "uuid", uuid,
132       "login", "login" + id,
133       "external_login", "ex-login" + id,
134       "external_identity_provider", "ex-provider" + id,
135       "external_id", id + 1,
136       "is_root", false,
137       "onboarded", false);
138   }
139 }