Search in sources :

Example 36 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestMaskCreation method testComposingPositiveMaskWithNegativeSubmasks.

/**
   * When positive mask for a PathSpec is composed with negative mask, which is sub-PathSpec, then
   * the result is a mask, which selects all fields in PathSpec using wildcard with negative mask
   * for sub-PathSpec. This is because negative mask has a higher priority then a positive mask.
   * @throws IOException
   */
@Test
public void testComposingPositiveMaskWithNegativeSubmasks() throws IOException {
    MaskTree mask = new MaskTree();
    mask.addOperation(new PathSpec("a", "b", "c"), MaskOperation.NEGATIVE_MASK_OP);
    mask.addOperation(new PathSpec("a"), MaskOperation.POSITIVE_MASK_OP);
    Assert.assertEquals(mask.toString(), dataMapFromString("{'a': {'$*': 1, 'b': {'c': 0}}}".replace('\'', '"')).toString());
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathSpec(com.linkedin.data.schema.PathSpec) Test(org.testng.annotations.Test)

Example 37 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestMaskCreation method testPositiveMaskSingleField.

@Test
public void testPositiveMaskSingleField() {
    MaskTree mask = MaskCreator.createPositiveMask(new PathSpec("foo"));
    Assert.assertEquals(mask.toString(), "{foo=1}");
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathSpec(com.linkedin.data.schema.PathSpec) Test(org.testng.annotations.Test)

Example 38 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestPatchGeneration method testSimplePositiveMask.

@Test
public void testSimplePositiveMask() throws Exception {
    List<PathSpec> fields = Arrays.asList(Group.fields().name(), Group.fields().description(), Group.fields().id());
    MaskTree mask = MaskCreator.createPositiveMask(fields);
    //"{id=1, description=1, name=1}"
    final DataMap idDescriptionNameMap = new DataMap();
    idDescriptionNameMap.put("id", 1);
    idDescriptionNameMap.put("description", 1);
    idDescriptionNameMap.put("name", 1);
    assertEquals(mask.getDataMap(), idDescriptionNameMap, "The MaskTree DataMap should be correct");
    //The ordering might be different but the URI should look something like:
    //"id,description,name";
    final String actualEncodedMaskURI = URIMaskUtil.encodeMaskForURI(mask);
    final Set<String> maskURISet = new HashSet<String>(Arrays.asList(actualEncodedMaskURI.split(",")));
    final Set<String> expectedURISet = new HashSet<String>();
    expectedURISet.add("id");
    expectedURISet.add("description");
    expectedURISet.add("name");
    Assert.assertEquals(maskURISet, expectedURISet, "The encoded mask should be correct");
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 39 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestResourceContext method testResourceContextURIDecoding.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "uriDecoding")
public void testResourceContextURIDecoding(ProtocolVersion version, String stringUri) throws Exception {
    URI uri = URI.create(stringUri);
    Map<String, String> headers = new HashMap<String, String>(1);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
    final MaskTree entityMask = context.getProjectionMask();
    //"{baz=1, foo=1, bar=1}"
    final DataMap expectedEntityMap = new DataMap();
    expectedEntityMap.put("baz", 1);
    expectedEntityMap.put("foo", 1);
    expectedEntityMap.put("bar", 1);
    Assert.assertEquals(entityMask.getDataMap(), expectedEntityMap, "The generated DataMap for the MaskTree should be correct");
    final MaskTree metadataMask = context.getMetadataProjectionMask();
    //"{region=1, city=1}"
    final DataMap expectedmetadataMap = new DataMap();
    expectedmetadataMap.put("region", 1);
    expectedmetadataMap.put("city", 1);
    Assert.assertEquals(metadataMask.getDataMap(), expectedmetadataMap, "The generated DataMap for the MaskTree should be correct");
    final MaskTree pagingMask = context.getPagingProjectionMask();
    //"{start=1, links=1}"
    final DataMap expectedPagingMap = new DataMap();
    expectedPagingMap.put("start", 1);
    expectedPagingMap.put("links", 1);
    Assert.assertEquals(pagingMask.getDataMap(), expectedPagingMap, "The generated DataMap for the MaskTree should be correct");
    DataMap parameters = context.getParameters();
    DataMap expectedParameters = new DataMap();
    expectedParameters.put("fields", "foo,bar,baz");
    expectedParameters.put("metadataFields", "city,region");
    expectedParameters.put("pagingFields", "start,links");
    expectedParameters.put("q", "test");
    DataMap testParam = new DataMap();
    DataList aValue = new DataList();
    aValue.add("b");
    aValue.add("c");
    aValue.add("d");
    testParam.put("a", aValue);
    expectedParameters.put("testParam", testParam);
    Assert.assertEquals(parameters, expectedParameters);
}
Also used : DataList(com.linkedin.data.DataList) HashMap(java.util.HashMap) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 40 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestResourceContext method testResourceContextWithQueryParamsGetProjectionMaskWithMaskSyntax.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "queryParamsProjectionMaskWithSyntax")
public void testResourceContextWithQueryParamsGetProjectionMaskWithMaskSyntax(ProtocolVersion version, String stringUri) throws Exception {
    URI uri = URI.create(stringUri);
    Map<String, String> headers = new HashMap<String, String>(1);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    ResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
    //Assert.assertEquals(mask.toString(), "{location={longitude=1, latitude=1}, locale=1, state=1}");
    final MaskTree entityMask = context.getProjectionMask();
    final DataMap expectedEntityMap = new DataMap();
    expectedEntityMap.put("locale", 1);
    expectedEntityMap.put("state", 1);
    final DataMap locationMap = new DataMap();
    locationMap.put("longitude", 1);
    locationMap.put("latitude", 1);
    expectedEntityMap.put("location", locationMap);
    Assert.assertEquals(entityMask.getDataMap(), expectedEntityMap, "The generated DataMap for the MaskTree should be correct");
    //"{region=1, profile={weight=1, height=1}, city=1}"
    final MaskTree metadataMask = context.getMetadataProjectionMask();
    final DataMap expectedMetadataMap = new DataMap();
    expectedMetadataMap.put("city", 1);
    expectedMetadataMap.put("region", 1);
    final DataMap profileMap = new DataMap();
    profileMap.put("weight", 1);
    profileMap.put("height", 1);
    expectedMetadataMap.put("profile", profileMap);
    Assert.assertEquals(metadataMask.getDataMap(), expectedMetadataMap, "The generated DataMap for the MaskTree should be correct");
//Note the lack of a test with paging here. This is because paging (CollectionMetadata) has a LinkArray which
//requires a wildcard path spec in the URI. That behavior is inconsistent with the other projections in this test,
//therefore it will be included in the subsequent test.
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

MaskTree (com.linkedin.data.transform.filter.request.MaskTree)49 Test (org.testng.annotations.Test)40 PathSpec (com.linkedin.data.schema.PathSpec)31 DataMap (com.linkedin.data.DataMap)23 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)9 Foo (com.linkedin.pegasus.generator.examples.Foo)8 HashMap (java.util.HashMap)8 ResourceContext (com.linkedin.restli.server.ResourceContext)7 URI (java.net.URI)7 RequestContext (com.linkedin.r2.message.RequestContext)6 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)6 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)6 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)5 ByteString (com.linkedin.data.ByteString)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 ProjectionMode (com.linkedin.restli.server.ProjectionMode)4 HashSet (java.util.HashSet)4 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)3 UnionTest (com.linkedin.pegasus.generator.test.UnionTest)3