Search in sources :

Example 6 with ComponentWsResponse

use of org.sonarqube.ws.Measures.ComponentWsResponse in project sonarqube by SonarSource.

the class ComponentActionTest method metric_without_a_domain.

@Test
public void metric_without_a_domain() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m.setValueType("INT").setDomain(null));
    db.measures().insertLiveMeasure(project, metricWithoutDomain);
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey()).setParam(PARAM_ADDITIONAL_FIELDS, "metrics").executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric).containsExactly(metricWithoutDomain.getKey());
    Common.Metric responseMetric = response.getMetrics().getMetrics(0);
    assertThat(responseMetric.getKey()).isEqualTo(metricWithoutDomain.getKey());
    assertThat(responseMetric.hasDomain()).isFalse();
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) ComponentDto(org.sonar.db.component.ComponentDto) Common(org.sonarqube.ws.Common) Test(org.junit.Test)

Example 7 with ComponentWsResponse

use of org.sonarqube.ws.Measures.ComponentWsResponse in project sonarqube by SonarSource.

the class ComponentActionTest method new_issue_count_measures_are_transformed_in_pr.

@Test
public void new_issue_count_measures_are_transformed_in_pr() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    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 bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
    MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setValueType("INT"));
    MetricDto violations = db.measures().insertMetric(m1 -> m1.setKey("violations").setValueType("INT"));
    MetricDto newViolations = db.measures().insertMetric(m1 -> m1.setKey("new_violations").setValueType("INT"));
    LiveMeasureDto bugMeasure = db.measures().insertLiveMeasure(file, bugs, m -> m.setValue(12.0d).setVariation(null));
    LiveMeasureDto newBugMeasure = db.measures().insertLiveMeasure(file, newBugs, m -> m.setVariation(1d).setValue(null));
    LiveMeasureDto violationMeasure = db.measures().insertLiveMeasure(file, violations, m -> m.setValue(20.0d).setVariation(null));
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, newBugs.getKey() + "," + bugs.getKey() + "," + newViolations.getKey()).executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
    Function<Measures.Measure, Double> extractVariation = m -> {
        if (m.getPeriods().getPeriodsValueCount() > 0) {
            return parseDouble(m.getPeriods().getPeriodsValue(0).getValue());
        }
        return null;
    };
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric, extractVariation, m -> m.getValue().isEmpty() ? null : parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(newBugs.getKey(), bugMeasure.getValue(), null), tuple(bugs.getKey(), null, bugMeasure.getValue()), tuple(newViolations.getKey(), violationMeasure.getValue(), null));
}
Also used : ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) TestComponentFinder(org.sonar.server.component.TestComponentFinder) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) Double.parseDouble(java.lang.Double.parseDouble) 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) 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) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Measures(org.sonarqube.ws.Measures) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Double.parseDouble(java.lang.Double.parseDouble) Test(org.junit.Test)

Example 8 with ComponentWsResponse

use of org.sonarqube.ws.Measures.ComponentWsResponse in project sonarqube by SonarSource.

the class ComponentActionTest method use_best_values.

@Test
public void use_best_values() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    userSession.addProjectPermission(UserRole.USER, project);
    MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT").setBestValue(7.0d).setOptimizedBestValue(true).setDomain(null));
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_METRIC_KEYS, metric.getKey()).setParam(PARAM_ADDITIONAL_FIELDS, "metrics").executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric, Measures.Measure::getValue, Measures.Measure::getBestValue).containsExactly(tuple(metric.getKey(), "7", true));
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 9 with ComponentWsResponse

use of org.sonarqube.ws.Measures.ComponentWsResponse in project sonarqube by SonarSource.

the class ComponentActionTest method provided_project.

@Test
public void provided_project() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
    ComponentWsResponse response = newRequest(project.getKey(), metric.getKey());
    assertThat(response.getMetrics().getMetricsCount()).isOne();
    assertThat(response.hasPeriod()).isFalse();
    assertThat(response.getPeriods().getPeriodsCount()).isZero();
    assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey());
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 10 with ComponentWsResponse

use of org.sonarqube.ws.Measures.ComponentWsResponse in project sonarqube by SonarSource.

the class ComponentActionTest 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"));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d));
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_BRANCH, file.getBranch()).setParam(PARAM_METRIC_KEYS, complexity.getKey()).executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent()).extracting(Component::getKey, Component::getBranch).containsExactlyInAnyOrder(file.getKey(), file.getBranch());
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
}
Also used : ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) TestComponentFinder(org.sonar.server.component.TestComponentFinder) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) Double.parseDouble(java.lang.Double.parseDouble) 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) 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) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Measures(org.sonarqube.ws.Measures) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Test(org.junit.Test)

Aggregations

ComponentWsResponse (org.sonarqube.ws.Measures.ComponentWsResponse)11 MetricDto (org.sonar.db.metric.MetricDto)10 Test (org.junit.Test)9 ComponentDto (org.sonar.db.component.ComponentDto)9 Measures (org.sonarqube.ws.Measures)5 SnapshotDto (org.sonar.db.component.SnapshotDto)4 Common (org.sonarqube.ws.Common)4 Double.parseDouble (java.lang.Double.parseDouble)3 String.format (java.lang.String.format)3 Function (java.util.function.Function)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)3 Assertions.tuple (org.assertj.core.api.Assertions.tuple)3 Rule (org.junit.Rule)3 WebService (org.sonar.api.server.ws.WebService)3 Param (org.sonar.api.server.ws.WebService.Param)3 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)3 System2 (org.sonar.api.utils.System2)3 UserRole (org.sonar.api.web.UserRole)3 USER (org.sonar.api.web.UserRole.USER)3