use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.
the class TestPatchGeneration method testNegativeMask.
@Test
public void testNegativeMask() throws Exception {
MaskTree mask = MaskCreator.createNegativeMask(Group.fields().badge(), Group.fields().id(), Group.fields().owner().id());
//"{id=0, owner={id=0}, badge=0}"
final DataMap idOwnerBadgeMap = new DataMap();
idOwnerBadgeMap.put("id", 0);
idOwnerBadgeMap.put("badge", 0);
final DataMap idMap = new DataMap();
idMap.put("id", 0);
idOwnerBadgeMap.put("owner", idMap);
Assert.assertEquals(mask.getDataMap(), idOwnerBadgeMap, "The MaskTree DataMap should match");
//The ordering might be different but the URI should look something like:
//"-id,owner:(-id),-badge";
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("owner:(-id)");
expectedURISet.add("-badge");
Assert.assertEquals(maskURISet, expectedURISet, "The encoded mask should be correct");
}
use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.
the class ManualProjectionsResource method get.
@RestMethod.Get
public Greeting get(Long key, @QueryParam("ignoreProjection") @Optional("false") boolean ignoreProjection) {
ResourceContext context = getContext();
Greeting greeting = new Greeting();
context.setProjectionMode(ProjectionMode.MANUAL);
MaskTree mask = context.getProjectionMask();
if (mask != null && ignoreProjection == false) {
if (mask.getOperations().get(Greeting.fields().message()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setMessage("Projected message!");
}
if (mask.getOperations().get(Greeting.fields().tone()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setTone(Tone.FRIENDLY);
}
if (mask.getOperations().get(Greeting.fields().id()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setId(key);
}
} else {
greeting.setMessage("Full greeting.");
greeting.setTone(Tone.FRIENDLY);
greeting.setId(key);
}
return greeting;
}
use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.
the class TestPatchGeneration method testNestedPositiveMask.
@Test
public void testNestedPositiveMask() throws Exception {
List<PathSpec> fields = Arrays.asList(Group.fields().id(), Group.fields().location().latitude(), Group.fields().location().longitude(), Group.fields().name());
MaskTree mask = MaskCreator.createPositiveMask(fields);
//"{id=1, location={longitude=1, latitude=1}, name=1}"
final DataMap idLocationNameMap = new DataMap();
idLocationNameMap.put("id", 1);
idLocationNameMap.put("name", 1);
final DataMap longLatMap = new DataMap();
longLatMap.put("longitude", 1);
longLatMap.put("latitude", 1);
idLocationNameMap.put("location", longLatMap);
Assert.assertEquals(mask.getDataMap(), idLocationNameMap, "The MaskTree DataMap should match");
//The ordering might be different but the URI should look something like:
//"id,location:(longitude,latitude),name";
final String actualEncodedMaskURI = URIMaskUtil.encodeMaskForURI(mask);
//We convert back into a MaskTree so we can compare DataMaps because the URI could be in any order
final MaskTree generatedMaskTree = URIMaskUtil.decodeMaskUriFormat(new StringBuilder(actualEncodedMaskURI));
Assert.assertEquals(generatedMaskTree.getDataMap(), idLocationNameMap, "The actual encoded Mask URI should be correct");
}
use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder 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.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<CreateKVResponse<Long, Foo>>();
Foo foo = new Foo();
foo.setStringField("foo1");
foo.setFruitsField(Fruits.APPLE);
createKVResponses.add(new CreateKVResponse<Long, Foo>(1L, foo));
BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<Long, Foo>(createKVResponses);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
Assert.assertTrue(responseData.getBatchCreateResponseEnvelope().isGetAfterCreate());
DataMap dataMap = responseData.getBatchCreateResponseEnvelope().getCreateResponses().get(0).getRecord().data().getDataMap("entity");
Assert.assertEquals(dataMap.size(), 1);
Assert.assertEquals(dataMap.get("fruitsField"), Fruits.APPLE.toString());
EasyMock.verify(mockContext);
}
use of com.linkedin.data.transform.filter.request.MaskTree 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.<Object, RestLiServiceException>emptyMap()).once();
EasyMock.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<Integer, Foo> results = new HashMap<Integer, Foo>();
Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
results.put(1, value);
BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
RecordTemplate record = responseData.getBatchResponseEnvelope().getBatchResponseMap().get(1).getRecord();
Assert.assertEquals(record.data().size(), 1);
Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
EasyMock.verify(mockContext);
}
Aggregations