@SafeVarargs public final LiveMeasureDto insertLiveMeasure(ComponentDto component, MetricDto metric, Consumer<LiveMeasureDto>... consumers) { LiveMeasureDto dto = newLiveMeasure(component, metric); Arrays.stream(consumers).forEach(c -> c.accept(dto)); dbClient.liveMeasureDao().insert(dbSession, dto); dbSession.commit(); return dto; }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure(); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList("_missing_"), singletonList(measure.getMetricId())); assertThat(selected).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList("_missing_"), singletonList(metric.getKey())); assertThat(selected).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure.getComponentUuid()), singletonList("_other_")); assertThat(selected).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); int otherMetricId = metric.getId() + 100; List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(measure.getComponentUuid()), singletonList(otherMetricId)); assertThat(selected).isEmpty(); }
@Test public void deleteByComponentUuidExcludingMetricIds_with_empty_metrics() { LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricId(1); LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricId(2); LiveMeasureDto measureOnOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricId(2); underTest.insertOrUpdate(db.getSession(), measure1); underTest.insertOrUpdate(db.getSession(), measure2); underTest.insertOrUpdate(db.getSession(), measureOnOtherComponent); int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Collections.emptyList()); assertThat(count).isEqualTo(2); verifyTableSize(1); verifyPersisted(measureOnOtherComponent); }
@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 selectByComponentUuidAndMetricKey() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); Optional<LiveMeasureDto> selected = underTest.selectByComponentUuidAndMetricKey(db.getSession(), measure.getComponentUuid(), metric.getKey()); assertThat(selected).isNotEmpty(); assertThat(selected.get()).isEqualToComparingFieldByField(measure); }
@Test public void selectByComponentUuidAndMetricKey_return_empty_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); assertThat(underTest.selectByComponentUuidAndMetricKey(db.getSession(), measure.getComponentUuid(), "_missing_")).isEmpty(); }
@Test public void upsert_does_not_update_row_if_values_are_not_changed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure(); underTest.upsert(db.getSession(), dto); // update int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(0); verifyPersisted(dto); verifyTableSize(1); }
@Test public void selectByComponentUuidAndMetricKey_return_empty_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); assertThat(underTest.selectByComponentUuidAndMetricKey(db.getSession(), "_missing_", metric.getKey())).isEmpty(); }
@Test public void upsert_updates_row_if_variation_is_removed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setVariation(40.0); underTest.upsert(db.getSession(), dto); // update dto.setVariation(null); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_variation_is_added() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setVariation(null); underTest.upsert(db.getSession(), dto); // update dto.setVariation(40.0); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_value_is_changed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setValue(40.0); underTest.upsert(db.getSession(), dto); // update dto.setValue(50.0); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_variation_is_changed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setVariation(40.0); underTest.upsert(db.getSession(), dto); // update dto.setVariation(50.0); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_value_is_removed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setValue(40.0); underTest.upsert(db.getSession(), dto); // update dto.setValue(null); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_value_is_added() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setValue(null); underTest.upsert(db.getSession(), dto); // update dto.setValue(40.0); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
private LiveMeasureDto insertMeasure(String componentUuid, String projectUuid, Metric metric) { LiveMeasureDto measure = newLiveMeasure() .setComponentUuid(componentUuid) .setProjectUuid(projectUuid) .setMetricId(metricRepository.getByKey(metric.getKey()).getId()); dbClient.liveMeasureDao().insertOrUpdate(db.getSession(), measure); return measure; }
@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"); }
private void addQualityProfiles(ComponentDto project, QualityProfile... qps) { MetricDto metric = newMetricDto().setKey(QUALITY_PROFILES_KEY); dbClient.metricDao().insert(db.getSession(), metric); dbClient.liveMeasureDao().insert(db.getSession(), newLiveMeasure(project, metric) .setData(qualityProfilesToJson(qps))); db.commit(); }