Search in sources :

Example 11 with PROJECT

use of org.sonar.api.resources.Qualifiers.PROJECT in project sonarqube by SonarSource.

the class ComponentIndexTest method assertResultOrder.

protected void assertResultOrder(String query, String... resultsInOrder) {
    ComponentDto project = indexProject("key-1", "Quality Product");
    List<ComponentDto> files = Arrays.stream(resultsInOrder).map(r -> ComponentTesting.newFileDto(project).setName(r)).peek(f -> f.setUuid(f.uuid() + "_" + f.name().replaceAll("[^a-zA-Z0-9]", ""))).collect(Collectors.toList());
    // index them, but not in the expected order
    files.stream().sorted(Comparator.comparing(ComponentDto::uuid).reversed()).forEach(this::index);
    assertExactResults(query, files.toArray(new ComponentDto[0]));
}
Also used : UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) Arrays(java.util.Arrays) PermissionIndexerTester(org.sonar.server.permission.index.PermissionIndexerTester) System2(org.sonar.api.utils.System2) EsTester(org.sonar.server.es.EsTester) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FILE(org.sonar.api.resources.Qualifiers.FILE) Collectors(java.util.stream.Collectors) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) Arrays.asList(java.util.Arrays.asList) ListAssert(org.assertj.core.api.ListAssert) ComponentTesting(org.sonar.db.component.ComponentTesting) ComponentTextSearchFeatureRule(org.sonar.server.es.textsearch.ComponentTextSearchFeatureRule) MODULE(org.sonar.api.resources.Qualifiers.MODULE) Comparator(java.util.Comparator) ComponentDto(org.sonar.db.component.ComponentDto)

Example 12 with PROJECT

use of org.sonar.api.resources.Qualifiers.PROJECT in project sonarqube by SonarSource.

the class SuggestionsActionTest method must_not_search_if_no_valid_tokens_are_provided.

@Test
public void must_not_search_if_no_valid_tokens_are_provided() {
    ComponentDto project = db.components().insertComponent(newPrivateProjectDto().setName("SonarQube"));
    componentIndexer.indexAll();
    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);
}
Also used : PermissionIndexerTester(org.sonar.server.permission.index.PermissionIndexerTester) IntStream.range(java.util.stream.IntStream.range) ComponentIndexer(org.sonar.server.component.index.ComponentIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FILE(org.sonar.api.resources.Qualifiers.FILE) PARAM_RECENTLY_BROWSED(org.sonar.server.component.ws.SuggestionsAction.PARAM_RECENTLY_BROWSED) Collections.singletonList(java.util.Collections.singletonList) WebService(org.sonar.api.server.ws.WebService) Arrays.asList(java.util.Arrays.asList) PARAM_MORE(org.sonar.server.component.ws.SuggestionsAction.PARAM_MORE) ComponentTesting.newPublicProjectDto(org.sonar.db.component.ComponentTesting.newPublicProjectDto) ComponentTesting(org.sonar.db.component.ComponentTesting) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) Mockito.doReturn(org.mockito.Mockito.doReturn) FavoriteFinder(org.sonar.server.favorite.FavoriteFinder) DbTester(org.sonar.db.DbTester) APP(org.sonar.api.resources.Qualifiers.APP) System2(org.sonar.api.utils.System2) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Collectors(java.util.stream.Collectors) Suggestion(org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion) Collectors.joining(java.util.stream.Collectors.joining) ComponentDto(org.sonar.db.component.ComponentDto) List(java.util.List) Stream(java.util.stream.Stream) VIEW(org.sonar.api.resources.Qualifiers.VIEW) SUBVIEW(org.sonar.api.resources.Qualifiers.SUBVIEW) Mockito.mock(org.mockito.Mockito.mock) EsTester(org.sonar.server.es.EsTester) SHORT_INPUT_WARNING(org.sonar.server.component.ws.SuggestionsAction.SHORT_INPUT_WARNING) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) MediaTypes(org.sonarqube.ws.MediaTypes) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) PARAM_QUERY(org.sonar.server.component.ws.SuggestionsAction.PARAM_QUERY) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) Qualifiers(org.sonar.api.resources.Qualifiers) UNIT_TEST_FILE(org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE) Change(org.sonar.api.server.ws.Change) ComponentIndex(org.sonar.server.component.index.ComponentIndex) Category(org.sonarqube.ws.Components.SuggestionsWsResponse.Category) MODULE(org.sonar.api.resources.Qualifiers.MODULE) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) TestResponse(org.sonar.server.ws.TestResponse) ComponentTesting.newModuleDto(org.sonar.db.component.ComponentTesting.newModuleDto) ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 13 with PROJECT

use of org.sonar.api.resources.Qualifiers.PROJECT in project sonarqube by SonarSource.

the class ListActionTest method response_contains_date_of_last_analysis.

@Test
public void response_contains_date_of_last_analysis() {
    Long lastAnalysisBranch = dateToLong(parseDateTime("2017-04-01T00:00:00+0100"));
    ComponentDto project = db.components().insertPrivateProject();
    userSession.logIn().addProjectPermission(USER, project);
    ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(org.sonar.db.component.BranchType.BRANCH));
    db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(branch2).setCreatedAt(lastAnalysisBranch));
    db.commit();
    indexIssues();
    permissionIndexerTester.allowOnlyAnyone(project);
    ListWsResponse response = ws.newRequest().setParam("project", project.getKey()).executeProtobuf(ListWsResponse.class);
    assertThat(response.getBranchesList()).extracting(ProjectBranches.Branch::getType, ProjectBranches.Branch::hasAnalysisDate, b -> "".equals(b.getAnalysisDate()) ? null : dateToLong(parseDateTime(b.getAnalysisDate()))).containsExactlyInAnyOrder(tuple(BranchType.BRANCH, false, null), tuple(BranchType.BRANCH, true, lastAnalysisBranch));
}
Also used : SCAN_EXECUTION(org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION) ComponentFinder(org.sonar.server.component.ComponentFinder) PermissionIndexerTester(org.sonar.server.permission.index.PermissionIndexerTester) AsyncIssueIndexing(org.sonar.server.issue.index.AsyncIssueIndexing) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) BranchType(org.sonarqube.ws.Common.BranchType) ProjectBranches(org.sonarqube.ws.ProjectBranches) EsTester(org.sonar.server.es.EsTester) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BUG(org.sonar.api.rules.RuleType.BUG) Branch(org.sonarqube.ws.ProjectBranches.Branch) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) DateUtils.dateToLong(org.sonar.api.utils.DateUtils.dateToLong) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) ComponentTesting(org.sonar.db.component.ComponentTesting) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) ResourceTypes(org.sonar.api.resources.ResourceTypes) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) USER(org.sonar.api.web.UserRole.USER) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) BRANCH(org.sonar.db.component.BranchType.BRANCH) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) ComponentDto(org.sonar.db.component.ComponentDto) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) Rule(org.junit.Rule) MetricDto(org.sonar.db.metric.MetricDto) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ListWsResponse(org.sonarqube.ws.ProjectBranches.ListWsResponse) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) Mockito.mock(org.mockito.Mockito.mock) ListWsResponse(org.sonarqube.ws.ProjectBranches.ListWsResponse) ProjectBranches(org.sonarqube.ws.ProjectBranches) DateUtils.dateToLong(org.sonar.api.utils.DateUtils.dateToLong) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 14 with PROJECT

use of org.sonar.api.resources.Qualifiers.PROJECT in project sonarqube by SonarSource.

the class ComponentTreeActionTest method fix_pull_request_new_issue_count_metrics.

@Test
public void fix_pull_request_new_issue_count_metrics() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
    MetricDto newBug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.NEW_BUGS_KEY));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, bug, m -> m.setValue(12.0d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, newBug.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
    assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
    assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getPeriods().getPeriodsValue(0).getValue()), Measure::getValue).containsExactlyInAnyOrder(tuple(newBug.getKey(), measure.getValue(), ""));
    assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getPeriod().getValue()), Measure::getValue).containsExactlyInAnyOrder(tuple(newBug.getKey(), measure.getValue(), ""));
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) SORT(org.sonar.api.server.ws.WebService.Param.SORT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MetricTesting(org.sonar.db.metric.MetricTesting) FILE(org.sonar.api.resources.Qualifiers.FILE) DEPRECATED_ADDITIONAL_PERIODS(org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_ADDITIONAL_PERIODS) LEAVES_STRATEGY(org.sonar.server.measure.ws.ComponentTreeAction.LEAVES_STRATEGY) DbSession(org.sonar.db.DbSession) PARAM_QUALIFIERS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_QUALIFIERS) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) NEW_SECURITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY) Measure(org.sonarqube.ws.Measures.Measure) PARAM_METRIC_SORT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT) Param(org.sonar.api.server.ws.WebService.Param) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) ComponentTesting(org.sonar.db.component.ComponentTesting) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) APP(org.sonar.api.resources.Qualifiers.APP) System2(org.sonar.api.utils.System2) NAME_SORT(org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT) METRIC_SORT(org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT) CoreMetrics(org.sonar.api.measures.CoreMetrics) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) ComponentTesting.newDirectory(org.sonar.db.component.ComponentTesting.newDirectory) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) PARAM_METRIC_SORT_FILTER(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT_FILTER) FLOAT(org.sonar.api.measures.Metric.ValueType.FLOAT) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) DISTRIB(org.sonar.api.measures.Metric.ValueType.DISTRIB) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) RATING(org.sonar.api.measures.Metric.ValueType.RATING) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) WITH_MEASURES_ONLY_METRIC_SORT_FILTER(org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) PARAM_METRIC_PERIOD_SORT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_PERIOD_SORT) UserSessionRule(org.sonar.server.tester.UserSessionRule) PARAM_STRATEGY(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_STRATEGY) Assertions.tuple(org.assertj.core.api.Assertions.tuple) PeriodValue(org.sonarqube.ws.Measures.PeriodValue) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) DIRECTORY(org.sonar.api.resources.Qualifiers.DIRECTORY) Test(org.junit.Test) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) INT(org.sonar.api.measures.Metric.ValueType.INT) UNIT_TEST_FILE(org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) METRIC_PERIOD_SORT(org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT) I18nRule(org.sonar.server.l18n.I18nRule) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) Metric(org.sonar.api.measures.Metric) ADDITIONAL_PERIOD(org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD) MetricDto(org.sonar.db.metric.MetricDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Example 15 with PROJECT

use of org.sonar.api.resources.Qualifiers.PROJECT in project sonarqube by SonarSource.

the class PermissionTemplateServiceTest method apply_permission_template_with_key_pattern_collision.

@Test
public void apply_permission_template_with_key_pattern_collision() {
    final String key = "hi-test";
    final String keyPattern = ".*-test";
    Stream<PermissionTemplateDto> templates = Stream.of(templateDb.insertTemplate(t -> t.setKeyPattern(keyPattern)), templateDb.insertTemplate(t -> t.setKeyPattern(keyPattern)));
    String templateNames = templates.map(PermissionTemplateDto::getName).sorted(String.CASE_INSENSITIVE_ORDER).map(x -> String.format("\"%s\"", x)).collect(Collectors.joining(", "));
    ComponentDto project = dbTester.components().insertPrivateProject(p -> p.setDbKey(key));
    assertThatThrownBy(() -> underTest.applyDefaultToNewComponent(session, project, null)).isInstanceOf(TemplateMatchingKeyException.class).hasMessageContaining("The \"%s\" key matches multiple permission templates: %s.", key, templateNames);
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ProjectIndexers(org.sonar.server.es.ProjectIndexers) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PermissionTemplateDbTester(org.sonar.db.permission.template.PermissionTemplateDbTester) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) ADMINISTER(org.sonar.db.permission.GlobalPermission.ADMINISTER) DbSession(org.sonar.db.DbSession) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) Collections.singletonList(java.util.Collections.singletonList) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) TestProjectIndexers(org.sonar.server.es.TestProjectIndexers) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) Nullable(javax.annotation.Nullable) GroupDto(org.sonar.db.user.GroupDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) APP(org.sonar.api.resources.Qualifiers.APP) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SequenceUuidFactory(org.sonar.core.util.SequenceUuidFactory) TemplateMatchingKeyException(org.sonar.server.exceptions.TemplateMatchingKeyException) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) VIEW(org.sonar.api.resources.Qualifiers.VIEW) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ComponentDto(org.sonar.db.component.ComponentDto) TemplateMatchingKeyException(org.sonar.server.exceptions.TemplateMatchingKeyException) Test(org.junit.Test)

Aggregations

Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Rule (org.junit.Rule)15 PROJECT (org.sonar.api.resources.Qualifiers.PROJECT)15 DbTester (org.sonar.db.DbTester)15 Test (org.junit.Test)14 System2 (org.sonar.api.utils.System2)14 ComponentDto (org.sonar.db.component.ComponentDto)14 UserSessionRule (org.sonar.server.tester.UserSessionRule)14 ResourceTypesRule (org.sonar.db.component.ResourceTypesRule)13 ComponentTesting (org.sonar.db.component.ComponentTesting)12 WsActionTester (org.sonar.server.ws.WsActionTester)12 JsonAssert.assertJson (org.sonar.test.JsonAssert.assertJson)12 Collections.singletonList (java.util.Collections.singletonList)11 List (java.util.List)11 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)11 Assertions.tuple (org.assertj.core.api.Assertions.tuple)10 APP (org.sonar.api.resources.Qualifiers.APP)10 String.format (java.lang.String.format)9 FILE (org.sonar.api.resources.Qualifiers.FILE)9 MetricDto (org.sonar.db.metric.MetricDto)9