Search in sources :

Example 81 with PathSpec

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

the class TestCreateResponseBuilder method testProjectionInBuildRestLiResponseData.

@Test
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.isReturnEntityRequested()).andReturn(true);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);
    RoutingResult routingResult = new RoutingResult(mockContext, null);
    Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
    CreateKVResponse<Integer, Foo> values = new CreateKVResponse<>(null, value);
    CreateResponseBuilder responseBuilder = new CreateResponseBuilder();
    RestLiResponseData<CreateResponseEnvelope> envelope = responseBuilder.buildRestLiResponseData(new RestRequestBuilder(new URI("/foo")).build(), routingResult, values, Collections.emptyMap(), Collections.emptyList());
    RecordTemplate record = envelope.getResponseEnvelope().getRecord();
    Assert.assertEquals(record.data().size(), 1);
    Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    Assert.assertTrue(envelope.getResponseEnvelope().isGetAfterCreate());
    EasyMock.verify(mockContext);
}
Also used : Foo(com.linkedin.pegasus.generator.examples.Foo) PathSpec(com.linkedin.data.schema.PathSpec) URI(java.net.URI) 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) 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 82 with PathSpec

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

the class GeneratePatchMethodInterceptor method handleGet.

private Object handleGet(Method method) {
    String propName = toCamelCase(method.getName(), 3);
    assertPropertyInSchema(propName);
    // And the patch would store the PathSpec of ["bar", "baz"] being set to 123.
    return GeneratePatchProxyFactory.newInstance(method.getReturnType(), _patchTree, new PathSpec(_pathSpec.getPathComponents(), propName));
}
Also used : ByteString(com.linkedin.data.ByteString) PathSpec(com.linkedin.data.schema.PathSpec)

Example 83 with PathSpec

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

the class GeneratePatchMethodInterceptor method handleRemove.

private Object handleRemove(String methodName) {
    String propName = toCamelCase(methodName, 6);
    assertPropertyInSchema(propName);
    _patchTree.addOperation(new PathSpec(_pathSpec.getPathComponents(), propName), PatchOpFactory.REMOVE_FIELD_OP);
    return null;
}
Also used : ByteString(com.linkedin.data.ByteString) PathSpec(com.linkedin.data.schema.PathSpec)

Example 84 with PathSpec

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

the class TestScatterGather method testRequest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void testRequest(BatchRequest<?> request, Set<String> expectedParams, Set<PathSpec> expectedFields, Map<Long, Greeting> expectedInput, Set<Set<String>> requestIdSets, Set<Long> requestIds) {
    Assert.assertEquals(request.getQueryParamsObjects().keySet(), expectedParams);
    if (expectedFields != null) {
        Set<PathSpec> actualFields = request.getFields();
        Assert.assertTrue(expectedFields.equals(actualFields));
    }
    Set<String> uriIds = request.getObjectIds().stream().map(Object::toString).collect(Collectors.toSet());
    if (expectedInput != null) {
        RecordTemplate inputRecordTemplate;
        if (request instanceof BatchUpdateRequest) {
            ResourceProperties resourceProperties = request.getResourceProperties();
            CollectionRequest inputRecord = (CollectionRequest) request.getInputRecord();
            inputRecordTemplate = CollectionRequestUtil.convertToBatchRequest(inputRecord, resourceProperties.getKeyType(), resourceProperties.getComplexKeyType(), resourceProperties.getKeyParts(), resourceProperties.getValueType());
        } else {
            inputRecordTemplate = request.getInputRecord();
        }
        checkInput(inputRecordTemplate.data().getDataMap(com.linkedin.restli.common.BatchRequest.ENTITIES), expectedInput, uriIds);
    }
    Set<String> theseIds = request.getObjectIds().stream().map(Object::toString).collect(Collectors.toSet());
    Assert.assertEquals(uriIds, theseIds);
    // no duplicate requests
    Assert.assertFalse(requestIdSets.contains(theseIds));
    for (String id : theseIds) {
        // no duplicate ids
        Assert.assertFalse(requestIds.contains(Long.parseLong(id)));
        requestIds.add(Long.parseLong(id));
    }
    requestIdSets.add(theseIds);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) RecordTemplate(com.linkedin.data.template.RecordTemplate) ResourceProperties(com.linkedin.restli.common.ResourceProperties) PathSpec(com.linkedin.data.schema.PathSpec)

Example 85 with PathSpec

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

the class TestScatterGather method testBuildSGGetRequests.

private static void testBuildSGGetRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
    Collection<ScatterGatherBuilder.RequestInfo<Greeting>> requests = buildScatterGatherGetRequests(sg, ids);
    Assert.assertEquals(requests.size(), numEndpoints);
    Set<Set<String>> requestIdSets = new HashSet<>();
    Set<Long> requestIds = new HashSet<>();
    for (ScatterGatherBuilder.RequestInfo<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<BatchResponse<Greeting>> request = requestInfo.getBatchRequest();
        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) BatchResponse(com.linkedin.restli.common.BatchResponse) PathSpec(com.linkedin.data.schema.PathSpec) HashSet(java.util.HashSet)

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