3 * Copyright (C) 2009-2024 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.v100;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.RegisterExtension;
27 import org.sonar.core.util.UuidFactory;
28 import org.sonar.core.util.UuidFactoryFast;
29 import org.sonar.db.MigrationDbTester;
30 import org.sonar.server.platform.db.migration.step.DataChange;
32 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
33 import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
34 import static org.assertj.core.api.Assertions.assertThat;
36 class UpdateUserLocalValueInUsersIT {
38 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
41 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(UpdateUserLocalValueInUsers.class);
43 private final DataChange underTest = new UpdateUserLocalValueInUsers(db.database());
46 void migration_updates_user_local_if_null() throws SQLException {
47 String userUuid1 = insertUser(true);
48 String userUuid2 = insertUser(false);
49 String userUuid3 = insertUser(null);
53 assertUserLocalIsUpdatedCorrectly(userUuid1, true);
54 assertUserLocalIsUpdatedCorrectly(userUuid2, false);
55 assertUserLocalIsUpdatedCorrectly(userUuid3, true);
59 void migration_should_be_reentrant() throws SQLException {
60 String userUuid1 = insertUser(true);
61 String userUuid2 = insertUser(false);
62 String userUuid3 = insertUser(null);
68 assertUserLocalIsUpdatedCorrectly(userUuid1, true);
69 assertUserLocalIsUpdatedCorrectly(userUuid2, false);
70 assertUserLocalIsUpdatedCorrectly(userUuid3, true);
73 private void assertUserLocalIsUpdatedCorrectly(String userUuid, boolean expected) {
74 String selectSql = String.format("select user_local from users where uuid='%s'", userUuid);
75 assertThat(db.select(selectSql).stream()
76 .map(row -> row.get("USER_LOCAL"))
78 .containsExactlyInAnyOrder(expected);
81 private String insertUser(Boolean userLocal) {
82 Map<String, Object> map = new HashMap<>();
83 String uuid = uuidFactory.create();
84 map.put("UUID", uuid);
85 map.put("LOGIN", randomAlphabetic(20));
86 map.put("EXTERNAL_LOGIN", randomAlphabetic(20));
87 map.put("EXTERNAL_IDENTITY_PROVIDER", "sonarqube");
88 map.put("EXTERNAL_ID", randomNumeric(5));
89 map.put("CREATED_AT", System.currentTimeMillis());
90 map.put("USER_LOCAL", userLocal);
91 map.put("RESET_PASSWORD", false);
92 db.executeInsert("users", map);