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.v101;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import javax.annotation.Nullable;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.core.util.UuidFactory;
29 import org.sonar.core.util.UuidFactoryFast;
30 import org.sonar.db.MigrationDbTester;
31 import org.sonar.server.platform.db.migration.step.DataChange;
33 import static org.assertj.core.api.Assertions.assertThat;
35 public class RemoveOrphanUserTokensIT {
36 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
39 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RemoveOrphanUserTokens.class);
41 private final DataChange underTest = new RemoveOrphanUserTokens(db.database());
44 public void migration_deletes_orphan_tokens() throws SQLException {
45 String project1Uuid = insertProject("project1");
47 String token1Uuid = insertUserToken("project1");
48 String token2Uuid = insertUserToken("orphan");
49 String token3Uuid = insertUserToken(null);
52 assertThat(db.select("select * from user_tokens"))
53 .extracting(r -> r.get("UUID"))
54 .containsOnly(token1Uuid, token3Uuid);
58 public void migration_should_be_reentrant() throws SQLException {
59 String project1Uuid = insertProject("project1");
61 String token1Uuid = insertUserToken("project1");
62 String token2Uuid = insertUserToken("orphan");
67 assertThat(db.select("select * from user_tokens"))
68 .extracting(r -> r.get("UUID"))
69 .containsOnly(token1Uuid);
72 private String insertUserToken( @Nullable String projectKey) {
73 Map<String, Object> map = new HashMap<>();
74 String uuid = uuidFactory.create();
75 map.put("UUID", uuid);
76 map.put("USER_UUID", "user" + uuid);
77 map.put("NAME", "name" + uuid);
78 map.put("TOKEN_HASH", "token" + uuid);
79 map.put("CREATED_AT", 1);
80 map.put("PROJECT_KEY", projectKey);
81 map.put("TYPE", "PROJECT_ANALYSIS_TOKEN");
83 db.executeInsert("user_tokens", map);
87 private String insertProject(String projectKey) {
88 Map<String, Object> map = new HashMap<>();
89 String uuid = uuidFactory.create();
90 map.put("UUID", uuid);
91 map.put("KEE", projectKey);
92 map.put("QUALIFIER", "TRK");
93 map.put("PRIVATE", true);
94 map.put("UPDATED_AT", System.currentTimeMillis());
95 db.executeInsert("projects", map);