Search in sources :

Example 1 with ProjectionMode

use of com.linkedin.restli.server.ProjectionMode 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)

Example 2 with ProjectionMode

use of com.linkedin.restli.server.ProjectionMode in project rest.li by linkedin.

the class TestCollectionResponseBuilder method dataProvider.

@DataProvider(name = "testData")
public Object[][] dataProvider() throws CloneNotSupportedException {
    Foo metadata = new Foo().setStringField("metadata").setIntField(7);
    Foo projectedMetadata = new Foo().setIntField(7);
    final List<Foo> generatedList = generateTestList();
    final List<Foo> testListWithProjection = generateTestListWithProjection();
    CollectionResult<Foo, Foo> collectionResult = new CollectionResult<Foo, Foo>(generatedList, generatedList.size(), metadata);
    DataMap dataProjectionDataMap = new DataMap();
    dataProjectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree dataMaskTree = new MaskTree(dataProjectionDataMap);
    DataMap metadataProjectionDataMap = new DataMap();
    metadataProjectionDataMap.put("intField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree metadataMaskTree = new MaskTree(metadataProjectionDataMap);
    DataMap pagingProjectDataMap = new DataMap();
    pagingProjectDataMap.put("count", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree pagingMaskTree = new MaskTree(pagingProjectDataMap);
    CollectionMetadata collectionMetadata1 = new CollectionMetadata().setCount(10).setStart(0).setLinks(new LinkArray());
    CollectionMetadata collectionMetadata2 = collectionMetadata1.clone().setTotal(2);
    CollectionMetadata collectionMetadataWithProjection = new CollectionMetadata().setCount(10);
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    return new Object[][] { // auto projection for data and metadata with null projection masks
    { generatedList, null, generatedList, collectionMetadata1, null, null, null, auto, auto }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, auto, auto }, // manual projection for data and metadata with null projection masks
    { generatedList, null, generatedList, collectionMetadata1, null, null, null, manual, manual }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, manual, manual }, // manual projection for data and metadata with non-null projection masks
    { generatedList, null, generatedList, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual }, // auto projection for data with non-null data and paging projection masks
    { generatedList, null, testListWithProjection, collectionMetadataWithProjection, dataMaskTree, null, pagingMaskTree, auto, auto }, // auto projection for data and metadata with non-null projection masks
    { collectionResult, projectedMetadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, auto }, // auto data projection, manual metadata projection, and auto (default) paging projection
    { collectionResult, metadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, manual } };
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) LinkArray(com.linkedin.restli.common.LinkArray) ProjectionMode(com.linkedin.restli.server.ProjectionMode) Foo(com.linkedin.pegasus.generator.examples.Foo) DataMap(com.linkedin.data.DataMap) DataProvider(org.testng.annotations.DataProvider)

Example 3 with ProjectionMode

use of com.linkedin.restli.server.ProjectionMode in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method dataProvider.

@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
public Object[][] dataProvider() {
    Map<CompoundKey, Foo> results = new HashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
    CompoundKey c3 = new CompoundKey().append("a", "a3").append("b", 3);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    Foo projectedRecord1 = new Foo().setStringField("record1");
    Foo record2 = new Foo().setStringField("record2").setIntField(7);
    Foo projectedRecord2 = new Foo().setStringField("record2");
    results.put(c1, record1);
    results.put(c2, record2);
    DataMap projectionDataMap = new DataMap();
    projectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree maskTree = new MaskTree(projectionDataMap);
    Map<String, Foo> protocol1TransformedResults = new HashMap<String, Foo>();
    protocol1TransformedResults.put("a=a1&b=1", record1);
    protocol1TransformedResults.put("a=a2&b=2", record2);
    Map<String, Foo> protocol1TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol1TransformedResultsWithProjection.put("a=a1&b=1", projectedRecord1);
    protocol1TransformedResultsWithProjection.put("a=a2&b=2", projectedRecord2);
    Map<String, Foo> protocol2TransformedResults = new HashMap<String, Foo>();
    protocol2TransformedResults.put("(a:a1,b:1)", record1);
    protocol2TransformedResults.put("(a:a2,b:2)", record2);
    Map<String, Foo> protocol2TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol2TransformedResultsWithProjection.put("(a:a1,b:1)", projectedRecord1);
    protocol2TransformedResultsWithProjection.put("(a:a2,b:2)", projectedRecord2);
    Map<String, ErrorResponse> protocol1Errors = Collections.singletonMap("a=a3&b=3", new ErrorResponse().setStatus(404));
    Map<String, ErrorResponse> protocol2Errors = Collections.singletonMap("(a:a3,b:3)", new ErrorResponse().setStatus(404));
    Map<CompoundKey, HttpStatus> statuses = new HashMap<CompoundKey, HttpStatus>();
    statuses.put(c1, HttpStatus.S_200_OK);
    statuses.put(c2, HttpStatus.S_200_OK);
    Map<CompoundKey, RestLiServiceException> exceptions = new HashMap<CompoundKey, RestLiServiceException>();
    exceptions.put(c3, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
    BatchResult<CompoundKey, Foo> batchResult = new BatchResult<CompoundKey, Foo>(results, statuses, exceptions);
    Map<Object, RestLiServiceException> exceptionsWithUntypedKey = new HashMap<Object, RestLiServiceException>(exceptions);
    ProtocolVersion protocolVersion1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    ProtocolVersion protocolVersion2 = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    return new Object[][] { // automatic projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, // manual projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, // manual projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, // automatic projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { results, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto } };
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) HttpStatus(com.linkedin.restli.common.HttpStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchResult(com.linkedin.restli.server.BatchResult) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) DataProvider(org.testng.annotations.DataProvider)

Example 4 with ProjectionMode

use of com.linkedin.restli.server.ProjectionMode in project rest.li by linkedin.

the class TestGetResponseBuilder method dataProvider.

@DataProvider(name = "testData")
public Object[][] dataProvider() {
    DataMap projectionDataMap = new DataMap();
    projectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree maskTree = new MaskTree(projectionDataMap);
    ProjectionMode manual = ProjectionMode.MANUAL;
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    return new Object[][] { // no projections with null projection masks and auto projection mode
    { getRecord(), HttpStatus.S_200_OK, null, auto }, { new GetResult<Foo>(getRecord(), HttpStatus.S_207_MULTI_STATUS), HttpStatus.S_207_MULTI_STATUS, null, auto }, // no projections with null projection masks and manual projection mode
    { getRecord(), HttpStatus.S_200_OK, null, manual }, { new GetResult<Foo>(getRecord(), HttpStatus.S_207_MULTI_STATUS), HttpStatus.S_207_MULTI_STATUS, null, manual }, // no projections with non-null projection masks and manual projection mode
    { getRecord(), HttpStatus.S_200_OK, maskTree, manual }, { new GetResult<Foo>(getRecord(), HttpStatus.S_207_MULTI_STATUS), HttpStatus.S_207_MULTI_STATUS, maskTree, manual }, // projections with non-null projection masks and auto projection mode
    { getRecord(), HttpStatus.S_200_OK, maskTree, auto }, { new GetResult<Foo>(getRecord(), HttpStatus.S_207_MULTI_STATUS), HttpStatus.S_207_MULTI_STATUS, maskTree, auto } };
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) Foo(com.linkedin.pegasus.generator.examples.Foo) DataMap(com.linkedin.data.DataMap) DataProvider(org.testng.annotations.DataProvider)

Aggregations

DataMap (com.linkedin.data.DataMap)4 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)4 ProjectionMode (com.linkedin.restli.server.ProjectionMode)4 Foo (com.linkedin.pegasus.generator.examples.Foo)3 DataProvider (org.testng.annotations.DataProvider)3 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)2 HashMap (java.util.HashMap)2 RequestContext (com.linkedin.r2.message.RequestContext)1 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)1 CompoundKey (com.linkedin.restli.common.CompoundKey)1 ErrorResponse (com.linkedin.restli.common.ErrorResponse)1 HttpStatus (com.linkedin.restli.common.HttpStatus)1 LinkArray (com.linkedin.restli.common.LinkArray)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 MutablePathKeys (com.linkedin.restli.internal.server.MutablePathKeys)1 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)1 BatchResult (com.linkedin.restli.server.BatchResult)1 CollectionResult (com.linkedin.restli.server.CollectionResult)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 URI (java.net.URI)1