]> source.dussan.org Git - sonarqube.git/blob
495476db7ff4fab24987e072cc23e21666f14f3c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.ce.task.projectanalysis.taskprocessor;
21
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.ce.task.CeTask;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.BranchDto;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.sonar.db.component.BranchType.BRANCH;
34
35 public class IgnoreOrphanBranchStepIT {
36   private final String ENTITY_UUID = "entity_uuid";
37   private final String BRANCH_UUID = "branch_uuid";
38
39   @Rule
40   public DbTester dbTester = DbTester.create(System2.INSTANCE);
41
42   private final CeTask.Component entity = new CeTask.Component(ENTITY_UUID, "component key", "component name");
43   private final CeTask.Component component = new CeTask.Component(BRANCH_UUID, "component key", "component name");
44   private final CeTask ceTask = new CeTask.Builder()
45     .setType("type")
46     .setUuid("uuid")
47     .setComponent(component)
48     .setEntity(entity)
49     .build();
50
51   private final DbClient dbClient = dbTester.getDbClient();
52   private final IgnoreOrphanBranchStep underTest = new IgnoreOrphanBranchStep(ceTask, dbClient);
53
54   @Test
55   public void execute() {
56     BranchDto branchDto = new BranchDto()
57       .setBranchType(BRANCH)
58       .setKey("branchName")
59       .setIsMain(false)
60       .setUuid(BRANCH_UUID)
61       .setProjectUuid("project_uuid")
62       .setNeedIssueSync(true);
63     dbClient.branchDao().insert(dbTester.getSession(), branchDto);
64     dbTester.commit();
65
66     underTest.execute(() -> null);
67
68     Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbTester.getSession(), BRANCH_UUID);
69     assertThat(branch.get().isNeedIssueSync()).isFalse();
70     assertThat(branch.get().isExcludeFromPurge()).isFalse();
71   }
72
73   @Test
74   public void execute_on_already_indexed_branch() {
75     BranchDto branchDto = new BranchDto()
76       .setBranchType(BRANCH)
77       .setKey("branchName")
78       .setUuid(BRANCH_UUID)
79       .setProjectUuid("project_uuid")
80       .setIsMain(false)
81       .setNeedIssueSync(false);
82     dbClient.branchDao().insert(dbTester.getSession(), branchDto);
83     dbTester.commit();
84
85     underTest.execute(() -> null);
86
87     Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbTester.getSession(), BRANCH_UUID);
88     assertThat(branch.get().isNeedIssueSync()).isFalse();
89     assertThat(branch.get().isExcludeFromPurge()).isFalse();
90   }
91
92   @Test
93   public void fail_if_missing_main_component_in_task() {
94     CeTask ceTask = new CeTask.Builder()
95       .setType("type")
96       .setUuid("uuid")
97       .setComponent(null)
98       .setEntity(null)
99       .build();
100     IgnoreOrphanBranchStep underTest = new IgnoreOrphanBranchStep(ceTask, dbClient);
101
102     assertThatThrownBy(() -> underTest.execute(() -> null))
103       .isInstanceOf(UnsupportedOperationException.class)
104       .hasMessage("entity not found in task");
105   }
106
107   @Test
108   public void verify_step_description() {
109     assertThat(underTest.getDescription()).isEqualTo("Ignore orphan component");
110   }
111
112 }