diff --git a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java index 483ecde59e..9c053a2eb8 100644 --- a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java +++ b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.sql.Connection; -import java.sql.Date; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -19,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -70,7 +69,7 @@ protected record ExternalEvent(String key, String event_type_name, String source } protected record ExternalSource(String key, String source_type_name, String derivation_group_name, String valid_at, String start_time, String end_time, String created_at){} protected record DerivedEvent(String key, String event_type_name, String source_key, String derivation_group_name, String start_time, String duration, String source_range, String valid_at){} - protected record PlanDerivationGroup(int plan_id, String derivation_group_name, boolean acknowledged){} + protected record PlanDerivationGroup(int plan_id, String derivation_group_name, boolean acknowledged, String last_acknowledged_at){} //endregion @@ -2108,9 +2107,45 @@ void deleteEventTypeWithRemainingEvent() throws SQLException { @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) class PlanDerivationGroupTests { + List getPlanDerivationGroupAssociations() throws SQLException { + List results = new ArrayList<>(); + + try (final var statement = connection.createStatement()) { + // create the event type + final var res = statement.executeQuery( + // language=sql + """ + SELECT * FROM merlin.plan_derivation_group ORDER BY plan_id, derivation_group_name; + """ + ); + + while (res.next()) { + results.add( + new PlanDerivationGroup( + res.getInt("plan_id"), + res.getString("derivation_group_name"), + res.getBoolean("acknowledged"), + res.getString("last_acknowledged_at") + ) + ); + } + } + + return results; + } + + void assertEqualsAsideFromLastAcknowledged(List expected, List actual) { + assertEquals(expected.size(), actual.size()); + for (int i = 0; i < actual.size(); i++) { + assertEquals(expected.get(i).plan_id(), actual.get(i).plan_id()); + assertEquals(expected.get(i).derivation_group_name(), actual.get(i).derivation_group_name()); + assertEquals(expected.get(i).acknowledged(), actual.get(i).acknowledged()); + } + } + /** * This test ensures that a derivation group can be associated with a plan, specifically checking that when the - * "acknowledged" field is set, "last_acknowledged_at" is updated, but no other derivation groups or plans + * "acknowledged" field is set, "last_acknowledged_at" is not updated, but no other derivation groups or plans * (and their associations are affected) */ @Test @@ -2134,36 +2169,16 @@ void associateDerivationGroupWithBasePlan() throws SQLException { assertDoesNotThrow(() -> associateDerivationGroupWithPlan(planIdDelta, derivationGroupNameControl)); assertDoesNotThrow(() -> associateDerivationGroupWithPlan(planIdDelta, derivationGroupNameDelta)); - // check that acknowledged is true for all entries - try (final var statement = connection.createStatement()) { - // create the event type - var res = statement.executeQuery( - // language=sql - """ - SELECT * FROM merlin.plan_derivation_group; - """ - ); - - List results = new ArrayList<>(); - while (res.next()) { - results.add( - new PlanDerivationGroup( - res.getInt("plan_id"), - res.getString("derivation_group_name"), - res.getBoolean("acknowledged") - ) - ); - } + // check that acknowledged is true for all entries, and save the last_acknowledged_at field results + List expectedResultsInitial = List.of( + new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true, ""), + new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true, ""), + new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, true, "") + ); + final List actualResultsInitial = getPlanDerivationGroupAssociations(); - List expectedResults = List.of( - new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true), - new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true), - new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, true) - ); - - assertEquals(expectedResults.size(), results.size()); - assertTrue(results.containsAll(expectedResults)); - } + // first, check other properties + assertEqualsAsideFromLastAcknowledged(expectedResultsInitial, actualResultsInitial); // insert a source to the changing derivation group insertExternalSource( @@ -2179,35 +2194,46 @@ void associateDerivationGroupWithBasePlan() throws SQLException { ); // check that acknowledged is now false for all non control (delta only) entries, true otherwise + List expectedResults = List.of( + new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true, actualResultsInitial.getFirst() + .last_acknowledged_at()), + new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true, actualResultsInitial.get(1) + .last_acknowledged_at()), + new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, false, actualResultsInitial.get(2) + .last_acknowledged_at()) + ); + final List actualResults = getPlanDerivationGroupAssociations(); + + // first, check other properties + assertEquals(expectedResults.size(), actualResults.size()); + assertTrue(actualResults.containsAll(expectedResults)); + + // final bit - update acknowledged to true for (planIdDelta, derivationGroupNameDelta) pair, see that + // last_acknowledged_at updates try (final var statement = connection.createStatement()) { - // create the event type - var res = statement.executeQuery( + // update acknowledged field for (planIdDelta, derivationGroupNameDelta) pair + statement.executeUpdate( // language=sql """ - SELECT * FROM merlin.plan_derivation_group; - """ + UPDATE merlin.plan_derivation_group + SET acknowledged = true + WHERE (plan_id, derivation_group_name) = (%d, '%s'); + """.formatted(planIdDelta, derivationGroupNameDelta) ); + } - List results = new ArrayList<>(); - while (res.next()) { - results.add( - new PlanDerivationGroup( - res.getInt("plan_id"), - res.getString("derivation_group_name"), - res.getBoolean("acknowledged") - ) - ); - } + final List postUpdateResults = getPlanDerivationGroupAssociations(); - List expectedResults = List.of( - new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true), - new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true), - new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, false) - ); + // check timestamps + assertEquals(postUpdateResults.getFirst(), actualResultsInitial.getFirst()); + assertEquals(postUpdateResults.get(1), actualResultsInitial.get(1)); - assertEquals(expectedResults.size(), results.size()); - assertTrue(results.containsAll(expectedResults)); - } + // comparing strings. The post update last_acknowledged_at should be later than the pre update one. + assertTrue(postUpdateResults.get(2).last_acknowledged_at().compareTo( + actualResultsInitial.get(2).last_acknowledged_at()) > 0); + + // check the other properties + assertEqualsAsideFromLastAcknowledged(actualResultsInitial, postUpdateResults); } } //endregion