Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-super committed Jan 22, 2025
1 parent 94ec66a commit ab457aa
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gov.nasa.jpl.aerie.e2e.procedural.scheduling.procedures;

import gov.nasa.ammos.aerie.procedural.scheduling.Goal;
import gov.nasa.ammos.aerie.procedural.scheduling.annotations.SchedulingProcedure;
import gov.nasa.ammos.aerie.procedural.scheduling.plan.EditablePlan;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart;
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

@SchedulingProcedure
public record ExternalEventsEventAttributeOptionalQueryGoal() implements Goal {
@Override
public void run(@NotNull final EditablePlan plan) {
// extract all events
for (final var e: plan.events()) {
// filter events that we schedule off of by their attributes
var optionalValue = e.attributes.get("optional");
if (optionalValue != null) {
var optional = optionalValue.asString();
if (optional.isPresent() && optional.get().equals("present")) {
plan.create(
"BiteBanana",
// place the directive such that it is coincident with the event's start
new DirectiveStart.Absolute(e.getInterval().start),
Map.of("biteSize", SerializedValue.of(1)));
}
}
}
plan.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gov.nasa.jpl.aerie.e2e.procedural.scheduling.procedures;

import gov.nasa.ammos.aerie.procedural.scheduling.Goal;
import gov.nasa.ammos.aerie.procedural.scheduling.annotations.SchedulingProcedure;
import gov.nasa.ammos.aerie.procedural.scheduling.plan.EditablePlan;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart;
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

@SchedulingProcedure
public record ExternalEventsSourceAttributeOptionalQueryGoal() implements Goal {
@Override
public void run(@NotNull final EditablePlan plan) {
// extract all events
for (final var e: plan.events()) {
// filter events that we schedule off of by their source's attributes
var optionalValue = e.source.attributes.get("optional");
if (optionalValue != null) {
var optional = optionalValue.asString();
if (optional.isPresent() && optional.get().equals("present")) {
plan.create(
"BiteBanana",
// place the directive such that it is coincident with the event's start
new DirectiveStart.Absolute(e.getInterval().start),
Map.of("biteSize", SerializedValue.of(1)));
}
}
}
plan.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
"properties": {
"version": {
"type": "number"
},
"optional": {
"type": "string"
}
}
},
"required": ["version"]
}
""";
private final static String EVENT_ATTRIBUTE_SCHEMA = """
Expand All @@ -50,8 +54,12 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
},
"code": {
"type": "string"
},
"optional": {
"type": "string"
}
}
},
"required": ["version"]
}
""";

Expand Down Expand Up @@ -94,7 +102,8 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
"""
{
"projectUser": "UserA",
"code": "A"
"code": "A",
"optional": "present"
}
"""
),
Expand All @@ -108,7 +117,8 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
"""
{
"projectUser": "UserB",
"code": "B"
"code": "B",
"optional": "present"
}
"""
)
Expand All @@ -124,7 +134,8 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
"2024-10-01T00:00:00Z",
"""
{
"version": 2
"version": 2,
"optional": "present"
}
"""
);
Expand All @@ -140,7 +151,8 @@ public class ExternalEventsTests extends ProceduralSchedulingSetup {
"""
{
"projectUser": "UserB",
"code": "B"
"code": "B",
"optional": "present"
}
"""
),
Expand Down Expand Up @@ -345,7 +357,6 @@ void testExternalSourceAttribute() throws IOException {
}
}

// TODO: test based on event attributes
@Test
void testExternalEventAttribute() throws IOException {
// first, run the goal
Expand Down Expand Up @@ -385,4 +396,85 @@ void testExternalEventAttribute() throws IOException {
assertEquals(activityStartTime.toString(), expected.get(i).start_time());
}
}

// TODO: test based on source optional attribute
@Test
void testOptionalSourceAttribute() throws IOException {
// first, run the goal
try (final var gateway = new GatewayRequests(playwright)) {
int procedureJarId = gateway.uploadJarFile("build/libs/ExternalEventsSourceAttributeOptionalQueryGoal.jar");
// Add Scheduling Procedure
procedureId = hasura.createSchedulingSpecProcedure(
"Test Scheduling Procedure",
procedureJarId,
specId,
0
);
}
hasura.awaitScheduling(specId);
final var plan = hasura.getPlan(planId);
final var activities = plan.activityDirectives();

// ensure the orderings line up
activities.sort(Comparator.comparing(Plan.ActivityDirective::startOffset));

// get the set of events we expect (select events with projectUser = UserA)
List<HasuraRequests.ExternalEvent> expected = new ArrayList<>();
expected.add(additionalExternalEvents.get(0));
expected.add(additionalExternalEvents.get(1));
expected.add(additionalExternalEvents.get(2));

// explicitly ensure the orderings line up
expected.sort(Comparator.comparing(HasuraRequests.ExternalEvent::start_time));

// compare arrays
assertEquals(expected.size(), activities.size());
for (int i = 0; i < activities.size(); i++) {
Instant activityStartTime = Duration.addToInstant(
Instant.parse(planStartTimestamp),
Duration.fromString(activities.get(i).startOffset())
);
assertEquals(activityStartTime.toString(), expected.get(i).start_time());
}
}

@Test
void testOptionalEventAttribute() throws IOException {
// first, run the goal
try (final var gateway = new GatewayRequests(playwright)) {
int procedureJarId = gateway.uploadJarFile("build/libs/ExternalEventsEventAttributeOptionalQueryGoal.jar");
// Add Scheduling Procedure
procedureId = hasura.createSchedulingSpecProcedure(
"Test Scheduling Procedure",
procedureJarId,
specId,
0
);
}
hasura.awaitScheduling(specId);
final var plan = hasura.getPlan(planId);
final var activities = plan.activityDirectives();

// ensure the orderings line up
activities.sort(Comparator.comparing(Plan.ActivityDirective::startOffset));

// get the set of events we expect (select events with projectUser = UserA)
List<HasuraRequests.ExternalEvent> expected = new ArrayList<>();
expected.add(additionalExternalEvents.getFirst());
expected.add(externalEvents.get(1));
expected.add(externalEvents.get(2));

// explicitly ensure the orderings line up
expected.sort(Comparator.comparing(HasuraRequests.ExternalEvent::start_time));

// compare arrays
assertEquals(expected.size(), activities.size());
for (int i = 0; i < activities.size(); i++) {
Instant activityStartTime = Duration.addToInstant(
Instant.parse(planStartTimestamp),
Duration.fromString(activities.get(i).startOffset())
);
assertEquals(activityStartTime.toString(), expected.get(i).start_time());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1196,19 +1196,11 @@ private List<ExternalEvent> parseExternalEvents(final JsonArray eventsJson, fina
// TODO: all of the properties of eventJson are strings, except attributes. Including things like source_range, etc.,
// that should be objects...find a better way to handle this
final var eventAttributes = SchedulerParsers
.parseJson(e.getJsonObject("attributes").toString(), new SerializedValueJsonParser()).asMap().get();
// convert eventAttributes to a Map<String, SerializedValue)

// final var sourceAttributesJson = e.getJsonObject("external_source").getJsonObject("attributes");
// final var sourceAttributes = new HashMap<String, SerializedValue>();
// for (final var attributeJson: sourceAttributesJson.entrySet()) {
// sourceAttributes.put(
// attributeJson.getKey(),
// serializedValueP.parse(attributeJson.getValue()).getSuccessOrThrow()
// );
// }
.parseJson(e.getString("attributes"), new SerializedValueJsonParser()).asMap().get();
// TODO: convert eventAttributes to a Map<String, SerializedValue>?

final var sourceAttributes = SchedulerParsers
.parseJson(e.getJsonObject("external_source").getJsonObject("attributes").toString(), new SerializedValueJsonParser()).asMap().get();
.parseJson(e.getJsonObject("external_source").getString("attributes"), new SerializedValueJsonParser()).asMap().get();

result.add(new ExternalEvent(
e.getString("event_key"),
Expand Down

0 comments on commit ab457aa

Please sign in to comment.