From 837cbcdc2562c0cd041ed558d05bb7dbba4be603 Mon Sep 17 00:00:00 2001 From: Michael Arrastia Date: Wed, 28 Mar 2018 17:22:56 +0100 Subject: Update published event to include header and body Originally, the published event only contained the raw graph request payload. This has now been updated to include the following changes: - encapsulate the graph request in a body property - add new event header with details such as timestamp, request-id, event-type Issue-ID: AAI-954 Change-Id: I780b6f52a01aafdcd7d09156e9d3a99c25be90a3 Signed-off-by: Michael Arrastia --- .../onap/crud/event/GraphEventEnvelopeTest.java | 48 ++++++++++++++++++++++ .../response/GraphEventResponseHandlerTest.java | 31 ++++++++++++++ .../java/org/onap/crud/test/util/TestUtil.java | 38 +++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java create mode 100644 src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java create mode 100644 src/test/java/org/onap/crud/test/util/TestUtil.java (limited to 'src/test/java') diff --git a/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java b/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java new file mode 100644 index 0000000..51100d5 --- /dev/null +++ b/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java @@ -0,0 +1,48 @@ +package org.onap.crud.event; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import org.junit.Test; +import org.onap.crud.entity.Vertex; +import org.onap.crud.event.GraphEvent.GraphEventOperation; +import org.onap.crud.event.envelope.GraphEventEnvelope; +import org.onap.crud.test.util.TestUtil; +import org.skyscreamer.jsonassert.Customization; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.skyscreamer.jsonassert.comparator.CustomComparator; +import com.google.gson.Gson; +import com.google.gson.JsonElement; + +public class GraphEventEnvelopeTest { + + @Test + public void testPublishedEventFormat() throws Exception { + String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope.json"); + + GraphEvent body = GraphEvent.builder(GraphEventOperation.CREATE) + .vertex(GraphEventVertex.fromVertex(new Vertex.Builder("pserver").build(), "v13")).build(); + String graphEventEnvelope = new GraphEventEnvelope(body).toJson(); + + JSONAssert.assertEquals(expectedEnvelope, graphEventEnvelope, + new CustomComparator(JSONCompareMode.STRICT, new Customization("header.request-id", (o1, o2) -> true), + new Customization("header.timestamp", (o1, o2) -> true), + new Customization("body.timestamp", (o1, o2) -> true), + new Customization("body.transaction-id", (o1, o2) -> true))); + + Gson gson = new Gson(); + GraphEventEnvelope envelope = gson.fromJson(graphEventEnvelope, GraphEventEnvelope.class); + assertThat(envelope.getHeader().getRequestId(), is(envelope.getBody().getTransactionId())); + } + + @Test + public void testConsumedEventFormat() throws Exception { + String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json"); + Gson gson = new Gson(); + GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class); + JsonElement jsonElement = envelope.getPolicyViolations().getAsJsonArray().get(0); + + assertThat(jsonElement.getAsJsonObject().get("summary").getAsString(), is("a summary")); + assertThat(jsonElement.getAsJsonObject().get("policyName").getAsString(), is("a policy name")); + } +} diff --git a/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java b/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java new file mode 100644 index 0000000..5c0da98 --- /dev/null +++ b/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java @@ -0,0 +1,31 @@ +package org.onap.crud.event.response; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import org.junit.Test; +import org.onap.crud.event.envelope.GraphEventEnvelope; +import org.onap.crud.test.util.TestUtil; +import com.google.gson.Gson; + +public class GraphEventResponseHandlerTest { + + @Test + public void testPolicyViolationsNotDetected() throws Exception { + String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel-no-violations.json"); + Gson gson = new Gson(); + GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class); + + GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler(); + assertThat(graphEventResponseHandler.hasPolicyViolations(envelope), is(false)); + } + + @Test + public void testPolicyViolationsDetected() throws Exception { + String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json"); + Gson gson = new Gson(); + GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class); + + GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler(); + assertThat(graphEventResponseHandler.hasPolicyViolations(envelope), is(true)); + } +} diff --git a/src/test/java/org/onap/crud/test/util/TestUtil.java b/src/test/java/org/onap/crud/test/util/TestUtil.java new file mode 100644 index 0000000..69732ae --- /dev/null +++ b/src/test/java/org/onap/crud/test/util/TestUtil.java @@ -0,0 +1,38 @@ +package org.onap.crud.test.util; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Helper methods for locating and reading test data files. + * + */ +public class TestUtil { + + public static Path getPath(String resourceFilename) throws URISyntaxException { + URL resource = ClassLoader.getSystemResource(resourceFilename); + if (resource != null) { + return Paths.get(resource.toURI()); + } + + // If the resource is not found relative to the classpath, try to get it from the file system directly. + File file = new File(resourceFilename); + if (!file.exists()) { + throw new RuntimeException("Resource does not exist: " + resourceFilename); + } + return file.toPath(); + } + + public static String getContentUtf8(Path filePath) throws IOException { + return new String(Files.readAllBytes(filePath)); + } + + public static String getFileAsString(String resourceFilename) throws IOException, URISyntaxException { + return getContentUtf8(getPath(resourceFilename)); + } +} -- cgit 1.2.3-korg