]> source.dussan.org Git - sonarqube.git/blob
5cd05bd2230d1381536139f9f0ab67c3733bcaad
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.version.v101;
21
22 import java.sql.SQLException;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.core.util.UuidFactory;
26 import org.sonar.core.util.UuidFactoryFast;
27 import org.sonar.db.MigrationDbTester;
28 import org.sonar.server.platform.db.migration.step.DataChange;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.tuple;
32
33 public class PopulateReportSchedulesIT {
34   @Rule
35   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateReportSchedules.class);
36
37   private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
38   private final DataChange underTest = new PopulateReportSchedules(db.database());
39
40   @Test
41   public void execute_shouldPopulateFromPortfolioProperties() throws SQLException {
42     insertPortfolio("uuid1");
43     insertPortfolioProperty("uuid1", "1234");
44
45     underTest.execute();
46
47     assertThat(db.select("select * from report_schedules"))
48       .extracting(m -> m.get("PORTFOLIO_UUID"), m -> m.get("BRANCH_UUID"), m -> m.get("LAST_SEND_TIME_IN_MS"))
49       .containsOnly(tuple("uuid1", null, 1234L));
50   }
51
52   @Test
53   public void execute_shouldPopulateFromBranchProperties() throws SQLException {
54     insertBranch("uuid1");
55     insertProjectBranchProperty("uuid1", "1234");
56
57     underTest.execute();
58
59     assertThat(db.select("select * from report_schedules"))
60       .extracting(m -> m.get("PORTFOLIO_UUID"), m -> m.get("BRANCH_UUID"), m -> m.get("LAST_SEND_TIME_IN_MS"))
61       .containsOnly(tuple(null, "uuid1", 1234L));
62   }
63
64   @Test
65   public void execute_whenPropertyMatchesBothBranchAndPortfolio_shouldNotPopulate() throws SQLException {
66     insertBranch("uuid1");
67     insertPortfolio("uuid1");
68     insertProjectBranchProperty("uuid1", "1234");
69
70     underTest.execute();
71     underTest.execute();
72
73     assertThat(db.select("select * from report_schedules")).isEmpty();
74   }
75
76   private void insertPortfolio(String uuid) {
77     db.executeInsert("portfolios",
78       "uuid", uuid,
79       "kee", "kee_" + uuid,
80       "name", "name_" + uuid,
81       "root_uuid", uuid,
82       "private", true,
83       "selection_mode", "manual",
84       "created_at", 1000,
85       "updated_at", 1000
86     );
87   }
88
89   private void insertProjectBranchProperty(String portfolioUuid, String value) {
90     db.executeInsert("properties",
91       "uuid", uuidFactory.create(),
92       "prop_key", "sonar.governance.report.project.branch.lastSendTimeInMs",
93       "is_empty", false,
94       "text_value", value,
95       "created_at", 1000,
96       "entity_uuid", portfolioUuid
97     );
98   }
99
100   private void insertPortfolioProperty(String portfolioUuid, String value) {
101     db.executeInsert("properties",
102       "uuid", uuidFactory.create(),
103       "prop_key", "sonar.governance.report.lastSendTimeInMs",
104       "is_empty", false,
105       "text_value", value,
106       "created_at", 1000,
107       "entity_uuid", portfolioUuid
108     );
109   }
110
111   private void insertBranch(String uuid) {
112     db.executeInsert("project_branches",
113       "uuid", uuid,
114       "project_uuid", "project_" + uuid,
115       "kee", "kee_" + uuid,
116       "branch_type", "BRANCH",
117       "created_at", 1000,
118       "updated_at", 1000,
119       "need_issue_sync", false,
120       "is_main", true
121     );
122   }
123 }