3 * Copyright (C) 2009-2020 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.v84.metrics.livemeasures;
22 import java.sql.SQLException;
23 import java.util.List;
25 import org.assertj.core.groups.Tuple;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.db.CoreDbTester;
29 import org.sonar.server.platform.db.migration.step.DataChange;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.tuple;
34 public class PopulateLiveMeasuresMetricUuidTest {
36 public CoreDbTester db = CoreDbTester.createForSchema(PopulateLiveMeasuresMetricUuidTest.class, "schema.sql");
38 private DataChange underTest = new PopulateLiveMeasuresMetricUuid(db.database());
41 public void populate_uuids() throws SQLException {
46 insertLiveMeasure(4L, 1L);
47 insertLiveMeasure(5L, 2L);
48 insertLiveMeasure(6L, 3L);
52 assertThatTableContains(
53 tuple("uuid4", 1L, "uuid1"),
54 tuple("uuid5", 2L, "uuid2"),
55 tuple("uuid6", 3L, "uuid3")
60 public void delete_orphan_rows() throws SQLException {
65 insertLiveMeasure(4L, 10L);
66 insertLiveMeasure(5L, 2L);
67 insertLiveMeasure(6L, 3L);
71 assertThatTableContains(
72 tuple("uuid5", 2L, "uuid2"),
73 tuple("uuid6", 3L, "uuid3")
78 public void migration_is_reentrant() throws SQLException {
83 insertLiveMeasure(4L, 1L);
84 insertLiveMeasure(5L, 2L);
85 insertLiveMeasure(6L, 3L);
91 assertThatTableContains(
92 tuple("uuid4", 1L, "uuid1"),
93 tuple("uuid5", 2L, "uuid2"),
94 tuple("uuid6", 3L, "uuid3")
98 private void assertThatTableContains(Tuple... tuples) {
99 List<Map<String, Object>> select = db.select("select uuid, metric_id, metric_uuid from live_measures");
100 assertThat(select).extracting(m -> m.get("UUID"), m -> m.get("METRIC_ID"), m -> m.get("METRIC_UUID"))
101 .containsExactlyInAnyOrder(tuples);
104 private void insertMetric(Long id) {
105 db.executeInsert("metrics",
108 "name", "name" + id);
111 private void insertLiveMeasure(Long id, Long metricId) {
112 db.executeInsert("live_measures",
114 "metric_id", metricId,
115 "component_uuid", "component" + id,
116 "project_uuid", "project" + id + 2,
117 "created_at", id + 3,
118 "updated_at", id + 4);