]> source.dussan.org Git - sonarqube.git/blob
9c82b10856b1e48ca81adb19010b66571a0700e5
[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.projectexport.branches;
21
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;
26 import java.util.Map;
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;
44
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;
50
51 public class ExportBranchesStepIT {
52   private static final String PROJECT_UUID = "PROJECT_UUID";
53
54   @Rule
55   public DbTester dbTester = DbTester.createWithExtensionMappers(System2.INSTANCE, ProjectExportMapper.class);
56   @Rule
57   public LogTester logTester = new LogTester();
58
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(
63     new BranchDto()
64       .setBranchType(BranchType.BRANCH)
65       .setProjectUuid(PROJECT_UUID)
66       .setKey("branch-1")
67       .setUuid("branch-1-uuid")
68       .setMergeBranchUuid("master")
69       .setIsMain(false)
70       .setExcludeFromPurge(true),
71     new BranchDto()
72       .setBranchType(BranchType.PULL_REQUEST)
73       .setProjectUuid(PROJECT_UUID)
74       .setKey("branch-3")
75       .setUuid("branch-3-uuid")
76       .setIsMain(false)
77       .setMergeBranchUuid("master"),
78     new BranchDto()
79       .setBranchType(BranchType.BRANCH)
80       .setProjectUuid(PROJECT_UUID)
81       .setKey("branch-4")
82       .setUuid("branch-4-uuid")
83       .setIsMain(false)
84       .setMergeBranchUuid("branch-1-uuid"),
85     new BranchDto()
86       .setBranchType(BranchType.BRANCH)
87       .setProjectUuid(PROJECT_UUID)
88       .setKey("branch-5")
89       .setUuid("branch-5-uuid")
90       .setIsMain(false)
91       .setMergeBranchUuid("master")
92       .setExcludeFromPurge(true));
93
94   @Before
95   public void setUp() {
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);
102     }
103     dbTester.commit();
104     when(projectHolder.projectDto()).thenReturn(projectData.getProjectDto());
105   }
106
107   @Test
108   public void export_branches() {
109     underTest.execute(new TestComputationStepContext());
110
111     assertThat(logTester.logs(Level.DEBUG)).contains("3 branches exported");
112     Map<String, ProjectDump.Branch> branches = dumpWriter.getWrittenMessagesOf(DumpElement.BRANCHES)
113       .stream()
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");
132   }
133
134   @Test
135   public void throws_ISE_if_error() {
136     dumpWriter.failIfMoreThan(1, DumpElement.BRANCHES);
137
138     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
139       .isInstanceOf(IllegalStateException.class)
140       .hasMessage("Branch export failed after processing 1 branch(es) successfully");
141   }
142
143   @Test
144   public void getDescription_is_defined() {
145     assertThat(underTest.getDescription()).isEqualTo("Export branches");
146   }
147 }