diff options
author | Michael Arrastia <MArrasti@amdocs.com> | 2018-03-28 17:22:56 +0100 |
---|---|---|
committer | Michael Arrastia <MArrasti@amdocs.com> | 2018-03-29 18:38:17 +0100 |
commit | 837cbcdc2562c0cd041ed558d05bb7dbba4be603 (patch) | |
tree | 370e72f52e0420e0fa633de73a6fd47d03d17422 /src/test/java/org | |
parent | 0c19b1386259b7144bc6c95954f965c469835522 (diff) |
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 <MArrasti@amdocs.com>
Diffstat (limited to 'src/test/java/org')
3 files changed, 117 insertions, 0 deletions
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)); + } +} |