use of com.nextdoor.bender.deserializer.FieldNotFoundException 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");
String missingNestedField = "$.an_obj.baz";
String expectedErrorMessage = missingNestedField + " is not in payload.";
GenericJsonDeserializer deser = new GenericJsonDeserializer(Collections.emptyList());
deser.init();
DeserializedEvent event = deser.deserialize(input);
try {
event.getField(missingNestedField);
fail();
} catch (FieldNotFoundException e) {
assertEquals(e.getMessage(), expectedErrorMessage);
}
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class PartitionOperationTest method testGetEvaluatedPartitionsStringMultipleFieldsNull.
@Test(expected = OperationException.class)
public void testGetEvaluatedPartitionsStringMultipleFieldsNull() throws FieldNotFoundException {
List<PartitionSpec> partitionSpecs = new ArrayList<PartitionSpec>(1);
List<String> sources = Arrays.asList("one", "two");
PartitionSpec spec = new PartitionSpec("foo", sources, PartitionSpec.Interpreter.STRING);
partitionSpecs.add(spec);
PartitionOperation op = new PartitionOperation(partitionSpecs);
InternalEvent ievent = new InternalEvent("foo", null, 1);
DummyStringEvent devent = spy(new DummyStringEvent("baz"));
ievent.setEventObj(devent);
doThrow(FieldNotFoundException.class).doThrow(FieldNotFoundException.class).when(devent).getFieldAsString(any());
try {
op.perform(ievent);
} catch (OperationException e) {
assertEquals("unable to find value for partition 'foo'", e.getMessage());
throw e;
}
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class PartitionOperationTest method testGetEvaluatedPartitionsStringMultipleFields.
@Test
public void testGetEvaluatedPartitionsStringMultipleFields() throws FieldNotFoundException {
List<PartitionSpec> partitionSpecs = new ArrayList<PartitionSpec>(1);
List<String> sources = Arrays.asList("one", "two");
PartitionSpec spec = new PartitionSpec("foo", sources, PartitionSpec.Interpreter.STRING);
partitionSpecs.add(spec);
PartitionOperation op = new PartitionOperation(partitionSpecs);
InternalEvent ievent = new InternalEvent("foo", null, 1);
DummyStringEvent devent = spy(new DummyStringEvent(""));
ievent.setEventObj(devent);
doThrow(FieldNotFoundException.class).doReturn("5").when(devent).getFieldAsString(any());
op.perform(ievent);
LinkedHashMap<String, String> actual = ievent.getPartitions();
LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>(1);
expected.put("foo", "5");
assertEquals(expected, actual);
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class GenericJsonEvent method removeField.
@Override
public Object removeField(String fieldName) throws FieldNotFoundException {
if (this.payload == null) {
throw new FieldNotFoundException(fieldName + " is not in payload because payload is null");
}
Object o = getField(fieldName);
JsonPathProvider.delete(this.payload, fieldName);
return o;
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class GenericJsonEvent method getField.
@Override
public Object getField(String field) throws FieldNotFoundException {
if (this.payload == null) {
throw new FieldNotFoundException(field + " is not in payload because payload is null");
}
JsonObject json = this.payload.getAsJsonObject();
Object obj;
try {
obj = JsonPathProvider.read(json, field);
} catch (InvalidPathException e) {
throw new FieldNotFoundException("Field cannot be found because " + field + " is an invalid path");
}
if (obj == null || obj instanceof JsonNull) {
throw new FieldNotFoundException(field + " is not in payload.");
}
if (obj instanceof JsonPrimitive) {
if (((JsonPrimitive) obj).isString()) {
return ((JsonPrimitive) obj).getAsString();
}
}
return obj;
}
Aggregations