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.step;
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;
31 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.sonar.server.platform.db.migration.step.ForceReloadingOfAllPlugins.OVERWRITE_HASH;
35 public class ForceReloadingOfAllPluginsIT {
36 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
39 public CoreDbTester db = CoreDbTester.createForSchema(ForceReloadingOfAllPluginsIT.class, "schema.sql");
41 private final DataChange underTest = new ForceReloadingOfAllPlugins(db.database());
44 public void migration_overwrite_file_hash_on_all_plugins() throws SQLException {
45 String pluginUuid1 = insertPlugin();
46 String pluginUuid2 = insertPlugin();
50 assertPluginFileHashOverwrite(pluginUuid1);
51 assertPluginFileHashOverwrite(pluginUuid2);
55 public void migration_should_be_reentrant() throws SQLException {
56 String pluginUuid1 = insertPlugin();
57 String pluginUuid2 = insertPlugin();
63 assertPluginFileHashOverwrite(pluginUuid1);
64 assertPluginFileHashOverwrite(pluginUuid2);
67 private void assertPluginFileHashOverwrite(String pluginUuid) {
68 String selectSql = String.format("select file_hash from plugins where uuid='%s'", pluginUuid);
69 var selectResult = db.select(selectSql);
70 assertThat(selectResult.get(0)).containsEntry("FILE_HASH", OVERWRITE_HASH);
73 private String insertPlugin() {
74 Map<String, Object> map = new HashMap<>();
75 String uuid = uuidFactory.create();
76 map.put("UUID", uuid);
77 map.put("KEE", randomAlphabetic(20));
78 map.put("FILE_HASH", randomAlphabetic(32));
79 map.put("CREATED_AT", System.currentTimeMillis());
80 map.put("UPDATED_AT", System.currentTimeMillis());
81 map.put("TYPE", "EXTERNAL");
82 map.put("REMOVED", false);
83 db.executeInsert("plugins", map);