From e5e971eb5ea1e450ee362c0c1213cec0bcac56e5 Mon Sep 17 00:00:00 2001 From: John Myers Date: Sat, 6 Sep 2014 11:58:55 -0700 Subject: [PATCH] Support receiving SMILE-encoded events --- .../event/collector/EventResource.java | 20 +++++ .../event/collector/TestEventResource.java | 36 +++++++++ .../event/collector/TestServer.java | 79 ++++++++++++++++++- 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/proofpoint/event/collector/EventResource.java b/src/main/java/com/proofpoint/event/collector/EventResource.java index 82a71ba..8cca3d6 100644 --- a/src/main/java/com/proofpoint/event/collector/EventResource.java +++ b/src/main/java/com/proofpoint/event/collector/EventResource.java @@ -63,6 +63,15 @@ public Response write(List events) return processEvents(WRITER, events, WRITE); } + // Separate method to permit gathering of statistics + @POST + @Consumes("application/x-jackson-smile") + public Response writeSmile(List events) + throws IOException + { + return write(events); + } + @POST @Path("/distribute") @Consumes(MediaType.APPLICATION_JSON) @@ -72,6 +81,17 @@ public Response distribute(List events) return processEvents(DISTRIBUTOR, events, DISTRIBUTE); } + + // Separate method to permit gathering of statistics + @POST + @Path("/distribute") + @Consumes("application/x-jackson-smile") + public Response distributeSmile(List events) + throws IOException + { + return distribute(events); + } + private Response processEvents(EventProcessor processor, List events, ProcessType processType) throws IOException { diff --git a/src/test/java/com/proofpoint/event/collector/TestEventResource.java b/src/test/java/com/proofpoint/event/collector/TestEventResource.java index d4cdfe5..4079e6f 100644 --- a/src/test/java/com/proofpoint/event/collector/TestEventResource.java +++ b/src/test/java/com/proofpoint/event/collector/TestEventResource.java @@ -79,6 +79,24 @@ public void testWrite() verifyMetrics(WRITE, ImmutableList.of(new EventMetric("Test", VALID, 1))); } + @Test + public void testWriteSmile() + throws IOException + { + EventResource resource = new EventResource(ImmutableSet.of(writer), new ServerConfig().setAcceptedEventTypes("Test"), eventCollectorStats); + + Event event = new Event("Test", UUID.randomUUID().toString(), "test.local", new DateTime(), ARBITRARY_DATA); + + List events = ImmutableList.of(event); + Response response = resource.writeSmile(events); + + verifyAcceptedResponse(response); + + verifyWrittenAndDistributedEvents(events, ImmutableList.of()); + + verifyMetrics(WRITE, ImmutableList.of(new EventMetric("Test", VALID, 1))); + } + @Test public void testWriteUnsupportedType() throws IOException @@ -140,6 +158,24 @@ public void testDistribute() verifyMetrics(DISTRIBUTE, ImmutableList.of(new EventMetric("Test", VALID, 1))); } + @Test + public void testDistributeSmile() + throws IOException + { + EventResource resource = new EventResource(ImmutableSet.of(writer), new ServerConfig().setAcceptedEventTypes("Test"), eventCollectorStats); + + Event event = new Event("Test", UUID.randomUUID().toString(), "test.local", new DateTime(), ARBITRARY_DATA); + + List events = ImmutableList.of(event); + Response response = resource.distributeSmile(events); + + verifyAcceptedResponse(response); + + verifyWrittenAndDistributedEvents(ImmutableList.of(), events); + + verifyMetrics(DISTRIBUTE, ImmutableList.of(new EventMetric("Test", VALID, 1))); + } + @Test public void testDistributeUnsupportedType() throws IOException diff --git a/src/test/java/com/proofpoint/event/collector/TestServer.java b/src/test/java/com/proofpoint/event/collector/TestServer.java index 6215432..136591c 100755 --- a/src/test/java/com/proofpoint/event/collector/TestServer.java +++ b/src/test/java/com/proofpoint/event/collector/TestServer.java @@ -15,7 +15,11 @@ */ package com.proofpoint.event.collector; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.io.Files; import com.google.common.io.Resources; @@ -24,6 +28,7 @@ import com.proofpoint.bootstrap.LifeCycleManager; import com.proofpoint.discovery.client.testing.TestingDiscoveryModule; import com.proofpoint.event.client.JsonEventModule; +import com.proofpoint.http.client.BodyGenerator; import com.proofpoint.http.client.HttpClient; import com.proofpoint.http.client.StatusResponseHandler.StatusResponse; import com.proofpoint.http.client.StringResponseHandler.StringResponse; @@ -43,6 +48,7 @@ import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.net.URI; import java.util.concurrent.ExecutionException; @@ -60,7 +66,8 @@ public class TestServer { - private JsonCodec OBJECT_CODEC = JsonCodec.jsonCodec(Object.class); + private static final JsonCodec OBJECT_CODEC = JsonCodec.jsonCodec(Object.class); + private static final ObjectMapper MAPPER = new ObjectMapper(new SmileFactory()).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); private HttpClient client; private TestingHttpServer server; private File tempStageDir; @@ -145,6 +152,41 @@ public void testPostSingle() assertEquals(response.getStatusCode(), Status.ACCEPTED.getStatusCode()); } + @Test + public void testPostSmile() + throws IOException, ExecutionException, InterruptedException + { + StatusResponse response = client.execute(preparePost() + .setUri(urlFor("/v2/event")) + .setHeader("Content-Type", "application/x-jackson-smile") + .setBodyGenerator(new BodyGenerator() + { + @Override + public void write(OutputStream outputStream) + throws Exception + { + MAPPER.writeValue(outputStream, ImmutableList.of( + ImmutableMap.of( + "type", "Test", + "uuid", "DCD36293-3072-4AFD-B6E3-A9EB9CE1F219", + "host", "test.local", + "timestamp", "2011-03-30T16:10:16.000Z", + "data", ImmutableMap.of( + "foo", "bar", + "hello", "world" + ) + ) + + ) + ); + } + }) + .build(), + createStatusResponseHandler()); + + assertEquals(response.getStatusCode(), Status.ACCEPTED.getStatusCode()); + } + @Test public void testPostMultiple() throws IOException, ExecutionException, InterruptedException @@ -201,6 +243,41 @@ public void testDistributeSingle() assertEquals(response.getStatusCode(), Status.ACCEPTED.getStatusCode()); } + @Test + public void testDistributeSmile() + throws IOException, ExecutionException, InterruptedException + { + StatusResponse response = client.execute(preparePost() + .setUri(urlFor("/v2/event/distribute")) + .setHeader("Content-Type", "application/x-jackson-smile") + .setBodyGenerator(new BodyGenerator() + { + @Override + public void write(OutputStream outputStream) + throws Exception + { + MAPPER.writeValue(outputStream, ImmutableList.of( + ImmutableMap.of( + "type", "Test", + "uuid", "DCD36293-3072-4AFD-B6E3-A9EB9CE1F219", + "host", "test.local", + "timestamp", "2011-03-30T16:10:16.000Z", + "data", ImmutableMap.of( + "foo", "bar", + "hello", "world" + ) + ) + + ) + ); + } + }) + .build(), + createStatusResponseHandler()); + + assertEquals(response.getStatusCode(), Status.ACCEPTED.getStatusCode()); + } + private URI urlFor(String path) { return server.getBaseUrl().resolve(path);