From bc30c6aefbf2bb5ceafffc8f15824bb3e5fd63b5 Mon Sep 17 00:00:00 2001 From: Tristan Vuong Date: Thu, 18 Jan 2024 16:44:01 +0000 Subject: [PATCH 1/2] Fix Create Report Schedule Iterations implementation --- .../writers/CreateReportScheduleIteration.kt | 17 +++++- .../testing/v2/ReportSchedulesServiceTest.kt | 55 ++++++++++++++++++- .../PostgresReportSchedulesServiceTest.kt | 1 + 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt b/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt index 2933e43b598..a6b93421474 100644 --- a/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt +++ b/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt @@ -82,7 +82,22 @@ class CreateReportScheduleIteration(private val reportScheduleIteration: ReportS bind("$10", createTime) } - transactionContext.run { executeStatement(statement) } + val reportScheduleUpdateStatement = + boundStatement( + """ + UPDATE ReportSchedules SET LatestReportScheduleIterationId = $1 + WHERE MeasurementConsumerId = $2 AND ReportScheduleId = $3 + """.trimIndent() + ) { + bind("$1", reportScheduleIterationId) + bind("$2", reportScheduleResult.measurementConsumerId) + bind("$3", reportScheduleResult.reportScheduleId) + } + + transactionContext.run { + executeStatement(statement) + executeStatement(reportScheduleUpdateStatement) + } return reportScheduleIteration.copy { this.externalReportScheduleIterationId = externalReportScheduleIterationId diff --git a/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt b/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt index 4d35849a6af..9020bf5a368 100644 --- a/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt +++ b/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt @@ -39,6 +39,7 @@ import org.wfanet.measurement.internal.reporting.v2.ListReportSchedulesRequestKt import org.wfanet.measurement.internal.reporting.v2.MeasurementConsumersGrpcKt.MeasurementConsumersCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.MetricCalculationSpec import org.wfanet.measurement.internal.reporting.v2.MetricCalculationSpecsGrpcKt.MetricCalculationSpecsCoroutineImplBase +import org.wfanet.measurement.internal.reporting.v2.ReportScheduleIterationsGrpcKt.ReportScheduleIterationsCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.ReportKt import org.wfanet.measurement.internal.reporting.v2.ReportSchedule import org.wfanet.measurement.internal.reporting.v2.ReportScheduleKt @@ -51,6 +52,7 @@ import org.wfanet.measurement.internal.reporting.v2.getReportScheduleRequest import org.wfanet.measurement.internal.reporting.v2.listReportSchedulesRequest import org.wfanet.measurement.internal.reporting.v2.report import org.wfanet.measurement.internal.reporting.v2.reportSchedule +import org.wfanet.measurement.internal.reporting.v2.reportScheduleIteration import org.wfanet.measurement.internal.reporting.v2.stopReportScheduleRequest @RunWith(JUnit4::class) @@ -61,7 +63,8 @@ abstract class ReportSchedulesServiceTest val reportSchedulesService: T, val measurementConsumersService: MeasurementConsumersCoroutineImplBase, val reportingSetsService: ReportingSetsCoroutineImplBase, - val metricCalculationSpecsService: MetricCalculationSpecsCoroutineImplBase + val metricCalculationSpecsService: MetricCalculationSpecsCoroutineImplBase, + val reportScheduleIterationsService: ReportScheduleIterationsCoroutineImplBase, ) /** Instance of the service under test. */ @@ -70,6 +73,7 @@ abstract class ReportSchedulesServiceTest private lateinit var measurementConsumersService: MeasurementConsumersCoroutineImplBase private lateinit var reportingSetsService: ReportingSetsCoroutineImplBase private lateinit var metricCalculationSpecsService: MetricCalculationSpecsCoroutineImplBase + private lateinit var reportScheduleIterationsService: ReportScheduleIterationsCoroutineImplBase /** Constructs the services being tested. */ protected abstract fun newServices(idGenerator: IdGenerator): Services @@ -81,6 +85,7 @@ abstract class ReportSchedulesServiceTest measurementConsumersService = services.measurementConsumersService reportingSetsService = services.reportingSetsService metricCalculationSpecsService = services.metricCalculationSpecsService + reportScheduleIterationsService = services.reportScheduleIterationsService } @Test @@ -496,6 +501,54 @@ abstract class ReportSchedulesServiceTest .isEqualTo(reportSchedule) } + @Test + fun `getReportSchedule succeeds after report schedules iteration is created`() = runBlocking { + createMeasurementConsumer(CMMS_MEASUREMENT_CONSUMER_ID, measurementConsumersService) + val reportingSet = createReportingSet(CMMS_MEASUREMENT_CONSUMER_ID, reportingSetsService) + val metricCalculationSpec = + createMetricCalculationSpec(CMMS_MEASUREMENT_CONSUMER_ID, metricCalculationSpecsService) + val reportSchedule = createReportScheduleForRequest(reportingSet, metricCalculationSpec) + val createRequest = createReportScheduleRequest { + this.reportSchedule = reportSchedule + externalReportScheduleId = "external-report-schedule-id" + } + val createdReportSchedule = service.createReportSchedule(createRequest) + + val createdReportScheduleIteration = reportScheduleIterationsService.createReportScheduleIteration( + reportScheduleIteration { + cmmsMeasurementConsumerId = CMMS_MEASUREMENT_CONSUMER_ID + externalReportScheduleId = createdReportSchedule.externalReportScheduleId + createReportRequestId = "123" + reportEventTime = timestamp { seconds = 100 } + } + ) + + val retrievedReportSchedule = + service.getReportSchedule( + getReportScheduleRequest { + cmmsMeasurementConsumerId = CMMS_MEASUREMENT_CONSUMER_ID + externalReportScheduleId = createdReportSchedule.externalReportScheduleId + } + ) + + assertThat(retrievedReportSchedule.externalReportScheduleId) + .isEqualTo(createRequest.externalReportScheduleId) + assertThat(retrievedReportSchedule.createTime.seconds).isGreaterThan(0) + assertThat(retrievedReportSchedule.updateTime.seconds).isGreaterThan(0) + assertThat(retrievedReportSchedule.createTime).isEqualTo(retrievedReportSchedule.updateTime) + assertThat(retrievedReportSchedule.state).isEqualTo(ReportSchedule.State.ACTIVE) + assertThat(retrievedReportSchedule) + .ignoringFields( + ReportSchedule.CREATE_TIME_FIELD_NUMBER, + ReportSchedule.UPDATE_TIME_FIELD_NUMBER, + ReportSchedule.EXTERNAL_REPORT_SCHEDULE_ID_FIELD_NUMBER, + ReportSchedule.STATE_FIELD_NUMBER + ) + .isEqualTo(reportSchedule.copy { + latestIteration = createdReportScheduleIteration + }) + } + @Test fun `getReportSchedule throws NOT_FOUND when report schedule not found`() = runBlocking { createMeasurementConsumer(CMMS_MEASUREMENT_CONSUMER_ID, measurementConsumersService) diff --git a/src/test/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/PostgresReportSchedulesServiceTest.kt b/src/test/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/PostgresReportSchedulesServiceTest.kt index 1d9d4219791..b706d9e9091 100644 --- a/src/test/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/PostgresReportSchedulesServiceTest.kt +++ b/src/test/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/PostgresReportSchedulesServiceTest.kt @@ -37,6 +37,7 @@ class PostgresReportSchedulesServiceTest : PostgresMeasurementConsumersService(idGenerator, client), PostgresReportingSetsService(idGenerator, client), PostgresMetricCalculationSpecsService(idGenerator, client), + PostgresReportScheduleIterationsService(idGenerator, client), ) } From 0f6a8459af2e2dd6b2d6fa17b29ed28bcfdbc0c9 Mon Sep 17 00:00:00 2001 From: Tristan Vuong Date: Thu, 18 Jan 2024 16:48:22 +0000 Subject: [PATCH 2/2] lint fix --- .../writers/CreateReportScheduleIteration.kt | 3 ++- .../testing/v2/ReportSchedulesServiceTest.kt | 23 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt b/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt index a6b93421474..201da66a9c1 100644 --- a/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt +++ b/src/main/kotlin/org/wfanet/measurement/reporting/deploy/v2/postgres/writers/CreateReportScheduleIteration.kt @@ -87,7 +87,8 @@ class CreateReportScheduleIteration(private val reportScheduleIteration: ReportS """ UPDATE ReportSchedules SET LatestReportScheduleIterationId = $1 WHERE MeasurementConsumerId = $2 AND ReportScheduleId = $3 - """.trimIndent() + """ + .trimIndent() ) { bind("$1", reportScheduleIterationId) bind("$2", reportScheduleResult.measurementConsumerId) diff --git a/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt b/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt index 9020bf5a368..9d18f063c12 100644 --- a/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt +++ b/src/main/kotlin/org/wfanet/measurement/reporting/service/internal/testing/v2/ReportSchedulesServiceTest.kt @@ -39,9 +39,9 @@ import org.wfanet.measurement.internal.reporting.v2.ListReportSchedulesRequestKt import org.wfanet.measurement.internal.reporting.v2.MeasurementConsumersGrpcKt.MeasurementConsumersCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.MetricCalculationSpec import org.wfanet.measurement.internal.reporting.v2.MetricCalculationSpecsGrpcKt.MetricCalculationSpecsCoroutineImplBase -import org.wfanet.measurement.internal.reporting.v2.ReportScheduleIterationsGrpcKt.ReportScheduleIterationsCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.ReportKt import org.wfanet.measurement.internal.reporting.v2.ReportSchedule +import org.wfanet.measurement.internal.reporting.v2.ReportScheduleIterationsGrpcKt.ReportScheduleIterationsCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.ReportScheduleKt import org.wfanet.measurement.internal.reporting.v2.ReportSchedulesGrpcKt.ReportSchedulesCoroutineImplBase import org.wfanet.measurement.internal.reporting.v2.ReportingSet @@ -514,14 +514,15 @@ abstract class ReportSchedulesServiceTest } val createdReportSchedule = service.createReportSchedule(createRequest) - val createdReportScheduleIteration = reportScheduleIterationsService.createReportScheduleIteration( - reportScheduleIteration { - cmmsMeasurementConsumerId = CMMS_MEASUREMENT_CONSUMER_ID - externalReportScheduleId = createdReportSchedule.externalReportScheduleId - createReportRequestId = "123" - reportEventTime = timestamp { seconds = 100 } - } - ) + val createdReportScheduleIteration = + reportScheduleIterationsService.createReportScheduleIteration( + reportScheduleIteration { + cmmsMeasurementConsumerId = CMMS_MEASUREMENT_CONSUMER_ID + externalReportScheduleId = createdReportSchedule.externalReportScheduleId + createReportRequestId = "123" + reportEventTime = timestamp { seconds = 100 } + } + ) val retrievedReportSchedule = service.getReportSchedule( @@ -544,9 +545,7 @@ abstract class ReportSchedulesServiceTest ReportSchedule.EXTERNAL_REPORT_SCHEDULE_ID_FIELD_NUMBER, ReportSchedule.STATE_FIELD_NUMBER ) - .isEqualTo(reportSchedule.copy { - latestIteration = createdReportScheduleIteration - }) + .isEqualTo(reportSchedule.copy { latestIteration = createdReportScheduleIteration }) } @Test