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;
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 {
@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");
@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);
@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());
@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());
@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());
@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());
@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);
@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);
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());
@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());
@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);
@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);
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());
}
assertThat(definition.responseExampleAsString()).isNotEmpty();
}
- private void logInAsProjectAdministrator(ComponentDto project) {
+ private void logInAsProjectAdministrator(ProjectDto project) {
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}