Search in sources :

Example 1 with PARAM_COMPONENT

use of org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT in project sonarqube by SonarSource.

the class ComponentTreeActionTest method pull_request.

@Test
public void pull_request() {
    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 complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, complexity.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.getValue())).containsExactlyInAnyOrder(tuple(complexity.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 2 with PARAM_COMPONENT

use of org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT in project sonarqube by SonarSource.

the class ComponentTreeActionTest method return_is_best_value_on_leak_measures.

@Test
public void return_is_best_value_on_leak_measures() {
    ComponentDto project = db.components().insertPrivateProject();
    db.components().insertSnapshot(project);
    userSession.anonymous().addProjectPermission(UserRole.USER, project);
    ComponentDto file = newFileDto(project, null);
    db.components().insertComponent(file);
    MetricDto matchingBestValue = db.measures().insertMetric(m -> m.setKey("new_lines").setValueType(INT.name()).setBestValue(100d));
    MetricDto doesNotMatchBestValue = db.measures().insertMetric(m -> m.setKey("new_lines_2").setValueType(INT.name()).setBestValue(100d));
    MetricDto noBestValue = db.measures().insertMetric(m -> m.setKey("new_violations").setValueType(INT.name()).setBestValue(null));
    db.measures().insertLiveMeasure(file, matchingBestValue, m -> m.setValue(null).setData((String) null).setVariation(100d));
    db.measures().insertLiveMeasure(file, doesNotMatchBestValue, m -> m.setValue(null).setData((String) null).setVariation(10d));
    db.measures().insertLiveMeasure(file, noBestValue, m -> m.setValue(null).setData((String) null).setVariation(42.0d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_METRIC_KEYS, "new_lines,new_lines_2,new_violations").executeProtobuf(ComponentTreeWsResponse.class);
    // file measures
    // verify backward compatibility
    List<Measure> fileMeasures = response.getComponentsList().get(0).getMeasuresList();
    assertThat(fileMeasures).extracting(Measure::getMetric, m -> m.getPeriods().getPeriodsValueList()).containsExactlyInAnyOrder(tuple(matchingBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("100").setBestValue(true).build())), tuple(doesNotMatchBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("10").setBestValue(false).build())), tuple(noBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("42").build())));
    assertThat(fileMeasures).extracting(Measure::getMetric, Measure::getPeriod).containsExactlyInAnyOrder(tuple(matchingBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("100").setBestValue(true).build()), tuple(doesNotMatchBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("10").setBestValue(false).build()), tuple(noBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("42").build()));
}
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) ComponentDto(org.sonar.db.component.ComponentDto) Measure(org.sonarqube.ws.Measures.Measure) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Example 3 with PARAM_COMPONENT

use of org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT in project sonarqube by SonarSource.

the class ComponentTreeActionTest method fail_when_setting_more_than_15_metric_keys.

@Test
public void fail_when_setting_more_than_15_metric_keys() {
    ComponentDto project = db.components().insertPrivateProject();
    db.components().insertSnapshot(project);
    List<String> metrics = IntStream.range(0, 20).mapToObj(i -> "metric" + i).collect(MoreCollectors.toList());
    db.commit();
    assertThatThrownBy(() -> {
        ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_METRIC_KEYS, Joiner.on(",").join(metrics)).execute();
    }).isInstanceOf(IllegalArgumentException.class).hasMessage("'metricKeys' can contains only 15 values, got 20");
}
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) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 4 with PARAM_COMPONENT

use of org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT in project sonarqube by SonarSource.

the class ComponentTreeActionTest method branch.

@Test
public void branch() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_BRANCH, file.getBranch()).setParam(PARAM_METRIC_KEYS, complexity.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
    assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch).containsExactlyInAnyOrder(file.getKey(), file.getBranch());
    assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(complexity.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 5 with PARAM_COMPONENT

use of org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT in project sonarqube by SonarSource.

the class SearchHistoryActionTest method branch.

@Test
public void branch() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d));
    SearchHistoryResponse result = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_BRANCH, "my_branch").setParam(PARAM_METRICS, "ncloc").executeProtobuf(SearchHistoryResponse.class);
    assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(1);
    HistoryMeasure historyMeasure = result.getMeasures(0);
    assertThat(historyMeasure.getMetric()).isEqualTo(nclocMetric.getKey());
    assertThat(historyMeasure.getHistoryList()).extracting(m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(measure.getValue());
}
Also used : MeasureDto(org.sonar.db.measure.MeasureDto) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Arrays.asList(java.util.Arrays.asList) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) PARAM_TO(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO) HistoryValue(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryValue) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) PARAM_FROM(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) TestComponentFinder(org.sonar.server.component.TestComponentFinder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) LongStream(java.util.stream.LongStream) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Paging(org.sonarqube.ws.Common.Paging) Test(org.junit.Test) STATUS_UNPROCESSED(org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED) 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) ValueType(org.sonar.api.measures.Metric.ValueType) PARAM_METRICS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) ComponentDto(org.sonar.db.component.ComponentDto) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) Test(org.junit.Test)

Aggregations

Double.parseDouble (java.lang.Double.parseDouble)10 String.format (java.lang.String.format)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)10 Assertions.tuple (org.assertj.core.api.Assertions.tuple)10 Rule (org.junit.Rule)10 Test (org.junit.Test)10 Param (org.sonar.api.server.ws.WebService.Param)10 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)10 System2 (org.sonar.api.utils.System2)10 UserRole (org.sonar.api.web.UserRole)10 DbTester (org.sonar.db.DbTester)10 PULL_REQUEST (org.sonar.db.component.BranchType.PULL_REQUEST)10 ComponentDto (org.sonar.db.component.ComponentDto)10 ComponentTesting.newFileDto (org.sonar.db.component.ComponentTesting.newFileDto)10 SnapshotDto (org.sonar.db.component.SnapshotDto)10 MetricDto (org.sonar.db.metric.MetricDto)10 PARAM_BRANCH (org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH)10 PARAM_COMPONENT (org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT)10 PARAM_PULL_REQUEST (org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST)10