/** * * This is part of the internal API. * This is a GET request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/measures/search">Further information about this action online (including a response example)</a> * @since 6.2 */ public SearchWsResponse search(SearchRequest request) { return call( new GetRequest(path("search")) .setParam("metricKeys", request.getMetricKeys() == null ? null : request.getMetricKeys().stream().collect(Collectors.joining(","))) .setParam("projectKeys", request.getProjectKeys() == null ? null : request.getProjectKeys().stream().collect(Collectors.joining(","))), SearchWsResponse.parser()); }
@Test public void do_not_verify_permissions_if_user_is_root() { MetricDto metric = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization()); db.measures().insertLiveMeasure(project1, metric, m -> m.setValue(15.5d)); userSession.setNonRoot(); SearchWsResponse result = call(singletonList(project1.getDbKey()), singletonList(metric.getKey())); assertThat(result.getMeasuresCount()).isEqualTo(0); userSession.setRoot(); result = call(singletonList(project1.getDbKey()), singletonList(metric.getKey())); assertThat(result.getMeasuresCount()).isEqualTo(1); }
@Test public void return_measures_on_leak_period() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); userSession.addProjectPermission(UserRole.USER, project); MetricDto coverage = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); db.measures().insertLiveMeasure(project, coverage, m -> m.setValue(15.5d).setVariation(10d)); SearchWsResponse result = call(singletonList(project.getDbKey()), singletonList(coverage.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures).hasSize(1); Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(coverage.getKey()); assertThat(measure.getValue()).isEqualTo("15.5"); assertThat(measure.getPeriods().getPeriodsValueList()) .extracting(Measures.PeriodValue::getIndex, Measures.PeriodValue::getValue) .containsOnly(tuple(1, "10.0")); }
/** * * This is part of the internal API. * This is a GET request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/measures/search">Further information about this action online (including a response example)</a> * @since 6.2 */ public SearchWsResponse search(SearchRequest request) { return call( new GetRequest(path("search")) .setParam("metricKeys", request.getMetricKeys() == null ? null : request.getMetricKeys().stream().collect(Collectors.joining(","))) .setParam("projectKeys", request.getProjectKeys() == null ? null : request.getProjectKeys().stream().collect(Collectors.joining(","))), SearchWsResponse.parser()); }
private SearchWsResponse buildResponse() { List<Measure> wsMeasures = buildWsMeasures(); return SearchWsResponse.newBuilder() .addAllMeasures(wsMeasures) .build(); }
@Test public void sort_by_metric_key_then_project_name() { MetricDto coverage = db.measures().insertMetric(m -> m.setKey("coverage").setValueType(FLOAT.name())); MetricDto complexity = db.measures().insertMetric(m -> m.setKey("complexity").setValueType(INT.name())); OrganizationDto organization = db.organizations().insert(); ComponentDto project1 = db.components().insertPrivateProject(organization, p -> p.setName("C")); ComponentDto project2 = db.components().insertPrivateProject(organization, p -> p.setName("A")); ComponentDto project3 = db.components().insertPrivateProject(organization, p -> p.setName("B")); userSession.addProjectPermission(UserRole.USER, project1); userSession.addProjectPermission(UserRole.USER, project2); userSession.addProjectPermission(UserRole.USER, project3); db.measures().insertLiveMeasure(project1, coverage, m -> m.setValue(5.5d)); db.measures().insertLiveMeasure(project2, coverage, m -> m.setValue(6.5d)); db.measures().insertLiveMeasure(project3, coverage, m -> m.setValue(7.5d)); db.measures().insertLiveMeasure(project1, complexity, m -> m.setValue(10d)); db.measures().insertLiveMeasure(project2, complexity, m -> m.setValue(15d)); db.measures().insertLiveMeasure(project3, complexity, m -> m.setValue(20d)); SearchWsResponse result = call(asList(project1.getDbKey(), project2.getDbKey(), project3.getDbKey()), asList(coverage.getKey(), complexity.getKey())); assertThat(result.getMeasuresList()).extracting(Measure::getMetric, Measure::getComponent) .containsExactly( tuple(complexity.getKey(), project2.getDbKey()), tuple(complexity.getKey(), project3.getDbKey()), tuple(complexity.getKey(), project1.getDbKey()), tuple(coverage.getKey(), project2.getDbKey()), tuple(coverage.getKey(), project3.getDbKey()), tuple(coverage.getKey(), project1.getDbKey())); }
@Test public void return_best_value() { ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization()); userSession.addProjectPermission(UserRole.USER, project); MetricDto matchBestValue = db.measures().insertMetric(m -> m.setValueType(FLOAT.name()).setBestValue(15.5d)); db.measures().insertLiveMeasure(project, matchBestValue, m -> m.setValue(15.5d)); MetricDto doesNotMatchBestValue = db.measures().insertMetric(m -> m.setValueType(INT.name()).setBestValue(50d)); db.measures().insertLiveMeasure(project, doesNotMatchBestValue, m -> m.setValue(40d)); MetricDto noBestValue = db.measures().insertMetric(m -> m.setValueType(INT.name()).setBestValue(null)); db.measures().insertLiveMeasure(project, noBestValue, m -> m.setValue(123d)); SearchWsResponse result = call(singletonList(project.getDbKey()), asList(matchBestValue.getKey(), doesNotMatchBestValue.getKey(), noBestValue.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures) .extracting(Measure::getMetric, Measure::getValue, Measure::getBestValue, Measure::hasBestValue) .containsExactlyInAnyOrder( tuple(matchBestValue.getKey(), "15.5", true, true), tuple(doesNotMatchBestValue.getKey(), "40", false, true), tuple(noBestValue.getKey(), "123", false, false)); }
@Test public void return_measures_on_sub_view() { OrganizationDto organization = db.organizations().insert(); ComponentDto view = db.components().insertPrivatePortfolio(organization); ComponentDto subView = db.components().insertComponent(newSubView(view)); userSession.addProjectPermission(UserRole.USER, subView); MetricDto metric = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); db.measures().insertLiveMeasure(subView, metric, m -> m.setValue(15.5d)); SearchWsResponse result = call(singletonList(subView.getDbKey()), singletonList(metric.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures).hasSize(1); Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(metric.getKey()); assertThat(measure.getValue()).isEqualTo("15.5"); }
@Test public void return_measures_on_view() { OrganizationDto organization = db.organizations().insert(); ComponentDto view = db.components().insertPrivatePortfolio(organization); userSession.addProjectPermission(UserRole.USER, view); MetricDto coverage = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); db.measures().insertLiveMeasure(view, coverage, m -> m.setValue(15.5d)); SearchWsResponse result = call(singletonList(view.getDbKey()), singletonList(coverage.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures).hasSize(1); Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(coverage.getKey()); assertThat(measure.getValue()).isEqualTo("15.5"); }
@Test public void return_measures_on_application() { OrganizationDto organization = db.organizations().insert(); ComponentDto application = db.components().insertPrivateApplication(organization); userSession.addProjectPermission(UserRole.USER, application); MetricDto coverage = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); db.measures().insertLiveMeasure(application, coverage, m -> m.setValue(15.5d)); SearchWsResponse result = call(singletonList(application.getDbKey()), singletonList(coverage.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures).hasSize(1); Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(coverage.getKey()); assertThat(measure.getValue()).isEqualTo("15.5"); }
@Test public void return_measures() { ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization()); userSession.addProjectPermission(UserRole.USER, project); MetricDto coverage = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); db.measures().insertLiveMeasure(project, coverage, m -> m.setValue(15.5d)); SearchWsResponse result = call(singletonList(project.getDbKey()), singletonList(coverage.getKey())); List<Measure> measures = result.getMeasuresList(); assertThat(measures).hasSize(1); Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(coverage.getKey()); assertThat(measure.getValue()).isEqualTo("15.5"); }
@Test public void does_not_return_branch_when_using_db_key() { MetricDto coverage = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); db.measures().insertLiveMeasure(branch, coverage, m -> m.setValue(10d)); userSession.addProjectPermission(UserRole.USER, project); SearchWsResponse result = call(singletonList(branch.getDbKey()), singletonList(coverage.getKey())); assertThat(result.getMeasuresList()).isEmpty(); }
private SearchWsResponse buildResponse() { List<Measure> wsMeasures = buildWsMeasures(); return SearchWsResponse.newBuilder() .addAllMeasures(wsMeasures) .build(); }
@Test public void only_returns_authorized_projects() { MetricDto metric = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization()); ComponentDto project2 = db.components().insertPrivateProject(db.getDefaultOrganization()); db.measures().insertLiveMeasure(project1, metric, m -> m.setValue(15.5d)); db.measures().insertLiveMeasure(project2, metric, m -> m.setValue(42.0d)); Arrays.stream(new ComponentDto[] {project1}).forEach(p -> userSession.addProjectPermission(UserRole.USER, p)); SearchWsResponse result = call(asList(project1.getDbKey(), project2.getDbKey()), singletonList(metric.getKey())); assertThat(result.getMeasuresList()).extracting(Measure::getComponent).containsOnly(project1.getDbKey()); }