@Test public void get_by_id_on_disabled_component() { ComponentDto enabledProject = db.components().insertPrivateProject(); ComponentDto disabledProject = db.components().insertPrivateProject(p -> p.setEnabled(false)); Optional<ComponentDto> result = underTest.selectById(dbSession, disabledProject.getId()); assertThat(result).isPresent(); assertThat(result.get().isEnabled()).isFalse(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_users_and_any_permission_on_private_project_without_any_permission_in_DB() { ComponentDto project = db.components().insertPrivateProject(organization); PermissionsTestHelper.ALL_PERMISSIONS .forEach(perm -> { assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, perm, project.getId())) .isEmpty(); }); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, randomPermission, project.getId())) .isEmpty(); }
@Test public void delete_doc_from_index_when_project_is_deleted() { ComponentDto project = db.components().insertPrivateProject(); indexProject(project, PROJECT_CREATION); assertThatIndexContainsOnly(project); db.getDbClient().componentDao().delete(db.getSession(), project.getId()); IndexingResult result = indexProject(project, PROJECT_DELETION); assertThat(es.countDocuments(INDEX_TYPE_PROJECT_MEASURES)).isEqualTo(0); assertThat(result.getTotal()).isEqualTo(1L); assertThat(result.getSuccess()).isEqualTo(1L); }
@Test public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_directory_with_specified_uuid() { ComponentDto privateProject = db.components().insertPrivateProject(); ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(privateProject, "A/B")); assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(directory.uuid())) .isEmpty(); }
@Test public void index_application_on_startup() { ComponentDto application = db.components().insertApplication(db.getDefaultOrganization()); ComponentDto project = db.components().insertPrivateProject(); db.components().insertComponent(newProjectCopy("PC1", project, application)); underTest.indexOnStartup(emptySet()); 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 get_by_uuids_on_removed_components() { ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(p -> p.setEnabled(false)); List<ComponentDto> results = underTest.selectByUuids(dbSession, asList(project1.uuid(), project2.uuid())); assertThat(results) .extracting(ComponentDto::getDbKey, ComponentDto::isEnabled) .containsExactlyInAnyOrder( tuple(project1.getDbKey(), true), tuple(project2.getDbKey(), false)); }
@Test public void filter_by_key_with_exact_match() { ComponentDto ignoredProject = db.components().insertPrivateProject(p -> p.setDbKey("ignored-project")); ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("shiny-project")); ComponentDto anotherIgnoreProject = db.components().insertPrivateProject(p -> p.setDbKey("another-shiny-project")); index(ignoredProject, project); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setQuery("shiny-project").build(), new SearchOptions()); assertThat(result.getIds()).containsExactlyInAnyOrder(project.uuid()); }
@Test public void return_project_quality_gate_over_default() { ComponentDto project = db.components().insertPrivateProject(); db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(),qg -> qg.setName("Sonar way")); QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); assertThat(result).isPresent(); assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); assertThat(result.get().isDefault()).isFalse(); }
@Test public void search_tags() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); db.issues().insert(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2"))); db.issues().insert(rule, project, project, issue -> issue.setTags(asList("tag3", "tag4", "tag5"))); issueIndexer.indexOnStartup(emptySet()); permissionIndexer.allowOnlyAnyone(project); TagsResponse result = ws.newRequest().executeProtobuf(TagsResponse.class); assertThat(result.getTagsList()).containsExactly("tag1", "tag2", "tag3", "tag4", "tag5"); }
@Test public void get_project_settings_from_db() { ComponentDto project = db.components().insertPrivateProject(); analysisMetadataHolder.setProject(Project.from(project)); insertProjectProperty(project, "key", "value"); Configuration config = underTest.getConfiguration(); assertThat(config.get("key")).hasValue("value"); }
@Test public void load_uuids_from_existing_components_in_db() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project)); Map<String, String> reportModulesPath = Collections.singletonMap(module.getKey(), "module1_path"); pathToKey = path -> path != null ? project.getDbKey() + ":" + path : project.getDbKey(); ComponentUuidFactoryWithMigration underTest = new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getDbKey(), pathToKey, reportModulesPath); assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid()); assertThat(underTest.getOrCreateForKey(module.getDbKey())).isEqualTo(module.uuid()); }
@Test public void scrollAllFilesForFileMove_has_no_effect_if_project_has_no_file() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultContext -> fail("handler should not be called")); }
@Test public void load_project_id_from_db() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); ComponentDto file = db.components().insertComponent(newFileDto(project)); long projectId = underTest.project(db.getSession(), new DefaultIssue().setProjectUuid(project.uuid())).getId(); assertThat(projectId).isEqualTo(project.getId()); }
@Test public void indexOnAnalysis_indexes_the_issues_of_project() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issue = db.issues().insertIssue(IssueTesting.newIssue(rule, project, file)); ComponentDto otherProject = db.components().insertPrivateProject(organization); ComponentDto fileOnOtherProject = db.components().insertComponent(newFileDto(otherProject)); underTest.indexOnAnalysis(project.uuid()); assertThatIndexHasOnly(issue); }
@Test public void fail_when_default_qgate_defined_does_not_exists() { ComponentDto project = db.components().insertPrivateProject(); QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); db.commit(); assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty(); }
@Test public void project_settings_override_global_settings() { settings.setProperty("key", "value"); ComponentDto project = db.components().insertPrivateProject(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value2")); Configuration projectConfig = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl()); assertThat(projectConfig.get("key")).hasValue("value2"); }
@Test public void selectMeasure_map_fields() { MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14).setVariation(0.1).setData("text_value")); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(result).as("Fail to map fields of %s", result.toString()).extracting( LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getVariation, LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue) .contains(project.uuid(), file.uuid(), metric.getId(), 3.14, 0.1, "text_value", "text_value"); }
@Test public void branch_issues_are_ignored_when_no_branch_param() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); IssueDoc projectIssue = newDoc(project); IssueDoc branchIssue = newDoc(branch); indexIssues(projectIssue, branchIssue); assertThatSearchReturnsOnly(IssueQuery.builder(), projectIssue.key()); }
@Test public void component_is_not_favorite() { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn("john").addProjectPermission(USER, project); String result = ws.newRequest() .setParam("component", project.getKey()) .execute() .getInput(); assertJson(result).isSimilarTo("{\n" + " \"fav\": false,\n" + "}\n"); }
@Test public void fail_if_not_enough_permission_to_access_issue() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleDefinitionDto rule = db.rules().insert(); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null)); userSession.logIn().addProjectPermission(CODEVIEWER, project, file); expectedException.expect(ForbiddenException.class); call(issue.getKey(), "confirm"); }