Search in sources :

Example 1 with TestContext

use of com.nextdoor.bender.aws.TestContext in project bender by Nextdoor.

the class BaseHandlerS3Test method testS3Config.

@Test
public void testS3Config() throws HandlerException {
    BaseHandler handler = new DummyHandler();
    handler.CONFIG_FILE = null;
    handler.s3ClientFactory = this.clientFactory;
    List<DummyEvent> events = new ArrayList<DummyEvent>(1);
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test");
    handler.handler(events, context);
    assertEquals("s3://" + S3_BUCKET + "/bender/config.json", handler.config.getConfigFile());
    assertEquals("Test Events", handler.config.getSources().get(0).getName());
}
Also used : DummyHandler(com.nextdoor.bender.handler.BaseHandlerTest.DummyHandler) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) DummyEvent(com.nextdoor.bender.handler.BaseHandlerTest.DummyEvent) Test(org.junit.Test)

Example 2 with TestContext

use of com.nextdoor.bender.aws.TestContext in project bender by Nextdoor.

the class BaseHandlerTest method testRegex.

@Test
public void testRegex() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config_regex.json";
    List<DummyEvent> events = new ArrayList<DummyEvent>(2);
    events.add(new DummyEvent("foo", 0));
    events.add(new DummyEvent("bar", 0));
    events.add(new DummyEvent("loop", 0));
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
    handler.handler(events, context);
    /*
     * Verify Events made it all the way through
     */
    assertEquals(1, BufferedTransporter.output.size());
    assertEquals("bar", BufferedTransporter.output.get(0));
}
Also used : TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with TestContext

use of com.nextdoor.bender.aws.TestContext in project bender by Nextdoor.

the class BaseHandlerTest method testIpcOnAddFailure.

@Test
public void testIpcOnAddFailure() throws Throwable {
    BaseHandler.CONFIG_FILE = "/config/handler_config.json";
    handler.skipWriteStats = true;
    List<DummyEvent> events = new ArrayList<DummyEvent>(2);
    events.add(new DummyEvent("foo", 0));
    events.add(new DummyEvent("bar", 0));
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
    handler.init(context);
    TransportBuffer tbSpy1 = spy(new ArrayTransportBuffer());
    TransportBuffer tbSpy2 = spy(new ArrayTransportBuffer());
    doCallRealMethod().doCallRealMethod().when(tbSpy1).add(any());
    doThrow(new IllegalStateException("expected")).when(tbSpy2).add(any());
    IpcSenderService spyIpc = spy(handler.getIpcService());
    TransportFactory tfSpy = spy(spyIpc.getTransportFactory());
    when(tfSpy.newTransportBuffer()).thenReturn(tbSpy1, tbSpy2);
    spyIpc.setTransportFactory(tfSpy);
    handler.setIpcService(spyIpc);
    handler.handler(events, context);
    assertEquals(1, spyIpc.getSuccessCountStat().getValue());
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) TransportFactory(com.nextdoor.bender.ipc.TransportFactory) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) ArrayTransportBuffer(com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer) TransportBuffer(com.nextdoor.bender.ipc.TransportBuffer) ArrayTransportBuffer(com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer) Test(org.junit.Test)

Example 4 with TestContext

use of com.nextdoor.bender.aws.TestContext in project bender by Nextdoor.

the class BaseHandlerTest method testFilterNullDeserialization.

@Test
public void testFilterNullDeserialization() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config.json";
    handler.skipWriteStats = true;
    List<DummyEvent> events = new ArrayList<DummyEvent>(1);
    events.add(new DummyEvent("foo", 0));
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
    handler.init(context);
    DeserializerProcessor proc = handler.sources.get(0).getDeserProcessor();
    Deserializer deserSpy = spy(proc.getDeserializer());
    when(deserSpy.deserialize(anyString())).thenReturn(null);
    proc.setDeserializer(deserSpy);
    handler.handler(events, context);
    assertEquals(0, BufferedTransporter.output.size());
}
Also used : Deserializer(com.nextdoor.bender.deserializer.Deserializer) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) DeserializerProcessor(com.nextdoor.bender.deserializer.DeserializerProcessor) Test(org.junit.Test)

Example 5 with TestContext

use of com.nextdoor.bender.aws.TestContext in project bender by Nextdoor.

the class S3HandlerTest method testSourceRegexFail.

@Test
public void testSourceRegexFail() throws Throwable {
    BaseHandler.CONFIG_FILE = "/com/nextdoor/bender/handler/config_s3_source.json";
    TestContext ctx = new TestContext();
    ctx.setFunctionName("unittest");
    ctx.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test-function:staging");
    BaseHandler<S3EventNotification> handler = (BaseHandler) getHandler();
    handler.init(ctx);
    handler.handler(getTestEvent("foo", false), ctx);
    assertEquals(0, DummyTransportHelper.BufferedTransporter.output.size());
}
Also used : S3EventNotification(com.amazonaws.services.s3.event.S3EventNotification) TestContext(com.nextdoor.bender.aws.TestContext) BaseHandler(com.nextdoor.bender.handler.BaseHandler) HandlerTest(com.nextdoor.bender.handler.HandlerTest) Test(org.junit.Test)

Aggregations

TestContext (com.nextdoor.bender.aws.TestContext)37 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)19 InternalEvent (com.nextdoor.bender.InternalEvent)10 HashMap (java.util.HashMap)10 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)9 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)9 LinkedHashMap (java.util.LinkedHashMap)9 DeserializationException (com.nextdoor.bender.deserializer.DeserializationException)6 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)6 TransportException (com.nextdoor.bender.ipc.TransportException)6 HandlerTest (com.nextdoor.bender.handler.HandlerTest)5 OperationException (com.nextdoor.bender.operation.OperationException)4 IOException (java.io.IOException)4 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)3 Deserializer (com.nextdoor.bender.deserializer.Deserializer)3 DeserializerProcessor (com.nextdoor.bender.deserializer.DeserializerProcessor)3 SerializationException (com.nextdoor.bender.serializer.SerializationException)3 S3EventNotification (com.amazonaws.services.s3.event.S3EventNotification)2 BaseHandler (com.nextdoor.bender.handler.BaseHandler)2