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.projectexport.branches;
22 import com.google.common.collect.ImmutableList;
23 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
24 import java.util.Date;
25 import java.util.List;
27 import java.util.function.Function;
28 import org.apache.commons.lang.time.DateUtils;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.slf4j.event.Level;
33 import org.sonar.api.testfixtures.log.LogTester;
34 import org.sonar.api.utils.System2;
35 import org.sonar.ce.task.projectexport.steps.DumpElement;
36 import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
37 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
38 import org.sonar.ce.task.step.TestComputationStepContext;
39 import org.sonar.db.DbTester;
40 import org.sonar.db.component.BranchDto;
41 import org.sonar.db.component.BranchType;
42 import org.sonar.db.component.ProjectData;
43 import org.sonar.db.project.ProjectExportMapper;
45 import static java.util.stream.Collectors.toMap;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.assertj.core.api.Assertions.assertThatThrownBy;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
51 public class ExportBranchesStepIT {
52 private static final String PROJECT_UUID = "PROJECT_UUID";
55 public DbTester dbTester = DbTester.createWithExtensionMappers(System2.INSTANCE, ProjectExportMapper.class);
57 public LogTester logTester = new LogTester();
59 private final ProjectHolder projectHolder = mock(ProjectHolder.class);
60 private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
61 private final ExportBranchesStep underTest = new ExportBranchesStep(dumpWriter, dbTester.getDbClient(), projectHolder);
62 private final List<BranchDto> branches = ImmutableList.of(
64 .setBranchType(BranchType.BRANCH)
65 .setProjectUuid(PROJECT_UUID)
67 .setUuid("branch-1-uuid")
68 .setMergeBranchUuid("master")
70 .setExcludeFromPurge(true),
72 .setBranchType(BranchType.PULL_REQUEST)
73 .setProjectUuid(PROJECT_UUID)
75 .setUuid("branch-3-uuid")
77 .setMergeBranchUuid("master"),
79 .setBranchType(BranchType.BRANCH)
80 .setProjectUuid(PROJECT_UUID)
82 .setUuid("branch-4-uuid")
84 .setMergeBranchUuid("branch-1-uuid"),
86 .setBranchType(BranchType.BRANCH)
87 .setProjectUuid(PROJECT_UUID)
89 .setUuid("branch-5-uuid")
91 .setMergeBranchUuid("master")
92 .setExcludeFromPurge(true));
96 logTester.setLevel(Level.DEBUG);
97 Date createdAt = new Date();
98 ProjectData projectData = dbTester.components().insertPublicProject(PROJECT_UUID);
99 for (BranchDto branch : branches) {
100 createdAt = DateUtils.addMinutes(createdAt, 10);
101 dbTester.components().insertProjectBranch(projectData.getProjectDto(), branch).setCreatedAt(createdAt);
104 when(projectHolder.projectDto()).thenReturn(projectData.getProjectDto());
108 public void export_branches() {
109 underTest.execute(new TestComputationStepContext());
111 assertThat(logTester.logs(Level.DEBUG)).contains("3 branches exported");
112 Map<String, ProjectDump.Branch> branches = dumpWriter.getWrittenMessagesOf(DumpElement.BRANCHES)
114 .collect(toMap(ProjectDump.Branch::getUuid, Function.identity()));
115 assertThat(branches).hasSize(3);
116 ProjectDump.Branch mainBranch = branches.values().stream().filter(ProjectDump.Branch::getIsMain).findFirst().get();
117 assertThat(mainBranch).isNotNull();
118 assertThat(mainBranch.getKee()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
119 assertThat(mainBranch.getProjectUuid()).isEqualTo(PROJECT_UUID);
120 assertThat(mainBranch.getMergeBranchUuid()).isEmpty();
121 assertThat(mainBranch.getBranchType()).isEqualTo("BRANCH");
122 ProjectDump.Branch branch1 = branches.get("branch-1-uuid");
123 assertThat(branch1.getKee()).isEqualTo("branch-1");
124 assertThat(branch1.getProjectUuid()).isEqualTo(PROJECT_UUID);
125 assertThat(branch1.getMergeBranchUuid()).isEqualTo("master");
126 assertThat(branch1.getBranchType()).isEqualTo("BRANCH");
127 ProjectDump.Branch branch5 = branches.get("branch-5-uuid");
128 assertThat(branch5.getKee()).isEqualTo("branch-5");
129 assertThat(branch5.getProjectUuid()).isEqualTo(PROJECT_UUID);
130 assertThat(branch5.getMergeBranchUuid()).isEqualTo("master");
131 assertThat(branch5.getBranchType()).isEqualTo("BRANCH");
135 public void throws_ISE_if_error() {
136 dumpWriter.failIfMoreThan(1, DumpElement.BRANCHES);
138 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
139 .isInstanceOf(IllegalStateException.class)
140 .hasMessage("Branch export failed after processing 1 branch(es) successfully");
144 public void getDescription_is_defined() {
145 assertThat(underTest.getDescription()).isEqualTo("Export branches");