]> source.dussan.org Git - sonarqube.git/blob
554a985b4556fc1c17d257067fca8d19c888aed4
[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.organizationmembers;
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 PopulateOrganizationMembersUserUuidTest {
32
33   @Rule
34   public CoreDbTester db = CoreDbTester.createForSchema(PopulateOrganizationMembersUserUuidTest.class, "schema.sql");
35
36   private DataChange underTest = new PopulateOrganizationMembersUserUuid(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 organizationUuid_1 = Uuids.createFast();
57     insertOrganizationMember(userId_1, organizationUuid_1);
58     String organizationUuid_2 = Uuids.createFast();
59     insertOrganizationMember(userId_2, organizationUuid_2);
60     String organizationUuid_3 = Uuids.createFast();
61     insertOrganizationMember(userId_3, organizationUuid_3);
62     String organizationUuid_4 = Uuids.createFast();
63     insertOrganizationMember(userId_4, organizationUuid_4);
64     String organizationUuid_5 = Uuids.createFast();
65     insertOrganizationMember(userId_1, organizationUuid_5);
66
67     underTest.execute();
68
69     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_1, organizationUuid_1, userUuid_1);
70     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_2, organizationUuid_2, userUuid_2);
71     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_3, organizationUuid_3, userUuid_3);
72     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_4, organizationUuid_4, userUuid_4);
73     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_1, organizationUuid_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 organizationUuid_1 = Uuids.createFast();
95     insertOrganizationMember(userId_1, organizationUuid_1);
96     String organizationUuid_2 = Uuids.createFast();
97     insertOrganizationMember(userId_2, organizationUuid_2);
98     String organizationUuid_3 = Uuids.createFast();
99     insertOrganizationMember(userId_3, organizationUuid_3);
100
101     underTest.execute();
102
103     String organizationUuid_4 = Uuids.createFast();
104     insertOrganizationMember(userId_3, organizationUuid_4);
105
106     // re-entrant
107     underTest.execute();
108
109     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_1, organizationUuid_1, userUuid_1);
110     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_2, organizationUuid_2, userUuid_2);
111     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_3, organizationUuid_3, userUuid_3);
112     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_3, organizationUuid_4, userUuid_3);
113   }
114
115   private void assertThatQProfileChangeRulesProfileUuidIsEqualTo(Long userId, String organizationUuid, String expectedUuid) {
116     assertThat(db.select(String.format("select user_uuid from organization_members where user_id = %d and organization_uuid = '%s'", userId, organizationUuid))
117       .stream()
118       .map(row -> row.get("USER_UUID"))
119       .findFirst())
120         .hasValue(expectedUuid);
121   }
122
123   private void insertOrganizationMember(Long userId, String organizationUuid) {
124     db.executeInsert("organization_members",
125       "user_id", userId,
126       "organization_uuid", organizationUuid);
127   }
128
129   private void insertUser(Long id, String uuid) {
130     db.executeInsert("users",
131       "id", id,
132       "uuid", uuid,
133       "login", "login" + id,
134       "external_login", "ex-login" + id,
135       "external_identity_provider", "ex-provider" + id,
136       "external_id", id + 1,
137       "is_root", false,
138       "onboarded", false);
139   }
140 }