3 * Copyright (C) 2009-2017 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.v60;
22 import java.sql.SQLException;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.db.CoreDbTester;
29 import static java.lang.String.valueOf;
30 import static org.assertj.core.api.Assertions.assertThat;
32 public class CleanOrphanRowsInResourceIndexTest {
35 public CoreDbTester db = CoreDbTester.createForSchema(CleanOrphanRowsInResourceIndexTest.class,
36 "in_progress_resourceindex.sql");
38 private CleanOrphanRowsInResourceIndex underTest = new CleanOrphanRowsInResourceIndex(db.database());
41 public void migration_has_no_effect_on_empty_table() throws SQLException {
44 assertThat(db.countRowsOfTable("resource_index")).isEqualTo(0);
48 public void migration_deletes_any_row_with_a_null_uuid() throws SQLException {
49 insertResourceIndex(1, true, true);
50 insertResourceIndex(2, false, false);
51 insertResourceIndex(3, true, false);
52 insertResourceIndex(4, false, true);
53 insertResourceIndex(5, true, true);
57 assertThat(idsOfRowsInResourceIndex()).containsOnly(1l, 5l);
61 public void migration_is_reentrant() throws SQLException {
62 insertResourceIndex(1, true, true);
63 insertResourceIndex(2, false, false);
67 assertThat(idsOfRowsInResourceIndex()).containsOnly(1l);
71 assertThat(idsOfRowsInResourceIndex()).containsOnly(1l);
74 private List<Long> idsOfRowsInResourceIndex() {
75 return db.select("select ID from resource_index").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
78 private void insertResourceIndex(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) {
83 "POSITION", valueOf(id + 100),
84 "NAME_SIZE", valueOf(id + 1000),
85 "RESOURCE_ID", valueOf(id + 300),
86 "ROOT_PROJECT_ID", valueOf(id + 4000),
87 "QUALIFIER", "PROJECT");
89 if (hasComponentUiid) {
90 db.executeUpdateSql("update resource_index set COMPONENT_UUID=? where id=?", "uuid_" + id, valueOf(id));
92 if (hasRootComponentUuid) {
93 db.executeUpdateSql("update resource_index set ROOT_COMPONENT_UUID=? where id=?", "root_uuid_" + id, valueOf(id));