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)]; }
@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 count_enabled_modules_by_project_uuid() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); db.components().insertComponent(newModuleDto(module)); ComponentDto subModule2 = newModuleDto(module); subModule2.setEnabled(false); db.components().insertComponent(subModule2); int result = underTest.countEnabledModulesByProjectUuid(dbSession, project.uuid()); assertThat(result).isEqualTo(2); }
@Test public void update_index_on_project_creation() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IndexingResult result = indexProject(project, PROJECT_CREATION); assertThatIndexContainsOnly(project, file); // two requests (one per component) assertThat(result.getTotal()).isEqualTo(2L); assertThat(result.getSuccess()).isEqualTo(2L); }
@Test public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_module_with_specified_uuid() { ComponentDto privateProject = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(privateProject)); assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(module.uuid())) .isEmpty(); }
@Test public void insert_data() { byte[] data = "text_value".getBytes(StandardCharsets.UTF_8); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); LiveMeasureDto measure = newLiveMeasure(file, metric).setData(data); underTest.insert(db.getSession(), measure); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(new String(result.getData(), StandardCharsets.UTF_8)).isEqualTo("text_value"); assertThat(result.getDataAsString()).isEqualTo("text_value"); }
@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 index_application() { ComponentDto application = db.components().insertApplication(db.getDefaultOrganization()); ComponentDto project = db.components().insertPrivateProject(); db.components().insertComponent(newProjectCopy("PC1", project, application)); underTest.index(application.uuid()); List<ViewDoc> result = es.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); assertThat(result).hasSize(1); ViewDoc resultApp = result.get(0); assertThat(resultApp.uuid()).isEqualTo(application.uuid()); assertThat(resultApp.projects()).containsExactlyInAnyOrder(project.uuid()); }
@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 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"); }
private ComponentDto insertProjectWithBranchAndRelatedData() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto module = db.components().insertComponent(newModuleDto(branch)); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto file = db.components().insertComponent(newFileDto(subModule)); db.issues().insert(rule, branch, file); db.issues().insert(rule, branch, subModule); db.issues().insert(rule, branch, module); return project; }
@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 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 indexOnAnalysis_indexes_project() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.indexOnAnalysis(project.uuid()); assertThatIndexContainsOnly(project, file); }
@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 selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_project_copy_with_specified_project_uuid() { ComponentDto privateProject = db.components().insertPrivateProject(); ComponentDto view = db.components().insertView(); db.components().insertComponent(ComponentTesting.newProjectCopy("a", view, privateProject)); assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid())) .extracting(IdUuidPair::getUuid) .containsOnly(view.uuid()); }
@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 deleteProject_fails_with_IAE_if_specified_component_is_file() { ComponentDto privateProject = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(privateProject)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Couldn't find root component with uuid " + file.uuid()); underTest.deleteProject(dbSession, file.uuid()); }
@Test public void deleteProject_fails_with_IAE_if_specified_component_is_subview() { ComponentDto view = db.components().insertView(); ComponentDto subview = db.components().insertComponent(ComponentTesting.newSubView(view)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Couldn't find root component with uuid " + subview.uuid()); underTest.deleteProject(dbSession, subview.uuid()); }
@Test public void fail_if_both_componentId_and_pull_request_parameters_provided() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Parameter 'componentId' cannot be used at the same time as 'branch' or 'pullRequest'"); ws.newRequest() .setParam("uuid", file.uuid()) .setParam("pullRequest", file.getPullRequest()) .execute(); }