Search in sources :

Example 26 with PathSpec

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

the class TestTransformer method testReplaceByNameNested.

/**
 * Removes multiple nodes in a complex type, including non-leaf nodes.
 */
@Test
public void testReplaceByNameNested() throws Exception {
    SimpleTestData data = IteratorTestData.createSimpleTestData();
    Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.nameEquals("foo")).replace(new DataList());
    assertEquals(Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.pathMatchesPathSpec(new PathSpec("nested", "nested", "foo", PathSpec.WILDCARD))).count(), 0);
    assertEquals(Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.pathMatchesPathSpec(new PathSpec("foo", PathSpec.WILDCARD))).count(), 0);
}
Also used : DataList(com.linkedin.data.DataList) SimpleTestData(com.linkedin.data.it.IteratorTestData.SimpleTestData) PathSpec(com.linkedin.data.schema.PathSpec) Test(org.testng.annotations.Test)

Example 27 with PathSpec

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

the class TestTransformer method testReplaceBySchemaNameNested.

@Test
public void testReplaceBySchemaNameNested() throws Exception {
    SimpleTestData data = IteratorTestData.createSimpleTestData();
    Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.dataSchemaNameEquals("Bar")).replace(500);
    List<Object> accumulate = new ArrayList<>(Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.pathMatchesPathSpec(new PathSpec("nested", "nested", "foo", PathSpec.WILDCARD))).accumulateValues());
    assertEquals(accumulate.size(), 2);
    assertEquals(accumulate.get(0), 500);
    assertEquals(accumulate.get(1), 500);
    accumulate = new ArrayList<>(Builder.create(data.getDataElement(), IterationOrder.PRE_ORDER).filterBy(Predicates.pathMatchesPathSpec(new PathSpec("foo", PathSpec.WILDCARD))).accumulateValues());
    assertEquals(accumulate.size(), 3);
    assertEquals(accumulate.get(0), 500);
    assertEquals(accumulate.get(1), 500);
    assertEquals(accumulate.get(2), 500);
}
Also used : SimpleTestData(com.linkedin.data.it.IteratorTestData.SimpleTestData) ArrayList(java.util.ArrayList) PathSpec(com.linkedin.data.schema.PathSpec) Test(org.testng.annotations.Test)

Example 28 with PathSpec

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

the class TestAnnotationCompatibilityChecker method annotationCompatibilityCheckTestData.

@DataProvider
private Object[][] annotationCompatibilityCheckTestData() throws IOException {
    // Set up expected result: both previous schema and current schema contain the same PathSpecs.
    CompatibilityCheckContext checkContext = generateAnnotationCheckContext(new PathSpec("TestSchema1/field1/$field"));
    CompatibilityCheckContext checkContext1 = generateAnnotationCheckContext(new PathSpec("TestSchema1/field2/$field"));
    AnnotationCompatibilityResult expectResultWithCompatibleChange1 = generateExpectResult(new CompatibilityMessage(checkContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Updating annotation field \"%s\" value is backward compatible change", ANNOTATION_FIELD_NAME));
    AnnotationCompatibilityResult expectResultWithInCompatibleChange1 = generateExpectResult(new CompatibilityMessage(checkContext1.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Deleting existed annotation \"%s\" is backward incompatible change", BAR_ANNOTATION_NAMESPACE));
    // Set up expected result: only previous schema contains the resolvedProperty with the same annotation namespace as SchemaAnnotationHandler
    CompatibilityCheckContext checkContext2 = generateAnnotationCheckContext(new PathSpec("TestSchema2/field1/$field"));
    AnnotationCompatibilityResult expectResult2 = generateExpectResult(new CompatibilityMessage(checkContext2.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Adding new annotation \"%s\" is backward compatible change", BAR_ANNOTATION_NAMESPACE));
    // Set up expected result: only current schema contains the resolvedProperty with the same annotation namespace as SchemaAnnotationHandler
    CompatibilityCheckContext checkContext3 = generateAnnotationCheckContext(new PathSpec("TestSchema3/field1/$field"));
    AnnotationCompatibilityResult expectResult3 = generateExpectResult(new CompatibilityMessage(checkContext3.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Deleting existed annotation \"%s\" is backward incompatible change", BAR_ANNOTATION_NAMESPACE));
    // Set up expected results: multiple handlers.
    CompatibilityCheckContext checkContext4 = generateAnnotationCheckContext(new PathSpec("TestSchema4/field1/$field"));
    AnnotationCompatibilityResult barHandlerExpectResult = generateExpectResult(new CompatibilityMessage(checkContext4.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Adding new annotation \"%s\" is backward compatible change", BAR_ANNOTATION_NAMESPACE));
    AnnotationCompatibilityResult bazHandlerExpectResult = generateExpectResult(new CompatibilityMessage(checkContext4.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Updating annotation field \"%s\" value is backward compatible change", ANNOTATION_FIELD_NAME));
    // Set up expected results: field has annotation, field type schema also has annotation.
    AnnotationCompatibilityResult fieldAnnotationResult = new AnnotationCompatibilityResult();
    CompatibilityCheckContext checkContext5 = generateAnnotationCheckContext(new PathSpec("TestSchema5/field1"));
    AnnotationCompatibilityResult fieldTypeSchemaAnnotationResult = generateExpectResult(new CompatibilityMessage(checkContext5.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Updating annotation field \"%s\" value is backward compatible change", ANNOTATION_FIELD_NAME));
    // Set up expected results: field has annotation, field type schema also has annotation.
    CompatibilityCheckContext unionMemberKeyCheckContext = generateAnnotationCheckContext(new PathSpec("TestSchema6/field1/u1/$unionMemberKey"));
    AnnotationCompatibilityResult unionMemberKeyAnnotationResult = generateExpectResult(new CompatibilityMessage(unionMemberKeyCheckContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Updating annotation field \"%s\" value is backward compatible change", ANNOTATION_FIELD_NAME));
    CompatibilityCheckContext unionMemberSchemaCheckContext = generateAnnotationCheckContext(new PathSpec("TestSchema6/field1/u1"));
    AnnotationCompatibilityResult unionMemberSchemaAnnotationResult = generateExpectResult(new CompatibilityMessage(unionMemberSchemaCheckContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Updating annotation field \"%s\" value is backward compatible change", ANNOTATION_FIELD_NAME));
    // Set up expected result: an extension annotation field value is updated.
    CompatibilityCheckContext schoolContext = generateAnnotationCheckContext(new PathSpec("SchoolExtensions/testField/$field"));
    AnnotationCompatibilityResult schoolExtensionExpectResult = generateExpectResult(new CompatibilityMessage(schoolContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Updating extension annotation field: \"%s\" value is considering as a backward incompatible change.", "using"));
    // Set up expected result: an extension annotation field is removed.
    CompatibilityCheckContext fruitContext = generateAnnotationCheckContext(new PathSpec("FruitExtensions/testField/$field"));
    AnnotationCompatibilityResult fruitExtensionExpectResult = generateExpectResult(new CompatibilityMessage(fruitContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Removing extension annotation field: \"%s\" is considering as a backward incompatible change.", "params"));
    // Set up expected result: a new field with annotation is added.
    CompatibilityCheckContext fooContext = generateAnnotationCheckContext(new PathSpec("FooExtensions/barField/$field"));
    AnnotationCompatibilityResult fooExtensionExpectResult = generateExpectResult(new CompatibilityMessage(fooContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_COMPATIBLE_CHANGE, "Adding extension annotation on new field: \"%s\" is backward compatible change", "barField"));
    // Existing fields annotations do not change, checkCompatibility will return an empty result.
    AnnotationCompatibilityResult emptyResult = new AnnotationCompatibilityResult();
    // Set up expected result: an extension annotation is removed.
    CompatibilityCheckContext albumContext = generateAnnotationCheckContext(new PathSpec("AlbumExtensions/testField/$field"));
    AnnotationCompatibilityResult albumExtensionExpectResult = generateExpectResult(new CompatibilityMessage(albumContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Removing extension annotation is a backward incompatible change.", ""));
    // Set up expected result: an extension annotation is removed.
    CompatibilityCheckContext companyContext = generateAnnotationCheckContext(new PathSpec("CompanyExtensions/testField/$field"));
    AnnotationCompatibilityResult companyExtensionExpectResult = generateExpectResult(new CompatibilityMessage(companyContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Adding extension annotation field: \"%s\" is a backward incompatible change.", "using"));
    // Set up expected result: a field with extension annotation is removed.
    CompatibilityCheckContext bookContext = generateAnnotationCheckContext(new PathSpec("BookExtensions/testField/$field"));
    AnnotationCompatibilityResult bookExtensionExpectResult = generateExpectResult(new CompatibilityMessage(bookContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Removing field: \"%s\" with extension annotation is a backward incompatible change.", "testField"));
    // Set up expected result: adding extension annotation on an existing field
    CompatibilityCheckContext jobContext = generateAnnotationCheckContext(new PathSpec("JobExtensions/testField/$field"));
    AnnotationCompatibilityResult jobExtensionExpectResult = generateExpectResult(new CompatibilityMessage(jobContext.getPathSpecToSchema(), CompatibilityMessage.Impact.ANNOTATION_INCOMPATIBLE_CHANGE, "Adding extension annotation on an existing field: \"%s\" is backward incompatible change", "testField"));
    return new Object[][] { { "previousSchema/TestSchema1.pdl", "currentSchema/TestSchema1.pdl", Collections.singletonList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE)), Arrays.asList(expectResultWithCompatibleChange1, expectResultWithInCompatibleChange1) }, { "previousSchema/TestSchema2.pdl", "currentSchema/TestSchema2.pdl", Collections.singletonList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE)), Collections.singletonList(expectResult2) }, { "previousSchema/TestSchema3.pdl", "currentSchema/TestSchema3.pdl", Collections.singletonList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE)), Collections.singletonList(expectResult3) }, { "previousSchema/TestSchema4.pdl", "currentSchema/TestSchema4.pdl", Arrays.asList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE), generateSchemaAnnotationHandler(BAZ_ANNOTATION_NAMESPACE)), Arrays.asList(barHandlerExpectResult, bazHandlerExpectResult) }, { "previousSchema/TestSchema5.pdl", "currentSchema/TestSchema5.pdl", Arrays.asList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE), generateSchemaAnnotationHandler(BAZ_ANNOTATION_NAMESPACE)), Arrays.asList(fieldAnnotationResult, fieldTypeSchemaAnnotationResult) }, { "previousSchema/TestSchema6.pdl", "currentSchema/TestSchema6.pdl", Arrays.asList(generateSchemaAnnotationHandler(BAR_ANNOTATION_NAMESPACE), generateSchemaAnnotationHandler(BAZ_ANNOTATION_NAMESPACE)), Arrays.asList(unionMemberSchemaAnnotationResult, unionMemberKeyAnnotationResult) }, { "previousSchema/SchoolExtensions.pdl", "currentSchema/SchoolExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(schoolExtensionExpectResult) }, { "previousSchema/FruitExtensions.pdl", "currentSchema/FruitExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(fruitExtensionExpectResult) }, { "previousSchema/FooExtensions.pdl", "currentSchema/FooExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Arrays.asList(emptyResult, fooExtensionExpectResult) }, { "previousSchema/AlbumExtensions.pdl", "currentSchema/AlbumExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(albumExtensionExpectResult) }, { "previousSchema/CompanyExtensions.pdl", "currentSchema/CompanyExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(companyExtensionExpectResult) }, { "previousSchema/BookExtensions.pdl", "currentSchema/BookExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Arrays.asList(bookExtensionExpectResult, emptyResult) }, { "previousSchema/JobExtensions.pdl", "currentSchema/JobExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(jobExtensionExpectResult) }, { "previousSchema/IdentityExtensions.pdl", "currentSchema/IdentityExtensions.pdl", Collections.singletonList(new ExtensionSchemaAnnotationHandler()), Collections.singletonList(emptyResult) } };
}
Also used : ExtensionSchemaAnnotationHandler(com.linkedin.data.schema.annotation.ExtensionSchemaAnnotationHandler) AnnotationCompatibilityResult(com.linkedin.data.schema.annotation.SchemaAnnotationHandler.AnnotationCompatibilityResult) CompatibilityCheckContext(com.linkedin.data.schema.annotation.SchemaAnnotationHandler.CompatibilityCheckContext) PathSpec(com.linkedin.data.schema.PathSpec) DataProvider(org.testng.annotations.DataProvider)

Example 29 with PathSpec

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

the class TestDataTemplateUtil method assertPresentInFields.

public static <T extends DataTemplate<?>> void assertPresentInFields(Class<T> templateClass, String fieldName) {
    try {
        // fields
        Method fieldsMethod = templateClass.getMethod("fields");
        PathSpec recordPathSpec = (PathSpec) fieldsMethod.invoke(null);
        Method fieldPathSpecMethod = recordPathSpec.getClass().getMethod(fieldName);
        PathSpec fieldPathSpec = (PathSpec) fieldPathSpecMethod.invoke(recordPathSpec);
        List<String> components = fieldPathSpec.getPathComponents();
        assertEquals(components.size(), 1);
        assertEquals(components.get(0), fieldName);
    } catch (NoSuchMethodException exc) {
        fail("Unexpected exception", exc);
    } catch (IllegalAccessException exc) {
        fail("Unexpected exception", exc);
    } catch (InvocationTargetException exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : Method(java.lang.reflect.Method) PathSpec(com.linkedin.data.schema.PathSpec) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with PathSpec

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

the class TestQueryParamsUtil method testCustomProjectionDataMapSerializerReturningNull.

@Test
public void testCustomProjectionDataMapSerializerReturningNull() {
    Map<String, Object> queryParams = new HashMap<>();
    Set<PathSpec> specSet = new HashSet<>();
    specSet.add(new PathSpec("random"));
    queryParams.put(RestConstants.FIELDS_PARAM, specSet);
    queryParams.put(RestConstants.PAGING_FIELDS_PARAM, specSet);
    queryParams.put(RestConstants.METADATA_FIELDS_PARAM, specSet);
    DataMap dataMap = QueryParamsUtil.convertToDataMap(queryParams, Collections.emptyMap(), AllProtocolVersions.LATEST_PROTOCOL_VERSION, (paramName, pathSpecs) -> null);
    Assert.assertNull(dataMap.getDataMap(RestConstants.FIELDS_PARAM));
    Assert.assertNull(dataMap.getDataMap(RestConstants.PAGING_FIELDS_PARAM));
    Assert.assertNull(dataMap.getDataMap(RestConstants.METADATA_FIELDS_PARAM));
}
Also used : HashMap(java.util.HashMap) PathSpec(com.linkedin.data.schema.PathSpec) HashSet(java.util.HashSet) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

PathSpec (com.linkedin.data.schema.PathSpec)101 Test (org.testng.annotations.Test)74 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)40 DataMap (com.linkedin.data.DataMap)31 HashSet (java.util.HashSet)16 RecordTemplate (com.linkedin.data.template.RecordTemplate)11 Map (java.util.Map)10 Set (java.util.Set)10 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)9 HashMap (java.util.HashMap)8 DataList (com.linkedin.data.DataList)7 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)7 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)7 List (java.util.List)7 ByteString (com.linkedin.data.ByteString)6 DataSchema (com.linkedin.data.schema.DataSchema)5 MaskOperation (com.linkedin.data.transform.filter.request.MaskOperation)5 Foo (com.linkedin.pegasus.generator.examples.Foo)5 RequestContext (com.linkedin.r2.message.RequestContext)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5