]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14256 Create events not working for branches
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 10 Dec 2020 20:05:13 +0000 (14:05 -0600)
committersonartech <sonartech@sonarsource.com>
Mon, 14 Dec 2020 20:07:14 +0000 (20:07 +0000)
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java

index 2ee5805eb4caabedf02de2e35f3287e8ec928e38..e91b49a23ecf474af51d889f33b20477f3987f5a 100644 (file)
@@ -413,6 +413,16 @@ public class ComponentDbTester {
     return insertSnapshot(branchDto, defaults());
   }
 
+  public SnapshotDto insertSnapshot(ProjectDto project) {
+    return insertSnapshot(project, defaults());
+  }
+
+  public SnapshotDto insertSnapshot(ProjectDto project, Consumer<SnapshotDto> consumer) {
+    SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(project.getUuid());
+    consumer.accept(snapshotDto);
+    return insertSnapshot(snapshotDto);
+  }
+
   public SnapshotDto insertSnapshot(BranchDto branchDto, Consumer<SnapshotDto> consumer) {
     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(branchDto);
     consumer.accept(snapshotDto);
index d61f0bdb2d35c70eaaed89c435e0edb5b766f98f..79c4036f3d8efb9bb1dc68e4c55ab99704bb2218 100644 (file)
@@ -130,12 +130,13 @@ public class CreateEventAction implements ProjectAnalysesWsAction {
 
   private SnapshotDto getAnalysis(DbSession dbSession, CreateEventRequest request) {
     return dbClient.snapshotDao().selectByUuid(dbSession, request.getAnalysis())
-      .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", request.getAnalysis())));
+      .orElseThrow(() -> new NotFoundException(format("Analysis '%s' not found", request.getAnalysis())));
   }
 
   private ProjectDto getProjectOrApplication(DbSession dbSession, SnapshotDto analysis) {
-    return dbClient.projectDao().selectByUuid(dbSession, analysis.getComponentUuid())
-      .orElseThrow(() -> new IllegalStateException(String.format("Project of analysis '%s' is not found", analysis.getUuid())));
+    return dbClient.branchDao().selectByUuid(dbSession, analysis.getComponentUuid())
+      .flatMap(branch -> dbClient.projectDao().selectByUuid(dbSession, branch.getProjectUuid()))
+      .orElseThrow(() -> new IllegalStateException(String.format("Project of analysis '%s' not found", analysis.getUuid())));
   }
 
   private void checkRequest(CreateEventRequest request, ProjectDto project) {
index b7a294410cf0b0f3db891453ef70404d6d081085..2fd172120c2acde4f0318ef2df096a8e0fb34a92 100644 (file)
@@ -33,11 +33,10 @@ import org.sonar.core.util.UuidFactoryFast;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.SnapshotDto;
-import org.sonar.db.component.SnapshotTesting;
 import org.sonar.db.event.EventDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.tester.UserSessionRule;
@@ -49,18 +48,15 @@ import org.sonarqube.ws.ProjectAnalyses.CreateEventResponse;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import static org.sonar.db.component.ComponentTesting.newApplication;
 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
-import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.db.component.SnapshotTesting.newAnalysis;
 import static org.sonar.db.component.SnapshotTesting.newSnapshot;
-import static org.sonar.test.JsonAssert.assertJson;
-import static org.sonarqube.ws.client.WsRequest.Method.POST;
 import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER;
 import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_CATEGORY;
 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME;
+import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.client.WsRequest.Method.POST;
 
 public class CreateEventActionTest {
 
@@ -85,8 +81,8 @@ public class CreateEventActionTest {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject();
-    SnapshotDto analysis = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newAnalysis(project).setUuid("A2"));
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setUuid("A2"));
     db.commit();
     uuidFactory = mock(UuidFactory.class);
     when(uuidFactory.create()).thenReturn("E1");
@@ -104,8 +100,32 @@ public class CreateEventActionTest {
 
   @Test
   public void create_event_in_db() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
+    when(system.now()).thenReturn(123_456_789L);
+    logInAsProjectAdministrator(project);
+
+    CreateEventResponse result = call(VERSION.name(), "5.6.3", analysis.getUuid());
+
+    List<EventDto> dbEvents = dbClient.eventDao().selectByComponentUuid(dbSession, analysis.getComponentUuid());
+    assertThat(dbEvents).hasSize(1);
+    EventDto dbEvent = dbEvents.get(0);
+    assertThat(dbEvent.getName()).isEqualTo("5.6.3");
+    assertThat(dbEvent.getCategory()).isEqualTo(VERSION.getLabel());
+    assertThat(dbEvent.getDescription()).isNull();
+    assertThat(dbEvent.getAnalysisUuid()).isEqualTo(analysis.getUuid());
+    assertThat(dbEvent.getComponentUuid()).isEqualTo(analysis.getComponentUuid());
+    assertThat(dbEvent.getUuid()).isEqualTo(result.getEvent().getKey());
+    assertThat(dbEvent.getCreatedAt()).isEqualTo(123_456_789L);
+    assertThat(dbEvent.getDate()).isEqualTo(analysis.getCreatedAt());
+  }
+
+  @Test
+  public void create_event_in_branch() {
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project);
+    SnapshotDto analysis = db.components().insertSnapshot(branch);
+
     when(system.now()).thenReturn(123_456_789L);
     logInAsProjectAdministrator(project);
 
@@ -126,8 +146,8 @@ public class CreateEventActionTest {
 
   @Test
   public void create_event_as_project_admin() {
-    ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "P1");
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     CreateEventResponse result = call(VERSION.name(), "5.6.3", analysis.getUuid());
@@ -137,8 +157,8 @@ public class CreateEventActionTest {
 
   @Test
   public void create_version_event() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     call(VERSION.name(), "5.6.3", analysis.getUuid());
@@ -149,8 +169,8 @@ public class CreateEventActionTest {
 
   @Test
   public void create_other_event_with_ws_response() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     CreateEventResponse result = call(OTHER.name(), "Project Import", analysis.getUuid());
@@ -167,8 +187,8 @@ public class CreateEventActionTest {
 
   @Test
   public void create_event_without_description() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     CreateEventResponse result = call(OTHER.name(), "Project Import", analysis.getUuid());
@@ -180,7 +200,7 @@ public class CreateEventActionTest {
 
   @Test
   public void create_event_on_application() {
-    ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
+    ProjectDto application = db.components().insertPrivateApplicationDto();
     SnapshotDto analysis = db.components().insertSnapshot(application);
     logInAsProjectAdministrator(application);
 
@@ -192,23 +212,23 @@ public class CreateEventActionTest {
 
   @Test
   public void create_2_version_events_on_same_project() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto firstAnalysis = db.components().insertProjectAndSnapshot(project);
-    SnapshotDto secondAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(project);
+    SnapshotDto secondAnalysis = db.components().insertSnapshot(project);
     db.commit();
     logInAsProjectAdministrator(project);
 
     call(VERSION.name(), "5.6.3", firstAnalysis.getUuid());
     call(VERSION.name(), "6.3", secondAnalysis.getUuid());
 
-    List<EventDto> events = dbClient.eventDao().selectByComponentUuid(dbSession, project.uuid());
+    List<EventDto> events = dbClient.eventDao().selectByComponentUuid(dbSession, project.getUuid());
     assertThat(events).hasSize(2);
   }
 
   @Test
   public void fail_if_not_blank_name() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     expectedException.expect(IllegalArgumentException.class);
@@ -222,15 +242,15 @@ public class CreateEventActionTest {
     userSession.logIn();
 
     expectedException.expect(NotFoundException.class);
-    expectedException.expectMessage("Analysis 'A42' is not found");
+    expectedException.expectMessage("Analysis 'A42' not found");
 
     call(OTHER.name(), "Project Import", "A42");
   }
 
   @Test
   public void fail_if_2_version_events_on_the_same_analysis() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
     call(VERSION.name(), "5.6.3", analysis.getUuid());
 
@@ -242,8 +262,8 @@ public class CreateEventActionTest {
 
   @Test
   public void fail_if_2_other_events_on_same_analysis_with_same_name() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
     call(OTHER.name(), "Project Import", analysis.getUuid());
 
@@ -255,8 +275,8 @@ public class CreateEventActionTest {
 
   @Test
   public void fail_if_category_other_than_authorized() {
-    ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization());
-    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
+    ProjectDto project = db.components().insertPrivateProjectDto();
+    SnapshotDto analysis = db.components().insertSnapshot(project);
     logInAsProjectAdministrator(project);
 
     expectedException.expect(IllegalArgumentException.class);
@@ -269,7 +289,7 @@ public class CreateEventActionTest {
 
   @Test
   public void fail_if_create_version_event_on_application() {
-    ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
+    ProjectDto application = db.components().insertPrivateApplicationDto();
     SnapshotDto analysis = db.components().insertSnapshot(application);
     logInAsProjectAdministrator(application);
 
@@ -286,7 +306,7 @@ public class CreateEventActionTest {
     db.commit();
 
     expectedException.expect(IllegalStateException.class);
-    expectedException.expectMessage("Project of analysis 'A1' is not found");
+    expectedException.expectMessage("Project of analysis 'A1' not found");
 
     call(VERSION.name(), "5.6.3", analysis.getUuid());
   }
@@ -311,7 +331,7 @@ public class CreateEventActionTest {
     assertThat(definition.responseExampleAsString()).isNotEmpty();
   }
 
-  private void logInAsProjectAdministrator(ComponentDto project) {
+  private void logInAsProjectAdministrator(ProjectDto project) {
     userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
   }