Search in sources :

Example 1 with WsClientException

use of oap.ws.WsClientException in project oap by oaplatform.

the class JsonPartialValidatorPeer method validate.

@Override
@SneakyThrows
public ValidationErrors validate(Object value, LinkedHashMap<Reflection.Parameter, Object> originalValues) {
    try {
        final Object objectId = getValue(originalValues, validate.idParameterName());
        final String id = objectId instanceof Optional ? ((Optional) objectId).get().toString() : objectId.toString();
        final Object root = method.invoke(instance, id);
        if (root == null) {
            return ValidationErrors.empty();
        }
        final String fetchedRoot = Binder.json.marshal(Binder.json.clone(root));
        log.trace("Retrieved object [{}] with id [{}]", fetchedRoot, id);
        final Map rootMap = Binder.json.unmarshal(Map.class, fetchedRoot);
        final Map partialValue = Binder.json.unmarshal(Map.class, (String) value);
        Map child = rootMap;
        for (String pathElement : validate.path().split("(?<=})\\.")) {
            if (pathElement.contains("$")) {
                final String[] split = pathElement.split("\\.");
                final Object next = child.get(split[0]);
                Preconditions.checkState(next != null, "schema has no elements for value " + split[0]);
                Preconditions.checkState(next instanceof List, split[0] + " should be of type list");
                final Object idValue = ((Optional) getValue(originalValues, split[1].replaceAll("\\$|\\{|\\}", ""))).get();
                final Optional matchedChild = ((List) next).stream().filter(o -> idValue.equals(((Map) o).get("id").toString())).findFirst();
                child = (Map) matchedChild.get();
                continue;
            }
            final Object next = child.get(pathElement);
            if (next == null) {
                child.put(pathElement, Collections.singletonList(partialValue));
                break;
            } else if (next instanceof List) {
                final List childElements = (List) next;
                childElements.removeIf(elements -> {
                    final Object partialValueId = partialValue.get("id");
                    return partialValueId != null && partialValueId.toString().equals(((Map) elements).get("id").toString());
                });
                childElements.add(partialValue);
                child.put(pathElement, childElements);
                break;
            } else {
                child = (Map) next;
            }
        }
        return ValidationErrors.errors(factory.validate(rootMap, validate.ignoreRequired()));
    } catch (JsonException e) {
        throw new WsClientException(e.getMessage(), e);
    }
}
Also used : Binder(oap.json.Binder) ResourceSchemaStorage(oap.json.schema.ResourceSchemaStorage) WsException(oap.ws.WsException) SneakyThrows(lombok.SneakyThrows) Reflection(oap.reflect.Reflection) JsonSchema(oap.json.schema.JsonSchema) JsonException(oap.json.JsonException) Reflect(oap.reflect.Reflect) LinkedHashMap(java.util.LinkedHashMap) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) WsClientException(oap.ws.WsClientException) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) JsonException(oap.json.JsonException) Optional(java.util.Optional) WsClientException(oap.ws.WsClientException) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SneakyThrows(lombok.SneakyThrows)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Binder (oap.json.Binder)1 JsonException (oap.json.JsonException)1 JsonSchema (oap.json.schema.JsonSchema)1 ResourceSchemaStorage (oap.json.schema.ResourceSchemaStorage)1 Reflect (oap.reflect.Reflect)1 Reflection (oap.reflect.Reflection)1 WsClientException (oap.ws.WsClientException)1 WsException (oap.ws.WsException)1