3 * Copyright (C) 2009-2019 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.v70;
22 import java.sql.SQLException;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.function.Function;
27 import org.assertj.core.groups.Tuple;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.internal.TestSystem2;
32 import org.sonar.db.CoreDbTester;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
37 public class PopulateLiveMeasuresTest {
39 private System2 system2 = new TestSystem2().setNow(1_500_000_000_000L);
42 public CoreDbTester db = CoreDbTester.createForSchema(PopulateLiveMeasuresTest.class, "initial.sql");
44 private PopulateLiveMeasures underTest = new PopulateLiveMeasures(db.database(), system2);
47 public void do_nothing_when_no_data() throws SQLException {
48 assertThat(db.countRowsOfTable("PROJECT_MEASURES")).isEqualTo(0);
50 assertThat(db.countRowsOfTable("LIVE_MEASURES")).isEqualTo(0);
54 public void execute_must_update_database() throws SQLException {
55 generateProjectMeasures();
59 assertThat(getLiveMeasures()).extracting(
60 field("COMPONENT_UUID"),
61 field("PROJECT_UUID"),
67 ).containsExactlyInAnyOrder(generateLiveMeasures());
70 private Function<Map<String, Object>, Object> field(String name) {
71 return m -> m.get(name);
75 public void migration_is_reentrant() throws SQLException {
76 generateProjectMeasures();
81 assertThat(getLiveMeasures()).extracting(
82 field("COMPONENT_UUID"),
83 field("PROJECT_UUID"),
89 ).containsExactlyInAnyOrder(generateLiveMeasures());
92 private void generateProjectMeasures() {
93 Map<String, Object> project = new HashMap<>();
94 project.put("UUID", "PRJ1");
95 project.put("ORGANIZATION_UUID", "ORG1");
96 project.put("UUID_PATH", "X");
97 project.put("ROOT_UUID", "X");
98 project.put("PROJECT_UUID", "PRJ1");
99 project.put("PRIVATE", "FALSE");
100 db.executeInsert("PROJECTS", project);
102 Map<String, Object> analysis1 = new HashMap<>();
103 analysis1.put("UUID", "A1");
104 analysis1.put("ISLAST", "FALSE");
105 analysis1.put("COMPONENT_UUID", "PRJ1");
106 db.executeInsert("SNAPSHOTS", analysis1);
108 Map<String, Object> analysis2 = new HashMap<>();
109 analysis2.put("UUID", "A2");
110 analysis2.put("ISLAST", "TRUE");
111 analysis2.put("COMPONENT_UUID", "PRJ1");
112 db.executeInsert("SNAPSHOTS", analysis2);
114 Map<String, Object> measure1 = new HashMap<>();
115 measure1.put("COMPONENT_UUID", "PRJ1");
116 measure1.put("ANALYSIS_UUID", "A1");
117 measure1.put("METRIC_ID", "123");
118 db.executeInsert("PROJECT_MEASURES", measure1);
120 Map<String, Object> measure2 = new HashMap<>();
121 measure2.put("COMPONENT_UUID", "PRJ1");
122 measure2.put("ANALYSIS_UUID", "A2");
123 measure2.put("METRIC_ID", "123");
124 measure2.put("VALUE", "234");
125 measure2.put("TEXT_VALUE", "TEXT_VALUEx");
126 measure2.put("VARIATION_VALUE_1", "345");
127 measure2.put("MEASURE_DATA", "FFFF");
128 db.executeInsert("PROJECT_MEASURES", measure2);
130 // measures with person_id not null are purged later
131 // by another migration
132 Map<String, Object> personMeasure = new HashMap<>();
133 personMeasure.put("COMPONENT_UUID", "PRJ1");
134 personMeasure.put("ANALYSIS_UUID", "A2");
135 personMeasure.put("METRIC_ID", "200");
136 personMeasure.put("VALUE", "234");
137 personMeasure.put("TEXT_VALUE", "TEXT_VALUEx");
138 personMeasure.put("VARIATION_VALUE_1", "345");
139 personMeasure.put("MEASURE_DATA", "FFFF");
140 personMeasure.put("PERSON_ID", "99");
141 db.executeInsert("PROJECT_MEASURES", personMeasure);
144 private List<Map<String, Object>> getLiveMeasures() {
145 return db.select("SELECT * FROM LIVE_MEASURES");
148 private Tuple[] generateLiveMeasures() {
150 tuple("PRJ1", "PRJ1", 123L, 234.0, "TEXT_VALUEx", 345.0, new byte[] {-1, -1})