]> source.dussan.org Git - sonarqube.git/blob
e122c3e700b297c268e9ee51270a80b78931b1ec
[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 PopulateReportSubscriptionsIT {
34   @Rule
35   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateReportSubscriptions.class);
36
37   private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
38   private final DataChange underTest = new PopulateReportSubscriptions(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_subscriptions"))
48       .extracting(m -> m.get("PORTFOLIO_UUID"), m -> m.get("BRANCH_UUID"), m -> m.get("USER_UUID"))
49       .containsOnly(tuple("uuid1", null, "1234"));
50   }
51
52   @Test
53   public void execute_shouldPopulateFromBranchProperties() throws SQLException {
54     insertBranch("uuid1");
55     insertBranchProperty("uuid1", "1234");
56
57     underTest.execute();
58
59     assertThat(db.select("select * from report_subscriptions"))
60       .extracting(m -> m.get("PORTFOLIO_UUID"), m -> m.get("BRANCH_UUID"), m -> m.get("USER_UUID"))
61       .containsOnly(tuple(null, "uuid1", "1234"));
62   }
63
64   @Test
65   public void execute_whenPropertyMatchesBothBranchAndPortfolio_shouldNotPopulate() throws SQLException {
66     insertBranch("uuid1");
67     insertPortfolio("uuid1");
68     insertBranchProperty("uuid1", "1234");
69
70     underTest.execute();
71     underTest.execute();
72
73     assertThat(db.select("select * from report_subscriptions")).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 insertBranchProperty(String branchUuid, String userUuid){
90     insertProperty( branchUuid, userUuid, "sonar.governance.report.userNotification");
91   }
92
93   private void insertPortfolioProperty(String branchUuid, String userUuid){
94     insertProperty( branchUuid, userUuid, "sonar.governance.report.project.branch.userNotification");
95   }
96
97   private void insertProperty(String componentUuid, String userUuid, String propertyKey) {
98     db.executeInsert("properties",
99       "uuid", uuidFactory.create(),
100       "prop_key", propertyKey,
101       "is_empty", false,
102       "text_value", "true",
103       "created_at", 1000,
104       "entity_uuid", componentUuid,
105       "user_uuid", userUuid
106     );
107   }
108
109   private void insertBranch(String uuid) {
110     db.executeInsert("project_branches",
111       "uuid", uuid,
112       "project_uuid", "project_" + uuid,
113       "kee", "kee_" + uuid,
114       "branch_type", "BRANCH",
115       "created_at", 1000,
116       "updated_at", 1000,
117       "need_issue_sync", false,
118       "is_main", true
119     );
120   }
121 }