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());
}
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"));
}
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());
}
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"));
}
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"));
}
Aggregations