Search in sources :

Example 41 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class DataTemplateUtil method areEqual.

@SuppressWarnings("unchecked")
private static <T extends DataTemplate<?>> boolean areEqual(T data1, T data2, ValidationOptions validationOption) {
    if (data1 == null || data2 == null) {
        return data1 == data2;
    }
    // return true if data1 and data2 are already equal
    if (data1.equals(data2)) {
        return true;
    }
    // since fix-up will modify the original data, we need to make a copy before performing fix-up to avoid such side-effects.
    try {
        T data1Copy = (T) data1.copy();
        T data2Copy = (T) data2.copy();
        ValidationResult validateResult1 = ValidateDataAgainstSchema.validate(data1Copy, validationOption);
        ValidationResult validateResult2 = ValidateDataAgainstSchema.validate(data2Copy, validationOption);
        if (validateResult1.hasFix() || validateResult2.hasFix()) {
            return data1Copy.equals(data2Copy);
        } else {
            // no fix-up is done
            return false;
        }
    } catch (CloneNotSupportedException e) {
        return false;
    }
}
Also used : ValidationResult(com.linkedin.data.schema.validation.ValidationResult)

Example 42 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateResourceFacade.

private JDefinedClass generateResourceFacade(ResourceSchema resource, File sourceFile, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, String rootPath) throws JClassAlreadyExistsException, IOException {
    final ValidationResult validationResult = ValidateDataAgainstSchema.validate(resource.data(), resource.schema(), new ValidationOptions());
    if (!validationResult.isValid()) {
        throw new IllegalArgumentException(String.format("Resource validation error.  Resource File '%s', Error Details '%s'", sourceFile, validationResult.toString()));
    }
    final String packageName = resource.getNamespace();
    final JPackage clientPackage = (packageName == null || packageName.isEmpty()) ? getPackage() : getPackage(packageName);
    final String className;
    if (_version == RestliVersion.RESTLI_2_0_0) {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_2_0_0, null, resource.getName(), true);
    } else {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_1_0_0, null, resource.getName(), true);
    }
    final JDefinedClass facadeClass = clientPackage._class(className);
    annotate(facadeClass, sourceFile.getAbsolutePath(), rootPath);
    final JFieldVar baseUriField;
    final JFieldVar requestOptionsField;
    final JExpression baseUriGetter = JExpr.invoke("getBaseUriTemplate");
    final JExpression requestOptionsGetter = JExpr.invoke("getRequestOptions");
    if (_version == RestliVersion.RESTLI_2_0_0) {
        baseUriField = null;
        requestOptionsField = null;
        facadeClass._extends(BuilderBase.class);
    } else {
        // for old builder, instead of extending from RequestBuilderBase, add fields and getters in the class
        baseUriField = facadeClass.field(JMod.PRIVATE | JMod.FINAL, String.class, "_baseUriTemplate");
        requestOptionsField = facadeClass.field(JMod.PRIVATE, RestliRequestOptions.class, "_requestOptions");
        facadeClass.method(JMod.PRIVATE, String.class, "getBaseUriTemplate").body()._return(baseUriField);
        facadeClass.method(JMod.PUBLIC, RestliRequestOptions.class, "getRequestOptions").body()._return(requestOptionsField);
    }
    // make the original resource path available via a private final static variable.
    final JFieldVar originalResourceField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, String.class, "ORIGINAL_RESOURCE_PATH");
    final String resourcePath = getResourcePath(resource.getPath());
    originalResourceField.init(JExpr.lit(resourcePath));
    // create reference to RestliRequestOptions.DEFAULT_OPTIONS
    final JClass restliRequestOptionsClass = getCodeModel().ref(RestliRequestOptions.class);
    final JFieldRef defaultOptionsField = restliRequestOptionsClass.staticRef("DEFAULT_OPTIONS");
    if (_version == RestliVersion.RESTLI_1_0_0) {
        // same getPathComponents() logic as in RequestBuilderBase
        final JMethod pathComponentsGetter = facadeClass.method(JMod.PUBLIC, String[].class, "getPathComponents");
        pathComponentsGetter.body()._return(getCodeModel().ref(URIParamUtils.class).staticInvoke("extractPathComponentsFromUriTemplate").arg(baseUriField));
        // method that expresses the following logic
        // (requestOptions == null) ? return RestliRequestOptions.DEFAULT_OPTIONS : requestOptions;
        final JMethod requestOptionsAssigner = facadeClass.method(JMod.PRIVATE | JMod.STATIC, RestliRequestOptions.class, "assignRequestOptions");
        final JVar requestOptionsAssignerParam = requestOptionsAssigner.param(RestliRequestOptions.class, "requestOptions");
        final JConditional requestNullCheck = requestOptionsAssigner.body()._if(requestOptionsAssignerParam.eq(JExpr._null()));
        requestNullCheck._then().block()._return(defaultOptionsField);
        requestNullCheck._else().block()._return(requestOptionsAssignerParam);
    }
    /*
    There will be 4 constructors:
      ()
      (RestliRequestOptions)
      (String)
      (String, RestliRequestOptions)
    */
    final JMethod noArgConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod requestOptionsOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod resourceNameOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod mainConstructor = facadeClass.constructor(JMod.PUBLIC);
    // no-argument constructor, delegates to the request options override constructor
    noArgConstructor.body().invoke(THIS).arg(defaultOptionsField);
    // request options override constructor
    final JVar requestOptionsOverrideOptionsParam = requestOptionsOverrideConstructor.param(RestliRequestOptions.class, "requestOptions");
    if (_version == RestliVersion.RESTLI_2_0_0) {
        requestOptionsOverrideConstructor.body().invoke(SUPER).arg(originalResourceField).arg(requestOptionsOverrideOptionsParam);
    } else {
        requestOptionsOverrideConstructor.body().assign(baseUriField, originalResourceField);
        final JInvocation requestOptionsOverrideAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(requestOptionsOverrideOptionsParam);
        requestOptionsOverrideConstructor.body().assign(requestOptionsField, requestOptionsOverrideAssignRequestOptions);
    }
    // primary resource name override constructor, delegates to the main constructor
    final JVar resourceNameOverrideResourceNameParam = resourceNameOverrideConstructor.param(_stringClass, "primaryResourceName");
    resourceNameOverrideConstructor.body().invoke(THIS).arg(resourceNameOverrideResourceNameParam).arg(defaultOptionsField);
    // main constructor
    final JVar mainConsResourceNameParam = mainConstructor.param(_stringClass, "primaryResourceName");
    final JVar mainConsOptionsParam = mainConstructor.param(RestliRequestOptions.class, "requestOptions");
    final JExpression baseUriExpr;
    if (resourcePath.contains("/")) {
        baseUriExpr = originalResourceField.invoke("replaceFirst").arg(JExpr.lit("[^/]*/")).arg(mainConsResourceNameParam.plus(JExpr.lit("/")));
    } else {
        baseUriExpr = mainConsResourceNameParam;
    }
    if (_version == RestliVersion.RESTLI_2_0_0) {
        mainConstructor.body().invoke(SUPER).arg(baseUriExpr).arg(mainConsOptionsParam);
    } else {
        final JInvocation mainAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(mainConsOptionsParam);
        mainConstructor.body().assign(baseUriField, baseUriExpr);
        mainConstructor.body().assign(requestOptionsField, mainAssignRequestOptions);
    }
    final String resourceName = CodeUtil.capitalize(resource.getName());
    final JMethod primaryResourceGetter = facadeClass.method(JMod.PUBLIC | JMod.STATIC, String.class, "getPrimaryResource");
    primaryResourceGetter.body()._return(originalResourceField);
    final List<String> pathKeys = getPathKeys(resourcePath, pathToAssocKeys);
    JClass keyTyperefClass = null;
    final JClass keyClass;
    JClass keyKeyClass = null;
    JClass keyParamsClass = null;
    final Class<?> resourceSchemaClass;
    Map<String, AssocKeyTypeInfo> assocKeyTypeInfos = Collections.emptyMap();
    StringArray supportsList = null;
    RestMethodSchemaArray restMethods = null;
    FinderSchemaArray finders = null;
    BatchFinderSchemaArray batchFinders = null;
    ResourceSchemaArray subresources = null;
    ActionSchemaArray resourceActions = null;
    ActionSchemaArray entityActions = null;
    final JFieldVar resourceSpecField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, _resourceSpecClass, "_resourceSpec");
    if (resource.getCollection() != null) {
        resourceSchemaClass = CollectionSchema.class;
        final CollectionSchema collection = resource.getCollection();
        final String keyName = collection.getIdentifier().getName();
        // ComplexKeyResource parameterized by those two.
        if (collection.getIdentifier().getParams() == null) {
            keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
            final JClass declaredClass = getClassRefForSchema(RestSpecCodec.textToSchema(collection.getIdentifier().getType(), _schemaResolver), facadeClass);
            if (!declaredClass.equals(keyClass)) {
                keyTyperefClass = declaredClass;
            }
        } else {
            keyKeyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
            keyParamsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
            keyClass = getCodeModel().ref(ComplexResourceKey.class).narrow(keyKeyClass, keyParamsClass);
        }
        pathKeyTypes.put(keyName, keyClass);
        supportsList = collection.getSupports();
        restMethods = collection.getMethods();
        finders = collection.getFinders();
        batchFinders = collection.getBatchFinders();
        subresources = collection.getEntity().getSubresources();
        resourceActions = collection.getActions();
        entityActions = collection.getEntity().getActions();
    } else if (resource.getAssociation() != null) {
        resourceSchemaClass = AssociationSchema.class;
        final AssociationSchema association = resource.getAssociation();
        keyClass = getCodeModel().ref(CompoundKey.class);
        supportsList = association.getSupports();
        restMethods = association.getMethods();
        finders = association.getFinders();
        batchFinders = association.getBatchFinders();
        subresources = association.getEntity().getSubresources();
        resourceActions = association.getActions();
        entityActions = association.getEntity().getActions();
        assocKeyTypeInfos = generateAssociationKey(facadeClass, association, resourceSpecField);
        final String keyName = getAssociationKey(resource, association);
        pathKeyTypes.put(keyName, keyClass);
        final List<String> assocKeys = new ArrayList<>(4);
        for (Map.Entry<String, AssocKeyTypeInfo> entry : assocKeyTypeInfos.entrySet()) {
            assocKeys.add(entry.getKey());
            assocKeyTypes.put(entry.getKey(), entry.getValue().getBindingType());
        }
        pathToAssocKeys.put(keyName, assocKeys);
    } else if (resource.getSimple() != null) {
        resourceSchemaClass = SimpleSchema.class;
        final SimpleSchema simpleSchema = resource.getSimple();
        keyClass = _voidClass;
        supportsList = simpleSchema.getSupports();
        restMethods = simpleSchema.getMethods();
        subresources = simpleSchema.getEntity().getSubresources();
        resourceActions = simpleSchema.getActions();
    } else if (resource.getActionsSet() != null) {
        resourceSchemaClass = ActionsSetSchema.class;
        final ActionsSetSchema actionsSet = resource.getActionsSet();
        resourceActions = actionsSet.getActions();
        keyClass = _voidClass;
    } else {
        throw new IllegalArgumentException("unsupported resource type for resource: '" + resourceName + '\'');
    }
    generateOptions(facadeClass, baseUriGetter, requestOptionsGetter);
    if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class || resourceSchemaClass == SimpleSchema.class) {
        final JClass schemaClass = getJavaBindingType(resource.getSchema(), null).schemaClass;
        final Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
        final JInvocation supportedMethodsExpr;
        if (supportedMethods.isEmpty()) {
            supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
        } else {
            supportedMethodsExpr = _enumSetClass.staticInvoke("of");
            for (ResourceMethod resourceMethod : supportedMethods) {
                validateResourceMethod(resourceSchemaClass, resourceName, resourceMethod);
                supportedMethodsExpr.arg(_resourceMethodClass.staticRef(resourceMethod.name()));
            }
        }
        final JBlock staticInit = facadeClass.init();
        final JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        final JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class) {
            final JClass assocKeyClass = getCodeModel().ref(TypeInfo.class);
            final JClass hashMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, assocKeyClass);
            final JVar keyPartsVar = staticInit.decl(hashMapClass, "keyParts").init(JExpr._new(hashMapClass));
            for (Map.Entry<String, AssocKeyTypeInfo> typeInfoEntry : assocKeyTypeInfos.entrySet()) {
                final AssocKeyTypeInfo typeInfo = typeInfoEntry.getValue();
                final JInvocation typeArg = JExpr._new(assocKeyClass).arg(typeInfo.getBindingType().dotclass()).arg(typeInfo.getDeclaredType().dotclass());
                staticInit.add(keyPartsVar.invoke("put").arg(typeInfoEntry.getKey()).arg(typeArg));
            }
            staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(keyTyperefClass == null ? keyClass.dotclass() : keyTyperefClass.dotclass()).arg(keyKeyClass == null ? JExpr._null() : keyKeyClass.dotclass()).arg(keyKeyClass == null ? JExpr._null() : keyParamsClass.dotclass()).arg(schemaClass.dotclass()).arg(keyPartsVar));
        } else // simple schema
        {
            staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(schemaClass.dotclass()));
        }
        generateBasicMethods(facadeClass, baseUriGetter, keyClass, schemaClass, supportedMethods, restMethods, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, resource.data().getDataMap("annotations"), rootPath);
        if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class) {
            generateFinders(facadeClass, baseUriGetter, finders, keyClass, schemaClass, assocKeyTypeInfos, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
            generateBatchFinders(facadeClass, baseUriGetter, batchFinders, keyClass, schemaClass, assocKeyTypeInfos, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
        }
        generateSubResources(sourceFile, subresources, pathKeyTypes, assocKeyTypes, pathToAssocKeys, rootPath);
    } else // action set
    {
        final JBlock staticInit = facadeClass.init();
        final JInvocation supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
        final JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        final JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(keyClass.dotclass()).arg(JExpr._null()).arg(JExpr._null()).arg(JExpr._null()).arg(getCodeModel().ref(Collections.class).staticInvoke("<String, Class<?>>emptyMap")));
    }
    generateActions(facadeClass, baseUriGetter, resourceActions, entityActions, keyClass, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
    generateClassJavadoc(facadeClass, resource);
    if (!checkVersionAndDeprecateBuilderClass(facadeClass, true)) {
        checkRestSpecAndDeprecateRootBuildersClass(facadeClass, resource);
    }
    return facadeClass;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BatchFinderSchemaArray(com.linkedin.restli.restspec.BatchFinderSchemaArray) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ActionsSetSchema(com.linkedin.restli.restspec.ActionsSetSchema) StringArray(com.linkedin.data.template.StringArray) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) BatchFinderSchemaArray(com.linkedin.restli.restspec.BatchFinderSchemaArray) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) JConditional(com.sun.codemodel.JConditional) ArrayList(java.util.ArrayList) DataList(com.linkedin.data.DataList) List(java.util.List) Collections(java.util.Collections) JVar(com.sun.codemodel.JVar) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) JFieldRef(com.sun.codemodel.JFieldRef) RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) JDefinedClass(com.sun.codemodel.JDefinedClass) SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) ResourceSchemaArray(com.linkedin.restli.restspec.ResourceSchemaArray) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JFieldVar(com.sun.codemodel.JFieldVar) URIParamUtils(com.linkedin.restli.internal.common.URIParamUtils) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DataMap(com.linkedin.data.DataMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 43 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class TestDataTranslator method testPegasusDefaultToAvroOptionalTranslation.

@Test(dataProvider = "defaultToAvroOptionalTranslationProvider", description = "generic record to data map should not care about the specific list implementation")
public void testPegasusDefaultToAvroOptionalTranslation(Object... testSchemaTextAndDataMap) throws IOException {
    // Test if the pegasus default field has been correctly translated
    // i.e. if value present, translate it
    // if no value present, don't translate it
    // Create pegasus schema text
    String rawPegasusTestSchemaText;
    PegasusToAvroDefaultFieldTranslationMode schemaTranslationMode = null;
    String expectedAvroSchemaString;
    String dataMapString;
    PegasusToAvroDefaultFieldTranslationMode dataTranslationMode = null;
    String expectedAvroRecordJsonString;
    rawPegasusTestSchemaText = (String) testSchemaTextAndDataMap[0];
    schemaTranslationMode = (PegasusToAvroDefaultFieldTranslationMode) testSchemaTextAndDataMap[1];
    expectedAvroSchemaString = (String) testSchemaTextAndDataMap[2];
    dataMapString = (String) testSchemaTextAndDataMap[3];
    dataTranslationMode = (PegasusToAvroDefaultFieldTranslationMode) testSchemaTextAndDataMap[4];
    expectedAvroRecordJsonString = (String) testSchemaTextAndDataMap[5];
    boolean isError = (boolean) testSchemaTextAndDataMap[6];
    String errorMsg = (String) testSchemaTextAndDataMap[7];
    List<String> schemaTextForTesting = null;
    try {
        // Test this also works for TypeRef
        if (rawPegasusTestSchemaText.contains("##T_START")) {
            String noTyperefSchemaText = rawPegasusTestSchemaText.replace("##T_START", "").replace("##T_END", "");
            String typerefSchemaText = rawPegasusTestSchemaText.replace("##T_START", "{ \"type\" : \"typeref\", \"name\" : \"Ref\", \"ref\" : ").replace("##T_END", "}");
            schemaTextForTesting = Arrays.asList(noTyperefSchemaText, typerefSchemaText);
        } else {
            schemaTextForTesting = Arrays.asList(rawPegasusTestSchemaText);
        }
        for (String pegasusSchemaText : schemaTextForTesting) {
            // Create pegasus schema
            RecordDataSchema recordDataSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(pegasusSchemaText);
            // Translate to Avro Schema so can create the GenericRecord data holder
            Schema avroSchema = SchemaTranslator.dataToAvroSchema(recordDataSchema, new DataToAvroSchemaTranslationOptions(schemaTranslationMode));
            // AvroSchema translated needs to be as expected
            Schema expectedAvroSchema = Schema.parse(expectedAvroSchemaString);
            assertEquals(avroSchema, expectedAvroSchema);
            // Have a DataMap from pegasus schema
            DataMap dataMap = TestUtil.dataMapFromString(dataMapString);
            // Create option, pass into data translator
            DataMapToAvroRecordTranslationOptions options = new DataMapToAvroRecordTranslationOptionsBuilder().defaultFieldDataTranslationMode(dataTranslationMode).build();
            // Translate to generic record
            GenericRecord avroRecord = DataTranslator.dataMapToGenericRecord(dataMap, recordDataSchema, avroSchema, options);
            String avroJson = AvroUtil.jsonFromGenericRecord(avroRecord);
            // avroJson compare
            assertEquals(avroJson, expectedAvroRecordJsonString);
            // validation result test
            DataMap dataMapResult = DataTranslator.genericRecordToDataMap(avroRecord, recordDataSchema, avroSchema);
            ValidationResult vr = ValidateDataAgainstSchema.validate(dataMapResult, recordDataSchema, new // Not filling back Default value
            ValidationOptions(// Not filling back Default value
            RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL));
            DataMap fixedInputDataMap = (DataMap) vr.getFixed();
            assertTrue(vr.isValid());
            assertEquals(dataMapResult, fixedInputDataMap);
            // serialize avroRecord to binary and back
            byte[] avroBytes = AvroUtil.bytesFromGenericRecord(avroRecord);
            GenericRecord avroRecordFromBytes = AvroUtil.genericRecordFromBytes(avroBytes, avroRecord.getSchema());
            byte[] avroBytesAgain = AvroUtil.bytesFromGenericRecord(avroRecordFromBytes);
            assertEquals(avroBytes, avroBytesAgain);
            // check result of roundtrip binary serialization
            DataMap dataMapFromBinaryResult = DataTranslator.genericRecordToDataMap(avroRecordFromBytes, recordDataSchema, avroSchema);
            vr = ValidateDataAgainstSchema.validate(dataMapFromBinaryResult, recordDataSchema, new // Not filling back Default value
            ValidationOptions(// Not filling back Default value
            RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL));
            fixedInputDataMap = (DataMap) vr.getFixed();
            assertTrue(vr.isValid());
            assertEquals(dataMapResult, fixedInputDataMap);
        }
    } catch (Exception e) {
        assertTrue(isError);
        assertEquals(e.getMessage(), errorMsg);
    }
}
Also used : ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) Schema(org.apache.avro.Schema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 44 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class TestDataTranslator method testDataTranslation.

private void testDataTranslation(String schemaText, String[][] row) throws IOException {
    boolean debug = false;
    if (debug)
        out.print(schemaText);
    RecordDataSchema recordDataSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(schemaText);
    Schema avroSchema = SchemaTranslator.dataToAvroSchema(recordDataSchema);
    if (debug)
        out.println(avroSchema);
    // translate data
    for (int col = 1; col < row.length; col++) {
        String result;
        GenericRecord avroRecord = null;
        Exception exc = null;
        if (debug)
            out.println(col + " DataMap: " + row[col][0]);
        DataMap dataMap = TestUtil.dataMapFromString(row[col][0]);
        // translate from Pegasus to Avro
        try {
            avroRecord = DataTranslator.dataMapToGenericRecord(dataMap, recordDataSchema, avroSchema);
            String avroJson = AvroUtil.jsonFromGenericRecord(avroRecord);
            if (debug)
                out.println(col + " GenericRecord: " + avroJson);
            result = avroJson;
        } catch (Exception e) {
            exc = e;
            result = TestUtil.stringFromException(e);
            if (debug)
                out.println(col + " Exception: " + result);
        }
        int start = 1;
        boolean oneWay = false;
        if (start < row[col].length && row[col][start] == ONE_WAY) {
            oneWay = true;
            start++;
        }
        // verify
        for (int i = start; i < row[col].length; i++) {
            if (debug)
                out.println(col + " Test:" + row[col][i]);
            if (debug && exc != null && result.contains(row[col][i]) == false)
                exc.printStackTrace(out);
            String expectedBeforeNamespaceProcessor = row[col][i];
            String expected = TestAvroUtil.namespaceProcessor(expectedBeforeNamespaceProcessor);
            if (debug && expected != expectedBeforeNamespaceProcessor)
                out.println(" Expected:" + expected);
            assertTrue(result.contains(expected));
        }
        if (avroRecord != null) {
            // translate from Avro back to Pegasus
            DataMap dataMapResult = DataTranslator.genericRecordToDataMap(avroRecord, recordDataSchema, avroSchema);
            ValidationResult vr = ValidateDataAgainstSchema.validate(dataMap, recordDataSchema, new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL));
            DataMap fixedInputDataMap = (DataMap) vr.getFixed();
            assertTrue(vr.isValid());
            if (oneWay == false) {
                assertEquals(dataMapResult, fixedInputDataMap);
            }
            // serialize avroRecord to binary and back
            byte[] avroBytes = AvroUtil.bytesFromGenericRecord(avroRecord);
            GenericRecord avroRecordFromBytes = AvroUtil.genericRecordFromBytes(avroBytes, avroRecord.getSchema());
            byte[] avroBytesAgain = AvroUtil.bytesFromGenericRecord(avroRecordFromBytes);
            assertEquals(avroBytes, avroBytesAgain);
            // check result of roundtrip binary serialization
            DataMap dataMapFromBinaryResult = DataTranslator.genericRecordToDataMap(avroRecordFromBytes, recordDataSchema, avroSchema);
            vr = ValidateDataAgainstSchema.validate(dataMapFromBinaryResult, recordDataSchema, new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL));
            fixedInputDataMap = (DataMap) vr.getFixed();
            assertTrue(vr.isValid());
            if (oneWay == false) {
                assertEquals(dataMapResult, fixedInputDataMap);
            }
        }
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) Schema(org.apache.avro.Schema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) GenericRecord(org.apache.avro.generic.GenericRecord) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 45 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testRecordSchema.

@Test
public void testRecordSchema() {
    final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(Certification.class);
    final DataMap value = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
    final ValidationResult result = ValidateDataAgainstSchema.validate(value, schema, new ValidationOptions());
    Assert.assertTrue(result.isValid(), Arrays.toString(result.getMessages().toArray()));
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) Certification(com.linkedin.pegasus.generator.test.Certification) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Aggregations

ValidationResult (com.linkedin.data.schema.validation.ValidationResult)46 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)20 DataMap (com.linkedin.data.DataMap)18 Test (org.testng.annotations.Test)18 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)12 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 DataSchema (com.linkedin.data.schema.DataSchema)8 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)8 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)8 Map (java.util.Map)8 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 Message (com.linkedin.data.message.Message)5 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 ResourceMethod (com.linkedin.restli.common.ResourceMethod)4 List (java.util.List)4 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)3 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)3 DataElement (com.linkedin.data.element.DataElement)3 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)3