3 * Copyright (C) 2009-2022 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.sonar.api.resources.Qualifiers;
33 import org.sonar.api.resources.Scopes;
34 import org.sonar.api.utils.System2;
35 import org.sonar.api.utils.log.LogTester;
36 import org.sonar.api.utils.log.LoggerLevel;
37 import org.sonar.ce.task.projectexport.steps.DumpElement;
38 import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
39 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
40 import org.sonar.ce.task.step.TestComputationStepContext;
41 import org.sonar.db.DbTester;
42 import org.sonar.db.component.BranchDto;
43 import org.sonar.db.component.BranchType;
44 import org.sonar.db.component.ComponentDto;
45 import org.sonar.db.project.ProjectExportMapper;
47 import static java.util.stream.Collectors.toMap;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.assertj.core.api.Assertions.assertThatThrownBy;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
52 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
54 public class ExportBranchesStepTest {
55 private static final String PROJECT_UUID = "PROJECT_UUID";
56 private static final ComponentDto PROJECT = new ComponentDto()
58 .setScope(Scopes.PROJECT)
59 .setQualifier(Qualifiers.PROJECT)
60 .setKey("the_project")
61 .setName("The Project")
62 .setDescription("The project description")
64 .setUuid(PROJECT_UUID)
65 .setUuidPath(UUID_PATH_OF_ROOT)
66 .setRootUuid(PROJECT_UUID)
68 .setModuleUuidPath("." + PROJECT_UUID + ".")
69 .setBranchUuid(PROJECT_UUID);
72 public DbTester dbTester = DbTester.createWithExtensionMappers(System2.INSTANCE, ProjectExportMapper.class);
74 public LogTester logTester = new LogTester();
76 private final ProjectHolder projectHolder = mock(ProjectHolder.class);
77 private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
78 private final ExportBranchesStep underTest = new ExportBranchesStep(dumpWriter, dbTester.getDbClient(), projectHolder);
79 private final List<BranchDto> branches = ImmutableList.of(
81 .setBranchType(BranchType.BRANCH)
82 .setProjectUuid(PROJECT_UUID)
84 .setUuid("branch-1-uuid")
85 .setMergeBranchUuid("master")
86 .setExcludeFromPurge(true),
88 .setBranchType(BranchType.PULL_REQUEST)
89 .setProjectUuid(PROJECT_UUID)
91 .setUuid("branch-3-uuid")
92 .setMergeBranchUuid("master"),
94 .setBranchType(BranchType.BRANCH)
95 .setProjectUuid(PROJECT_UUID)
97 .setUuid("branch-4-uuid")
98 .setMergeBranchUuid("branch-1-uuid"),
100 .setBranchType(BranchType.BRANCH)
101 .setProjectUuid(PROJECT_UUID)
103 .setUuid("branch-5-uuid")
104 .setMergeBranchUuid("master")
105 .setExcludeFromPurge(true));
108 public void setUp() {
109 Date createdAt = new Date();
110 ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT).setCreatedAt(createdAt);
111 for (BranchDto branch : branches) {
112 createdAt = DateUtils.addMinutes(createdAt, 10);
113 dbTester.components().insertProjectBranch(PROJECT, branch).setCreatedAt(createdAt);
116 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(projectDto));
120 public void export_branches() {
121 underTest.execute(new TestComputationStepContext());
123 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("3 branches exported");
124 Map<String, ProjectDump.Branch> branches = dumpWriter.getWrittenMessagesOf(DumpElement.BRANCHES)
126 .collect(toMap(ProjectDump.Branch::getUuid, Function.identity()));
127 assertThat(branches).hasSize(3);
128 ProjectDump.Branch mainBranch = branches.get(PROJECT_UUID);
129 assertThat(mainBranch).isNotNull();
130 assertThat(mainBranch.getKee()).isEqualTo(BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME);
131 assertThat(mainBranch.getProjectUuid()).isEqualTo(PROJECT_UUID);
132 assertThat(mainBranch.getMergeBranchUuid()).isEmpty();
133 assertThat(mainBranch.getBranchType()).isEqualTo("BRANCH");
134 ProjectDump.Branch branch1 = branches.get("branch-1-uuid");
135 assertThat(branch1.getKee()).isEqualTo("branch-1");
136 assertThat(branch1.getProjectUuid()).isEqualTo(PROJECT_UUID);
137 assertThat(branch1.getMergeBranchUuid()).isEqualTo("master");
138 assertThat(branch1.getBranchType()).isEqualTo("BRANCH");
139 ProjectDump.Branch branch5 = branches.get("branch-5-uuid");
140 assertThat(branch5.getKee()).isEqualTo("branch-5");
141 assertThat(branch5.getProjectUuid()).isEqualTo(PROJECT_UUID);
142 assertThat(branch5.getMergeBranchUuid()).isEqualTo("master");
143 assertThat(branch5.getBranchType()).isEqualTo("BRANCH");
147 public void throws_ISE_if_error() {
148 dumpWriter.failIfMoreThan(1, DumpElement.BRANCHES);
150 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
151 .isInstanceOf(IllegalStateException.class)
152 .hasMessage("Branch export failed after processing 1 branch(es) successfully");
156 public void getDescription_is_defined() {
157 assertThat(underTest.getDescription()).isEqualTo("Export branches");