Search in sources :

Example 6 with ArraySplitOperation

use of com.nextdoor.bender.operation.json.array.ArraySplitOperation 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 7 with ArraySplitOperation

use of com.nextdoor.bender.operation.json.array.ArraySplitOperation 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 8 with ArraySplitOperation

use of com.nextdoor.bender.operation.json.array.ArraySplitOperation in project bender by Nextdoor.

the class ArraySplitOperationTest method testEventObject.

@Test
public void testEventObject() throws JsonSyntaxException, UnsupportedEncodingException, IOException {
    JsonParser parser = new JsonParser();
    JsonElement input = parser.parse(getResourceString("array_input.json"));
    GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject());
    ArraySplitOperation operation = new ArraySplitOperation("$.arr");
    InternalEvent ievent = new InternalEvent("", null, 0);
    ievent.setEventObj(devent);
    List<String> actual = operation.perform(ievent).stream().map(i -> {
        return i.getEventObj().getPayload().toString();
    }).collect(Collectors.toList());
    List<String> expected = Arrays.asList("{\"foo\":1}", "{\"foo\":2}", "{\"foo\":3}");
    assertEquals(expected, actual);
}
Also used : Arrays(java.util.Arrays) ArraySplitOperation(com.nextdoor.bender.operation.json.array.ArraySplitOperation) JsonSyntaxException(com.google.gson.JsonSyntaxException) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) InternalEvent(com.nextdoor.bender.InternalEvent) JsonParser(com.google.gson.JsonParser) Collectors(java.util.stream.Collectors) TestContext(com.nextdoor.bender.aws.TestContext) OperationTest(com.nextdoor.bender.operations.json.OperationTest) JsonElement(com.google.gson.JsonElement) List(java.util.List) GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) Map(java.util.Map) LambdaContext(com.nextdoor.bender.LambdaContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Assert.assertEquals(org.junit.Assert.assertEquals) OperationException(com.nextdoor.bender.operation.OperationException) ArraySplitOperation(com.nextdoor.bender.operation.json.array.ArraySplitOperation) GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test) OperationTest(com.nextdoor.bender.operations.json.OperationTest)

Aggregations

JsonElement (com.google.gson.JsonElement)8 InternalEvent (com.nextdoor.bender.InternalEvent)8 GenericJsonEvent (com.nextdoor.bender.deserializer.json.GenericJsonEvent)8 ArraySplitOperation (com.nextdoor.bender.operation.json.array.ArraySplitOperation)8 OperationTest (com.nextdoor.bender.operations.json.OperationTest)8 Test (org.junit.Test)8 JsonParser (com.google.gson.JsonParser)7 LambdaContext (com.nextdoor.bender.LambdaContext)7 TestContext (com.nextdoor.bender.aws.TestContext)7 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 OperationException (com.nextdoor.bender.operation.OperationException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1