Search in sources :

Example 36 with PathSpec

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

the class TestScatterGather method testBuildSGGetKVRequests.

private static void testBuildSGGetKVRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> requests = buildScatterGatherGetKVRequests(sg, ids);
    Assert.assertEquals(requests.size(), numEndpoints);
    Set<Set<String>> requestIdSets = new HashSet<>();
    Set<Long> requestIds = new HashSet<>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, 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, 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) HashSet(java.util.HashSet)

Example 37 with PathSpec

use of com.linkedin.data.schema.PathSpec 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 38 with PathSpec

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

the class TestBatchCreateResponseBuilder method testProjectionInBuildRestLiResponseData.

@Test
@SuppressWarnings("unchecked")
public void testProjectionInBuildRestLiResponseData() throws URISyntaxException {
    MaskTree maskTree = new MaskTree();
    maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false).atLeastOnce();
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(true);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>();
    Foo foo = new Foo();
    foo.setStringField("foo1");
    foo.setFruitsField(Fruits.APPLE);
    createKVResponses.add(new CreateKVResponse<>(1L, foo));
    BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, Collections.emptyMap(), Collections.emptyList());
    Assert.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
    CreateIdEntityStatus<Long, Foo> item = (CreateIdEntityStatus<Long, Foo>) responseData.getResponseEnvelope().getCreateResponses().get(0).getRecord();
    Assert.assertEquals(item.getLocation(), "/foo/1");
    DataMap dataMap = item.data().getDataMap("entity");
    Assert.assertEquals(dataMap.size(), 1);
    Assert.assertEquals(dataMap.get("fruitsField"), Fruits.APPLE.toString());
    EasyMock.verify(mockContext);
}
Also used : CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) PathSpec(com.linkedin.data.schema.PathSpec) URI(java.net.URI) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) Test(org.testng.annotations.Test)

Example 39 with PathSpec

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

the class TestBatchGetResponseBuilder method testProjectionInBuildRestliResponseData.

@Test
public void testProjectionInBuildRestliResponseData() {
    MaskTree maskTree = new MaskTree();
    maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
    EasyMock.expect(mockContext.getBatchKeyErrors()).andReturn(Collections.emptyMap()).once();
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.expect(mockContext.isFillInDefaultsRequested()).andReturn(false).anyTimes();
    EasyMock.replay(mockContext);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<Integer, Foo> results = new HashMap<>();
    Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
    results.put(1, value);
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData<BatchGetResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.emptyMap(), Collections.emptyList());
    RecordTemplate record = responseData.getResponseEnvelope().getBatchResponseMap().get(1).getRecord();
    Assert.assertEquals(record.data().size(), 1);
    Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    EasyMock.verify(mockContext);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) PathSpec(com.linkedin.data.schema.PathSpec) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 40 with PathSpec

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

the class TestGetResponseBuilder method testProjectionInBuildRestliResponseData.

@Test
public void testProjectionInBuildRestliResponseData() {
    MaskTree maskTree = new MaskTree();
    maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
    ServerResourceContext mockContext = getMockResourceContext(maskTree, ProjectionMode.AUTOMATIC);
    RoutingResult routingResult = new RoutingResult(mockContext, getMockResourceMethodDescriptor());
    Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
    GetResponseBuilder responseBuilder = new GetResponseBuilder();
    RestLiResponseData<GetResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(null, routingResult, value, Collections.emptyMap(), Collections.emptyList());
    RecordTemplate record = responseData.getResponseEnvelope().getRecord();
    Assert.assertEquals(record.data().size(), 1);
    Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    EasyMock.verify(mockContext);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) Foo(com.linkedin.pegasus.generator.examples.Foo) 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