]> source.dussan.org Git - sonarqube.git/blob
02e7f87db133aacd3dc671001900522545542ab7
[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.steps;
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 javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.slf4j.event.Level;
31 import org.sonar.api.resources.Qualifiers;
32 import org.sonar.api.testfixtures.log.LogTester;
33 import org.sonar.api.utils.System2;
34 import org.sonar.ce.task.step.TestComputationStepContext;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.BranchDto;
37 import org.sonar.db.component.BranchType;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
40 import org.sonar.db.newcodeperiod.NewCodePeriodType;
41 import org.sonar.db.project.ProjectDto;
42 import org.sonar.db.project.ProjectExportMapper;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.assertj.core.api.Assertions.assertThatThrownBy;
46 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
47 import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
48 import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
49
50 public class ExportNewCodePeriodsStepIT {
51
52   private static final String PROJECT_UUID = "project_uuid";
53   private static final String ANOTHER_PROJECT_UUID = "another_project_uuid";
54   private static final ComponentDto ANOTHER_PROJECT = new ComponentDto()
55     .setUuid(ANOTHER_PROJECT_UUID)
56     .setUuidPath(UUID_PATH_OF_ROOT)
57     .setBranchUuid(ANOTHER_PROJECT_UUID)
58     .setQualifier(Qualifiers.PROJECT)
59     .setName("another_project")
60     .setKey("another_project");
61
62   private static final List<BranchDto> PROJECT_BRANCHES = ImmutableList.of(
63     new BranchDto().setBranchType(BranchType.PULL_REQUEST).setProjectUuid(PROJECT_UUID).setKey("pr-1").setUuid("pr-uuid-1").setMergeBranchUuid("master").setIsMain(false),
64     new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-1").setUuid("branch-uuid-1").setMergeBranchUuid("master")
65       .setExcludeFromPurge(true).setIsMain(false),
66     new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-2").setUuid("branch-uuid-2").setMergeBranchUuid("master")
67       .setExcludeFromPurge(false).setIsMain(false));
68
69   private static final List<BranchDto> ANOTHER_PROJECT_BRANCHES = ImmutableList.of(
70     new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(ANOTHER_PROJECT_UUID).setKey("branch-3").setUuid("branch-uuid-3").setMergeBranchUuid("master")
71       .setExcludeFromPurge(true).setIsMain(false));
72
73   private ProjectDto project;
74
75   @Rule
76   public LogTester logTester = new LogTester();
77   @Rule
78   public DbTester dbTester = DbTester.createWithExtensionMappers(System2.INSTANCE, ProjectExportMapper.class);
79
80   private MutableProjectHolder projectHolder = new MutableProjectHolderImpl();
81   private FakeDumpWriter dumpWriter = new FakeDumpWriter();
82   private ExportNewCodePeriodsStep underTest = new ExportNewCodePeriodsStep(dbTester.getDbClient(), projectHolder, dumpWriter);
83
84   @Before
85   public void setUp() {
86     logTester.setLevel(Level.DEBUG);
87     Date createdAt = new Date();
88     project = dbTester.components().insertPrivateProject(PROJECT_UUID).getProjectDto();
89     PROJECT_BRANCHES.forEach(branch -> dbTester.components().insertProjectBranch(project, branch).setCreatedAt(createdAt));
90
91     ComponentDto anotherProjectDto = dbTester.components().insertPublicProject(ANOTHER_PROJECT).getMainBranchComponent();
92     ANOTHER_PROJECT_BRANCHES.forEach(branch -> dbTester.components().insertProjectBranch(anotherProjectDto, branch).setCreatedAt(createdAt));
93
94     dbTester.commit();
95     projectHolder.setProjectDto(project);
96   }
97
98   @Test
99   public void export_zero_new_code_periods() {
100     underTest.execute(new TestComputationStepContext());
101
102     assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.NEW_CODE_PERIODS)).isEmpty();
103     assertThat(logTester.logs(Level.DEBUG)).contains("0 new code periods exported");
104   }
105
106   @Test
107   public void export_only_project_new_code_periods_on_branches_excluded_from_purge() {
108     NewCodePeriodDto newCodePeriod1 = newDto("uuid1", project.getUuid(), null, SPECIFIC_ANALYSIS, "analysis-uuid");
109     NewCodePeriodDto newCodePeriod2 = newDto("uuid2", project.getUuid(), "branch-uuid-1", SPECIFIC_ANALYSIS, "analysis-uuid");
110     // the following new code periods are not exported
111     NewCodePeriodDto newCodePeriod3 = newDto("uuid3", project.getUuid(), "branch-uuid-2", SPECIFIC_ANALYSIS, "analysis-uuid");
112     NewCodePeriodDto anotherProjectNewCodePeriods = newDto("uuid4", ANOTHER_PROJECT.uuid(), "branch-uuid-3", SPECIFIC_ANALYSIS, "analysis-uuid");
113     NewCodePeriodDto globalNewCodePeriod = newDto("uuid5", null, null, PREVIOUS_VERSION, null);
114     insertNewCodePeriods(newCodePeriod1, newCodePeriod2, newCodePeriod3, anotherProjectNewCodePeriods, globalNewCodePeriod);
115
116     underTest.execute(new TestComputationStepContext());
117
118     List<ProjectDump.NewCodePeriod> exportedProps = dumpWriter.getWrittenMessagesOf(DumpElement.NEW_CODE_PERIODS);
119     assertThat(exportedProps).hasSize(2);
120     assertThat(exportedProps).extracting(ProjectDump.NewCodePeriod::getUuid).containsOnly("uuid1", "uuid2");
121     assertThat(logTester.logs(Level.DEBUG)).contains("2 new code periods exported");
122   }
123
124   @Test
125   public void test_exported_fields() {
126     NewCodePeriodDto dto = newDto("uuid1", project.getUuid(), "branch-uuid-1", SPECIFIC_ANALYSIS, "analysis-uuid");
127     insertNewCodePeriods(dto);
128
129     underTest.execute(new TestComputationStepContext());
130
131     ProjectDump.NewCodePeriod exportedNewCodePeriod = dumpWriter.getWrittenMessagesOf(DumpElement.NEW_CODE_PERIODS).get(0);
132     assertThat(exportedNewCodePeriod.getUuid()).isEqualTo(dto.getUuid());
133     assertThat(exportedNewCodePeriod.getProjectUuid()).isEqualTo(dto.getProjectUuid());
134     assertThat(exportedNewCodePeriod.getBranchUuid()).isEqualTo(dto.getBranchUuid());
135     assertThat(exportedNewCodePeriod.getType()).isEqualTo(dto.getType().name());
136     assertThat(exportedNewCodePeriod.getValue()).isEqualTo(dto.getValue());
137   }
138
139   @Test
140   public void throws_ISE_if_error() {
141     dumpWriter.failIfMoreThan(1, DumpElement.NEW_CODE_PERIODS);
142     insertNewCodePeriods(
143       newDto("uuid1", project.getUuid(), null, SPECIFIC_ANALYSIS, "analysis-uuid"),
144       newDto("uuid2", project.getUuid(), "branch-uuid-1", SPECIFIC_ANALYSIS, "analysis-uuid"));
145
146     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
147       .isInstanceOf(IllegalStateException.class)
148       .hasMessage("New Code Periods Export failed after processing 1 new code periods successfully");
149   }
150
151   @Test
152   public void test_getDescription() {
153     assertThat(underTest.getDescription()).isEqualTo("Export new code periods");
154   }
155
156   private static NewCodePeriodDto newDto(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
157     return new NewCodePeriodDto()
158       .setUuid(uuid)
159       .setProjectUuid(projectUuid)
160       .setBranchUuid(branchUuid)
161       .setType(type)
162       .setValue(value);
163   }
164
165   private void insertNewCodePeriods(NewCodePeriodDto... dtos) {
166     for (NewCodePeriodDto dto : dtos) {
167       dbTester.getDbClient().newCodePeriodDao().insert(dbTester.getSession(), dto);
168     }
169     dbTester.commit();
170   }
171 }