]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19556 Fix usage of branchDao in tests
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Wed, 21 Jun 2023 14:00:01 +0000 (16:00 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 22 Jun 2023 20:02:53 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/ShowActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/AuthorsActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/measure/ws/ComponentActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/source/ws/IssueSnippetsActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CurrentActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java

index cd6db266a8ccb92921d9963bc50f3525e34bfec2..063ec52e6b3701b08b4aea2fd257d9ab77828cee 100644 (file)
@@ -66,7 +66,7 @@ public class ShowActionIT {
   @Rule
   public final UserSessionRule userSession = UserSessionRule.standalone().logIn();
   @Rule
-  public final DbTester db = DbTester.create(System2.INSTANCE);
+  public final DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final WsActionTester ws = new WsActionTester(new ShowAction(userSession, db.getDbClient(), TestComponentFinder.from(db),
     new IssueIndexSyncProgressChecker(db.getDbClient())));
@@ -217,7 +217,8 @@ public class ShowActionIT {
     ProjectData projectData = db.components().insertPublicProject();
     ComponentDto publicProject = projectData.getMainBranchComponent();
 
-    userSession.registerProjects(projectData.getProjectDto());
+    userSession.registerProjects(projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
 
     ShowWsResponse result = newRequest(publicProject.getKey());
     assertThat(result.getComponent().hasVisibility()).isTrue();
@@ -349,7 +350,8 @@ public class ShowActionIT {
     userSession.addProjectBranchMapping(projectData3.projectUuid(), branch4);
     userSession.addProjectBranchMapping(projectData3.projectUuid(), branch5);
 
-    userSession.addProjectPermission(UserRole.USER, projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto());
+    userSession.addProjectPermission(UserRole.USER, projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto())
+        .registerBranches(projectData1.getMainBranchDto(), projectData2.getMainBranchDto(), projectData3.getMainBranchDto());
     userSession.registerPortfolios(portfolio1, portfolio2, subview);
     userSession.registerProjects(projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto());
 
index 893f4607e4b0a804780e42e246c292c964977621..3904df5dd7f623fb609a0f6a65fa11fbe918d38b 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.server.component.ComponentFinder;
@@ -82,8 +83,9 @@ public class AuthorsActionIT {
   public void search_authors() {
     String leia = "leia.organa";
     String luke = "luke.skywalker";
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
@@ -100,8 +102,9 @@ public class AuthorsActionIT {
   public void search_authors_by_query() {
     String leia = "leia.organa";
     String luke = "luke.skywalker";
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
@@ -121,9 +124,11 @@ public class AuthorsActionIT {
   public void search_authors_by_project() {
     String leia = "leia.organa";
     String luke = "luke.skywalker";
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project1, project2);
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData1.getProjectDto(), projectData2.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
     db.issues().insertIssue(rule, project2, project2, issue -> issue.setAuthorLogin(luke));
@@ -152,9 +157,10 @@ public class AuthorsActionIT {
   public void search_authors_by_portfolio() {
     String leia = "leia.organa";
     ComponentDto portfolio = db.components().insertPrivatePortfolio();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     db.components().insertComponent(newProjectCopy(project, portfolio));
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
     indexIssues();
@@ -171,9 +177,10 @@ public class AuthorsActionIT {
   public void search_authors_by_application() {
     String leia = "leia.organa";
     ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     db.components().insertComponent(newProjectCopy(project, application));
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
     indexIssues();
@@ -191,8 +198,9 @@ public class AuthorsActionIT {
     String han = "han.solo";
     String leia = "leia.organa";
     String luke = "luke.skywalker";
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(han));
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
@@ -212,8 +220,9 @@ public class AuthorsActionIT {
   @Test
   public void should_ignore_authors_of_hotspot() {
     String luke = "luke.skywalker";
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     db.issues().insertHotspot(project, project, issue -> issue
       .setAuthorLogin(luke));
     indexIssues();
@@ -239,17 +248,18 @@ public class AuthorsActionIT {
   @Test
   public void fail_when_project_is_not_a_project() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThatThrownBy(() -> {
       ws.newRequest()
         .setParam("project", file.getKey())
         .execute();
     })
-      .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage(format("Component '%s' must be a project", file.getKey()));
+      .isInstanceOf(NotFoundException.class)
+      .hasMessage(format("Component '%s' not found", file.getKey()));
   }
 
   @Test
@@ -262,13 +272,14 @@ public class AuthorsActionIT {
         .execute();
     })
       .isInstanceOf(NotFoundException.class)
-      .hasMessage("Component key 'unknown' not found");
+      .hasMessage("Component 'unknown' not found");
   }
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("luke.skywalker"));
     db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("leia.organa"));
index 5744a0c46c3ef0f95fe003a7e2e50b0393d1c412..941ea2d924d6f3e470ac98e152e01715a74ba835 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.measure.LiveMeasureDto;
 import org.sonar.db.metric.MetricDto;
@@ -65,7 +66,7 @@ public class ComponentActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final WsActionTester ws = new WsActionTester(new ComponentAction(db.getDbClient(), TestComponentFinder.from(db), userSession));
 
@@ -88,26 +89,30 @@ public class ComponentActionIT {
 
   @Test
   public void provided_project() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
-    ComponentWsResponse response = newRequest(project.getKey(), metric.getKey());
+    ComponentWsResponse response = newRequest(mainBranch.getKey(), metric.getKey());
 
     assertThat(response.getMetrics().getMetricsCount()).isOne();
     assertThat(response.hasPeriod()).isFalse();
-    assertThat(response.getComponent().getKey()).isEqualTo(project.getKey());
+    assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
   }
 
   @Test
   public void without_additional_fields() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
+    db.components().insertSnapshot(mainBranch);
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
     String response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, metric.getKey())
       .execute().getInput();
 
@@ -118,13 +123,15 @@ public class ComponentActionIT {
 
   @Test
   public void branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
     String branchName = "my_branch";
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
     LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
 
@@ -143,9 +150,11 @@ public class ComponentActionIT {
 
   @Test
   public void branch_not_set_if_main_branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
     MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
 
     ComponentWsResponse response = ws.newRequest()
@@ -160,12 +169,13 @@ public class ComponentActionIT {
 
   @Test
   public void pull_request() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
     LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
 
@@ -184,12 +194,14 @@ public class ComponentActionIT {
 
   @Test
   public void new_issue_count_measures_are_transformed_in_pr() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
     MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setValueType("INT"));
     MetricDto violations = db.measures().insertMetric(m1 -> m1.setKey("violations").setValueType("INT"));
@@ -222,12 +234,13 @@ public class ComponentActionIT {
 
   @Test
   public void new_issue_count_measures_are_not_transformed_if_they_dont_exist_in_pr() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setOptimizedBestValue(false).setValueType("INT"));
     MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setOptimizedBestValue(false).setValueType("INT"));
 
@@ -245,44 +258,47 @@ public class ComponentActionIT {
 
   @Test
   public void reference_key_in_the_response() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
     ComponentDto view = db.components().insertPrivatePortfolio();
     userSession.addProjectPermission(USER, view);
     db.components().insertSnapshot(view);
-    ComponentDto projectCopy = db.components().insertComponent(newProjectCopy("project-uuid-copy", project, view));
+    ComponentDto projectCopy = db.components().insertComponent(newProjectCopy("project-uuid-copy", mainBranch, view));
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
     ComponentWsResponse response = newRequest(projectCopy.getKey(), metric.getKey());
 
-    assertThat(response.getComponent().getRefKey()).isEqualTo(project.getKey());
+    assertThat(response.getComponent().getRefKey()).isEqualTo(mainBranch.getKey());
   }
 
   @Test
   public void use_deprecated_component_id_parameter() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
     ComponentWsResponse response = ws.newRequest()
-      .setParam("component", project.getKey())
+      .setParam("component", mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, metric.getKey())
       .executeProtobuf(ComponentWsResponse.class);
 
-    assertThat(response.getComponent().getKey()).isEqualTo(project.getKey());
+    assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
   }
 
   @Test
   public void metric_without_a_domain() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
     MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m
       .setValueType("INT")
       .setDomain(null));
-    db.measures().insertLiveMeasure(project, metricWithoutDomain);
+    db.measures().insertLiveMeasure(mainBranch, metricWithoutDomain);
 
     ComponentWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
       .executeProtobuf(ComponentWsResponse.class);
@@ -295,9 +311,9 @@ public class ComponentActionIT {
 
   @Test
   public void use_best_values() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
-    userSession.addProjectPermission(USER, project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
+    userSession.addProjectPermission(USER, mainBranch);
     MetricDto metric = db.measures().insertMetric(m -> m
       .setValueType("INT")
       .setBestValue(7.0d)
@@ -317,24 +333,27 @@ public class ComponentActionIT {
 
   @Test
   public void fail_when_a_metric_is_not_found() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+        .registerBranches(projectData.getMainBranchDto());
+    db.components().insertSnapshot(mainBranch);
     db.measures().insertMetric(m -> m.setKey("ncloc").setValueType("INT"));
     db.measures().insertMetric(m -> m.setKey("complexity").setValueType("INT"));
 
-    assertThatThrownBy(() -> newRequest(project.getKey(), "ncloc, complexity, unknown-metric, another-unknown-metric"))
+    assertThatThrownBy(() -> newRequest(mainBranch.getKey(), "ncloc, complexity, unknown-metric, another-unknown-metric"))
       .isInstanceOf(NotFoundException.class)
       .hasMessage("The following metric keys are not found: unknown-metric, another-unknown-metric");
   }
 
   @Test
   public void fail_when_empty_metric_keys_parameter() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    db.components().insertSnapshot(mainBranch);
 
-    assertThatThrownBy(() -> newRequest(project.getKey(), ""))
+    assertThatThrownBy(() -> newRequest(mainBranch.getKey(), ""))
       .isInstanceOf(BadRequestException.class)
       .hasMessage("At least one metric key must be provided");
   }
@@ -342,11 +361,11 @@ public class ComponentActionIT {
   @Test
   public void fail_when_not_enough_permission() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
-    assertThatThrownBy(() -> newRequest(project.getKey(), metric.getKey()))
+    assertThatThrownBy(() -> newRequest(mainBranch.getKey(), metric.getKey()))
       .isInstanceOf(ForbiddenException.class);
   }
 
@@ -366,27 +385,29 @@ public class ComponentActionIT {
 
   @Test
   public void fail_when_component_is_removed() {
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setEnabled(false)).getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject(p -> p.setEnabled(false));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
     MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, metric.getKey())
         .execute();
     })
       .isInstanceOf(NotFoundException.class)
-      .hasMessage(String.format("Component key '%s' not found", project.getKey()));
+      .hasMessage(String.format("Component key '%s' not found", mainBranch.getKey()));
   }
 
   @Test
   public void fail_if_branch_does_not_exist() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
-    userSession.addProjectPermission(USER, project);
-    db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
 
     assertThatThrownBy(() -> {
       ws.newRequest()
@@ -401,13 +422,15 @@ public class ComponentActionIT {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto analysis = db.components().insertSnapshot(project,
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .registerBranches(projectData.getMainBranchDto());
+    SnapshotDto analysis = db.components().insertSnapshot(mainBranch,
       s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
         .setPeriodMode("previous_version")
         .setPeriodParam("1.0-SNAPSHOT"));
-    ComponentDto file = db.components().insertComponent(newFileDto(project)
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch)
       .setKey("MY_PROJECT:ElementImpl.java")
       .setName("ElementImpl.java")
       .setLanguage("java")
index f3a8bc12f5927695320e1051fa6d0090ad1c7c74..05a5513b20babaad458d7a0c5b1ea51b11607d47 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.issue.IssueDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.protobuf.DbCommons;
@@ -72,12 +73,14 @@ public class IssueSnippetsActionIT {
 
   private final DbClient dbClient = db.getDbClient();
   private final FileSourceTester fileSourceTester = new FileSourceTester(db);
-  private ComponentDto project;
+  private ProjectData project;
   private WsActionTester actionTester;
+  private ComponentDto mainBranchComponent;
 
   @Before
   public void setUp() {
-    project = db.components().insertPrivateProject("projectUuid").getMainBranchComponent();
+    project = db.components().insertPrivateProject("projectUuid");
+    mainBranchComponent = project.getMainBranchComponent();
 
     HtmlSourceDecorator htmlSourceDecorator = mock(HtmlSourceDecorator.class);
     when(htmlSourceDecorator.getDecoratedSourceAsHtml(anyString(), anyString(), anyString()))
@@ -100,10 +103,10 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void should_display_single_location_single_file() {
-    ComponentDto file = insertFile(project, "file");
+    ComponentDto file = insertFile(mainBranchComponent, "file");
     DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
     fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
 
@@ -113,7 +116,7 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void should_add_measures_to_components() {
-    ComponentDto file = insertFile(project, "file");
+    ComponentDto file = insertFile(mainBranchComponent, "file");
 
     MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
     db.measures().insertLiveMeasure(file, lines, m -> m.setValue(200d));
@@ -130,7 +133,7 @@ public class IssueSnippetsActionIT {
 
     DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
     fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
 
@@ -140,12 +143,12 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void issue_references_a_non_existing_component() {
-    ComponentDto file = insertFile(project, "file");
-    ComponentDto file2 = newFileDto(project, null, "nonexisting");
+    ComponentDto file = insertFile(mainBranchComponent, "file");
+    ComponentDto file2 = newFileDto(mainBranchComponent, null, "nonexisting");
 
     DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
     fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     String issueKey = insertIssue(file, newLocation(file2.uuid(), 5, 5));
 
@@ -155,8 +158,8 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void no_code_to_display() {
-    ComponentDto file = insertFile(project, "file");
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    ComponentDto file = insertFile(mainBranchComponent, "file");
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
 
@@ -166,8 +169,8 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void fail_if_no_project_permission() {
-    ComponentDto file = insertFile(project, "file");
-    userSession.logIn().addProjectPermission(USER, project, file);
+    ComponentDto file = insertFile(mainBranchComponent, "file");
+    userSession.logIn().addProjectPermission(USER, project.getProjectDto());
     String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
 
     var request = actionTester.newRequest().setParam("issueKey", issueKey);
@@ -177,9 +180,9 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void fail_if_issue_not_found() {
-    ComponentDto file = insertFile(project, "file");
+    ComponentDto file = insertFile(mainBranchComponent, "file");
     insertIssue(file, newLocation(file.uuid(), 5, 5));
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     var request = actionTester.newRequest().setParam("issueKey", "invalid");
     assertThatThrownBy(request::execute)
@@ -189,8 +192,8 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void fail_if_parameter_missing() {
-    ComponentDto file = insertFile(project, "file");
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+    ComponentDto file = insertFile(mainBranchComponent, "file");
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     var request = actionTester.newRequest();
     assertThatThrownBy(request::execute)
@@ -200,14 +203,14 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void should_display_multiple_locations_multiple_files() {
-    ComponentDto file1 = insertFile(project, "file1");
-    ComponentDto file2 = insertFile(project, "file2");
+    ComponentDto file1 = insertFile(mainBranchComponent, "file1");
+    ComponentDto file2 = insertFile(mainBranchComponent, "file2");
 
     DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
     fileSourceTester.insertFileSource(file1, 10, dto -> dto.setSourceData(fileSources));
     fileSourceTester.insertFileSource(file2, 10, dto -> dto.setSourceData(fileSources));
 
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file1, file2);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     String issueKey1 = insertIssue(file1, newLocation(file1.uuid(), 5, 5),
       newLocation(file1.uuid(), 9, 9), newLocation(file2.uuid(), 1, 5));
@@ -219,12 +222,12 @@ public class IssueSnippetsActionIT {
 
   @Test
   public void should_connect_snippets_close_to_each_other() {
-    ComponentDto file1 = insertFile(project, "file1");
+    ComponentDto file1 = insertFile(mainBranchComponent, "file1");
 
     DbFileSources.Data fileSources = FileSourceTesting.newFakeData(20).build();
     fileSourceTester.insertFileSource(file1, 20, dto -> dto.setSourceData(fileSources));
 
-    userSession.logIn().addProjectPermission(CODEVIEWER, project, file1);
+    userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
 
     // these two locations should get connected, making a single range 3-14
     String issueKey1 = insertIssue(file1, newLocation(file1.uuid(), 5, 5),
@@ -252,9 +255,9 @@ public class IssueSnippetsActionIT {
     RuleDto rule = db.rules().insert();
     DbIssues.Flow flow = DbIssues.Flow.newBuilder().addAllLocation(Arrays.asList(locations)).build();
 
-    IssueDto issue = db.issues().insert(rule, project, file, i -> {
+    IssueDto issue = db.issues().insert(rule, project.getMainBranchComponent(), file, i -> {
       i.setLocations(DbIssues.Locations.newBuilder().addFlow(flow).build());
-      i.setProjectUuid(project.uuid());
+      i.setProjectUuid(mainBranchComponent.uuid());
       i.setLine(locations[0].getTextRange().getStartLine());
     });
 
index 0d1d89ef5b10a44d53031233481bc8a24b860501..c9f6fa59d5a19d7f245f169951b678d85b17e316 100644 (file)
@@ -57,7 +57,7 @@ public class CurrentActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
   private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
index 07be0d6bb215c2c3bf4b3e253c207a493885eee7..e99e929498da6390ca1642592d1b6f8f726040b0 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
 import org.sonar.api.rules.RuleType;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
@@ -35,8 +34,10 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
 import org.sonar.server.issue.index.IssueQuery;
@@ -75,8 +76,8 @@ public class AuthorsAction implements IssuesWsAction {
     NewAction action = controller.createAction("authors")
       .setSince("5.1")
       .setDescription("Search SCM accounts which match a given query.<br/>" +
-        "Requires authentication."
-        + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
+                      "Requires authentication."
+                      + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
       .setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
       .setChangelog(new Change("7.4", "The maximum size of 'ps' is set to 100"))
       .setHandler(this);
@@ -97,8 +98,8 @@ public class AuthorsAction implements IssuesWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       checkIfComponentNeedIssueSync(dbSession, request.param(PARAM_PROJECT));
 
-      Optional<ComponentDto> project = getProject(dbSession, request.param(PARAM_PROJECT));
-      List<String> authors = getAuthors(project.orElse(null), request);
+      Optional<EntityDto> entity = getEntity(dbSession, request.param(PARAM_PROJECT));
+      List<String> authors = getAuthors(entity.orElse(null), request, dbSession);
       AuthorsResponse wsResponse = AuthorsResponse.newBuilder().addAllAuthors(authors).build();
       writeProtobuf(wsResponse, request, response);
     }
@@ -112,27 +113,32 @@ public class AuthorsAction implements IssuesWsAction {
     }
   }
 
-  private Optional<ComponentDto> getProject(DbSession dbSession, @Nullable String projectKey) {
+  private Optional<EntityDto> getEntity(DbSession dbSession, @Nullable String projectKey) {
     if (projectKey == null) {
       return Optional.empty();
     }
-    ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
-    checkArgument(project.scope().equals(Scopes.PROJECT), "Component '%s' must be a project", projectKey);
-    return Optional.of(project);
+    EntityDto entity = componentFinder.getEntityByKey(dbSession, projectKey);
+    return Optional.of(entity);
   }
 
-  private List<String> getAuthors(@Nullable ComponentDto project, Request request) {
+  private List<String> getAuthors(@Nullable EntityDto entity, Request request, DbSession dbSession) {
     IssueQuery.Builder issueQueryBuilder = IssueQuery.builder();
-    ofNullable(project).ifPresent(p -> {
-      switch (p.qualifier()) {
+    ofNullable(entity).ifPresent(e -> {
+      switch (e.getQualifier()) {
         case Qualifiers.PROJECT:
-          issueQueryBuilder.projectUuids(Set.of(p.uuid()));
+          issueQueryBuilder.projectUuids(Set.of(e.getUuid()));
           return;
-        case Qualifiers.APP, Qualifiers.VIEW:
-          issueQueryBuilder.viewUuids(Set.of(p.uuid()));
+        case Qualifiers.VIEW:
+          issueQueryBuilder.viewUuids(Set.of(e.getUuid()));
+          return;
+        case Qualifiers.APP:
+          issueQueryBuilder.viewUuids(Set.of(e.getUuid()));
+          BranchDto branchDto = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, entity.getUuid())
+            .orElseThrow(() -> new NotFoundException("Main branch of application %s not found".formatted(e.getUuid())));
+          issueQueryBuilder.branchUuid(branchDto.getUuid());
           return;
         default:
-          throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", p.qualifier()));
+          throw new IllegalArgumentException(String.format("Entity of type '%s' is not supported", e.getQualifier()));
       }
     });
     return issueIndex.searchAuthors(