Search in sources :

Example 1 with GenericJsonEvent

use of com.nextdoor.bender.deserializer.json.GenericJsonEvent in project bender by Nextdoor.

the class DeleteFieldOperationTest method testExistingField.

@Test
public void testExistingField() {
    InternalEvent ievent = new InternalEvent("foo", null, 1);
    JsonObject actualJsonObject = new JsonObject();
    actualJsonObject.addProperty("ex", "exists");
    GenericJsonEvent jsonEvent = new GenericJsonEvent(actualJsonObject);
    ievent.setEventObj(jsonEvent);
    DeleteFieldOperation operation = new DeleteFieldOperation("ex");
    operation.perform(ievent);
    JsonObject expectedJsonObject = new JsonObject();
    assertEquals(expectedJsonObject, actualJsonObject);
}
Also used : GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonObject(com.google.gson.JsonObject) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Example 2 with GenericJsonEvent

use of com.nextdoor.bender.deserializer.json.GenericJsonEvent in project bender by Nextdoor.

the class DeleteFieldOperationTest method testNonExistingField.

@Test
public void testNonExistingField() {
    InternalEvent ievent = new InternalEvent("foo", null, 1);
    JsonObject actualJsonObject = new JsonObject();
    actualJsonObject.addProperty("different", "exists");
    GenericJsonEvent jsonEvent = new GenericJsonEvent(actualJsonObject);
    ievent.setEventObj(jsonEvent);
    DeleteFieldOperation operation = new DeleteFieldOperation("ex");
    operation.perform(ievent);
    JsonObject expectedJsonObject = new JsonObject();
    expectedJsonObject.addProperty("different", "exists");
    assertEquals(expectedJsonObject, actualJsonObject);
}
Also used : GenericJsonEvent(com.nextdoor.bender.deserializer.json.GenericJsonEvent) JsonObject(com.google.gson.JsonObject) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Example 3 with GenericJsonEvent

use of com.nextdoor.bender.deserializer.json.GenericJsonEvent in project bender by Nextdoor.

the class RegexFilterOperationTest method testFilterMatches.

@Test
public void testFilterMatches() throws IOException, FieldNotFoundException {
    JsonParser parser = new JsonParser();
    JsonElement input = parser.parse(getResourceString("filter_input.json"));
    GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject());
    InternalEvent ievent = new InternalEvent("", null, 0);
    ievent.setEventObj(devent);
    String path = "$.simple_object.key";
    Pattern patternMatch = Pattern.compile("(val)");
    Pattern patternFail = Pattern.compile("(fail)");
    Boolean match = true;
    assertNotNull(devent.getFieldAsString(path));
    RegexFilterOperation opMatch = new RegexFilterOperation(patternMatch, path, match);
    RegexFilterOperation opFail = new RegexFilterOperation(patternFail, path, match);
    assertFalse(opMatch.test(ievent));
    assertTrue(opFail.test(ievent));
}
Also used : Pattern(java.util.regex.Pattern) 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)

Example 4 with GenericJsonEvent

use of com.nextdoor.bender.deserializer.json.GenericJsonEvent in project bender by Nextdoor.

the class ArraySplitOperationTest method testEventString.

@Test
public void testEventString() 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(InternalEvent::getEventString).collect(Collectors.toList());
    List<String> expected = Arrays.asList("{\"foo\":1}", "{\"foo\":2}", "{\"foo\":3}");
    assertEquals(expected, actual);
}
Also used : 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)

Example 5 with GenericJsonEvent

use of com.nextdoor.bender.deserializer.json.GenericJsonEvent in project bender by Nextdoor.

the class ArraySplitOperationTest method testTimeMatch.

@Test
public void testTimeMatch() 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, 123);
    ievent.setEventObj(devent);
    ievent.setEventTime(124);
    List<InternalEvent> events = operation.perform(ievent);
    assertEquals(ievent.getArrivalTime(), events.get(0).getArrivalTime());
    assertEquals(ievent.getEventTime(), events.get(0).getEventTime());
    assertEquals(ievent.getArrivalTime(), events.get(1).getArrivalTime());
    assertEquals(ievent.getEventTime(), events.get(1).getEventTime());
    assertEquals(ievent.getArrivalTime(), events.get(2).getArrivalTime());
    assertEquals(ievent.getEventTime(), events.get(2).getEventTime());
}
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)

Aggregations

GenericJsonEvent (com.nextdoor.bender.deserializer.json.GenericJsonEvent)17 InternalEvent (com.nextdoor.bender.InternalEvent)15 Test (org.junit.Test)15 JsonElement (com.google.gson.JsonElement)13 OperationTest (com.nextdoor.bender.operations.json.OperationTest)13 JsonParser (com.google.gson.JsonParser)12 ArraySplitOperation (com.nextdoor.bender.operation.json.array.ArraySplitOperation)8 LambdaContext (com.nextdoor.bender.LambdaContext)7 TestContext (com.nextdoor.bender.aws.TestContext)7 JsonObject (com.google.gson.JsonObject)4 Pattern (java.util.regex.Pattern)4 DeserializationException (com.nextdoor.bender.deserializer.DeserializationException)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Matcher (com.google.re2j.Matcher)1 OperationException (com.nextdoor.bender.operation.OperationException)1 JsonRootNodeOperation (com.nextdoor.bender.operation.json.key.JsonRootNodeOperation)1 JsonRootNodeOperationFactory (com.nextdoor.bender.operation.json.key.JsonRootNodeOperationFactory)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Arrays (java.util.Arrays)1