3 * Copyright (C) 2009-2022 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.v95;
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.step.DataChange;
32 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.sonar.server.platform.db.migration.version.v95.OverwritePluginFileHash.OVERWRITE_HASH;
36 public class OverwritePluginFileHashTest {
37 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
40 public CoreDbTester db = CoreDbTester.createForSchema(OverwritePluginFileHashTest.class, "schema.sql");
42 private final DataChange underTest = new OverwritePluginFileHash(db.database());
45 public void migration_overwrite_file_hash_on_all_plugins() throws SQLException {
46 String pluginUuid1 = insertPlugin();
47 String pluginUuid2 = insertPlugin();
51 assertPluginFileHashOverwrite(pluginUuid1);
52 assertPluginFileHashOverwrite(pluginUuid2);
56 public void migration_should_be_reentrant() throws SQLException {
57 String pluginUuid1 = insertPlugin();
58 String pluginUuid2 = insertPlugin();
64 assertPluginFileHashOverwrite(pluginUuid1);
65 assertPluginFileHashOverwrite(pluginUuid2);
68 private void assertPluginFileHashOverwrite(String pluginUuid) {
69 String selectSql = String.format("select file_hash from plugins where uuid='%s'", pluginUuid);
70 var selectResult = db.select(selectSql);
71 assertThat(selectResult.get(0)).containsEntry("FILE_HASH", OVERWRITE_HASH);
74 private String insertPlugin() {
75 Map<String, Object> map = new HashMap<>();
76 String uuid = uuidFactory.create();
77 map.put("UUID", uuid);
78 map.put("KEE", randomAlphabetic(20));
79 map.put("FILE_HASH", randomAlphabetic(32));
80 map.put("CREATED_AT", System.currentTimeMillis());
81 map.put("UPDATED_AT", System.currentTimeMillis());
82 map.put("TYPE", "EXTERNAL");
83 map.put("REMOVED", false);
84 db.executeInsert("plugins", map);