Search in sources :

Example 31 with PathSpec

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

the class TestQueryParamsDataMap method testProcessProjections.

/**
 * Creates MaskTrees and extracts the subsequent DataMap to verify that processProjections can correctly
 * convert it correctly into a Map that can later be constructed and encoded into an URI
 */
@Test
public void testProcessProjections() {
    // Construct a MaskTree from a series of PathSpecs. Extract the subsequent Datamap representation.
    final MaskTree rootObjectsMask = MaskCreator.createPositiveMask(new PathSpec("foo", PathSpec.WILDCARD, "bar"));
    final MaskTree metadataMask = MaskCreator.createPositiveMask(new PathSpec("foo", "bar"), new PathSpec("bar", "baz"), new PathSpec("qux"));
    final MaskTree pagingMask = MaskCreator.createPositiveMask(new PathSpec("total"), new PathSpec("count"), new PathSpec("links", PathSpec.WILDCARD, "rel"));
    // For each type of projection, plus one query string parameter
    final DataMap resultMap = new DataMap(4);
    resultMap.put(RestConstants.FIELDS_PARAM, rootObjectsMask.getDataMap());
    resultMap.put(RestConstants.METADATA_FIELDS_PARAM, metadataMask.getDataMap());
    resultMap.put(RestConstants.PAGING_FIELDS_PARAM, pagingMask.getDataMap());
    resultMap.put("someQueryString", "someValue");
    final Map<String, List<String>> processedProjections = new LinkedHashMap<>();
    final DataMap processedDataMap = QueryParamsDataMap.processProjections(resultMap, processedProjections);
    Assert.assertTrue(processedDataMap.size() == 1, "Processed datamap should only have one item left!");
    final Map<String, Set<String>> expectedProcessedProjections = new LinkedHashMap<>();
    // "{fields=[foo:($*:(bar))], metadataFields=[foo:(bar),bar:(baz),qux], pagingFields=[total,count,links:($*:(rel))]}"
    expectedProcessedProjections.put(RestConstants.FIELDS_PARAM, Collections.singleton("foo:($*:(bar))"));
    expectedProcessedProjections.put(RestConstants.METADATA_FIELDS_PARAM, new HashSet<>(Arrays.asList("foo:(bar)", "bar:(baz)", "qux")));
    expectedProcessedProjections.put(RestConstants.PAGING_FIELDS_PARAM, new HashSet<>(Arrays.asList("total", "count", "links:($*:(rel))")));
    Assert.assertEquals(processedProjections.size(), expectedProcessedProjections.size(), "We must have the correct number of" + " expected projections!");
    for (final Map.Entry<String, List<String>> entry : processedProjections.entrySet()) {
        // Acceptable because these are always comma delimited
        final Set<String> actualProjectionValueSet = new HashSet<>(Arrays.asList(entry.getValue().get(0).split(",")));
        Assert.assertEquals(actualProjectionValueSet, expectedProcessedProjections.get(entry.getKey()), "The individual projection " + "for " + entry.getKey() + " does not match what is expected!");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) LinkedHashMap(java.util.LinkedHashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) DataList(com.linkedin.data.DataList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 32 with PathSpec

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

the class RestSpecCodec method serializeTypeFields.

private void serializeTypeFields(DataMap data, PathSpec path) throws IOException {
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        final PathSpec currentElement = new PathSpec(path.getPathComponents(), entry.getKey());
        if (isPegasusTypeField(currentElement) && entry.getValue() instanceof DataMap) {
            final String value = new String(_dataCodec.mapToBytes((DataMap) entry.getValue()), RestConstants.DEFAULT_CHARSET);
            data.put(entry.getKey(), value);
        } else if (entry.getValue() instanceof DataMap) {
            serializeTypeFields((DataMap) entry.getValue(), currentElement);
        } else if (entry.getValue() instanceof DataList) {
            for (Object o : (DataList) entry.getValue()) {
                if (o instanceof DataMap) {
                    serializeTypeFields((DataMap) o, currentElement);
                }
            }
        }
    }
}
Also used : DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap)

Example 33 with PathSpec

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

the class TestPatchGeneration method testNestedPositiveMask.

@Test
public void testNestedPositiveMask() throws Exception {
    List<PathSpec> fields = Arrays.asList(Group.fields().id(), Group.fields().location().latitude(), Group.fields().location().longitude(), Group.fields().name());
    MaskTree mask = MaskCreator.createPositiveMask(fields);
    // "{id=1, location={longitude=1, latitude=1}, name=1}"
    final DataMap idLocationNameMap = new DataMap();
    idLocationNameMap.put("id", 1);
    idLocationNameMap.put("name", 1);
    final DataMap longLatMap = new DataMap();
    longLatMap.put("longitude", 1);
    longLatMap.put("latitude", 1);
    idLocationNameMap.put("location", longLatMap);
    Assert.assertEquals(mask.getDataMap(), idLocationNameMap, "The MaskTree DataMap should match");
    // The ordering might be different but the URI should look something like:
    // "id,location:(longitude,latitude),name";
    final String actualEncodedMaskURI = URIMaskUtil.encodeMaskForURI(mask);
    // We convert back into a MaskTree so we can compare DataMaps because the URI could be in any order
    final MaskTree generatedMaskTree = URIMaskUtil.decodeMaskUriFormat(actualEncodedMaskURI);
    Assert.assertEquals(generatedMaskTree.getDataMap(), idLocationNameMap, "The actual encoded Mask URI should be correct");
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 34 with PathSpec

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

the class TestScatterGather method testBuildSGGetEntityRequests.

private static void testBuildSGGetEntityRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> requests = buildScatterGatherGetEntityRequests(sg, ids);
    Assert.assertEquals(requests.size(), numEndpoints);
    Set<Set<String>> requestIdSets = new HashSet<>();
    Set<Long> requestIds = new HashSet<>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>> requestInfo : requests) {
        // URI will be something like "greetings/?ids=21&ids=4&ids=53&ids=60&ids=66&ids=88&ids=93&foo=bar"
        BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> request = requestInfo.getRequest();
        Set<String> expectedParams = new HashSet<>();
        expectedParams.add(RestConstants.QUERY_BATCH_IDS_PARAM);
        expectedParams.add("foo");
        expectedParams.add(RestConstants.FIELDS_PARAM);
        Set<PathSpec> expectedFields = Collections.singleton(new PathSpec("message"));
        testRequest(request, expectedParams, expectedFields, null, requestIdSets, requestIds);
    }
    Assert.assertTrue(requestIds.containsAll(Arrays.asList(ids)));
    Assert.assertEquals(requestIds.size(), ids.length);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Set(java.util.Set) HashSet(java.util.HashSet) PathSpec(com.linkedin.data.schema.PathSpec) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) HashSet(java.util.HashSet)

Example 35 with PathSpec

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

the class TestEmptyUnionValidation method testUnionEmptyWithProjection.

@Test
public void testUnionEmptyWithProjection() throws RemoteInvocationException {
    ValidateEmptyUnion expected = new ValidateEmptyUnion();
    expected.setFoo(new ValidateEmptyUnion.Foo());
    List<PathSpec> spec = Collections.singletonList(ValidateEmptyUnion.fields().foo().Fuzz());
    EmptyUnionRequestBuilders requestBuilders = new EmptyUnionRequestBuilders();
    GetRequest<ValidateEmptyUnion> req = requestBuilders.get().id(1L).fields(spec.toArray(new PathSpec[spec.size()])).build();
    ValidateEmptyUnion actual = getClient().sendRequest(req).getResponse().getEntity();
    Assert.assertEquals(actual, expected);
}
Also used : ValidateEmptyUnion(com.linkedin.restli.examples.greetings.api.ValidateEmptyUnion) EmptyUnionRequestBuilders(com.linkedin.restli.examples.greetings.client.EmptyUnionRequestBuilders) PathSpec(com.linkedin.data.schema.PathSpec) 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