@CheckForNull private String findDataFromComponent(DbSession dbSession, ComponentDto component) { return dbClient.liveMeasureDao().selectMeasure(dbSession, component.uuid(), CoreMetrics.DUPLICATIONS_DATA_KEY) .map(LiveMeasureDto::getDataAsString) .orElse(null); } }
private Level getQualityGate(DbSession dbSession, ComponentDto project) { return Level.valueOf(dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), ALERT_STATUS_KEY) .map(LiveMeasureDto::getTextValue) .orElseThrow(() -> new ProjectBadgesException("Quality gate has not been found"))); }
private LiveMeasureDto getMeasure(DbSession dbSession, ComponentDto project, String metricKey) { return dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), metricKey) .orElseThrow(() -> new ProjectBadgesException("Measure has not been found")); }
private Optional<String> loadQualityGateDetails(DbSession dbSession, ProjectAndSnapshot projectAndSnapshot, boolean onAnalysis) { if (onAnalysis) { if (!projectAndSnapshot.snapshotDto.isPresent()) { return Optional.empty(); } // get the gate status as it was computed during the specified analysis String analysisUuid = projectAndSnapshot.snapshotDto.get().getUuid(); return dbClient.measureDao().selectMeasure(dbSession, analysisUuid, projectAndSnapshot.project.projectUuid(), CoreMetrics.QUALITY_GATE_DETAILS_KEY) .map(MeasureDto::getData); } // do not restrict to a specified analysis, use the live measure Optional<LiveMeasureDto> measure = dbClient.liveMeasureDao().selectMeasure(dbSession, projectAndSnapshot.project.projectUuid(), CoreMetrics.QUALITY_GATE_DETAILS_KEY); return measure.map(LiveMeasureDto::getDataAsString); }
private void writeProfiles(JsonWriter json, DbSession dbSession, ComponentDto component) { Set<QualityProfile> qualityProfiles = dbClient.liveMeasureDao().selectMeasure(dbSession, component.projectUuid(), QUALITY_PROFILES_KEY) .map(LiveMeasureDto::getDataAsString) .map(data -> QPMeasureData.fromJson(data).getProfiles()) .orElse(emptySortedSet()); Map<String, QProfileDto> dtoByQPKey = dbClient.qualityProfileDao().selectByUuids(dbSession, qualityProfiles.stream().map(QualityProfile::getQpKey).collect(Collectors.toList())) .stream() .collect(uniqueIndex(QProfileDto::getKee)); json.name("qualityProfiles").beginArray(); qualityProfiles.forEach(qp -> writeToJson(json, qp, !dtoByQPKey.containsKey(qp.getQpKey()))); json.endArray(); }
private void completeStatistics(List<SecurityStandardCategoryStatistics> input, ComponentDto project, String standard, boolean includeCwe) { try (DbSession dbSession = dbClient.openSession(false)) { Set<QualityProfile> qualityProfiles = dbClient.liveMeasureDao().selectMeasure(dbSession, project.projectUuid(), QUALITY_PROFILES_KEY) .map(LiveMeasureDto::getDataAsString) .map(data -> QPMeasureData.fromJson(data).getProfiles())
private void assertThatMeasureHasValue(String componentUuid, Metric metric, int expectedValue) { Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(), componentUuid, metric.getKey()); assertThat(persisted).isPresent(); assertThat(persisted.get().getValue()).isEqualTo((double) expectedValue); }
private Optional<LiveMeasureDto> selectMeasure(String componentUuid, Metric metric) { return dbClient.liveMeasureDao().selectMeasure(db.getSession(), componentUuid, metric.getKey()); }
private void assertThatRatingMeasureHasLeakValue(ComponentDto component, Rating expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), ratingMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getVariation()).isEqualTo((double) expectedValue.getIndex()); }
private void assertThatIntMeasureHasLeakValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isNull(); assertThat(measure.getVariation()).isEqualTo(expectedValue); }
private LiveMeasureDto assertThatIntMeasureHasValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedValue); return measure; }
private LiveMeasureDto assertThatRatingMeasureHasValue(ComponentDto component, Rating expectedRating) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), ratingMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedRating.getIndex()); assertThat(measure.getDataAsString()).isEqualTo(expectedRating.name()); return measure; }
private void assertThatMeasureHasValue(LiveMeasureDto template, int expectedValue) { Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(), template.getComponentUuid(), metricRepository.getById(template.getMetricId()).getKey()); assertThat(persisted).isPresent(); assertThat(persisted.get().getValue()).isEqualTo((double) expectedValue); }
@Test public void selectMeasure() { MetricDto metric = db.measures().insertMetric(); LiveMeasureDto stored = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), stored); // metric exists but not component assertThat(underTest.selectMeasure(db.getSession(), "_missing_", metric.getKey())).isEmpty(); // component exists but not metric assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), "_missing_")).isEmpty(); // component and metric don't match assertThat(underTest.selectMeasure(db.getSession(), "_missing_", "_missing_")).isEmpty(); // matches assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), metric.getKey()).get()) .isEqualToComparingFieldByField(stored); }
@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"); }
private void assertThatMeasureDoesNotExist(LiveMeasureDto template) { assertThat(dbClient.liveMeasureDao().selectMeasure(db.getSession(), template.getComponentUuid(), metricRepository.getById(template.getMetricId()).getKey())) .isEmpty(); }
@Test public void selectMeasure_map_fields() { MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14).setVariation(0.1).setData("text_value")); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(result).as("Fail to map fields of %s", result.toString()).extracting( LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getVariation, LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue) .contains(project.uuid(), file.uuid(), metric.getId(), 3.14, 0.1, "text_value", "text_value"); }
@CheckForNull private String findDataFromComponent(DbSession dbSession, ComponentDto component) { return dbClient.liveMeasureDao().selectMeasure(dbSession, component.uuid(), CoreMetrics.DUPLICATIONS_DATA_KEY) .map(LiveMeasureDto::getDataAsString) .orElse(null); } }
private Level getQualityGate(DbSession dbSession, ComponentDto project) { return Level.valueOf(dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), ALERT_STATUS_KEY) .map(LiveMeasureDto::getTextValue) .orElseThrow(() -> new ProjectBadgesException("Quality gate has not been found"))); }
private void writeProfiles(JsonWriter json, DbSession dbSession, ComponentDto component) { json.name("qualityProfiles").beginArray(); dbClient.liveMeasureDao().selectMeasure(dbSession, component.projectUuid(), QUALITY_PROFILES_KEY) .map(LiveMeasureDto::getDataAsString) .ifPresent(data -> QPMeasureData.fromJson(data).getProfiles().forEach(writeToJson(json))); json.endArray(); }