Search in sources :

Example 11 with ComponentTreeWsResponse

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

the class ComponentTreeActionTest method portfolio_local_reference_in_portfolio.

@Test
public void portfolio_local_reference_in_portfolio() {
    ComponentDto view = db.components().insertComponent(ComponentTesting.newPortfolio("VIEW1-UUID").setDbKey("Apache-Projects").setName("Apache Projects"));
    ComponentDto view2 = db.components().insertPrivatePortfolio();
    ComponentDto localView = db.components().insertComponent(ComponentTesting.newSubPortfolio(view, "SUB-VIEW-UUID", "All-Projects").setName("All projects").setCopyComponentUuid(view2.uuid()));
    db.components().insertSnapshot(view);
    MetricDto ncloc = insertNclocMetric();
    db.measures().insertLiveMeasure(localView, ncloc, m -> m.setValue(5d));
    ComponentTreeWsResponse result = ws.newRequest().setParam(PARAM_COMPONENT, view.getKey()).setParam(PARAM_METRIC_KEYS, ncloc.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
    assertThat(result.getComponentsList()).extracting(Component::getKey, Component::getRefKey, Component::getQualifier).containsExactlyInAnyOrder(tuple(localView.getKey(), view2.getKey(), "SVW"));
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentDto(org.sonar.db.component.ComponentDto) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Example 12 with ComponentTreeWsResponse

use of org.sonarqube.ws.Measures.ComponentTreeWsResponse 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 13 with ComponentTreeWsResponse

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

the class ComponentTreeActionTest method use_best_value_for_rating.

@Test
public void use_best_value_for_rating() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.anonymous().addProjectPermission(UserRole.USER, project);
    SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime()).setPeriodMode("previous_version").setPeriodParam("1.0-SNAPSHOT"));
    ComponentDto directory = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
    db.components().insertComponent(directory);
    ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
    db.components().insertComponent(file);
    MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_SECURITY_RATING_KEY).setOptimizedBestValue(true).setBestValue(1d).setValueType(RATING.name()));
    db.commit();
    db.measures().insertLiveMeasure(directory, metric, m -> m.setVariation(2d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_METRIC_KEYS, NEW_SECURITY_RATING_KEY).setParam(PARAM_ADDITIONAL_FIELDS, "metrics").executeProtobuf(ComponentTreeWsResponse.class);
    // directory
    assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriods().getPeriodsValue(0).getValue()).isEqualTo("2.0");
    assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("2.0");
    // file measures
    assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriods().getPeriodsValue(0).getValue()).isEqualTo("1.0");
    assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("1.0");
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Example 14 with ComponentTreeWsResponse

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

the class ComponentTreeAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    ComponentTreeWsResponse componentTreeWsResponse = doHandle(toComponentTreeWsRequest(request));
    writeProtobuf(componentTreeWsResponse, request, response);
}
Also used : ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse)

Example 15 with ComponentTreeWsResponse

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

the class ComponentTreeActionTest method load_measures_when_no_leave_qualifier.

@Test
public void load_measures_when_no_leave_qualifier() {
    resourceTypes.setLeavesQualifiers();
    ComponentDto project = db.components().insertPrivateProject();
    db.components().insertSnapshot(project);
    db.components().insertComponent(newFileDto(project, null));
    insertNclocMetric();
    ComponentTreeWsResponse result = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_STRATEGY, LEAVES_STRATEGY).setParam(PARAM_METRIC_KEYS, "ncloc").executeProtobuf(ComponentTreeWsResponse.class);
    assertThat(result.getBaseComponent().getKey()).isEqualTo(project.getKey());
    assertThat(result.getComponentsCount()).isZero();
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Aggregations

ComponentTreeWsResponse (org.sonarqube.ws.Measures.ComponentTreeWsResponse)22 ComponentDto (org.sonar.db.component.ComponentDto)21 Test (org.junit.Test)20 MetricDto (org.sonar.db.metric.MetricDto)19 SnapshotDto (org.sonar.db.component.SnapshotDto)16 Measure (org.sonarqube.ws.Measures.Measure)6 Metric (org.sonar.api.measures.Metric)5 Joiner (com.google.common.base.Joiner)4 Double.parseDouble (java.lang.Double.parseDouble)4 String.format (java.lang.String.format)4 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 IntStream (java.util.stream.IntStream)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Assertions.tuple (org.assertj.core.api.Assertions.tuple)4 Rule (org.junit.Rule)4 CoreMetrics (org.sonar.api.measures.CoreMetrics)4 NEW_SECURITY_RATING_KEY (org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY)4 DISTRIB (org.sonar.api.measures.Metric.ValueType.DISTRIB)4