@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 delete_by_metric_id() { UserDto user = db.users().insertUser(); ComponentDto project = db.components().insertPrivateProject(); MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true)); CustomMeasureDto measure = db.measures().insertCustomMeasure(user, project, metric); underTest.deleteByMetricIds(session, singletonList(measure.getMetricId())); assertThat(underTest.selectById(session, measure.getId())).isNull(); }
@Test public void fail_if_more_than_100_project_keys() { List<String> keys = IntStream.rangeClosed(1, 101) .mapToObj(i -> db.components().insertPrivateProject()) .map(ComponentDto::getDbKey) .collect(Collectors.toList()); MetricDto metric = db.measures().insertMetric(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("101 projects provided, more than maximum authorized (100)"); call(keys, singletonList(metric.getKey())); }
private void insertMeasure(double value, ComponentDto componentDto, MetricDto metric) { db.measures().insertLiveMeasure(componentDto, metric, m -> m.setValue(value)); }
@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")); ComponentDto file = db.components().insertComponent(newFileDto(branch)); SnapshotDto analysis = db.components().insertSnapshot(branch); MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d)); SearchHistoryResponse result = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) .setParam(PARAM_BRANCH, "my_branch") .setParam(PARAM_METRICS, "ncloc") .executeProtobuf(SearchHistoryResponse.class); assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(1); HistoryMeasure historyMeasure = result.getMeasures(0); assertThat(historyMeasure.getMetric()).isEqualTo(nclocMetric.getKey()); assertThat(historyMeasure.getHistoryList()) .extracting(m -> parseDouble(m.getValue())) .containsExactlyInAnyOrder(measure.getValue()); }
@Test public void selectOrganizationsWithNcloc_with_not_existing_uuid() { MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(CoreMetrics.NCLOC_KEY)); OrganizationDto org1 = db.organizations().insert(); assertThat(underTest.selectOrganizationsWithNcloc(dbSession, Lists.newArrayList("xxxx"))) .isEmpty(); }
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 pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); SnapshotDto analysis = db.components().insertSnapshot(branch); MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d)); SearchHistoryResponse result = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) .setParam(PARAM_PULL_REQUEST, "pr-123") .setParam(PARAM_METRICS, "ncloc") .executeProtobuf(SearchHistoryResponse.class); assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(1); HistoryMeasure historyMeasure = result.getMeasures(0); assertThat(historyMeasure.getMetric()).isEqualTo(nclocMetric.getKey()); assertThat(historyMeasure.getHistoryList()) .extracting(m -> parseDouble(m.getValue())) .containsExactlyInAnyOrder(measure.getValue()); }
@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 select_by_metric_key_and_text_value() { UserDto user = db.users().insertUser(); ComponentDto project = db.components().insertPrivateProject(); MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true)); CustomMeasureDto customMeasure1 = db.measures().insertCustomMeasure(user, project, metric, m -> m.setTextValue("value")); CustomMeasureDto customMeasure2 = db.measures().insertCustomMeasure(user, project, metric, m -> m.setTextValue("value")); CustomMeasureDto customMeasure3 = db.measures().insertCustomMeasure(user, project, metric, m -> m.setTextValue("other value")); assertThat(underTest.selectByMetricKeyAndTextValue(session, metric.getKey(), "value")) .extracting(CustomMeasureDto::getId) .containsExactlyInAnyOrder(customMeasure1.getId(), customMeasure2.getId()) .doesNotContain(customMeasure3.getId()); assertThat(underTest.selectByMetricKeyAndTextValue(session, metric.getKey(), "unknown")).isEmpty(); assertThat(underTest.selectByMetricKeyAndTextValue(session, "unknown", "value")).isEmpty(); } }
@Test public void insert_data() { byte[] data = "text_value".getBytes(StandardCharsets.UTF_8); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); LiveMeasureDto measure = newLiveMeasure(file, metric).setData(data); underTest.insert(db.getSession(), measure); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(new String(result.getData(), StandardCharsets.UTF_8)).isEqualTo("text_value"); assertThat(result.getDataAsString()).isEqualTo("text_value"); }
@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 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 update() { UserDto user = db.users().insertUser(); ComponentDto project = db.components().insertPrivateProject(); MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true)); CustomMeasureDto measure = db.measures().insertCustomMeasure(user, project, metric, m -> m.setDescription("old-description")); underTest.update(session, measure.setDescription("new-description")); assertThat(underTest.selectById(session, measure.getId()).getDescription()).isEqualTo("new-description"); }
@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()).isEqualTo(1); assertThat(response.getPeriods().getPeriodsCount()).isEqualTo(0); assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey()); }
@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 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 delete() { UserDto user = db.users().insertUser(); ComponentDto project = db.components().insertPrivateProject(); MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true)); CustomMeasureDto measure = db.measures().insertCustomMeasure(user, project, metric); underTest.delete(session, measure.getId()); assertThat(underTest.selectById(session, measure.getId())).isNull(); }
private MetricDto insertMetric() { return db.measures().insertMetric(m -> m .setValueType(INT.name()) .setHidden(false) .setDirection(0)); } }
@Test public void event_contains_newQualityGate_computed_by_LiveQualityGateComputer() { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData(Metric.Level.ERROR.name())); db.measures().insertLiveMeasure(project, intMetric, m -> m.setVariation(42.0).setValue(null)); BranchDto branch = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), project.projectUuid(), "master") .orElseThrow(() -> new IllegalStateException("Can't find master branch")); List<QGChangeEvent> result = run(file1, newQualifierBasedIntLeakFormula()); assertThat(result) .extracting(QGChangeEvent::getQualityGateSupplier) .extracting(Supplier::get) .containsExactly(Optional.of(newQualityGate)); verify(qGateComputer).loadQualityGate(any(DbSession.class), eq(organization), eq(project), eq(branch)); verify(qGateComputer).getMetricsRelatedTo(qualityGate); verify(qGateComputer).refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class)); }