private ComponentDto randomComponent(OrganizationDto organization) { ComponentDto project = dbTester.components().insertPublicProject(organization); ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project)); ComponentDto dir = dbTester.components().insertComponent(ComponentTesting.newDirectory(project, "foo")); ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(project)); ComponentDto[] components = new ComponentDto[] {project, module, dir, file}; return components[random.nextInt(components.length)]; }
@Test public void facet_on_projectUuids() { OrganizationDto organizationDto = newOrganizationDto(); ComponentDto project = newPrivateProjectDto(organizationDto, "ABCD"); ComponentDto project2 = newPrivateProjectDto(organizationDto, "EFGH"); indexIssues( newDoc("I1", newFileDto(project, null)), newDoc("I2", newFileDto(project, null)), newDoc("I3", newFileDto(project2, null))); assertThatFacetHasExactly(IssueQuery.builder(), "projects", entry("ABCD", 2L), entry("EFGH", 1L)); }
public static ComponentDto newProjectCopy(ComponentDto project, ComponentDto view) { return newProjectCopy(Uuids.createFast(), project, view); }
private static ComponentDto createButDoNotInsertBranch(ComponentDto project) { BranchType randomBranchType = BranchType.values()[new Random().nextInt(BranchType.values().length)]; BranchDto branchDto = newBranchDto(project.projectUuid(), randomBranchType); return ComponentTesting.newProjectBranch(project, branchDto); }
@Test public void commitAndIndex_of_module_indexes_the_project() { OrganizationDto organization = OrganizationTesting.newOrganizationDto(); ComponentDto project = ComponentTesting.newPublicProjectDto(organization); ComponentDto module = ComponentTesting.newModuleDto(project); FakeIndexers underTest = new FakeIndexers(); underTest.commitAndIndex(mock(DbSession.class), singletonList(module), Cause.PROJECT_CREATION); assertThat(underTest.calls).containsExactly(project.uuid()); }
@SafeVarargs public final ComponentDto insertMainBranch(OrganizationDto organization, String mainBranchName, Consumer<ComponentDto>... dtoPopulators) { ComponentDto project = newPrivateProjectDto(organization); BranchDto branchDto = newBranchDto(project, LONG).setKey(mainBranchName); Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(project)); insertComponent(project); dbClient.branchDao().insert(dbSession, branchDto); db.commit(); return project; }
@Test public void onEnd_fails_with_IAE_if_component_is_not_a_project() { when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT); OrganizationDto organization = OrganizationTesting.newOrganizationDto(); ComponentDto project = dbTester.components().insertPrivateProject(); ComponentDto module = dbTester.components().insertComponent(newModuleDto(project)); ComponentDto directory = dbTester.components().insertComponent(newDirectory(module, randomAlphanumeric(12))); ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(project)); ComponentDto view = dbTester.components().insertComponent(ComponentTesting.newView(organization)); ComponentDto subView = dbTester.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto projectCopy = dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project, subView)); ComponentDto application = dbTester.components().insertComponent(ComponentTesting.newApplication(organization)); Arrays.asList(module, directory, file, view, subView, projectCopy, application) .forEach(component -> { try { when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(component.uuid(), null, null))); when(notificationService.hasProjectSubscribersForTypes(component.uuid(), singleton(ReportAnalysisFailureNotification.TYPE))) .thenReturn(true); underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, ceTaskResultMock, throwableMock); fail("An IllegalArgumentException should have been thrown for component " + component); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).isEqualTo(String.format( "Component %s must be a project (scope=%s, qualifier=%s)", component.uuid(), component.scope(), component.qualifier())); } }); }
@Test public void execute_changes_private_flag_of_specified_project_and_all_children_to_specified_new_visibility() { ComponentDto project = randomPublicOrPrivateProject(); boolean initiallyPrivate = project.isPrivate(); BranchDto branchDto = ComponentTesting.newBranchDto(project); dbClient.branchDao().insert(dbSession, branchDto); ComponentDto branch = ComponentTesting.newProjectBranch(project, branchDto); ComponentDto module = ComponentTesting.newModuleDto(project); ComponentDto dir = ComponentTesting.newDirectory(project, "path"); ComponentDto file = ComponentTesting.newFileDto(project); dbTester.components().insertComponents(branch, module, dir, file); userSessionRule.addProjectPermission(UserRole.ADMIN, project); request.setParam(PARAM_PROJECT, project.getDbKey()) .setParam(PARAM_VISIBILITY, initiallyPrivate ? PUBLIC : PRIVATE) .execute(); assertThat(isPrivateInDb(project)).isEqualTo(!initiallyPrivate); assertThat(isPrivateInDb(branch)).isEqualTo(!initiallyPrivate); assertThat(isPrivateInDb(module)).isEqualTo(!initiallyPrivate); assertThat(isPrivateInDb(dir)).isEqualTo(!initiallyPrivate); assertThat(isPrivateInDb(file)).isEqualTo(!initiallyPrivate); }
@Test public void getUuidPathLikeIncludingSelf() { OrganizationDto organizationDto = newOrganizationDto(); ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto).setUuidPath(ComponentDto.UUID_PATH_OF_ROOT); assertThat(project.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + ".%"); ComponentDto module = ComponentTesting.newModuleDto(project); assertThat(module.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + "." + module.uuid() + ".%"); ComponentDto file = ComponentTesting.newFileDto(module); assertThat(file.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + "." + module.uuid() + "." + file.uuid() + ".%"); }
@Test public void facet_on_directories_return_100_entries_plus_selected_values() { OrganizationDto organizationDto = newOrganizationDto(); ComponentDto project = newPrivateProjectDto(organizationDto); indexIssues(rangeClosed(1, 110).mapToObj(i -> newDoc(newFileDto(project, newDirectory(project, "dir" + i))).setDirectoryPath("a" + i)).toArray(IssueDoc[]::new)); IssueDoc issue1 = newDoc(newFileDto(project, newDirectory(project, "path1"))).setDirectoryPath("directory1"); IssueDoc issue2 = newDoc(newFileDto(project, newDirectory(project, "path2"))).setDirectoryPath("directory2"); indexIssues(issue1, issue2); assertThatFacetHasSize(IssueQuery.builder().build(), "directories", 100); assertThatFacetHasSize(IssueQuery.builder().directories(asList(issue1.directoryPath(), issue2.directoryPath())).build(), "directories", 102); }
@Test public void fail_when_component_is_a_module() { ComponentDto module = db.components().insertComponent(newModuleDto(ComponentTesting.newPrivateProjectDto(db.organizations().insert()))); failIfComponentIsNotAProjectOrView(module); }
private ComponentDto setUpProjectWithBranch() { ComponentDto project = newPrivateProjectDto(newOrganizationDto()); ComponentDto branch = newProjectBranch(project, newBranchDto(project).setKey(BRANCH_NAME)); ComponentDto file = newFileDto(branch); treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren( builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build()); return branch; }
@Test public void return_projects_composing_a_view() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); ComponentDto view = newView(db.getDefaultOrganization(), "view-uuid"); db.components().insertViewAndSnapshot(view); db.components().insertComponent(newProjectCopy("project-copy-uuid", project, view)); userSession.logIn() .registerComponents(project, view); TreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT_ID, view.uuid()).executeProtobuf(TreeWsResponse.class); assertThat(response.getBaseComponent().getId()).isEqualTo(view.uuid()); assertThat(response.getComponentsCount()).isEqualTo(1); assertThat(response.getComponents(0).getId()).isEqualTo("project-copy-uuid"); assertThat(response.getComponents(0).getRefId()).isEqualTo("project-uuid"); }
@Test public void fail_when_component_is_a_subview() { ComponentDto file = db.components().insertComponent(newSubView(ComponentTesting.newView(db.organizations().insert()))); failIfComponentIsNotAProjectOrView(file); }
@Test public void components_contains_sub_projects() { OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-1")); ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("ProjectHavingModule")); indexPermissions(); ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project).setDbKey("ModuleHavingFile")); ComponentDto file = db.components().insertComponent(newFileDto(module, null, "BCDE").setDbKey("FileLinkedToModule")); IssueDto issue = newDto(newRule(), file, project); dbClient.issueDao().insert(session, issue); session.commit(); indexIssues(); ws.newRequest().setParam(PARAM_ADDITIONAL_FIELDS, "_all").execute() .assertJson(this.getClass(), "components_contains_sub_projects.json"); }
@Test public void search_private_projects() { userSession.addPermission(ADMINISTER, db.getDefaultOrganization()); db.components().insertComponents( ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("private-key"), ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()).setDbKey("public-key")); SearchWsResponse response = call(SearchRequest.builder().setVisibility("private").build()); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly("private-key"); }
@Test public void delete_row_in_ce_task_input_referring_to_a_row_in_ce_queue_when_deleting_project() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); ComponentDto branch = ComponentTesting.newProjectBranch(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newProjectBranch(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); CeQueueDto projectTask = insertCeQueue(project); insertCeTaskInput(projectTask.getUuid()); CeQueueDto branchTask = insertCeQueue(branch); insertCeTaskInput(branchTask.getUuid()); CeQueueDto anotherBranchTask = insertCeQueue(anotherBranch); insertCeTaskInput(anotherBranchTask.getUuid()); CeQueueDto anotherProjectTask = insertCeQueue(anotherProject); insertCeTaskInput(anotherProjectTask.getUuid()); insertCeTaskInput("non existing task"); dbSession.commit(); underTest.deleteProject(dbSession, branch.uuid()); dbSession.commit(); assertThat(uuidsIn("ce_queue")).containsOnly(projectTask.getUuid(), anotherBranchTask.getUuid(), anotherProjectTask.getUuid()); assertThat(taskUuidsIn("ce_task_input")).containsOnly(projectTask.getUuid(), anotherBranchTask.getUuid(), anotherProjectTask.getUuid(), "non existing task"); underTest.deleteProject(dbSession, project.uuid()); dbSession.commit(); assertThat(uuidsIn("ce_queue")).containsOnly(anotherProjectTask.getUuid()); assertThat(taskUuidsIn("ce_task_input")).containsOnly(anotherProjectTask.getUuid(), "non existing task"); }
@Test public void fail_when_component_is_a_directory() { ComponentDto file = db.components().insertComponent(newDirectory(ComponentTesting.newPrivateProjectDto(db.organizations().insert()), "A/B")); failIfComponentIsNotAProjectOrView(file); }
@Test public void rule_id_is_not_set_on_updated_issue_when_rule_is_removed() { RuleDefinitionDto rule = db.rules().insert(r -> r.setStatus(REMOVED)); ComponentDto project = db.components().insertMainBranch(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(module)); DefaultIssue issue = newIssue(rule, project, file).toDefaultIssue(); Collection<IssueDto> results = underTest.save(db.getSession(), singletonList(issue)); assertThat(results).hasSize(1); assertThat(results.iterator().next().getRuleId()).isNull(); }
@Test public void count_provisioned() { OrganizationDto organization = db.organizations().insert(); db.components().insertPrivateProject(organization); db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization)); Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setOnProvisionedOnly(true); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT).build())).isEqualTo(1); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.VIEW).build())).isEqualTo(0); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build())).isEqualTo(1); }