3 * Copyright (C) 2009-2023 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 org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.core.util.UuidFactory;
28 import org.sonar.core.util.UuidFactoryFast;
29 import org.sonar.db.CoreDbTester;
30 import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
31 import org.sonar.server.platform.db.migration.step.DataChange;
33 import static org.assertj.core.api.Assertions.assertThat;
35 public class RemoveOrphanUserTokensTest {
36 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
39 public CoreDbTester db = CoreDbTester.createForSchema(RemoveOrphanUserTokensTest.class, "schema.sql");
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");
51 assertThat(db.select("select * from user_tokens"))
52 .extracting(r -> r.get("UUID"))
53 .containsOnly(token1Uuid);
57 public void migration_should_be_reentrant() throws SQLException {
58 String project1Uuid = insertProject("project1");
60 String token1Uuid = insertUserToken("project1");
61 String token2Uuid = insertUserToken("orphan");
66 assertThat(db.select("select * from user_tokens"))
67 .extracting(r -> r.get("UUID"))
68 .containsOnly(token1Uuid);
71 private String insertUserToken(String projectKey) {
72 Map<String, Object> map = new HashMap<>();
73 String uuid = uuidFactory.create();
74 map.put("UUID", uuid);
75 map.put("USER_UUID", "user" + uuid);
76 map.put("NAME", "name" + uuid);
77 map.put("TOKEN_HASH", "token" + uuid);
78 map.put("CREATED_AT", 1);
79 map.put("PROJECT_KEY", projectKey);
80 map.put("TYPE", "PROJECT_ANALYSIS_TOKEN");
82 db.executeInsert("user_tokens", map);
86 private String insertProject(String projectKey) {
87 Map<String, Object> map = new HashMap<>();
88 String uuid = uuidFactory.create();
89 map.put("UUID", uuid);
90 map.put("KEE", projectKey);
91 map.put("QUALIFIER", "TRK");
92 map.put("PRIVATE", true);
93 map.put("UPDATED_AT", System.currentTimeMillis());
94 db.executeInsert("projects", map);