]> source.dussan.org Git - sonarqube.git/blob
05a194a1b64d5ef5ecdb7099b5bd67b2d151c059
[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.qprofileeditusers;
21
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;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30
31 public class PopulateQProfileEditUsersUserUuidTest {
32
33   @Rule
34   public CoreDbTester db = CoreDbTester.createForSchema(PopulateQProfileEditUsersUserUuidTest.class, "schema.sql");
35
36   private DataChange underTest = new PopulateQProfileEditUsersUserUuid(db.database());
37
38   @Test
39   public void populate_uuids() throws SQLException {
40     long userId_1 = 1L;
41     String userUuid_1 = "uuid-1";
42     insertUser(userId_1, userUuid_1);
43
44     long userId_2 = 2L;
45     String userUuid_2 = "uuid-2";
46     insertUser(userId_2, userUuid_2);
47
48     long userId_3 = 3L;
49     String userUuid_3 = "uuid-3";
50     insertUser(userId_3, userUuid_3);
51
52     long userId_4 = 4L;
53     String userUuid_4 = "uuid-4";
54     insertUser(userId_4, userUuid_4);
55
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);
66
67     underTest.execute();
68
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);
74   }
75
76   @Test
77   public void migration_is_reentrant() throws SQLException {
78     long userId_1 = 1L;
79     String userUuid_1 = "uuid-1";
80     insertUser(userId_1, userUuid_1);
81
82     long userId_2 = 2L;
83     String userUuid_2 = "uuid-2";
84     insertUser(userId_2, userUuid_2);
85
86     long userId_3 = 3L;
87     String userUuid_3 = "uuid-3";
88     insertUser(userId_3, userUuid_3);
89
90     long userId_4 = 4L;
91     String userUuid_4 = "uuid-4";
92     insertUser(userId_4, userUuid_4);
93
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);
100
101     underTest.execute();
102
103     String qprofileEditUserUuid_4 = Uuids.createFast();
104     insertQProfileEditUser(qprofileEditUserUuid_4, userId_4);
105     String qprofileEditUserUuid_5 = Uuids.createFast();
106     insertQProfileEditUser(qprofileEditUserUuid_5, userId_1);
107
108     // re-entrant
109     underTest.execute();
110
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);
116   }
117
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))
120       .stream()
121       .map(row -> row.get("USER_UUID"))
122       .findFirst())
123         .hasValue(expectedUuid);
124   }
125
126   private void insertQProfileEditUser(String uuid, long userId) {
127     db.executeInsert("qprofile_edit_users",
128       "uuid", uuid,
129       "user_id", userId,
130       "qprofile_uuid", Uuids.createFast(),
131       "created_at", System.currentTimeMillis());
132   }
133
134   private void insertUser(Long id, String uuid) {
135     db.executeInsert("users",
136       "id", id,
137       "uuid", uuid,
138       "login", "login" + id,
139       "external_login", "ex-login" + id,
140       "external_identity_provider", "ex-provider" + id,
141       "external_id", id + 1,
142       "is_root", false,
143       "onboarded", false);
144   }
145 }