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.projectanalysis.issue;
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;
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;
44 public class AnticipatedTransitionRepositoryImplTest {
47 public final DbTester db = DbTester.create(System2.INSTANCE);
49 private final DbClient dbClient = db.getDbClient();
51 private final AnticipatedTransitionRepository underTest = new AnticipatedTransitionRepositoryImpl(dbClient);
54 public void cleanUpDb() {
59 public void giveAnticipatedTransitionsForFile_shouldBeReturnedCorrectly() {
61 String projectKey = "projectKey1";
62 String projectUuid = "projectUuid1";
63 dbClient.projectDao().insert(db.getSession(), getProjectDto(projectUuid, projectKey));
65 insertAnticipatedTransition(projectUuid, "file1.js");
66 insertAnticipatedTransition(projectUuid, "file2.js");
67 insertAnticipatedTransition(projectUuid, "file2.js");
69 db.getSession().commit();
71 String componentUuid = "componentUuid";
72 Component file = getFileComponent(componentUuid, projectKey, "file1.js");
73 var anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
74 assertThat(anticipatedTransitions).hasSize(1);
76 file = getFileComponent(componentUuid, projectKey, "file2.js");
77 anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
78 assertThat(anticipatedTransitions).hasSize(2);
80 file = getFileComponent(componentUuid, projectKey, "file3.js");
81 anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
82 assertThat(anticipatedTransitions).isEmpty();
86 public void giveProjectBranchAvailable_projectUuidShouldBeCalculatedFromThere() {
88 String projectKey = "projectKey2";
89 String projectUuid = "projectUuid2";
90 String mainFile = "file1.js";
91 dbClient.projectDao().insert(db.getSession(), getProjectDto(projectUuid, projectKey));
93 BranchDto branchDto = getBranchDto(projectUuid, "branch");
94 dbClient.branchDao().insert(db.getSession(), branchDto);
96 ComponentDto fileDto = getComponentDto(projectKey + ":" + mainFile, branchDto.getUuid());
97 dbClient.componentDao().insertOnMainBranch(db.getSession(), fileDto);
99 insertAnticipatedTransition(projectUuid, mainFile);
100 insertAnticipatedTransition(projectUuid, "file2.js");
101 insertAnticipatedTransition(projectUuid, "file2.js");
103 db.getSession().commit();
105 Component file = getFileComponent(fileDto.uuid(), projectKey, mainFile);
106 var anticipatedTransitions = underTest.getAnticipatedTransitionByComponent(file);
107 assertThat(anticipatedTransitions).hasSize(1);
110 private void insertAnticipatedTransition(String projectUuid, String filename) {
111 var anticipatedTransition = getAnticipatedTransition(projectUuid, filename);
112 dbClient.anticipatedTransitionDao().insert(db.getSession(), anticipatedTransition);
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());
126 private BranchDto getBranchDto(String projectUuid, String key) {
127 BranchDto branchDto = new BranchDto();
128 branchDto.setProjectUuid(projectUuid)
129 .setUuid(Uuids.createFast())
131 .setBranchType(BranchType.BRANCH)
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);
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))
151 .setStatus(Component.Status.ADDED)
152 .setShortName(filename)
153 .setReportAttributes(mock(ReportAttributes.class)).build();
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());