Search in sources :

Example 6 with FieldNotFoundException

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

the class JsonRootNodeOperation method mutateEvent.

/**
 * The {@link DeserializedEvent} payload must be a {@link JsonObject}.
 *
 * @param event Event with payload to mutate.
 */
protected void mutateEvent(DeserializedEvent event) throws OperationException {
    Object payload = event.getPayload();
    if (payload == null) {
        return;
    }
    if (!(payload instanceof JsonObject)) {
        throw new OperationException("Payload data is not a JsonObject");
    }
    Object o;
    try {
        o = event.getField(path);
    } catch (FieldNotFoundException e) {
        throw new OperationException(e);
    }
    if (!(o instanceof JsonObject)) {
        throw new OperationException("specified node '" + path + "' is not an object");
    }
    event.setPayload(o);
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) OperationException(com.nextdoor.bender.operation.OperationException)

Example 7 with FieldNotFoundException

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

the class TimeOperation method perform.

@Override
public InternalEvent perform(InternalEvent ievent) {
    String field;
    try {
        field = ievent.getEventObj().getFieldAsString(timeField);
    } catch (FieldNotFoundException e) {
        throw new OperationException("time field " + timeField + " does not exist in " + ievent.getEventString());
    }
    ievent.setEventTime(getTimestamp(field, timeFieldType));
    return ievent;
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) OperationException(com.nextdoor.bender.operation.OperationException)

Example 8 with FieldNotFoundException

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

the class GenericJsonDeserializerTest method testGetMissingField.

@Test
public void testGetMissingField() throws UnsupportedEncodingException, IOException {
    String input = TestUtils.getResourceString(this.getClass(), "basic.json");
    String missingField = "$.not_a_member";
    String expectedErrorMessage = missingField + " is not in payload.";
    GenericJsonDeserializer deser = new GenericJsonDeserializer(Collections.emptyList());
    deser.init();
    DeserializedEvent event = deser.deserialize(input);
    try {
        event.getField(missingField);
        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 9 with FieldNotFoundException

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

the class RegexFilterOperation method test.

/**
 * Returns true if the event matches the filter.
 *
 * @param ievent {@link DeserializedEvent} has payload to filter against.
 */
@Override
public boolean test(InternalEvent ievent) {
    DeserializedEvent devent = ievent.getEventObj();
    if (devent == null) {
        return false;
    }
    boolean found;
    try {
        String field = devent.getFieldAsString(this.path);
        found = this.pattern.matcher(field).matches();
    } catch (FieldNotFoundException e) {
        found = false;
    }
    /*
     * When false is returned then the event is excluded from the stream.
     */
    return this.exclude != found;
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException)

Example 10 with FieldNotFoundException

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

the class GenericJsonEventTest method testRemoveMissingField.

@Test(expected = FieldNotFoundException.class)
public void testRemoveMissingField() throws FieldNotFoundException {
    GenericJsonEvent event = getEmptyEvent();
    JsonObject payload = (JsonObject) event.getPayload();
    try {
        Object obj = event.removeField("$.foo");
    } catch (FieldNotFoundException e) {
        assertEquals(1, payload.size());
        assertEquals("b", payload.get("a").getAsString());
        throw e;
    }
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

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