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;
24 import javax.annotation.Nullable;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.resources.Qualifiers;
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 PopulateAnalysisUuidOnMeasuresTest {
35 private static final String TABLE_MEASURES = "project_measures";
36 private static final String TABLE_SNAPSHOTS = "snapshots";
39 public CoreDbTester db = CoreDbTester.createForSchema(PopulateAnalysisUuidOnMeasuresTest.class,
42 private PopulateAnalysisUuidOnMeasures underTest = new PopulateAnalysisUuidOnMeasures(db.database());
45 public void migration_has_no_effect_on_empty_tables() throws SQLException {
48 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(0);
52 public void migration_populates_analysis_uuids() throws SQLException {
53 insertSnapshot(1, "U1", Qualifiers.PROJECT, null);
54 insertSnapshot(2, "U2", Qualifiers.DIRECTORY, 1L);
55 insertSnapshot(3, "U3", Qualifiers.FILE, 1L);
62 verifyAnalysisUuid(21, "U1");
63 verifyAnalysisUuid(22, "U1");
64 verifyAnalysisUuid(23, "U1");
68 public void migration_is_reentrant() throws SQLException {
69 insertSnapshot(1, "U1", Qualifiers.PROJECT, 1L);
73 verifyAnalysisUuid(21, "U1");
76 verifyAnalysisUuid(21, "U1");
79 private void verifyAnalysisUuid(int measureId, @Nullable String expectedAnalysisUuid) {
80 Map<String, Object> rows = db.selectFirst("select analysis_uuid as \"analysisUuid\" from project_measures where id=" + measureId);
81 assertThat(rows.get("analysisUuid")).isEqualTo(expectedAnalysisUuid);
84 private String insertSnapshot(long id, String uuid, String qualifier, @Nullable Long rootSnapshotId) {
100 throw new IllegalArgumentException();
106 "COMPONENT_UUID", valueOf(id + 10),
107 "ROOT_COMPONENT_UUID", valueOf(id + 10),
108 "ROOT_SNAPSHOT_ID", rootSnapshotId != null ? valueOf(rootSnapshotId) : null,
109 "QUALIFIER", qualifier,
110 "DEPTH", valueOf(depth));
114 private void insertMeasure(long id, long snapshotId) {
118 "SNAPSHOT_ID", valueOf(snapshotId),
119 "METRIC_ID", valueOf(id + 100),
120 "VALUE", valueOf(id + 200),
121 "COMPONENT_UUID", valueOf(id + 300));