]> source.dussan.org Git - sonarqube.git/blob
1c225bdef7449fd248857dd9271c24137ae40c85
[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.projectanalysis.issue;
21
22 import java.util.Date;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.ComponentImpl;
29 import org.sonar.ce.task.projectanalysis.component.ReportAttributes;
30 import org.sonar.core.util.Uuids;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.BranchDto;
34 import org.sonar.db.component.BranchType;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.issue.AnticipatedTransitionDto;
37 import org.sonar.db.project.CreationMethod;
38 import org.sonar.db.project.ProjectDto;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
43
44 public class AnticipatedTransitionRepositoryImplTest {
45
46   @Rule
47   public final DbTester db = DbTester.create(System2.INSTANCE);
48
49   private final DbClient dbClient = db.getDbClient();
50
51   private final AnticipatedTransitionRepository underTest = new AnticipatedTransitionRepositoryImpl(dbClient);
52
53   @Before
54   public void cleanUpDb() {
55
56   }
57
58   @Test
59   public void giveAnticipatedTransitionsForFile_shouldBeReturnedCorrectly() {
60     //given
61     String projectKey = "projectKey1";
62     String projectUuid = "projectUuid1";
63     dbClient.projectDao().insert(db.getSession(), getProjectDto(projectUuid, projectKey));
64
65     insertAnticipatedTransition(projectUuid, "file1.js");
66     insertAnticipatedTransition(projectUuid, "file2.js");
67     insertAnticipatedTransition(projectUuid, "file2.js");
68
69     db.getSession().commit();
70
71     String componentUuid = "componentUuid";
72     Component file = getFileComponent(componentUuid, projectKey, "file1.js");
73     var anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
74     assertThat(anticipatedTransitions).hasSize(1);
75
76     file = getFileComponent(componentUuid, projectKey, "file2.js");
77     anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
78     assertThat(anticipatedTransitions).hasSize(2);
79
80     file = getFileComponent(componentUuid, projectKey, "file3.js");
81     anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
82     assertThat(anticipatedTransitions).isEmpty();
83   }
84
85   @Test
86   public void giveProjectBranchAvailable_projectUuidShouldBeCalculatedFromThere() {
87     //given
88     String projectKey = "projectKey2";
89     String projectUuid = "projectUuid2";
90     String mainFile = "file1.js";
91     dbClient.projectDao().insert(db.getSession(), getProjectDto(projectUuid, projectKey));
92
93     BranchDto branchDto = getBranchDto(projectUuid, "branch");
94     dbClient.branchDao().insert(db.getSession(), branchDto);
95
96     ComponentDto fileDto = getComponentDto(projectKey + ":" + mainFile, branchDto.getUuid());
97     dbClient.componentDao().insertOnMainBranch(db.getSession(), fileDto);
98
99     insertAnticipatedTransition(projectUuid, mainFile);
100     insertAnticipatedTransition(projectUuid, "file2.js");
101     insertAnticipatedTransition(projectUuid, "file2.js");
102
103     db.getSession().commit();
104
105     Component file = getFileComponent(fileDto.uuid(), projectKey, mainFile);
106     var anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
107     assertThat(anticipatedTransitions).hasSize(1);
108   }
109
110   private void insertAnticipatedTransition(String projectUuid, String filename) {
111     var anticipatedTransition = getAnticipatedTransition(projectUuid, filename);
112     dbClient.anticipatedTransitionDao().insert(db.getSession(), anticipatedTransition);
113   }
114
115   private ComponentDto getComponentDto(String componentKey, String branchUuid) {
116     ComponentDto componentDto = new ComponentDto();
117     componentDto.setQualifier("FIL")
118       .setUuid(Uuids.createFast())
119       .setKey(componentKey)
120       .setBranchUuid(branchUuid)
121       .setUuidPath(Uuids.createFast());
122     return componentDto;
123   }
124
125
126   private BranchDto getBranchDto(String projectUuid, String key) {
127     BranchDto branchDto = new BranchDto();
128     branchDto.setProjectUuid(projectUuid)
129       .setUuid(Uuids.createFast())
130       .setIsMain(true)
131       .setBranchType(BranchType.BRANCH)
132       .setKey(key);
133     return branchDto;
134   }
135
136   private ProjectDto getProjectDto(String projectUuid, String projectKey) {
137     ProjectDto projectDto = new ProjectDto();
138     projectDto.setKey(projectKey);
139     projectDto.setUuid(projectUuid);
140     projectDto.setQualifier("TRK");
141     projectDto.setName("project");
142     projectDto.setCreationMethod(CreationMethod.LOCAL_API);
143     return projectDto;
144   }
145
146   private Component getFileComponent(String componenUuid, String projectKey, String filename) {
147     return ComponentImpl.builder(FILE)
148       .setUuid(componenUuid)
149       .setKey(String.format("%s:%s", projectKey, filename))
150       .setName(filename)
151       .setStatus(Component.Status.ADDED)
152       .setShortName(filename)
153       .setReportAttributes(mock(ReportAttributes.class)).build();
154   }
155
156   private AnticipatedTransitionDto getAnticipatedTransition(String projectUuid, String filename) {
157     return new AnticipatedTransitionDto(Uuids.createFast(), projectUuid, "admin", "wontfix", null, null, null, null, "rule:key", filename, (new Date()).getTime());
158   }
159
160 }