@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 select_components_having_same_key() { OrganizationDto organizationDto = db.organizations().insert(); insertProject(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey(PROJECT_KEY)); insertProject(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey(PROJECT_KEY)); insertProject(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey(PROJECT_KEY)); insertProject(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("ANOTHER_PROJECT_KEY")); assertThat(underTest.selectComponentsHavingSameKeyOrderedById(db.getSession(), PROJECT_KEY)).hasSize(3); }
@Test public void indexOnStartup_indexes_all_projects() { OrganizationDto organization = db.organizations().insert(); SnapshotDto project1 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); SnapshotDto project2 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); SnapshotDto project3 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project1, project2, project3); }
@Test public void paginate_results() { IntStream.rangeClosed(1, 9) .forEach(i -> addComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("K" + i).setName("N" + i))); ComponentDto unauthorizedProject = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization())); db.favorites().add(unauthorizedProject, USER_ID); SearchResponse result = call(2, 3); assertThat(result.getFavoritesCount()).isEqualTo(3); assertThat(result.getFavoritesList()) .extracting(Favorite::getKey) .containsExactly("K4", "K5", "K6"); }
@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 search_by_key_query() { insertProjectsAuthorizedForUser( ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("project-_%-key"), ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("project-key-without-escaped-characters")); SearchWsResponse response = call(new SearchRequest().setQuery("project-_%-key").setQualifiers(singletonList(PROJECT))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly("project-_%-key"); }
@Test public void setProject_throws_ISE_when_called_twice() { AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); underTest.setProject(Project.from(newPrivateProjectDto(newOrganizationDto()))); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Project has already been set"); underTest.setProject(Project.from(newPrivateProjectDto(newOrganizationDto()))); }
private ComponentDto createProject(String projectKey) { ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()).setDbKey(projectKey); dbClient.componentDao().insert(dbSession, project); dbSession.commit(); return project; } }
@Test public void must_not_search_if_no_valid_tokens_are_provided() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organization).setName("SonarQube")); componentIndexer.indexOnStartup(null); authorizationIndexerTester.allowOnlyAnyone(project); SuggestionsWsResponse response = ws.newRequest() .setMethod("POST") .setParam(PARAM_QUERY, "S o") .executeProtobuf(SuggestionsWsResponse.class); assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty(); assertThat(response.getWarning()).contains(SHORT_INPUT_WARNING); }
@Test public void set_no_base_project_snapshot_when_no_last_snapshot() { setAnalysisMetadataHolder(); OrganizationDto organizationDto = dbTester.organizations().insert(); ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); insertSnapshot(newAnalysis(project).setLast(false)); reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); underTest.execute(new TestComputationStepContext()); assertThat(analysisMetadataHolder.isFirstAnalysis()).isTrue(); }
@Test public void suggestions_without_query_should_not_contain_recently_browsed_without_permission() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organization)); componentIndexer.indexOnStartup(null); SuggestionsWsResponse response = ws.newRequest() .setMethod("POST") .setParam(PARAM_RECENTLY_BROWSED, project.getDbKey()) .executeProtobuf(SuggestionsWsResponse.class); assertThat(response.getResultsList()) .flatExtracting(Category::getItemsList) .isEmpty(); }
@Test public void simulate_bulk_update_key_does_not_return_disable_components() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "A").setDbKey("project")); db.components().insertComponent(newModuleDto(project).setDbKey("project:enabled-module")); db.components().insertComponent(newModuleDto(project).setDbKey("project:disabled-module").setEnabled(false)); db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "D").setDbKey("other-project")); Map<String, String> result = underTest.simulateBulkUpdateKey(dbSession, "A", "project", "new-project"); assertThat(result).containsOnly( entry("project", "new-project"), entry("project:enabled-module", "new-project:enabled-module")); }
@Test public void fail_if_component_uuid_and_key_are_provided() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); db.components().insertComponent(project); expectedException.expect(IllegalArgumentException.class); callByComponentUuidOrComponentKey(project.uuid(), project.getDbKey()); }
@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_search_query_has_less_than_3_characters() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("'q' length (2) is shorter than the minimum authorized (3)"); db.components().insertComponent(newPrivateProjectDto(db.organizations().insert(), "project-uuid")); db.commit(); ws.newRequest() .setParam(PARAM_COMPONENT_ID, "project-uuid") .setParam(Param.TEXT_QUERY, "fi") .execute(); }
@Test public void selectByQuery_filter_on_visibility() { db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("private-key")); db.components().insertComponent(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()).setDbKey("public-key")); ComponentQuery privateProjectsQuery = ComponentQuery.builder().setPrivate(true).setQualifiers(PROJECT).build(); ComponentQuery publicProjectsQuery = ComponentQuery.builder().setPrivate(false).setQualifiers(PROJECT).build(); ComponentQuery allProjectsQuery = ComponentQuery.builder().setPrivate(null).setQualifiers(PROJECT).build(); assertThat(underTest.selectByQuery(dbSession, privateProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsExactly("private-key"); assertThat(underTest.selectByQuery(dbSession, publicProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsExactly("public-key"); assertThat(underTest.selectByQuery(dbSession, allProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsOnly("public-key", "private-key"); }
@Test public void select_descendants_with_leaves_strategy() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), PROJECT_UUID); db.components().insertProjectAndSnapshot(project); db.components().insertComponent(newModuleDto("module-1-uuid", project)); db.components().insertComponent(newFileDto(project, null, "file-1-uuid")); db.components().insertComponent(newFileDto(project, null, "file-2-uuid")); db.commit(); ComponentTreeQuery query = newTreeQuery(PROJECT_UUID).setStrategy(LEAVES).build(); List<ComponentDto> result = underTest.selectDescendants(dbSession, query); assertThat(result).extracting("uuid").containsOnly("file-1-uuid", "file-2-uuid", "module-1-uuid"); }
@Test public void fail_if_component_is_removed() { userSession.logIn().setRoot(); ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization())); db.components().insertComponent(newFileDto(project).setDbKey("file-key").setEnabled(false)); expectedException.expect(NotFoundException.class); expectedException.expectMessage("Component key 'file-key' not found"); newRequest(null, "file-key"); }
@Test public void simulate_bulk_update_key_fails_if_invalid_componentKey() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organizationDto, "A").setDbKey("project")); db.components().insertComponent(newModuleDto(project).setDbKey("project:enabled-module")); db.components().insertComponent(newModuleDto(project).setDbKey("project:disabled-module").setEnabled(false)); thrown.expect(IllegalArgumentException.class); underTest.simulateBulkUpdateKey(dbSession, "A", "project", "project?"); }
@Test public void not_fail_if_analysis_date_is_after_last_analysis() { ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY); dbClient.componentDao().insert(dbTester.getSession(), project); dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01 dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build()); underTest.execute(new TestComputationStepContext()); }