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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations