Search in sources :

Example 11 with FieldNotFoundException

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);
    }
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) Test(org.junit.Test)

Example 12 with FieldNotFoundException

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;
    }
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) DummyStringEvent(com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyStringEvent) OperationException(com.nextdoor.bender.operation.OperationException) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Example 13 with FieldNotFoundException

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);
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) DummyStringEvent(com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyStringEvent) InternalEvent(com.nextdoor.bender.InternalEvent) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 14 with FieldNotFoundException

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;
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) JsonObject(com.google.gson.JsonObject)

Example 15 with FieldNotFoundException

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;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) InvalidPathException(com.jayway.jsonpath.InvalidPathException) JsonNull(com.google.gson.JsonNull)

Aggregations

FieldNotFoundException (com.nextdoor.bender.deserializer.FieldNotFoundException)17 OperationException (com.nextdoor.bender.operation.OperationException)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 JsonObject (com.google.gson.JsonObject)4 DeserializedEvent (com.nextdoor.bender.deserializer.DeserializedEvent)3 ArrayList (java.util.ArrayList)3 InternalEvent (com.nextdoor.bender.InternalEvent)2 DummyStringEvent (com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyStringEvent)2 Map (java.util.Map)2 JsonNull (com.google.gson.JsonNull)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 InvalidPathException (com.jayway.jsonpath.InvalidPathException)1 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)1 CityResponse (com.maxmind.geoip2.model.CityResponse)1 Variable (com.nextdoor.bender.operation.substitution.Variable)1 RegexSubField (com.nextdoor.bender.operation.substitution.regex.RegexSubstitutionConfig.RegexSubField)1 GeoProperty (com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1