Search in sources :

Example 11 with LambdaContext

use of com.nextdoor.bender.LambdaContext in project bender by Nextdoor.

the class ArraySplitOperationTest method testNonArray.

@Test(expected = OperationException.class)
public void testNonArray() throws JsonSyntaxException, UnsupportedEncodingException, IOException {
    JsonParser parser = new JsonParser();
    TestContext t = new TestContext();
    t.setFunctionName("foo");
    LambdaContext lctx = new LambdaContext(t);
    JsonElement input = parser.parse(getResourceString("array_input.json"));
    GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject());
    ArraySplitOperation operation = new ArraySplitOperation("$.arr[0]");
    InternalEvent ievent = new InternalEvent("", lctx, 123);
    ievent.setEventObj(devent);
    ievent.setEventTime(124);
    List<InternalEvent> events = operation.perform(ievent);
}
Also used : ArraySplitOperation(com.nextdoor.bender.operation.json.array.ArraySplitOperation) GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonElement(com.google.gson.JsonElement) TestContext(com.nextdoor.bender.aws.TestContext) LambdaContext(com.nextdoor.bender.LambdaContext) JsonParser(com.google.gson.JsonParser) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test) OperationTest(com.nextdoor.bender.operations.json.OperationTest)

Example 12 with LambdaContext

use of com.nextdoor.bender.LambdaContext in project bender by Nextdoor.

the class ArraySplitOperationTest method testArraySplitWithFieldsToKeep.

@Test
public void testArraySplitWithFieldsToKeep() throws IOException {
    TestContext t = new TestContext();
    t.setFunctionName("foo");
    LambdaContext lctx = new LambdaContext(t);
    JsonElement input = JsonParser.parseString(getResourceString("array_input_kinesis_format.json"));
    GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject());
    List<String> fieldsToKeep = Arrays.asList("owner", "logGroup", "logStream");
    ArraySplitOperation operation = new ArraySplitOperation("$.logEvents", fieldsToKeep);
    InternalEvent ievent = new InternalEvent("", lctx, 123);
    ievent.setEventObj(devent);
    ievent.setEventTime(124);
    List<InternalEvent> events = operation.perform(ievent);
    assertEquals(4, events.size());
    JsonElement actual = JsonParser.parseString(events.get(0).getEventString());
    assertTrue(actual.getAsJsonObject().has("owner"));
    assertTrue(actual.getAsJsonObject().has("logGroup"));
    assertTrue(actual.getAsJsonObject().has("logStream"));
    assertTrue(actual.getAsJsonObject().has("id"));
    assertTrue(actual.getAsJsonObject().has("message"));
    assertTrue(actual.getAsJsonObject().has("timestamp"));
    actual = JsonParser.parseString(events.get(1).getEventString());
    assertTrue(actual.getAsJsonObject().has("owner"));
    assertTrue(actual.getAsJsonObject().has("logGroup"));
    assertTrue(actual.getAsJsonObject().has("logStream"));
    assertTrue(actual.getAsJsonObject().has("id"));
    assertTrue(actual.getAsJsonObject().has("message"));
    assertTrue(actual.getAsJsonObject().has("timestamp"));
    actual = JsonParser.parseString(events.get(2).getEventString());
    assertTrue(actual.getAsJsonObject().has("owner"));
    assertTrue(actual.getAsJsonObject().has("logGroup"));
    assertTrue(actual.getAsJsonObject().has("logStream"));
    assertTrue(actual.getAsJsonObject().has("id"));
    assertTrue(actual.getAsJsonObject().has("message"));
    assertTrue(actual.getAsJsonObject().has("timestamp"));
    actual = JsonParser.parseString(events.get(3).getEventString());
    assertTrue(actual.getAsJsonObject().has("owner"));
    assertTrue(actual.getAsJsonObject().has("logGroup"));
    assertTrue(actual.getAsJsonObject().has("logStream"));
    assertTrue(actual.getAsJsonObject().has("id"));
    assertTrue(actual.getAsJsonObject().has("message"));
    assertTrue(actual.getAsJsonObject().has("timestamp"));
}
Also used : ArraySplitOperation(com.nextdoor.bender.operation.json.array.ArraySplitOperation) GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonElement(com.google.gson.JsonElement) TestContext(com.nextdoor.bender.aws.TestContext) LambdaContext(com.nextdoor.bender.LambdaContext) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test) OperationTest(com.nextdoor.bender.operations.json.OperationTest)

Example 13 with LambdaContext

use of com.nextdoor.bender.LambdaContext in project bender by Nextdoor.

the class ArraySplitOperationTest method testContextMatch.

@Test
public void testContextMatch() throws JsonSyntaxException, UnsupportedEncodingException, IOException {
    JsonParser parser = new JsonParser();
    TestContext t = new TestContext();
    t.setFunctionName("foo");
    LambdaContext lctx = new LambdaContext(t);
    JsonElement input = parser.parse(getResourceString("array_input.json"));
    GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject());
    ArraySplitOperation operation = new ArraySplitOperation("$.arr");
    InternalEvent ievent = new InternalEvent("", lctx, 1);
    ievent.setEventObj(devent);
    List<InternalEvent> actual = operation.perform(ievent);
    assertEquals(lctx, actual.get(0).getCtx());
    assertEquals(lctx, actual.get(1).getCtx());
    assertEquals(lctx, actual.get(2).getCtx());
}
Also used : ArraySplitOperation(com.nextdoor.bender.operation.json.array.ArraySplitOperation) GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonElement(com.google.gson.JsonElement) TestContext(com.nextdoor.bender.aws.TestContext) LambdaContext(com.nextdoor.bender.LambdaContext) JsonParser(com.google.gson.JsonParser) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test) OperationTest(com.nextdoor.bender.operations.json.OperationTest)

Example 14 with LambdaContext

use of com.nextdoor.bender.LambdaContext in project bender by Nextdoor.

the class StdoutTransportTest method testStdout.

@Test
public void testStdout() throws IllegalStateException, IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    System.setOut(new PrintStream(bo));
    StdoutTransport transport = new StdoutTransport();
    GenericTransportBuffer buf = new GenericTransportBuffer(1, false, new StdoutTransportSerializer());
    InternalEvent event = new InternalEvent("junk", new LambdaContext(new TestContext()), 123);
    event.setSerialized("junk");
    buf.add(event);
    transport.sendBatch(buf);
    bo.flush();
    String allWrittenLines = new String(bo.toByteArray());
    assertEquals("junk\n", allWrittenLines);
}
Also used : PrintStream(java.io.PrintStream) TestContext(com.nextdoor.bender.aws.TestContext) LambdaContext(com.nextdoor.bender.LambdaContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericTransportBuffer(com.nextdoor.bender.ipc.generic.GenericTransportBuffer) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Aggregations

LambdaContext (com.nextdoor.bender.LambdaContext)14 TestContext (com.nextdoor.bender.aws.TestContext)10 Test (org.junit.Test)10 InternalEvent (com.nextdoor.bender.InternalEvent)9 JsonElement (com.google.gson.JsonElement)6 GenericJsonEvent (com.nextdoor.bender.deserializer.json.GenericJsonEvent)6 ArraySplitOperation (com.nextdoor.bender.operation.json.array.ArraySplitOperation)6 OperationTest (com.nextdoor.bender.operations.json.OperationTest)6 JsonParser (com.google.gson.JsonParser)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 S3EventNotificationRecord (com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord)2 ContextSubstitution (com.nextdoor.bender.operation.substitution.context.ContextSubstitution)2 DummpyMapEvent (com.nextdoor.bender.testutils.DummyDeserializerHelper.DummpyMapEvent)2 SourceNotFoundException (com.nextdoor.bender.utils.SourceUtils.SourceNotFoundException)2 DynamodbStreamRecord (com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord)1 KinesisEventRecord (com.amazonaws.services.lambda.runtime.events.KinesisEvent.KinesisEventRecord)1 SNSRecord (com.amazonaws.services.lambda.runtime.events.SNSEvent.SNSRecord)1 S3EventNotification (com.amazonaws.services.s3.event.S3EventNotification)1 GenericTransportBuffer (com.nextdoor.bender.ipc.generic.GenericTransportBuffer)1