Skip to content

Commit

Permalink
update HasuraRequests.java for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-super committed Oct 23, 2024
1 parent 4314ada commit aecc5a7
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,59 @@
import gov.nasa.jpl.aerie.e2e.ExternalDatasetsTest;
import gov.nasa.jpl.aerie.e2e.types.GoalInvocationId;
import gov.nasa.jpl.aerie.e2e.utils.GatewayRequests;
import gov.nasa.jpl.aerie.e2e.utils.HasuraRequests;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.json.JsonArray;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ExternalEventsTests extends ProceduralSchedulingSetup {
private GoalInvocationId procedureId;
private int datasetId;

private final static String SOURCE_TYPE = "TestType";
private final static String EVENT_TYPE = "TestType";
private final static String DERIVATION_GROUP = "TestGroup";

private final HasuraRequests.ExternalSource externalSource = new HasuraRequests.ExternalSource(
"Test.json",
SOURCE_TYPE,
DERIVATION_GROUP,
"2024-01-01T00:00:00Z",
"2024-01-01T00:00:00Z",
"2024-01-08T00:00:00Z",
"2024-10-01T00:00:00Z"
);
private final List<HasuraRequests.ExternalEvent> externalEvents = List.of(
new HasuraRequests.ExternalEvent(
"Event_01",
EVENT_TYPE,
externalSource.key(),
externalSource.derivation_group_name(),
"2024-01-01T01:00:00Z",
"00:10:00"
),
new HasuraRequests.ExternalEvent(
"Event_02",
EVENT_TYPE,
externalSource.key(),
externalSource.derivation_group_name(),
"2024-01-01T03:00:00Z",
"00:10:00"
),
new HasuraRequests.ExternalEvent(
"Event_03",
EVENT_TYPE,
externalSource.key(),
externalSource.derivation_group_name(),
"2024-01-01T05:00:00Z",
"00:10:00"
)
);

@BeforeEach
void localBeforeEach() throws IOException {
try (final var gateway = new GatewayRequests(playwright)) {
Expand All @@ -37,17 +75,29 @@ void localBeforeEach() throws IOException {
);
}

// TODO pranav add a method to the HasuraRequests class for uploading events
// TODO pranav upload some events
// Upload some External Events (and associated infrastructure)
hasura.insertExternalSourceType(SOURCE_TYPE);
hasura.insertExternalEventType(EVENT_TYPE);
hasura.insertDerivationGroup(DERIVATION_GROUP, SOURCE_TYPE);
hasura.insertExternalSource(externalSource);
hasura.insertExternalEvents(externalEvents);

// Note: we do not need to manually delete this. The plan is deleted first in ProceduralSchedulingSetup.java's
// @AfterEach method, which cascades and deletes this. Attempting deletion will cause error.
hasura.insertPlanDerivationGroupAssociation(planId, DERIVATION_GROUP);
}

@AfterEach
void localAfterEach() throws IOException {
hasura.deleteSchedulingGoal(procedureId.goalId());
hasura.deleteExternalDataset(planId, datasetId);

// TODO pranav add a method to HasuraRequests for deleting events
// TODO pranav delete the events added in localBeforeEach
// External Event Related
hasura.deletePlanDerivationGroup(planId, DERIVATION_GROUP);
hasura.deleteExternalSource(externalSource);
hasura.deleteDerivationGroup(DERIVATION_GROUP);
hasura.deleteExternalSourceType(SOURCE_TYPE);
hasura.deleteExternalEventType(EVENT_TYPE);
}

@Test
Expand All @@ -57,7 +107,8 @@ void testExternalEventQuery() throws IOException {
final var plan = hasura.getPlan(planId);
final var activities = plan.activityDirectives();

// TODO pranav make sure the ExternalEventsGoal successfully made Bite Banana activities
// one-to-one with the uploaded events.
// TODO pranav make sure the ExternalEventsGoal successfully made Bite Banana activities one-to-one with the uploaded events.
}

// TODO pranav add another goal that filters by type and group, test it here
}
70 changes: 70 additions & 0 deletions e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ mutation CreateActivityDirective($activityDirectiveInsertInput: activity_directi
id
}
}"""),
CREATE_EXTERNAL_EVENT_TYPE("""
mutation CreateExternalEventType($eventType: external_event_type_insert_input!) {
createExternalEventType: insert_external_event_type_one(object: $eventType) {
name
}
}"""),
CREATE_EXTERNAL_EVENTS("""
mutation InsertExternalEvents($objects: [external_event_insert_input!]!) {
insertExternalEvents: insert_external_event(objects: $objects) {
returning {
key
}
}
}"""),
CREATE_EXTERNAL_SOURCE("""
mutation InsertExternalSource($object: external_source_insert_input!) {
insertExternalSource: insert_external_source_one(object: $object) {
key
}
}"""),
CREATE_EXTERNAL_SOURCE_TYPE("""
mutation CreateExternalSourceType($sourceType: external_source_type_insert_input!) {
createExternalSourceType: insert_external_source_type_one(object: $sourceType) {
name
}
}"""),
CREATE_DERIVATION_GROUP("""
mutation CreateDerivationGroup($derivationGroup: derivation_group_insert_input!) {
createDerivationGroup: insert_derivation_group_one(object: $derivationGroup) {
name
}
}"""),
CREATE_MISSION_MODEL("""
mutation CreateMissionModel($model: mission_model_insert_input!) {
insert_mission_model_one(object: $model) {
Expand All @@ -95,6 +127,12 @@ mutation CreatePlan($plan: plan_insert_input!) {
revision
}
}"""),
CREATE_PLAN_DERIVATION_GROUP("""
mutation CreatePlanDerivationGroup($source: plan_derivation_group_insert_input!) {
planExternalSourceLink: insert_plan_derivation_group_one(object: $source) {
derivation_group_name
}
}"""),
CREATE_SCHEDULING_SPEC_GOAL("""
mutation CreateSchedulingSpecGoal($spec_goal: scheduling_specification_goals_insert_input!) {
insert_scheduling_specification_goals_one(object: $spec_goal) {
Expand Down Expand Up @@ -140,12 +178,38 @@ mutation DeleteConstraint($id: Int!) {
id
}
}"""),
DELETE_DERIVATION_GROUP("""
mutation DeleteDerivationGroup($name: String!) {
deleteDerivationGroup: delete_derivation_group(where: { name: { _eq: $name } }) {
returning {
name
}
}
}"""),
DELETE_EXTERNAL_DATASET("""
mutation deleteExtProfile($plan_id: Int!, $dataset_id: Int!) {
delete_plan_dataset_by_pk(plan_id:$plan_id, dataset_id:$dataset_id) {
dataset_id
}
}"""),
DELETE_EXTERNAL_EVENT_TYPE("""
mutation DeleteExternalEventType($name: String!) {
deleteExternalEventType: delete_external_event_type_by_pk(name: $name) {
name
}
}"""),
DELETE_EXTERNAL_SOURCE("""
mutation DeleteExternalSource($derivationGroupName: String!, $sourceKey: String!) {
deleteExternalSource: delete_external_source_by_pk(derivation_group_name: $derivationGroupName, key: $sourceKey) {
key
}
}"""),
DELETE_EXTERNAL_SOURCE_TYPE("""
mutation DeleteExternalSourceType($name: String!) {
deleteExternalSourceType: delete_external_source_type_by_pk(name: $name) {
name
}
}"""),
DELETE_MISSION_MODEL("""
mutation DeleteModel($id: Int!) {
delete_mission_model_by_pk(id: $id) {
Expand Down Expand Up @@ -174,6 +238,12 @@ mutation DeletePlan($id: Int!) {
}
}
}"""),
DELETE_PLAN_DERIVATION_GROUP("""
mutation DeletePlanExternalSource($derivationGroupName: String!, $planId: Int!) {
planDerivationGroupLink: delete_plan_derivation_group_by_pk(derivation_group_name: $derivationGroupName, plan_id: $planId) {
derivation_group_name
}
}"""),
DELETE_SCHEDULING_GOAL("""
mutation DeleteSchedulingGoal($goalId: Int!) {
delete_scheduling_specification_goals(where: {goal_id: {_eq: $goalId}}){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonValue;
import javax.json.JsonObject;
import java.io.IOException;
Expand Down Expand Up @@ -98,6 +99,11 @@ private JsonObject makeRequest(
}
}

//region Records
public record ExternalEvent(String key, String event_type_name, String source_key, String derivation_group_name, String start_time, String duration) {}
public record ExternalSource(String key, String source_type_name, String derivation_group_name, String valid_at, String start_time, String end_time, String created_at){}
//endregion Records

//region Mission Model
public int createMissionModel(int jarId, String name, String mission, String version)
throws IOException, InterruptedException
Expand Down Expand Up @@ -988,6 +994,153 @@ public void deleteExternalDataset(int planId, int datasetId) throws IOException
}
//endregion

// region External Events
public String insertExternalSourceType(
String name
) throws IOException {
final var insertExternalSourceTypeBuilder = Json.createObjectBuilder()
.add("name", name)
.build();
final var variables = Json.createObjectBuilder().add("sourceType", insertExternalSourceTypeBuilder).build();
return makeRequest(GQL.CREATE_EXTERNAL_SOURCE_TYPE, variables)
.getJsonObject("createExternalSourceType")
.getString("name");
}
public String insertExternalEventType(
String name
) throws IOException {
final var insertExternalSourceTypeBuilder = Json.createObjectBuilder()
.add("name", name)
.build();
final var variables = Json.createObjectBuilder().add("eventType", insertExternalSourceTypeBuilder).build();
return makeRequest(GQL.CREATE_EXTERNAL_EVENT_TYPE, variables)
.getJsonObject("createExternalEventType")
.getString("name");
}
public String insertDerivationGroup(
String name,
String sourceTypeName
) throws IOException {
final var insertDerivationGroupBuilder = Json.createObjectBuilder()
.add("name", name)
.add("source_type_name", sourceTypeName)
.build();
final var variables = Json.createObjectBuilder().add("derivationGroup", insertDerivationGroupBuilder).build();
return makeRequest(GQL.CREATE_DERIVATION_GROUP, variables)
.getJsonObject("createDerivationGroup")
.getString("name");
}
public String insertExternalSource(
ExternalSource externalSource
) throws IOException {
final var insertExternalSourceBuilder = Json.createObjectBuilder()
.add("key", externalSource.key())
.add("source_type_name", externalSource.source_type_name())
.add("derivation_group_name", externalSource.derivation_group_name())
.add("valid_at", externalSource.valid_at())
.add("start_time", externalSource.start_time())
.add("end_time", externalSource.end_time())
.add("created_at", externalSource.created_at())
.build();
final var variables = Json.createObjectBuilder().add("object", insertExternalSourceBuilder).build();
return makeRequest(GQL.CREATE_EXTERNAL_SOURCE, variables)
.getJsonObject("insertExternalSource")
.getString("key");
}
public JsonArray insertExternalEvents(
List<ExternalEvent> externalEvents
) throws IOException {
JsonArrayBuilder formattedEvents = Json.createArrayBuilder();
for (ExternalEvent e : externalEvents) {
formattedEvents.add(
Json.createObjectBuilder()
.add("key", e.key())
.add("event_type_name", e.event_type_name())
.add("source_key", e.source_key())
.add("derivation_group_name", e.derivation_group_name())
.add("start_time", e.start_time())
.add("duration", e.duration())
.build()
);
}
final var variables = Json.createObjectBuilder()
.add("objects", formattedEvents.build())
.build();
return makeRequest(GQL.CREATE_EXTERNAL_EVENTS, variables)
.getJsonObject("insertExternalEvents")
.getJsonArray("returning");
}
public String insertPlanDerivationGroupAssociation(
int planId,
String derivationGroupName
) throws IOException {
final var insertPlanDerivationGroupBuilder = Json.createObjectBuilder()
.add("plan_id", planId)
.add("derivation_group_name", derivationGroupName)
.build();
final var variables = Json.createObjectBuilder().add("source", insertPlanDerivationGroupBuilder).build();
return makeRequest(GQL.CREATE_PLAN_DERIVATION_GROUP, variables)
.getJsonObject("planExternalSourceLink")
.getString("derivation_group_name");
}

public String deleteExternalSourceType(
String name
) throws IOException {
final var variables = Json.createObjectBuilder()
.add("name", name)
.build();
return makeRequest(GQL.DELETE_EXTERNAL_SOURCE_TYPE, variables)
.getJsonObject("deleteExternalSourceType")
.getString("name");
}
public String deleteExternalEventType(
String name
) throws IOException {
final var variables = Json.createObjectBuilder()
.add("name", name)
.build();
return makeRequest(GQL.DELETE_EXTERNAL_EVENT_TYPE, variables)
.getJsonObject("deleteExternalEventType")
.getString("name");
}
public JsonArray deleteDerivationGroup(
String name
) throws IOException {
final var variables = Json.createObjectBuilder()
.add("name", name)
.build();
return makeRequest(GQL.DELETE_DERIVATION_GROUP, variables)
.getJsonObject("deleteDerivationGroup")
.getJsonArray("returning");
}
public String deleteExternalSource(
ExternalSource externalSource
) throws IOException {
final var variables = Json.createObjectBuilder()
.add("sourceKey", externalSource.key())
.add("derivationGroupName", externalSource.derivation_group_name())
.build();
// NOTE: this deletes external events as well, as deletions of sources cascade to their contained events.
return makeRequest(GQL.DELETE_EXTERNAL_SOURCE, variables)
.getJsonObject("deleteExternalSource")
.getString("key");
}
public String deletePlanDerivationGroup(
int planId,
String derivationGroupName
) throws IOException {
final var variables = Json.createObjectBuilder()
.add("planId", planId)
.add("derivationGroupName", derivationGroupName)
.build();
return makeRequest(GQL.DELETE_PLAN_DERIVATION_GROUP, variables)
.getJsonObject("planDerivationGroupLink")
.getString("derivation_group_name");
}

// endregion

//region Constraints
public List<ConstraintRecord> checkConstraints(int planID) throws IOException {
final var variables = Json.createObjectBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ interface Plan {

/** Get external events associated with this plan. */
fun events(query: EventQuery): ExternalEvents
/** Get external events belonging to a given derivation group and type associated with this plan. */
/** Get external events belonging to a given derivation group and external event type associated with this plan. */
fun events(derivationGroup: String, type: String) = events(EventQuery(derivationGroup, type, null))
/** Get external events belonging to a given derivation group associated with this plan. */
fun events(derivationGroup: String) = events(EventQuery(derivationGroup, null, null))
/** Get all external events in all derivation groups associated with this plan. */
/** Get all external events across all derivation groups associated with this plan. */
fun events() = events(EventQuery())
}

0 comments on commit aecc5a7

Please sign in to comment.