From 8521550f843acf9d3f431962a707ff7f5a746bef Mon Sep 17 00:00:00 2001 From: pranav-super Date: Mon, 25 Nov 2024 10:34:44 -0800 Subject: [PATCH] update e2e tests --- .../jpl/aerie/e2e/ExternalEventsTests.java | 546 +++++++++--------- .../jpl/aerie/e2e/utils/GatewayRequests.java | 35 +- 2 files changed, 285 insertions(+), 296 deletions(-) diff --git a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/ExternalEventsTests.java b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/ExternalEventsTests.java index 3fb4e06f5f..45c59eedee 100644 --- a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/ExternalEventsTests.java +++ b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/ExternalEventsTests.java @@ -11,6 +11,7 @@ import javax.json.Json; import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -34,66 +35,50 @@ void beforeAll() { hasura = new HasuraRequests(playwright); } - // need a method to create external event type - void uploadExternalEventType( ) throws IOException { - final JsonObject schema = Json.createObjectBuilder() - .add("$schema", "http://json-schema.org/draft-07/schema") - .add("title", "TestEventType") - .add("description", "Schema for the attributes of the DSNContact External Event Type.") - .add("type", "object") - .add("additionalProperties", false) - .add("properties", Json.createObjectBuilder() - .add("projectUser", Json.createObjectBuilder() - .add("type", "string") - ) - .add("code", Json.createObjectBuilder() - .add("type", "string") - ) - ) - .add("required", Json.createArrayBuilder() - .add("projectUser") - .add("code") - ) - .build(); + // need a method to upload external event and source types + void uploadExternalSourceEventTypes() throws IOException { + final String schema = """ + { + "event_types": { + "TestEventType": { + "type": "object", + "required": ["projectUser", "code"], + "properties": { + "projectUser": { + "type": "string" + }, + "code": { + "type": "string" + } + } + } + }, + "source_types": { + "TestSourceType": { + "type": "object", + "properties": { + "version": { + "type": "number" + }, + "operator": { + "type": "string" + } + }, + "required": ["version", "operator"] + } + } + } + """; try (final var gateway = new GatewayRequests(playwright)) { - gateway.uploadExternalEventType("TestEventType", schema); - } - } - - // need a method to create external source type - void uploadExternalSourceType() throws IOException { - final String externalSourceTypeName = "TestSourceType"; - final JsonObject schema = Json.createObjectBuilder() - .add("$schema", "http://json-schema.org/draft-07/schema") - .add("title", "TestSourceType") - .add("description", "Schema for the attributes of the DSN Contact Confirmed External Source Type.") - .add("type", "object") - .add("additionalProperties", false) - .add("properties", Json.createObjectBuilder() - .add("version", Json.createObjectBuilder() - .add("type", "number") - ) - .add("operator", Json.createObjectBuilder() - .add("type", "string") - ) - ) - .add("required", Json.createArrayBuilder() - .add("version") - .add("operator") - ) - .build(); - - try (final var gateway = new GatewayRequests(playwright)) { - gateway.uploadExternalSourceType(externalSourceTypeName, schema); + gateway.uploadExternalSourceEventTypes(schema); } } @BeforeEach void beforeEach() throws IOException { // upload types - uploadExternalEventType(); - uploadExternalSourceType(); + uploadExternalSourceEventTypes(); } @AfterEach @@ -115,35 +100,39 @@ void afterEach() throws IOException { // test that a source goes in including all the attributes @Test void correctSourceAndEventAttributes() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", "A") - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - .add("operator", "alpha") - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": "A" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + final String source = """ + { + "attributes": { "version": 1, "operator": "alpha" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); try (final var gateway = new GatewayRequests(playwright)) { gateway.uploadExternalSource(externalSource); @@ -153,233 +142,258 @@ void correctSourceAndEventAttributes() throws IOException { // test that a source fails missing an attribute @Test void sourceMissingAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", "A") - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - // missing: operator - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": "A" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + // missing: operator attribute + final String source = """ + { + "attributes": { "version": 1 }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("Source's attributes are invalid")); + assertTrue(ex.getMessage().contains("should have required property 'operator'")); } // test that a source fails with an extra attribute @Test void sourceExtraAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", "A") - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - .add("operator", "alpha") - .add("extra", "attribute") // extra - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": "A" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + // has extra attribute "extra" + final String source = """ + { + "attributes": { "version": 1, "operator": "alpha", "extra": "attribute" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("Source's attributes are invalid")); + assertTrue(ex.getMessage().contains("should NOT have additional properties")); } // test that a source fails with an attribute of the wrong type @Test void sourceWrongTypeAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", "A") - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", "string") // expects int - .add("operator", "alpha") - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": "A" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + // has wrong type for attribute "version" - string instead of number + final String source = """ + { + "attributes": { "version": "string", "operator": "alpha" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("Source's attributes are invalid")); + assertTrue(ex.getMessage().contains("should be number")); } // test that an event fails missing an attribute @Test void eventMissingAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - // missing: code - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - .add("operator", "alpha") - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + // missing: code + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + final String source = """ + { + "attributes": { "version": "string", "operator": "alpha" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("External Event")); - assertTrue(ex.getMessage().contains("does not have a valid set of attributes, per it's type's schema:")); + assertTrue(ex.getMessage().contains("should have required property 'code'")); } // test that an event fails with an extra attribute @Test void eventExtraAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", "A") - .add("extra", "attribute") // extra - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - .add("operator", "alpha") - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + // has extra attribute "extra" + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": "A", + "extra": "extra" + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + final String source = """ + { + "attributes": { "version": "string", "operator": "alpha" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("External Event")); - assertTrue(ex.getMessage().contains("does not have a valid set of attributes, per it's type's schema:")); + assertTrue(ex.getMessage().contains("should NOT have additional properties")); } // test that an event fails with an attribute of the wrong type @Test void eventWrongTypeAttribute() throws IOException { - final var externalEvents = Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("projectUser", "UserA") - .add("code", 1) // should be a string - ) - .add("duration", "01:00:00") - .add("event_type_name", "TestEventType") - .add("key", "Event_01") - .add("start_time", "2024-01-21T01:00:00+00:00") - ); - - final var externalSource = Json.createObjectBuilder() - .add("source", Json.createObjectBuilder() - .add("attributes", Json.createObjectBuilder() - .add("version", 1) - .add("operator", "alpha") - ) - .add("derivation_group_name", "TestDerivationGroup") - .add("period", Json.createObjectBuilder() - .add("start_time", "2024-01-21T00:00:00+00:00") - .add("end_time", "2024-01-28T00:00:00+00:00") - ) - .add("key", "TestExternalSourceKey") - .add("source_type_name", "TestSourceType") - .add("valid_at", "2024-01-19T00:00:00+00:00") - ) - .add("external_events", externalEvents) - .build(); + // attribute "code" should be of type string + final String events = """ + [ + { + "attributes": { + "projectUser": "UserA", + "code": 1 + }, + "duration": "01:00:00", + "event_type_name": "TestEventType", + "key": "Event_01", + "start_time": "2024-01-21T01:00:00+00:00" + } + ] + """; + + final String source = """ + { + "attributes": { "version": "string", "operator": "alpha" }, + "derivation_group_name": "TestDerivationGroup", + "period": { + "start_time": "2024-01-21T00:00:00+00:00", + "end_time": "2024-01-28T00:00:00+00:00" + }, + "key": "TestExternalSourceKey", + "source_type_name": "TestSourceType", + "valid_at": "2024-01-19T00:00:00+00:00" + } + """; + + final JsonObject externalSource = Json.createObjectBuilder() + .add("source", source) + .add("external_events", events) + .build(); final var gateway = new GatewayRequests(playwright); final IOException ex = assertThrows(IOException.class, () -> gateway.uploadExternalSource(externalSource)); - assertTrue(ex.getMessage().contains("External Event")); - assertTrue(ex.getMessage().contains("does not have a valid set of attributes, per it's type's schema:")); + assertTrue(ex.getMessage().contains("should be string")); } } diff --git a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java index 96614249be..e646ba189b 100644 --- a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java +++ b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java @@ -99,14 +99,11 @@ public int uploadJarFile(String jarPath) throws IOException { } - public void uploadExternalEventType(String externalEventTypeName, JsonObject schema) throws IOException { - final var response = request.post("/uploadExternalEventType", RequestOptions.create() + public void uploadExternalSourceEventTypes(String schema) throws IOException { + final var response = request.post("/uploadExternalSourceEventTypes", RequestOptions.create() + .setHeader("Authorization", "Bearer "+token) .setHeader("Content-Type", "application/json") - .setData(Json.createObjectBuilder() - .add("external_event_type_name", externalEventTypeName) - .add("attribute_schema", schema) - .build() - .toString())); + .setData(schema)); // Process Response if(!response.ok()){ throw new IOException(response.statusText()); @@ -120,31 +117,9 @@ public void uploadExternalEventType(String externalEventTypeName, JsonObject sch } } - public void uploadExternalSourceType(String externalSourceTypeName, JsonObject schema) throws IOException { - final var response = request.post("/uploadExternalSourceType", RequestOptions.create() - .setHeader("Content-Type", "application/json") - .setData(Json.createObjectBuilder() - .add("external_source_type_name", externalSourceTypeName) - .add("attribute_schema", schema) - .add("allowed_event_types", Json.createArrayBuilder() - .add("TestEventType") - ) - .build() - .toString())); - if(!response.ok()){ - throw new IOException(response.statusText()); - } - try(final var reader = Json.createReader(new StringReader(response.text()))){ - final JsonObject bodyJson = reader.readObject(); - if(!bodyJson.containsKey("data")){ - System.err.println("Upload failed"); - throw new RuntimeException(bodyJson.toString()); - } - } - } - public void uploadExternalSource(JsonObject externalSource) throws IOException { final var response = request.post("/uploadExternalSource", RequestOptions.create() + .setHeader("Authorization", "Bearer "+token) .setHeader( "Content-Type", "application/json")