private ComponentDto insertProject(OrganizationDto organizationDto, Consumer<ComponentDto> projectConsumer, Measure... measures) { ComponentDto project = db.components().insertPublicProject(organizationDto, projectConsumer); Arrays.stream(measures).forEach(m -> db.measures().insertLiveMeasure(project, m.metric, m.consumer)); authorizationIndexerTester.allowOnlyAnyone(project); projectMeasuresIndexer.indexOnAnalysis(project.uuid()); return project; }
@Test public void event_contains_no_previousStatus_if_measure_exists_and_is_not_a_level() { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData("fooBar")); List<QGChangeEvent> result = run(file1); assertThat(result) .extracting(QGChangeEvent::getPreviousStatus) .containsExactly(Optional.empty()); }
@Test public void event_contains_no_previousStatus_if_measure_exists_and_has_no_value() { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData((String) null)); List<QGChangeEvent> result = run(file1); assertThat(result) .extracting(QGChangeEvent::getPreviousStatus) .containsExactly(Optional.empty()); }
@Test public void event_contains_no_previousStatus_if_measure_exists_and_is_empty() { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData("")); List<QGChangeEvent> result = run(file1); assertThat(result) .extracting(QGChangeEvent::getPreviousStatus) .containsExactly(Optional.empty()); }
@Test @UseDataProvider("metricLevels") public void event_contains_previousStatus_if_measure_exists(Metric.Level level) { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData(level.name())); db.measures().insertLiveMeasure(project, intMetric, m -> m.setVariation(42.0).setValue(null)); List<QGChangeEvent> result = run(file1, newQualifierBasedIntLeakFormula()); assertThat(result) .extracting(QGChangeEvent::getPreviousStatus) .containsExactly(Optional.of(level)); }
@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 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()); }
@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(); }
@Test public void non_main_branches_are_not_indexed() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(10d)); ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); dbTester.measures().insertLiveMeasure(branch, metric, m -> m.setValue(20d)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById).hasSize(1).containsOnlyKeys(project.uuid()); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures().get(metric.getKey())).isEqualTo(10d); }
private void setNcloc(double ncloc) { ComponentDto project = db.components().insertMainBranch(); MetricDto nclocMetric = db.measures().insertMetric(m -> m.setValueType(INT.toString()).setKey(NCLOC_KEY)); db.measures().insertLiveMeasure(project, nclocMetric, m -> m.setValue(ncloc)); } }
@Test public void ignore_numeric_measure_that_has_text_value_but_not_numeric_value() { OrganizationDto organization = dbTester.organizations().insert(); MetricDto metric1 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("coverage")); MetricDto metric2 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc")); ComponentDto project = dbTester.components().insertPrivateProject(organization); dbTester.measures().insertLiveMeasure(project, metric1, m -> m.setValue(10d).setData((String) null)); dbTester.measures().insertLiveMeasure(project, metric2, m -> m.setValue(null).setData("foo")); Map<String, Double> numericMeasures = createResultSetAndReturnDocsById().get(project.uuid()).getMeasures().getNumericMeasures(); assertThat(numericMeasures).containsOnly(entry("coverage", 10d)); }
@Test public void does_not_fail_when_quality_gate_has_no_value() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(LEVEL.name()).setKey("alert_status")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(null).setVariation(null).setData((String) null)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures()).isEmpty(); }
@Test public void return_project_measure_having_leak() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization, p -> p.setDbKey("Project-Key").setName("Project Name").setTagsString("platform,java")); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("new_lines")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setVariation(10d)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures()).containsOnly(entry("new_lines", 10d)); }
@Test public void return_language_distribution_measure() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(DATA.name()).setKey("ncloc_language_distribution")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(null).setData("<null>=2;java=6;xoo=18")); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages()) .containsOnly(entry("<null>", 2), entry("java", 6), entry("xoo", 18)); }
@Test public void quality_gate_passed() { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(OK.name())); TestResponse response = ws.newRequest() .setParam("project", project.getKey()) .execute(); checkResponse(response, OK); }
@Test public void quality_gate_failed() { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(ERROR.name())); TestResponse response = ws.newRequest() .setParam("project", project.getKey()) .execute(); checkResponse(response, ERROR); }
@Test public void fail_on_invalid_quality_gate() { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData("UNKNOWN")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("No enum constant org.sonar.api.measures.Metric.Level.UNKNOWN"); ws.newRequest() .setParam("project", project.getKey()) .execute(); }
@Test public void display_deprecated_warning_quality_gate() { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(WARN.name())); TestResponse response = ws.newRequest() .setParam("project", project.getKey()) .setParam("metric", metric.getKey()) .execute(); checkSvg(response, "quality gate", "warning", QUALITY_GATE_WARN); }
@Test public void quality_gate_on_application() { OrganizationDto organization = db.organizations().insert(); ComponentDto application = db.components().insertPublicApplication(organization); userSession.registerComponents(application); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(application, metric, m -> m.setData(ERROR.name())); TestResponse response = ws.newRequest() .setParam("project", application.getKey()) .execute(); checkResponse(response, ERROR); }
@Test public void return_error_if_measure_value_is_null() throws ParseException { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setValue(null).setData((String) null)); TestResponse response = ws.newRequest() .setParam("project", project.getKey()) .setParam("metric", metric.getKey()) .execute(); checkError(response, "Quality gate has not been found"); }