Search in sources :

Example 26 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class PurgeDaoTest method delete_application.

@Test
public void delete_application() {
    MetricDto metric = db.measures().insertMetric();
    ComponentDto project = db.components().insertPrivateProject();
    BranchDto projectBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid()).get();
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto app = db.components().insertPrivateApplication();
    ComponentDto appBranch = db.components().insertProjectBranch(app);
    ComponentDto otherApp = db.components().insertPrivateApplication();
    ComponentDto otherAppBranch = db.components().insertProjectBranch(otherApp);
    SnapshotDto appAnalysis = db.components().insertSnapshot(app);
    SnapshotDto appBranchAnalysis = db.components().insertSnapshot(appBranch);
    SnapshotDto otherAppAnalysis = db.components().insertSnapshot(otherApp);
    SnapshotDto otherAppBranchAnalysis = db.components().insertSnapshot(otherAppBranch);
    MeasureDto appMeasure = db.measures().insertMeasure(app, appAnalysis, metric);
    MeasureDto appBranchMeasure = db.measures().insertMeasure(appBranch, appBranchAnalysis, metric);
    MeasureDto otherAppMeasure = db.measures().insertMeasure(otherApp, otherAppAnalysis, metric);
    MeasureDto otherAppBranchMeasure = db.measures().insertMeasure(otherAppBranch, otherAppBranchAnalysis, metric);
    db.components().addApplicationProject(app, project);
    db.components().addApplicationProject(otherApp, project);
    db.components().addProjectBranchToApplicationBranch(dbClient.branchDao().selectByUuid(dbSession, appBranch.uuid()).get(), projectBranch);
    db.components().addProjectBranchToApplicationBranch(dbClient.branchDao().selectByUuid(dbSession, otherAppBranch.uuid()).get(), projectBranch);
    underTest.deleteProject(dbSession, app.uuid(), app.qualifier(), project.name(), project.getKey());
    dbSession.commit();
    assertThat(uuidsIn("components")).containsOnly(project.uuid(), otherApp.uuid(), otherAppBranch.uuid());
    assertThat(uuidsIn("projects")).containsOnly(project.uuid(), otherApp.uuid());
    assertThat(uuidsIn("snapshots")).containsOnly(otherAppAnalysis.getUuid(), otherAppBranchAnalysis.getUuid());
    assertThat(uuidsIn("project_branches")).containsOnly(project.uuid(), otherApp.uuid(), otherAppBranch.uuid());
    assertThat(uuidsIn("project_measures")).containsOnly(otherAppMeasure.getUuid(), otherAppBranchMeasure.getUuid());
    assertThat(uuidsIn("app_projects", "application_uuid")).containsOnly(otherApp.uuid());
    assertThat(uuidsIn("app_branch_project_branch", "application_branch_uuid")).containsOnly(otherAppBranch.uuid());
}
Also used : MeasureDto(org.sonar.db.measure.MeasureDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) MetricDto(org.sonar.db.metric.MetricDto) BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 27 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class QGChangeEventListenersImplTest method broadcastOnIssueChange_calls_listener_for_each_component_uuid_with_at_least_one_QGChangeEvent.

@Test
public void broadcastOnIssueChange_calls_listener_for_each_component_uuid_with_at_least_one_QGChangeEvent() {
    // branch has multiple issues
    BranchDto component2 = newBranchDto(project1Uuid + "2");
    DefaultIssue[] component2Issues = { newDefaultIssue(component2.getUuid()), newDefaultIssue(component2.getUuid()) };
    QGChangeEvent component2QGChangeEvent = newQGChangeEvent(component2);
    // branch 3 has multiple QGChangeEvent and only one issue
    BranchDto component3 = newBranchDto(project1Uuid + "3");
    DefaultIssue component3Issue = newDefaultIssue(component3.getUuid());
    QGChangeEvent[] component3QGChangeEvents = { newQGChangeEvent(component3), newQGChangeEvent(component3) };
    // branch 4 has multiple QGChangeEvent and multiples issues
    BranchDto component4 = newBranchDto(project1Uuid + "4");
    DefaultIssue[] component4Issues = { newDefaultIssue(component4.getUuid()), newDefaultIssue(component4.getUuid()) };
    QGChangeEvent[] component4QGChangeEvents = { newQGChangeEvent(component4), newQGChangeEvent(component4) };
    // branch 5 has no QGChangeEvent but one issue
    BranchDto component5 = newBranchDto(project1Uuid + "5");
    DefaultIssue component5Issue = newDefaultIssue(component5.getUuid());
    List<DefaultIssue> issues = Stream.of(Stream.of(component1Issue), Arrays.stream(component2Issues), Stream.of(component3Issue), Arrays.stream(component4Issues), Stream.of(component5Issue)).flatMap(s -> s).collect(Collectors.toList());
    List<DefaultIssue> changedIssues = randomizedList(issues);
    List<QGChangeEvent> qgChangeEvents = Stream.of(Stream.of(component1QGChangeEvent), Stream.of(component2QGChangeEvent), Arrays.stream(component3QGChangeEvents), Arrays.stream(component4QGChangeEvents)).flatMap(s -> s).collect(Collectors.toList());
    underTest.broadcastOnIssueChange(changedIssues, randomizedList(qgChangeEvents));
    listeners.forEach(listener -> {
        verifyListenerCalled(listener, component1QGChangeEvent, component1Issue);
        verifyListenerCalled(listener, component2QGChangeEvent, component2Issues);
        Arrays.stream(component3QGChangeEvents).forEach(component3QGChangeEvent -> verifyListenerCalled(listener, component3QGChangeEvent, component3Issue));
        Arrays.stream(component4QGChangeEvents).forEach(component4QGChangeEvent -> verifyListenerCalled(listener, component4QGChangeEvent, component4Issues));
    });
    verifyNoMoreInteractions(listener1, listener2, listener3);
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Mockito.doThrow(org.mockito.Mockito.doThrow) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Assert.fail(org.junit.Assert.fail) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) Tuple(org.assertj.core.groups.Tuple) ImmutableSet(com.google.common.collect.ImmutableSet) InOrder(org.mockito.InOrder) DefaultIssue(org.sonar.core.issue.DefaultIssue) Collections.emptySet(java.util.Collections.emptySet) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) Test(org.junit.Test) ChangedIssue(org.sonar.server.qualitygate.changeevent.QGChangeEventListener.ChangedIssue) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) Issue(org.sonar.api.issue.Issue) Collections(java.util.Collections) ChangedIssueImpl(org.sonar.server.qualitygate.changeevent.QGChangeEventListenersImpl.ChangedIssueImpl) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) BranchDto(org.sonar.db.component.BranchDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 28 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class QGChangeEventListenersImplTest method newBranchDto.

private static BranchDto newBranchDto(String uuid) {
    BranchDto branchDto = new BranchDto();
    branchDto.setUuid(uuid);
    return branchDto;
}
Also used : BranchDto(org.sonar.db.component.BranchDto)

Example 29 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class AsyncIssueIndexingImplTest method characteristics_are_defined.

@Test
public void characteristics_are_defined() {
    BranchDto dto = new BranchDto().setBranchType(BRANCH).setKey("branch_1").setUuid("branch_uuid1").setProjectUuid("project_uuid1");
    dbClient.branchDao().insert(dbTester.getSession(), dto);
    dbTester.commit();
    insertSnapshot("analysis_1", "project_uuid1", 1);
    BranchDto dto2 = new BranchDto().setBranchType(PULL_REQUEST).setKey("pr_1").setUuid("pr_uuid_1").setProjectUuid("project_uuid2");
    dbClient.branchDao().insert(dbTester.getSession(), dto2);
    dbTester.commit();
    insertSnapshot("analysis_2", "project_uuid2", 2);
    underTest.triggerOnIndexCreation();
    ArgumentCaptor<Collection<CeTaskSubmit>> captor = ArgumentCaptor.forClass(Collection.class);
    verify(ceQueue, times(1)).massSubmit(captor.capture());
    List<Collection<CeTaskSubmit>> captures = captor.getAllValues();
    assertThat(captures).hasSize(1);
    Collection<CeTaskSubmit> tasks = captures.get(0);
    assertThat(tasks).hasSize(2);
    assertThat(tasks).extracting(p -> p.getCharacteristics().get(BRANCH_TYPE_KEY), p -> p.getCharacteristics().get(CeTaskCharacteristicDto.BRANCH_KEY), p -> p.getCharacteristics().get(CeTaskCharacteristicDto.PULL_REQUEST)).containsExactlyInAnyOrder(tuple("BRANCH", "branch_1", null), tuple("PULL_REQUEST", null, "pr_1"));
}
Also used : BranchDto(org.sonar.db.component.BranchDto) Arrays(java.util.Arrays) BRANCH_TYPE_KEY(org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_TYPE_KEY) STATUS_PROCESSED(org.sonar.db.component.SnapshotDto.STATUS_PROCESSED) REPORT(org.sonar.db.ce.CeTaskTypes.REPORT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CeActivityDto(org.sonar.db.ce.CeActivityDto) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) BRANCH_ISSUE_SYNC(org.sonar.db.ce.CeTaskTypes.BRANCH_ISSUE_SYNC) CeTaskCharacteristicDto(org.sonar.db.ce.CeTaskCharacteristicDto) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Collection(java.util.Collection) UuidFactory(org.sonar.core.util.UuidFactory) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) BRANCH(org.sonar.db.component.BranchType.BRANCH) SequenceUuidFactory(org.sonar.core.util.SequenceUuidFactory) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) CeTaskSubmit(org.sonar.ce.queue.CeTaskSubmit) CeQueueDto(org.sonar.db.ce.CeQueueDto) Optional(java.util.Optional) Status(org.sonar.db.ce.CeActivityDto.Status) SnapshotDto(org.sonar.db.component.SnapshotDto) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) CeQueue(org.sonar.ce.queue.CeQueue) Mockito.mock(org.mockito.Mockito.mock) BranchDto(org.sonar.db.component.BranchDto) Collection(java.util.Collection) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) CeTaskSubmit(org.sonar.ce.queue.CeTaskSubmit) Test(org.junit.Test)

Example 30 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class AsyncIssueIndexingImplTest method trigger_with_lot_of_not_analyzed_project_should_not_raise_exception.

@Test
public void trigger_with_lot_of_not_analyzed_project_should_not_raise_exception() {
    for (int i = 0; i < 100; i++) {
        BranchDto dto = new BranchDto().setBranchType(BRANCH).setKey("branch_" + i).setUuid("branch_uuid" + i).setProjectUuid("project_uuid" + i);
        dbClient.branchDao().insert(dbTester.getSession(), dto);
        dbTester.commit();
        insertSnapshot("analysis_" + i, "project_uuid" + i, 1);
    }
    for (int i = 100; i < 200; i++) {
        BranchDto dto = new BranchDto().setBranchType(BRANCH).setKey("branch_" + i).setUuid("branch_uuid" + i).setProjectUuid("project_uuid" + i);
        dbClient.branchDao().insert(dbTester.getSession(), dto);
        dbTester.commit();
    }
    assertThatCode(underTest::triggerOnIndexCreation).doesNotThrowAnyException();
}
Also used : BranchDto(org.sonar.db.component.BranchDto) Test(org.junit.Test)

Aggregations

BranchDto (org.sonar.db.component.BranchDto)111 Test (org.junit.Test)62 ComponentDto (org.sonar.db.component.ComponentDto)52 ProjectDto (org.sonar.db.project.ProjectDto)42 DbSession (org.sonar.db.DbSession)31 SnapshotDto (org.sonar.db.component.SnapshotDto)22 List (java.util.List)15 ComponentTesting.newBranchDto (org.sonar.db.component.ComponentTesting.newBranchDto)13 DbClient (org.sonar.db.DbClient)12 MetricDto (org.sonar.db.metric.MetricDto)12 Map (java.util.Map)9 Optional (java.util.Optional)9 Nullable (javax.annotation.Nullable)9 WebService (org.sonar.api.server.ws.WebService)9 System2 (org.sonar.api.utils.System2)9 NotFoundException (org.sonar.server.exceptions.NotFoundException)9 Collection (java.util.Collection)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Rule (org.junit.Rule)8 Request (org.sonar.api.server.ws.Request)8