Search in sources :

Example 1 with ComponentUpdater

use of org.sonar.server.component.ComponentUpdater in project sonarqube by SonarSource.

the class BranchReportSubmitterTest method submit_report_on_missing_branch_of_missing_project_provisions_project_when_PROVISION_PROJECT_perm.

@Test
public void submit_report_on_missing_branch_of_missing_project_provisions_project_when_PROVISION_PROJECT_perm() {
    ComponentDto nonExistingProject = newPrivateProjectDto();
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS).addPermission(SCAN);
    Map<String, String> randomCharacteristics = randomNonEmptyMap();
    ComponentDto createdBranch = createButDoNotInsertBranch(nonExistingProject);
    BranchSupport.ComponentKey componentKey = createComponentKeyOfBranch(createdBranch);
    when(branchSupportDelegate.createComponentKey(nonExistingProject.getDbKey(), randomCharacteristics)).thenReturn(componentKey);
    when(componentUpdater.createWithoutCommit(any(), any(), eq(user.getUuid()), eq(user.getLogin()), any())).thenAnswer((Answer<ComponentDto>) invocation -> db.components().insertPrivateProject(nonExistingProject));
    when(branchSupportDelegate.createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), any())).thenReturn(createdBranch);
    when(permissionTemplateService.wouldUserHaveScanPermissionWithDefaultTemplate(any(DbSession.class), any(), eq(nonExistingProject.getKey()))).thenReturn(true);
    String taskUuid = mockSuccessfulPrepareSubmitCall();
    InputStream reportInput = IOUtils.toInputStream("{binary}", StandardCharsets.UTF_8);
    underTest.submit(nonExistingProject.getDbKey(), nonExistingProject.name(), randomCharacteristics, reportInput);
    BranchDto exitingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), nonExistingProject.uuid()).get();
    verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), eq(exitingProjectMainBranch));
    verify(branchSupportDelegate).createComponentKey(nonExistingProject.getDbKey(), randomCharacteristics);
    verify(branchSupportDelegate).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), eq(exitingProjectMainBranch));
    verifyNoMoreInteractions(branchSupportDelegate);
    verifyQueueSubmit(nonExistingProject, createdBranch, user, randomCharacteristics, taskUuid);
    verify(componentUpdater).commitAndIndex(any(DbSession.class), eq(nonExistingProject));
}
Also used : CeTaskTypes(org.sonar.db.ce.CeTaskTypes) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) Visibility(org.sonar.server.project.Visibility) ComponentTesting(org.sonar.db.component.ComponentTesting) Assert.fail(org.junit.Assert.fail) DbTester(org.sonar.db.DbTester) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) StandardCharsets(java.nio.charset.StandardCharsets) PermissionTemplateService(org.sonar.server.permission.PermissionTemplateService) IOUtils(org.apache.commons.io.IOUtils) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SCAN_EXECUTION(org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) FavoriteUpdater(org.sonar.server.favorite.FavoriteUpdater) UserDto(org.sonar.db.user.UserDto) RunWith(org.junit.runner.RunWith) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Mockito.spy(org.mockito.Mockito.spy) ProjectDefaultVisibility(org.sonar.server.project.ProjectDefaultVisibility) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Before(org.junit.Before) ComponentUpdater(org.sonar.server.component.ComponentUpdater) UserSessionRule(org.sonar.server.tester.UserSessionRule) Collections.emptyMap(java.util.Collections.emptyMap) BranchType(org.sonar.db.component.BranchType) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CeQueueImpl(org.sonar.ce.queue.CeQueueImpl) Mockito.verify(org.mockito.Mockito.verify) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) CeTaskSubmit(org.sonar.ce.queue.CeTaskSubmit) InputStream(java.io.InputStream) CeQueue(org.sonar.ce.queue.CeQueue) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) InputStream(java.io.InputStream) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)1 InputStream (java.io.InputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Random (java.util.Random)1 IntStream (java.util.stream.IntStream)1 IOUtils (org.apache.commons.io.IOUtils)1 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1