Search in sources :

Example 16 with DeserializedEvent

use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.

the class GenericJsonDeserializerTest method testInvalidNestedJson.

@Test
public void testInvalidNestedJson() throws UnsupportedEncodingException, IOException {
    DeserializedEvent devent = getEvent("nested_invalid.json");
    JsonObject obj = (JsonObject) devent.getPayload();
    /*
     * Verify that when there is invalid json MESSAGE is kept as a string
     */
    assertTrue(obj.has("MESSAGE"));
    assertTrue(obj.get("MESSAGE").isJsonPrimitive());
    assertTrue(obj.get("MESSAGE").getAsJsonPrimitive().isString());
    assertEquals("{\"a_string: \"foo\"}}", obj.get("MESSAGE").getAsString());
    /*
     * Double check another member wasn't dropped
     */
    assertTrue(obj.has("a_num"));
    assertTrue(obj.get("a_num").isJsonPrimitive());
    assertTrue(obj.get("a_num").getAsJsonPrimitive().isNumber());
    assertEquals(123, obj.get("a_num").getAsInt());
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 17 with DeserializedEvent

use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.

the class GenericJsonDeserializerTest method testBasicJson.

@Test
public void testBasicJson() throws UnsupportedEncodingException, IOException {
    DeserializedEvent devent = getEvent("basic.json");
    /*
     * Verify payload type
     */
    assertNotNull(devent.getPayload());
    assertEquals(devent.getPayload().getClass(), JsonObject.class);
    /*
     * Verify payload data
     */
    JsonObject obj = (JsonObject) devent.getPayload();
    assertTrue(obj.has("a_string"));
    assertTrue(obj.get("a_string").isJsonPrimitive());
    assertTrue(obj.get("a_string").getAsJsonPrimitive().isString());
    assertEquals("foo", obj.get("a_string").getAsString());
    assertTrue(obj.has("a_bool"));
    assertTrue(obj.get("a_bool").isJsonPrimitive());
    assertTrue(obj.get("a_bool").getAsJsonPrimitive().isBoolean());
    assertEquals(true, obj.get("a_bool").getAsBoolean());
    assertTrue(obj.has("a_number"));
    assertTrue(obj.get("a_number").isJsonPrimitive());
    assertTrue(obj.get("a_number").getAsJsonPrimitive().isNumber());
    assertEquals(1, obj.get("a_number").getAsInt());
    assertTrue(obj.has("an_obj"));
    assertTrue(obj.get("an_obj").isJsonObject());
    assertTrue(obj.get("an_obj").getAsJsonObject().has("foo"));
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 18 with DeserializedEvent

use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.

the class GenericJsonDeserializerTest method testRootNodeChangeNested.

@Test
public void testRootNodeChangeNested() throws UnsupportedEncodingException, IOException {
    DeserializedEvent devent = getEvent("nested.json", "$.MESSAGE.an_obj");
    JsonObject obj = (JsonObject) devent.getPayload();
    /*
     * Verify sub nested json was also converted
     */
    assertTrue(obj.has("a_num"));
    assertEquals(123, obj.get("a_num").getAsInt());
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 19 with DeserializedEvent

use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.

the class GenericJsonDeserializerTest method testGetMissingNestedField.

@Test
public void testGetMissingNestedField() throws UnsupportedEncodingException, IOException {
    String input = TestUtils.getResourceString(this.getClass(), "basic.json");
    GenericJsonDeserializer deser = new GenericJsonDeserializer(Collections.emptyList());
    deser.init();
    DeserializedEvent event = deser.deserialize(input);
    assertEquals(null, event.getField("$.an_obj.baz"));
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) Test(org.junit.Test)

Example 20 with DeserializedEvent

use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.

the class GenericJsonDeserializerTest method testGetField.

@Test
public void testGetField() throws UnsupportedEncodingException, IOException {
    String input = TestUtils.getResourceString(this.getClass(), "basic.json");
    GenericJsonDeserializer deser = new GenericJsonDeserializer(Collections.emptyList());
    deser.init();
    DeserializedEvent event = deser.deserialize(input);
    assertEquals("foo", event.getField("$.a_string"));
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) Test(org.junit.Test)

Aggregations

DeserializedEvent (com.nextdoor.bender.deserializer.DeserializedEvent)23 Test (org.junit.Test)20 JsonObject (com.google.gson.JsonObject)11 ArrayList (java.util.ArrayList)7 Pattern (java.util.regex.Pattern)4 Pattern (com.google.re2j.Pattern)3 OperationException (com.nextdoor.bender.operation.OperationException)2 Context (com.amazonaws.services.lambda.runtime.Context)1 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 JsonElement (com.google.gson.JsonElement)1 InternalEvent (com.nextdoor.bender.InternalEvent)1 AmazonS3ClientFactory (com.nextdoor.bender.aws.AmazonS3ClientFactory)1 BenderConfig (com.nextdoor.bender.config.BenderConfig)1 ConfigurationException (com.nextdoor.bender.config.ConfigurationException)1 HandlerResources (com.nextdoor.bender.config.HandlerResources)1 Source (com.nextdoor.bender.config.Source)1 DeserializerProcessor (com.nextdoor.bender.deserializer.DeserializerProcessor)1 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)1 TransportException (com.nextdoor.bender.ipc.TransportException)1 BenderLayout (com.nextdoor.bender.logging.BenderLayout)1