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.organizationmembers;
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 PopulateOrganizationMembersUserUuidTest {
34 public CoreDbTester db = CoreDbTester.createForSchema(PopulateOrganizationMembersUserUuidTest.class, "schema.sql");
36 private DataChange underTest = new PopulateOrganizationMembersUserUuid(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 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);
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);
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 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);
103 String organizationUuid_4 = Uuids.createFast();
104 insertOrganizationMember(userId_3, organizationUuid_4);
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);
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))
118 .map(row -> row.get("USER_UUID"))
120 .hasValue(expectedUuid);
123 private void insertOrganizationMember(Long userId, String organizationUuid) {
124 db.executeInsert("organization_members",
126 "organization_uuid", organizationUuid);
129 private void insertUser(Long id, String uuid) {
130 db.executeInsert("users",
133 "login", "login" + id,
134 "external_login", "ex-login" + id,
135 "external_identity_provider", "ex-provider" + id,
136 "external_id", id + 1,