Search in sources :

Example 71 with PathSpec

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

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

the class TestRestLiMethodInvocation method testCustomCrudParamsCollectionPartialUpdate.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "paramCollectionPartialUpdate")
public void testCustomCrudParamsCollectionPartialUpdate(ProtocolVersion version, String uri, String body) throws Exception {
    ResourceModel model = buildResourceModel(CombinedResources.CollectionWithCustomCrudParams.class);
    ResourceMethodDescriptor methodDescriptor = model.findMethod(ResourceMethod.PARTIAL_UPDATE);
    CombinedResources.CollectionWithCustomCrudParams resource = getMockResource(CombinedResources.CollectionWithCustomCrudParams.class);
    PatchTree p = new PatchTree();
    p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(Integer.valueOf(42)));
    PatchRequest<CombinedTestDataModels.Foo> expected = PatchRequest.createFromPatchDocument(p.getDataMap());
    EasyMock.expect(resource.myUpdate(eq("foo"), eq(expected), eq(1), eq("bar"))).andReturn(null).once();
    checkInvocation(resource, methodDescriptor, "POST", version, uri, body, buildPathKeys("testId", "foo"));
}
Also used : CombinedResources(com.linkedin.restli.server.combined.CombinedResources) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) PathSpec(com.linkedin.data.schema.PathSpec) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 73 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) {
        Collection<PathSpec> actualFields = (Collection<PathSpec>) request.getQueryParamsObjects().get(RestConstants.FIELDS_PARAM);
        for (PathSpec field : actualFields) {
            Assert.assertTrue(expectedFields.contains(field));
        }
    }
    Set<String> uriIds = new HashSet<String>();
    for (Long id : (Collection<Long>) request.getQueryParamsObjects().get(RestConstants.QUERY_BATCH_IDS_PARAM)) {
        uriIds.add(id.toString());
    }
    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<Object> idObjects = request.getObjectIds();
    Set<String> theseIds = new HashSet<String>(idObjects.size());
    for (Object o : idObjects) {
        theseIds.add(o.toString());
    }
    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) ResourceProperties(com.linkedin.restli.common.ResourceProperties) PathSpec(com.linkedin.data.schema.PathSpec) RecordTemplate(com.linkedin.data.template.RecordTemplate) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 74 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<String>>();
    Set<Long> requestIds = new HashSet<Long>();
    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<String>();
        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)

Example 75 with PathSpec

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

the class TestScatterGather method testGetEntityRequest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void testGetEntityRequest(BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> 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) {
        Collection<PathSpec> actualFields = (Collection<PathSpec>) request.getQueryParamsObjects().get(RestConstants.FIELDS_PARAM);
        for (PathSpec field : actualFields) {
            Assert.assertTrue(expectedFields.contains(field));
        }
    }
    Set<String> uriIds = new HashSet<String>();
    for (Long id : (Collection<Long>) request.getQueryParamsObjects().get(RestConstants.QUERY_BATCH_IDS_PARAM)) {
        uriIds.add(id.toString());
    }
    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<Object> idObjects = request.getObjectIds();
    Set<String> theseIds = new HashSet<String>(idObjects.size());
    for (Object o : idObjects) {
        theseIds.add(o.toString());
    }
    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) ResourceProperties(com.linkedin.restli.common.ResourceProperties) PathSpec(com.linkedin.data.schema.PathSpec) RecordTemplate(com.linkedin.data.template.RecordTemplate) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

PathSpec (com.linkedin.data.schema.PathSpec)79 Test (org.testng.annotations.Test)63 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)31 DataMap (com.linkedin.data.DataMap)21 HashSet (java.util.HashSet)11 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)9 DataList (com.linkedin.data.DataList)8 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)7 ByteString (com.linkedin.data.ByteString)6 ArrayList (java.util.ArrayList)6 Foo (com.linkedin.pegasus.generator.examples.Foo)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)5 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)5 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)5 AfterTest (org.testng.annotations.AfterTest)5 BeforeTest (org.testng.annotations.BeforeTest)5 TestRecord (com.linkedin.restli.client.test.TestRecord)4 List (java.util.List)4