Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Support receiving SMILE-encoded events
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Sep 8, 2014
1 parent a8d4f1d commit e5e971e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/com/proofpoint/event/collector/EventResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public Response write(List<Event> events)
return processEvents(WRITER, events, WRITE);
}

// Separate method to permit gathering of statistics
@POST
@Consumes("application/x-jackson-smile")
public Response writeSmile(List<Event> events)
throws IOException
{
return write(events);
}

@POST
@Path("/distribute")
@Consumes(MediaType.APPLICATION_JSON)
Expand All @@ -72,6 +81,17 @@ public Response distribute(List<Event> 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<Event> events)
throws IOException
{
return distribute(events);
}

private Response processEvents(EventProcessor processor, List<Event> events, ProcessType processType)
throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.<EventWriter>of(writer), new ServerConfig().setAcceptedEventTypes("Test"), eventCollectorStats);

Event event = new Event("Test", UUID.randomUUID().toString(), "test.local", new DateTime(), ARBITRARY_DATA);

List<Event> events = ImmutableList.of(event);
Response response = resource.writeSmile(events);

verifyAcceptedResponse(response);

verifyWrittenAndDistributedEvents(events, ImmutableList.<Event>of());

verifyMetrics(WRITE, ImmutableList.of(new EventMetric("Test", VALID, 1)));
}

@Test
public void testWriteUnsupportedType()
throws IOException
Expand Down Expand Up @@ -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.<EventWriter>of(writer), new ServerConfig().setAcceptedEventTypes("Test"), eventCollectorStats);

Event event = new Event("Test", UUID.randomUUID().toString(), "test.local", new DateTime(), ARBITRARY_DATA);

List<Event> events = ImmutableList.of(event);
Response response = resource.distributeSmile(events);

verifyAcceptedResponse(response);

verifyWrittenAndDistributedEvents(ImmutableList.<Event>of(), events);

verifyMetrics(DISTRIBUTE, ImmutableList.of(new EventMetric("Test", VALID, 1)));
}

@Test
public void testDistributeUnsupportedType()
throws IOException
Expand Down
79 changes: 78 additions & 1 deletion src/test/java/com/proofpoint/event/collector/TestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -60,7 +66,8 @@

public class TestServer
{
private JsonCodec<Object> OBJECT_CODEC = JsonCodec.jsonCodec(Object.class);
private static final JsonCodec<Object> 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e5e971e

Please sign in to comment.