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;
25 import javax.annotation.Nullable;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.db.CoreDbTester;
30 import static java.lang.String.valueOf;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class PopulateUuidColumnsOfResourceIndexTest {
36 public CoreDbTester db = CoreDbTester.createForSchema(PopulateUuidColumnsOfResourceIndexTest.class,
37 "in_progress_resourceindex_with_projects.sql");
39 private PopulateUuidColumnsOfResourceIndex underTest = new PopulateUuidColumnsOfResourceIndex(db.database());
42 public void migration_has_no_effect_on_empty_tables() throws SQLException {
45 assertThat(db.countRowsOfTable("resource_index")).isEqualTo(0);
46 assertThat(db.countRowsOfTable("projects")).isEqualTo(0);
50 public void migration_updates_uuid_columns_with_values_from_table_projects_when_they_exist() throws SQLException {
51 String uuid1 = insertComponent(40);
52 String uuid2 = insertComponent(50);
53 String uuid3 = insertComponent(60);
54 String uuid4 = insertComponent(70);
56 insertResourceIndex(1, 40, 50);
57 insertResourceIndex(2, 60, 70);
58 insertResourceIndex(3, 90, 70); // 90 does not exist
59 insertResourceIndex(4, 40, 100); // 100 does not exist
60 insertResourceIndex(5, 110, 100); // 110 and 100 do not exist
64 verifyResourceIndex(1, 40, uuid1, 50, uuid2);
65 verifyResourceIndex(2, 60, uuid3, 70, uuid4);
66 verifyResourceIndex(3, 90, null, 70, uuid4);
67 verifyResourceIndex(4, 40, uuid1, 100, null);
68 verifyResourceIndex(5, 110, null, 100, null);
72 public void migration_is_reentrant() throws SQLException {
73 String uuid1 = insertComponent(40);
74 String uuid2 = insertComponent(50);
75 insertResourceIndex(1, 40, 50);
78 verifyResourceIndex(1, 40, uuid1, 50, uuid2);
81 verifyResourceIndex(1, 40, uuid1, 50, uuid2);
85 private void verifyResourceIndex(long id, long resourceId, @Nullable String componentUuid, long rootProjectId, @Nullable String rootComponentUuid) {
86 List<Map<String, Object>> rows = db.select("select RESOURCE_ID, COMPONENT_UUID, ROOT_PROJECT_ID, ROOT_COMPONENT_UUID from resource_index where ID=" + id);
87 assertThat(rows).hasSize(1);
88 Map<String, Object> row = rows.get(0);
89 assertThat(row.get("RESOURCE_ID")).isEqualTo(resourceId);
90 assertThat(row.get("COMPONENT_UUID")).isEqualTo(componentUuid);
91 assertThat(row.get("ROOT_PROJECT_ID")).isEqualTo(rootProjectId);
92 assertThat(row.get("ROOT_COMPONENT_UUID")).isEqualTo(rootComponentUuid);
95 private String insertComponent(long id) {
96 String uuid = "uuid_" + id;
104 private void insertResourceIndex(long id, long resourceId, long rootProjectId) {
109 "POSITION", valueOf(id + 100),
110 "NAME_SIZE", valueOf(id + 1000),
111 "RESOURCE_ID", valueOf(resourceId),
112 "ROOT_PROJECT_ID", valueOf(rootProjectId),
113 "QUALIFIER", "PROJECT");