3 * Copyright (C) 2009-2023 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.ce.task.projectanalysis.taskprocessor;
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;
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;
35 public class IgnoreOrphanBranchStepIT {
36 private final String ENTITY_UUID = "entity_uuid";
37 private final String BRANCH_UUID = "branch_uuid";
40 public DbTester dbTester = DbTester.create(System2.INSTANCE);
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()
47 .setComponent(component)
51 private final DbClient dbClient = dbTester.getDbClient();
52 private final IgnoreOrphanBranchStep underTest = new IgnoreOrphanBranchStep(ceTask, dbClient);
55 public void execute() {
56 BranchDto branchDto = new BranchDto()
57 .setBranchType(BRANCH)
61 .setProjectUuid("project_uuid")
62 .setNeedIssueSync(true);
63 dbClient.branchDao().insert(dbTester.getSession(), branchDto);
66 underTest.execute(() -> null);
68 Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbTester.getSession(), BRANCH_UUID);
69 assertThat(branch.get().isNeedIssueSync()).isFalse();
70 assertThat(branch.get().isExcludeFromPurge()).isFalse();
74 public void execute_on_already_indexed_branch() {
75 BranchDto branchDto = new BranchDto()
76 .setBranchType(BRANCH)
79 .setProjectUuid("project_uuid")
81 .setNeedIssueSync(false);
82 dbClient.branchDao().insert(dbTester.getSession(), branchDto);
85 underTest.execute(() -> null);
87 Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbTester.getSession(), BRANCH_UUID);
88 assertThat(branch.get().isNeedIssueSync()).isFalse();
89 assertThat(branch.get().isExcludeFromPurge()).isFalse();
93 public void fail_if_missing_main_component_in_task() {
94 CeTask ceTask = new CeTask.Builder()
100 IgnoreOrphanBranchStep underTest = new IgnoreOrphanBranchStep(ceTask, dbClient);
102 assertThatThrownBy(() -> underTest.execute(() -> null))
103 .isInstanceOf(UnsupportedOperationException.class)
104 .hasMessage("entity not found in task");
108 public void verify_step_description() {
109 assertThat(underTest.getDescription()).isEqualTo("Ignore orphan component");