import org.sonarqube.ws.WsMeasures;
import org.sonarqube.ws.WsMeasures.ComponentTreeWsResponse;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY;
import static org.sonar.api.measures.Metric.ValueType.FLOAT;
import static org.sonar.api.measures.Metric.ValueType.INT;
import static org.sonar.api.measures.Metric.ValueType.RATING;
+import static org.sonar.api.server.ws.WebService.Param.SORT;
import static org.sonar.api.utils.DateUtils.parseDateTime;
+import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.component.ComponentTesting.newDirectory;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.ADDITIONAL_PERIODS;
+import static org.sonarqube.ws.client.measure.MeasuresWsParameters.DEPRECATED_PARAM_BASE_COMPONENT_KEY;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
-import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_BASE_COMPONENT_ID;
-import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_BASE_COMPONENT_KEY;
+import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRIC_KEYS;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRIC_PERIOD_SORT;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRIC_SORT;
@Test
public void json_example() {
- insertJsonExampleData();
+ ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("MY_PROJECT")
+ .setName("My Project"));
+ SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
+ .setPeriodMode("previous_version")
+ .setPeriodParam("1.0-SNAPSHOT"));
+ ComponentDto file1 = componentDb.insertComponent(newFileDto(project, null)
+ .setUuid("AVIwDXE-bJbJqrw6wFv5")
+ .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java")
+ .setName("ElementImpl.java")
+ .setLanguage("java")
+ .setQualifier(Qualifiers.FILE)
+ .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));
+ ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null)
+ .setUuid("AVIwDXE_bJbJqrw6wFwJ")
+ .setDbKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java")
+ .setName("ElementImplTest.java")
+ .setLanguage("java")
+ .setQualifier(Qualifiers.UNIT_TEST_FILE)
+ .setPath("src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java"));
+ ComponentDto dir = componentDb.insertComponent(newDirectory(project, "src/main/java/com/sonarsource/markdown/impl")
+ .setUuid("AVIwDXE-bJbJqrw6wFv8")
+ .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
+ .setQualifier(Qualifiers.DIRECTORY));
+
+ MetricDto complexity = insertComplexityMetric();
+ dbClient.measureDao().insert(dbSession,
+ newMeasureDto(complexity, file1, analysis)
+ .setValue(12.0d),
+ newMeasureDto(complexity, dir, analysis)
+ .setValue(35.0d)
+ .setVariation(0.0d),
+ newMeasureDto(complexity, project, analysis)
+ .setValue(42.0d));
+
+ MetricDto ncloc = insertNclocMetric();
+ dbClient.measureDao().insert(dbSession,
+ newMeasureDto(ncloc, file1, analysis)
+ .setValue(114.0d),
+ newMeasureDto(ncloc, dir, analysis)
+ .setValue(217.0d)
+ .setVariation(0.0d),
+ newMeasureDto(ncloc, project, analysis)
+ .setValue(1984.0d));
+
+ MetricDto newViolations = insertNewViolationsMetric();
+ dbClient.measureDao().insert(dbSession,
+ newMeasureDto(newViolations, file1, analysis)
+ .setVariation(25.0d),
+ newMeasureDto(newViolations, dir, analysis)
+ .setVariation(25.0d),
+ newMeasureDto(newViolations, project, analysis)
+ .setVariation(255.0d));
+
+ db.commit();
String response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-id")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations")
.setParam(PARAM_ADDITIONAL_FIELDS, "metrics,periods")
.execute()
@Test
public void empty_response() {
- componentDb.insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc, complexity")
.executeProtobuf(ComponentTreeWsResponse.class);
- assertThat(response.getBaseComponent().getId()).isEqualTo("project-uuid");
+ assertThat(response.getBaseComponent().getKey()).isEqualTo(project.getKey());
assertThat(response.getComponentsList()).isEmpty();
assertThat(response.getMetrics().getMetricsList()).isEmpty();
assertThat(response.getPeriods().getPeriodsList()).isEmpty();
@Test
public void load_measures_and_periods() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- componentDb.insertComponent(projectDto);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession,
- newAnalysis(projectDto)
+ newAnalysis(project)
.setPeriodDate(System.currentTimeMillis())
.setPeriodMode("last_version")
.setPeriodDate(System.currentTimeMillis()));
- userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
- ComponentDto directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
+ userSession.anonymous().addProjectPermission(UserRole.USER, project);
+ ComponentDto directoryDto = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
componentDb.insertComponent(directoryDto);
ComponentDto file = newFileDto(directoryDto, null, "file-uuid").setName("file-1");
componentDb.insertComponent(file);
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc,coverage")
.setParam(PARAM_ADDITIONAL_FIELDS, ADDITIONAL_PERIODS)
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void load_measures_with_best_value() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
- ComponentDto directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
+ userSession.anonymous().addProjectPermission(UserRole.USER, project);
+ ComponentDto directoryDto = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
componentDb.insertComponent(directoryDto);
ComponentDto file = newFileDto(directoryDto, null, "file-uuid").setName("file-1");
componentDb.insertComponent(file);
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc,coverage,new_violations")
.setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void use_best_value_for_rating() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
- componentDb.insertComponent(projectDto);
- SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(projectDto)
+ 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 directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
+ ComponentDto directoryDto = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
componentDb.insertComponent(directoryDto);
ComponentDto file = newFileDto(directoryDto, null, "file-uuid").setName("file-1");
componentDb.insertComponent(file);
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, NEW_SECURITY_RATING_KEY)
.setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void load_measures_multi_sort_with_metric_key_and_paginated() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- ComponentDto file9 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-9").setName("file-1"));
- ComponentDto file8 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-8").setName("file-1"));
- ComponentDto file7 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-7").setName("file-1"));
- ComponentDto file6 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-6").setName("file-1"));
- ComponentDto file5 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-5").setName("file-1"));
- ComponentDto file4 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-4").setName("file-1"));
- ComponentDto file3 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-3").setName("file-1"));
- ComponentDto file2 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-2").setName("file-1"));
- ComponentDto file1 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-1").setName("file-1"));
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
+ ComponentDto file9 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-9").setName("file-1"));
+ ComponentDto file8 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-8").setName("file-1"));
+ ComponentDto file7 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-7").setName("file-1"));
+ ComponentDto file6 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-6").setName("file-1"));
+ ComponentDto file5 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-5").setName("file-1"));
+ ComponentDto file4 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-4").setName("file-1"));
+ ComponentDto file3 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-3").setName("file-1"));
+ ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-2").setName("file-1"));
+ ComponentDto file1 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-1").setName("file-1"));
MetricDto coverage = insertCoverageMetric();
dbClient.measureDao().insert(dbSession,
newMeasureDto(coverage, file1, projectSnapshot).setValue(1.0d),
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
- .setParam(Param.SORT, NAME_SORT + ", " + METRIC_SORT)
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(SORT, NAME_SORT + ", " + METRIC_SORT)
.setParam(PARAM_METRIC_SORT, "coverage")
.setParam(PARAM_METRIC_KEYS, "coverage")
.setParam(PARAM_STRATEGY, "leaves")
@Test
public void sort_by_metric_value() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- ComponentDto file4 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-4"));
- ComponentDto file3 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-3"));
- ComponentDto file1 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-1"));
- ComponentDto file2 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-2"));
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
+ ComponentDto file4 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-4"));
+ ComponentDto file3 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-3"));
+ ComponentDto file1 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-1"));
+ ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-2"));
MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
dbClient.metricDao().insert(dbSession, ncloc);
dbClient.measureDao().insert(dbSession,
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
- .setParam(Param.SORT, METRIC_SORT)
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(SORT, METRIC_SORT)
.setParam(PARAM_METRIC_SORT, "ncloc")
.setParam(PARAM_METRIC_KEYS, "ncloc")
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void remove_components_without_measure_on_the_metric_sort() {
- ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
ComponentDto file1 = newFileDto(project, null, "file-uuid-1");
ComponentDto file2 = newFileDto(project, null, "file-uuid-2");
ComponentDto file3 = newFileDto(project, null, "file-uuid-3");
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, project.uuid())
- .setParam(Param.SORT, METRIC_SORT)
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(SORT, METRIC_SORT)
.setParam(PARAM_METRIC_SORT, "ncloc")
.setParam(PARAM_METRIC_KEYS, "ncloc")
.setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
@Test
public void sort_by_metric_period() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- ComponentDto file3 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-3"));
- ComponentDto file1 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-1"));
- ComponentDto file2 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-2"));
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
+ ComponentDto file3 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-3"));
+ ComponentDto file1 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-1"));
+ ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-2"));
MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
dbClient.metricDao().insert(dbSession, ncloc);
dbClient.measureDao().insert(dbSession,
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
- .setParam(Param.SORT, METRIC_PERIOD_SORT)
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(SORT, METRIC_PERIOD_SORT)
.setParam(PARAM_METRIC_SORT, "ncloc")
.setParam(PARAM_METRIC_KEYS, "ncloc")
.setParam(PARAM_METRIC_PERIOD_SORT, "1")
@Test
public void remove_components_without_measure_on_the_metric_period_sort() {
- ComponentDto projectDto = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
- SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- ComponentDto file4 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-4"));
- ComponentDto file3 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-3"));
- ComponentDto file2 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-2"));
- ComponentDto file1 = componentDb.insertComponent(newFileDto(projectDto, null, "file-uuid-1"));
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
+ ComponentDto file4 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-4"));
+ ComponentDto file3 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-3"));
+ ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-2"));
+ ComponentDto file1 = componentDb.insertComponent(newFileDto(project, null, "file-uuid-1"));
MetricDto ncloc = newMetricDto().setKey("new_ncloc").setValueType(INT.name()).setDirection(1);
dbClient.metricDao().insert(dbSession, ncloc);
dbClient.measureDao().insert(dbSession,
db.commit();
ComponentTreeWsResponse response = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
- .setParam(Param.SORT, METRIC_PERIOD_SORT + "," + NAME_SORT)
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(SORT, METRIC_PERIOD_SORT + "," + NAME_SORT)
.setParam(PARAM_METRIC_SORT, "new_ncloc")
.setParam(PARAM_METRIC_KEYS, "new_ncloc")
.setParam(PARAM_METRIC_PERIOD_SORT, "1")
@Test
public void load_measures_when_no_leave_qualifier() {
resourceTypes.setLeavesQualifiers();
- String projectUuid = "project-uuid";
- ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), projectUuid);
- componentDb.insertProjectAndSnapshot(project);
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
componentDb.insertComponent(newFileDto(project, null));
insertNclocMetric();
ComponentTreeWsResponse result = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, projectUuid)
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_STRATEGY, LEAVES_STRATEGY)
.setParam(PARAM_METRIC_KEYS, "ncloc")
.executeProtobuf(ComponentTreeWsResponse.class);
- assertThat(result.getBaseComponent().getId()).isEqualTo(projectUuid);
+ assertThat(result.getBaseComponent().getKey()).isEqualTo(project.getKey());
assertThat(result.getComponentsCount()).isEqualTo(0);
}
+ @Test
+ public void return_deprecated_id_in_the_response() {
+ ComponentDto project = db.components().insertPrivateProject();
+ SnapshotDto analysis = db.components().insertSnapshot(project);
+ ComponentDto file = componentDb.insertComponent(newFileDto(project));
+ MetricDto ncloc = insertNclocMetric();
+ db.measures().insertMeasure(file, analysis, ncloc, m -> m.setValue(2d));
+
+ ComponentTreeWsResponse response = ws.newRequest()
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(PARAM_METRIC_KEYS, ncloc.getKey())
+ .executeProtobuf(ComponentTreeWsResponse.class);
+
+ assertThat(response.getBaseComponent().getId()).isEqualTo(project.uuid());
+ assertThat(response.getComponentsList()).extracting(WsMeasures.Component::getId)
+ .containsExactlyInAnyOrder(file.uuid());
+ }
+
+ @Test
+ public void use_deprecated_base_component_id_parameter() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.addProjectPermission(USER, project);
+ insertNclocMetric();
+
+ ComponentTreeWsResponse response = ws.newRequest()
+ .setParam("baseComponentId", project.uuid())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .executeProtobuf(ComponentTreeWsResponse.class);
+
+ assertThat(response.getBaseComponent().getKey()).isEqualTo(project.getKey());
+ }
+
+ @Test
+ public void use_deprecated_base_component_key_parameter() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.addProjectPermission(USER, project);
+ insertNclocMetric();
+
+ ComponentTreeWsResponse response = ws.newRequest()
+ .setParam("baseComponentKey", project.getKey())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .executeProtobuf(ComponentTreeWsResponse.class);
+
+ assertThat(response.getBaseComponent().getKey()).isEqualTo(project.getKey());
+ }
+
@Test
public void metric_without_a_domain() {
ComponentDto project = db.components().insertPrivateProject();
db.measures().insertMeasure(project, analysis, metricWithoutDomain);
ComponentTreeWsResponse result = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_KEY, project.getKey())
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
.setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
.executeProtobuf(ComponentTreeWsResponse.class);
db.measures().insertMeasure(projectCopy, viewAnalysis, ncloc, m -> m.setValue(5d));
ComponentTreeWsResponse result = ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_KEY, view.getKey())
+ .setParam(PARAM_COMPONENT, view.getKey())
.setParam(PARAM_METRIC_KEYS, ncloc.getKey())
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void fail_when_metric_keys_parameter_is_empty() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("The 'metricKeys' parameter must contain at least one metric key");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "")
.executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_a_metric_is_not_found() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
insertNclocMetric();
insertNewViolationsMetric();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("The following metric keys are not found: unknown-metric, another-unknown-metric");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc, new_violations, unknown-metric, another-unknown-metric").executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_using_DISTRIB_metrics() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib1").setValueType(DISTRIB.name()));
dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib2").setValueType(DISTRIB.name()));
db.commit();
expectedException.expectMessage("Metrics distrib1, distrib2 can't be requested in this web service. Please use api/measures/component");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "distrib1,distrib2")
.execute();
}
@Test
public void fail_when_using_DATA_metrics() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
+
dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data1").setValueType(DISTRIB.name()));
dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data2").setValueType(DISTRIB.name()));
db.commit();
expectedException.expectMessage("Metrics data1, data2 can't be requested in this web service. Please use api/measures/component");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "data1,data2")
.execute();
}
@Test
public void fail_when_setting_more_than_15_metric_keys() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
List<String> metrics = IntStream.range(0, 20)
.mapToObj(i -> "metric" + i)
.collect(MoreCollectors.toList());
expectedException.expectMessage("'metricKeys' can contains only 15 values, got 20");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, Joiner.on(",").join(metrics))
.execute();
}
@Test
public void fail_when_search_query_have_less_than_3_characters() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
insertNclocMetric();
insertNewViolationsMetric();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("The 'q' parameter must have at least 3 characters");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc, new_violations")
.setParam(Param.TEXT_QUERY, "fi")
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void fail_when_insufficient_privileges() {
userSession.logIn();
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
+
expectedException.expect(ForbiddenException.class);
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
.executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_sort_by_metric_and_no_metric_sort_provided() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
expectedException.expect(BadRequestException.class);
expectedException
.expectMessage("To sort by a metric, the 's' parameter must contain 'metric' or 'metricPeriod', and a metric key must be provided in the 'metricSort' parameter");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
// PARAM_METRIC_SORT is not set
- .setParam(Param.SORT, METRIC_SORT)
+ .setParam(SORT, METRIC_SORT)
.executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_sort_by_metric_and_not_in_the_list_of_metric_keys() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
+
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("To sort by the 'complexity' metric, it must be in the list of metric keys in the 'metricKeys' parameter");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc,violations")
.setParam(PARAM_METRIC_SORT, "complexity")
- .setParam(Param.SORT, METRIC_SORT)
+ .setParam(SORT, METRIC_SORT)
.executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_sort_by_metric_period_and_no_metric_period_sort_provided() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
+
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("To sort by a metric period, the 's' parameter must contain 'metricPeriod' and the 'metricPeriodSort' must be provided.");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
.setParam(PARAM_METRIC_SORT, "ncloc")
// PARAM_METRIC_PERIOD_SORT_IS_NOT_SET
- .setParam(Param.SORT, METRIC_PERIOD_SORT)
+ .setParam(SORT, METRIC_PERIOD_SORT)
.executeProtobuf(ComponentTreeWsResponse.class);
}
@Test
public void fail_when_paging_parameter_is_too_big() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
insertNclocMetric();
+
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("The 'ps' parameter must be less than 500");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
.setParam(Param.PAGE_SIZE, "2540")
.executeProtobuf(ComponentTreeWsResponse.class);
@Test
public void fail_when_with_measures_only_and_no_metric_sort() {
- componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
insertNclocMetric();
+
expectedException.expect(BadRequestException.class);
expectedException
.expectMessage("To filter components based on the sort metric, the 's' parameter must contain 'metric' or 'metricPeriod' and the 'metricSort' parameter must be provided");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+ .setParam(PARAM_COMPONENT, project.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
.setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
.executeProtobuf(ComponentTreeWsResponse.class);
expectedException.expectMessage("Component key 'project-key' not found");
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_KEY, "project-key")
+ .setParam(DEPRECATED_PARAM_BASE_COMPONENT_KEY, "project-key")
.setParam(PARAM_METRIC_KEYS, "ncloc")
.execute();
}
@Test
public void fail_when_component_is_removed() {
- ComponentDto project = componentDb.insertComponent(newPrivateProjectDto(db.getDefaultOrganization()));
- componentDb.insertComponent(newFileDto(project).setDbKey("file-key").setEnabled(false));
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertSnapshot(project);
+ ComponentDto file = componentDb.insertComponent(newFileDto(project).setDbKey("file-key").setEnabled(false));
userSession.anonymous().addProjectPermission(UserRole.USER, project);
insertNclocMetric();
expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'file-key' not found");
+ expectedException.expectMessage(format("Component key '%s' not found", file.getKey()));
ws.newRequest()
- .setParam(PARAM_BASE_COMPONENT_KEY, "file-key")
+ .setParam(PARAM_COMPONENT, file.getKey())
.setParam(PARAM_METRIC_KEYS, "ncloc")
.execute();
}
.setUserManaged(false);
}
- private void insertJsonExampleData() {
- ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-id")
- .setDbKey("MY_PROJECT")
- .setName("My Project")
- .setQualifier(Qualifiers.PROJECT);
- componentDb.insertComponent(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 file1 = componentDb.insertComponent(newFileDto(project, null)
- .setUuid("AVIwDXE-bJbJqrw6wFv5")
- .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java")
- .setName("ElementImpl.java")
- .setLanguage("java")
- .setQualifier(Qualifiers.FILE)
- .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));
- ComponentDto file2 = componentDb.insertComponent(newFileDto(project, null)
- .setUuid("AVIwDXE_bJbJqrw6wFwJ")
- .setDbKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java")
- .setName("ElementImplTest.java")
- .setLanguage("java")
- .setQualifier(Qualifiers.UNIT_TEST_FILE)
- .setPath("src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java"));
- ComponentDto dir = componentDb.insertComponent(newDirectory(project, "src/main/java/com/sonarsource/markdown/impl")
- .setUuid("AVIwDXE-bJbJqrw6wFv8")
- .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
- .setQualifier(Qualifiers.DIRECTORY));
-
- MetricDto complexity = insertComplexityMetric();
- dbClient.measureDao().insert(dbSession,
- newMeasureDto(complexity, file1, projectSnapshot)
- .setValue(12.0d),
- newMeasureDto(complexity, dir, projectSnapshot)
- .setValue(35.0d)
- .setVariation(0.0d),
- newMeasureDto(complexity, project, projectSnapshot)
- .setValue(42.0d));
-
- MetricDto ncloc = insertNclocMetric();
- dbClient.measureDao().insert(dbSession,
- newMeasureDto(ncloc, file1, projectSnapshot)
- .setValue(114.0d),
- newMeasureDto(ncloc, dir, projectSnapshot)
- .setValue(217.0d)
- .setVariation(0.0d),
- newMeasureDto(ncloc, project, projectSnapshot)
- .setValue(1984.0d));
-
- MetricDto newViolations = insertNewViolationsMetric();
- dbClient.measureDao().insert(dbSession,
- newMeasureDto(newViolations, file1, projectSnapshot)
- .setVariation(25.0d),
- newMeasureDto(newViolations, dir, projectSnapshot)
- .setVariation(25.0d),
- newMeasureDto(newViolations, project, projectSnapshot)
- .setVariation(255.0d));
-
- db.commit();
- }
-
private MetricDto insertNewViolationsMetric() {
MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
.setKey("new_violations")