Search in sources :

Example 11 with MaskTree

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

the class TestMaskCreation method testComposingNegativeSubmasks.

/**
   * When negative mask for a PathSpec is composed with another negative mask, which is sub-PathSpec, then
   * the result is a negative mask with PathSpec only for the parent, because negative mask means
   * "remove field and all it's children".
   */
@Test
public void testComposingNegativeSubmasks() {
    MaskTree mask = new MaskTree();
    mask.addOperation(new PathSpec("a", "b", "c"), MaskOperation.NEGATIVE_MASK_OP);
    mask.addOperation(new PathSpec("a", "b"), MaskOperation.NEGATIVE_MASK_OP);
    mask.addOperation(new PathSpec("a"), MaskOperation.NEGATIVE_MASK_OP);
    Assert.assertEquals(mask.toString(), "{a=0}");
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathSpec(com.linkedin.data.schema.PathSpec) Test(org.testng.annotations.Test)

Example 12 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree 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<String, List<String>>();
    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<String, Set<String>>();
    //"{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<String>(Arrays.asList("foo:(bar)", "bar:(baz)", "qux")));
    expectedProcessedProjections.put(RestConstants.PAGING_FIELDS_PARAM, new HashSet<String>(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<String>(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 13 with MaskTree

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

the class TestURIMaskUtil method testDeodingFromURI.

private void testDeodingFromURI(String jsonMask, String uriMask, String description) throws IllegalMaskException, IOException {
    MaskTree decoded = URIMaskUtil.decodeMaskUriFormat(new StringBuilder(uriMask));
    DataMap expectedMask = dataMapFromString(jsonMask.replace('\'', '"'));
    assertEquals(decoded.getDataMap().toString(), expectedMask.toString(), "Decoding test case failed: \n" + description + "\nmask in URI: " + uriMask + "\nexcpected: " + expectedMask.toString() + "\nactual: " + decoded.toString());
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) DataMap(com.linkedin.data.DataMap)

Example 14 with MaskTree

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

the class TestResourceContextImpl method testOverrideMask.

@Test(dataProvider = "overrideMaskData")
public void testOverrideMask(ProjectionType projectionType, String stringUri, List<String> projectedFields) throws Exception {
    URI uri = URI.create(stringUri);
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), new TestResourceContext.MockRequest(uri), new RequestContext());
    // Assert the current projections before we set the override mask
    MaskTree projectionMask = getProjectionMask(resourceContext, projectionType);
    if (projectedFields.isEmpty()) {
        Assert.assertNull(projectionMask);
    } else {
        Assert.assertNotNull(projectionMask);
        Map<PathSpec, MaskOperation> maskOperations = projectionMask.getOperations();
        Assert.assertNotNull(maskOperations);
        Assert.assertEquals(maskOperations.size(), projectedFields.size());
        for (String projectedField : projectedFields) {
            Assert.assertTrue(maskOperations.containsKey(new PathSpec(projectedField)));
            Assert.assertEquals(maskOperations.get(new PathSpec(projectedField)), MaskOperation.POSITIVE_MASK_OP);
        }
    }
    final DataMap overrideDataMap = new DataMap();
    overrideDataMap.put("state", 1);
    setProjectionMask(resourceContext, projectionType, new MaskTree(overrideDataMap));
    // Assert the projections after the projection mask is overridden
    projectionMask = getProjectionMask(resourceContext, projectionType);
    Assert.assertNotNull(projectionMask);
    Map<PathSpec, MaskOperation> maskOperations = projectionMask.getOperations();
    Assert.assertNotNull(maskOperations);
    Assert.assertEquals(maskOperations.size(), 1);
    Assert.assertTrue(maskOperations.containsKey(new PathSpec("state")));
    Assert.assertEquals(maskOperations.get(new PathSpec("state")), MaskOperation.POSITIVE_MASK_OP);
}
Also used : URI(java.net.URI) PathSpec(com.linkedin.data.schema.PathSpec) TestResourceContext(com.linkedin.restli.server.test.TestResourceContext) MaskOperation(com.linkedin.data.transform.filter.request.MaskOperation) DataMap(com.linkedin.data.DataMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 15 with MaskTree

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

the class TestFilterRequestContextInternalImpl method testFilterRequestContextAdapter.

@Test
public void testFilterRequestContextAdapter() throws Exception {
    final String resourceName = "resourceName";
    final String resourceNamespace = "resourceNamespace";
    final ResourceMethod methodType = ResourceMethod.GET;
    final DataMap customAnnotations = new DataMap();
    customAnnotations.put("foo", "Bar");
    final ProjectionMode projectionMode = ProjectionMode.AUTOMATIC;
    final MaskTree maskTree = new MaskTree();
    final MutablePathKeys pathKeys = new PathKeysImpl();
    final Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put("Key1", "Value1");
    final URI requestUri = new URI("foo.bar.com");
    final ProtocolVersion protoVersion = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    final DataMap queryParams = new DataMap();
    queryParams.put("Param1", "Val1");
    final Map<String, Object> localAttrs = new HashMap<>();
    localAttrs.put("Key1", "Val1");
    final RequestContext r2RequestContext = new RequestContext();
    r2RequestContext.putLocalAttr("Key1", "Val1");
    final String finderName = UUID.randomUUID().toString();
    final String actionName = UUID.randomUUID().toString();
    when(resourceModel.getName()).thenReturn(resourceName);
    when(resourceModel.getNamespace()).thenReturn(resourceNamespace);
    when(resourceMethod.getResourceModel()).thenReturn(resourceModel);
    when(resourceMethod.getMethodType()).thenReturn(methodType);
    when(resourceMethod.getFinderName()).thenReturn(finderName);
    when(resourceMethod.getActionName()).thenReturn(actionName);
    when(resourceMethod.getCustomAnnotationData()).thenReturn(customAnnotations);
    when(resourceMethod.getMethod()).thenReturn(null);
    when(context.getProjectionMode()).thenReturn(projectionMode);
    when(context.getProjectionMask()).thenReturn(maskTree);
    when(context.getPathKeys()).thenReturn(pathKeys);
    when(context.getRequestHeaders()).thenReturn(requestHeaders);
    when(context.getRequestURI()).thenReturn(requestUri);
    when(context.getRestliProtocolVersion()).thenReturn(protoVersion);
    when(context.getParameters()).thenReturn(queryParams);
    when(context.getRawRequestContext()).thenReturn(r2RequestContext);
    FilterRequestContextInternalImpl filterContext = new FilterRequestContextInternalImpl(context, resourceMethod);
    Object spValue = new Object();
    String spKey = UUID.randomUUID().toString();
    filterContext.getFilterScratchpad().put(spKey, spValue);
    assertEquals(filterContext.getFilterResourceModel().getResourceName(), resourceName);
    assertEquals(filterContext.getFilterResourceModel().getResourceNamespace(), resourceNamespace);
    assertEquals(filterContext.getMethodType(), methodType);
    assertEquals(filterContext.getCustomAnnotations(), customAnnotations);
    assertEquals(filterContext.getProjectionMode(), projectionMode);
    assertEquals(filterContext.getProjectionMask(), maskTree);
    assertEquals(filterContext.getPathKeys(), pathKeys);
    assertEquals(filterContext.getRequestHeaders(), requestHeaders);
    assertEquals(filterContext.getRequestURI(), requestUri);
    assertEquals(filterContext.getRestliProtocolVersion(), protoVersion);
    assertEquals(filterContext.getQueryParameters(), queryParams);
    assertEquals(filterContext.getActionName(), actionName);
    assertEquals(filterContext.getFinderName(), finderName);
    assertEquals(filterContext.getRequestContextLocalAttrs(), localAttrs);
    assertNull(filterContext.getMethod());
    assertTrue(filterContext.getFilterScratchpad().get(spKey) == spValue);
    filterContext.getRequestHeaders().put("header2", "value2");
    assertEquals(requestHeaders.get("header2"), "value2");
    verify(resourceModel).getName();
    verify(resourceModel).getNamespace();
    verify(resourceMethod).getMethodType();
    verify(resourceMethod).getResourceModel();
    verify(resourceMethod).getCustomAnnotationData();
    verify(resourceMethod).getFinderName();
    verify(resourceMethod).getActionName();
    verify(resourceMethod).getMethod();
    verify(context).getProjectionMode();
    verify(context).getProjectionMask();
    verify(context).getPathKeys();
    verify(context, times(2)).getRequestHeaders();
    verify(context).getRequestURI();
    verify(context).getRestliProtocolVersion();
    verify(context).getParameters();
    verify(context).getRawRequestContext();
    verify(resourceMethod).getFinderMetadataType();
    verifyNoMoreInteractions(context, resourceMethod, resourceModel);
}
Also used : MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) HashMap(java.util.HashMap) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) DataMap(com.linkedin.data.DataMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) RequestContext(com.linkedin.r2.message.RequestContext) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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