Search in sources :

Example 1 with CompatibilityResult

use of com.linkedin.data.schema.compatibility.CompatibilityResult in project rest.li by linkedin.

the class TestCompatibilityChecker method testCompatibility.

private void testCompatibility(String olderSchemaText, String newerSchemaText, List<CompatibilityOptions> compatibilityOptions, boolean hasError, Object[] expected, int expectedIndex) throws IOException {
    if (_debug)
        out.println(olderSchemaText + "\n" + newerSchemaText);
    DataSchema olderSchema = TestUtil.dataSchemaFromString(olderSchemaText);
    DataSchema newerSchema = TestUtil.dataSchemaFromString(newerSchemaText);
    assertNotNull(olderSchema, olderSchemaText);
    assertNotNull(newerSchema, newerSchemaText);
    for (CompatibilityOptions option : compatibilityOptions) {
        CompatibilityResult result = CompatibilityChecker.checkCompatibility(olderSchema, newerSchema, option);
        String messageText = result.getMessages().toString();
        if (_debug)
            out.println(result);
        assertEquals(result.isError(), hasError, olderSchemaText + "\n" + newerSchemaText + "\n" + messageText);
        for (int i = expectedIndex; i < expected.length; i++) {
            String expectedText = ".*" + expected[i] + "\n.*";
            if (_debug)
                out.println(expectedText);
            Pattern pattern = Pattern.compile(expectedText, Pattern.DOTALL);
            Matcher matcher = pattern.matcher(messageText);
            boolean matches = matcher.matches();
            assertTrue(matches, messageText + "\n" + expectedText);
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CompatibilityResult(com.linkedin.data.schema.compatibility.CompatibilityResult) CompatibilityOptions(com.linkedin.data.schema.compatibility.CompatibilityOptions)

Example 2 with CompatibilityResult

use of com.linkedin.data.schema.compatibility.CompatibilityResult in project rest.li by linkedin.

the class ResourceCompatibilityChecker method checkType.

/**
   * @return whether the optionality check passes
   */
private boolean checkType(Object pathTail, String prevType, String currType, boolean allowNull) {
    if (prevType == null && currType == null && allowNull) {
        return true;
    }
    if (prevType == null || currType == null) {
        _infoMap.addRestSpecInfo(pathTail, CompatibilityInfo.Type.TYPE_MISSING, _infoPath);
        return false;
    }
    try {
        final DataSchema prevSchema = RestSpecCodec.textToSchema(prevType, _prevSchemaResolver);
        final DataSchema currSchema = RestSpecCodec.textToSchema(currType, _currSchemaResolver);
        CompatibilityResult compatibilityResult = CompatibilityChecker.checkCompatibility(prevSchema, currSchema, defaultOptions);
        if (!compatibilityResult.getMessages().isEmpty()) {
            if (prevType.equals(currType) && prevSchema instanceof NamedDataSchema) {
                if (!_namedSchemasChecked.contains(prevType)) {
                    addNamedCompatibilityMessages(compatibilityResult.getMessages());
                    _namedSchemasChecked.add(prevType);
                }
                return compatibilityResult.isError();
            }
            addCompatibilityMessages(pathTail, compatibilityResult.getMessages());
            return compatibilityResult.isError();
        }
        return true;
    } catch (IllegalArgumentException e) {
        _infoMap.addRestSpecInfo(pathTail, CompatibilityInfo.Type.TYPE_UNKNOWN, _infoPath, e.getMessage());
        return false;
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) CompatibilityResult(com.linkedin.data.schema.compatibility.CompatibilityResult)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)2 CompatibilityResult (com.linkedin.data.schema.compatibility.CompatibilityResult)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 CompatibilityOptions (com.linkedin.data.schema.compatibility.CompatibilityOptions)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1