use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class TestQueryParamsDataMap method testProcessProjections.
/**
* Creates MaskTrees and extracts the subsequent DataMap to verify that processProjections can correctly
* convert it correctly into a Map that can later be constructed and encoded into an URI
*/
@Test
public void testProcessProjections() {
// Construct a MaskTree from a series of PathSpecs. Extract the subsequent Datamap representation.
final MaskTree rootObjectsMask = MaskCreator.createPositiveMask(new PathSpec("foo", PathSpec.WILDCARD, "bar"));
final MaskTree metadataMask = MaskCreator.createPositiveMask(new PathSpec("foo", "bar"), new PathSpec("bar", "baz"), new PathSpec("qux"));
final MaskTree pagingMask = MaskCreator.createPositiveMask(new PathSpec("total"), new PathSpec("count"), new PathSpec("links", PathSpec.WILDCARD, "rel"));
// For each type of projection, plus one query string parameter
final DataMap resultMap = new DataMap(4);
resultMap.put(RestConstants.FIELDS_PARAM, rootObjectsMask.getDataMap());
resultMap.put(RestConstants.METADATA_FIELDS_PARAM, metadataMask.getDataMap());
resultMap.put(RestConstants.PAGING_FIELDS_PARAM, pagingMask.getDataMap());
resultMap.put("someQueryString", "someValue");
final Map<String, List<String>> processedProjections = new LinkedHashMap<>();
final DataMap processedDataMap = QueryParamsDataMap.processProjections(resultMap, processedProjections);
Assert.assertTrue(processedDataMap.size() == 1, "Processed datamap should only have one item left!");
final Map<String, Set<String>> expectedProcessedProjections = new LinkedHashMap<>();
// "{fields=[foo:($*:(bar))], metadataFields=[foo:(bar),bar:(baz),qux], pagingFields=[total,count,links:($*:(rel))]}"
expectedProcessedProjections.put(RestConstants.FIELDS_PARAM, Collections.singleton("foo:($*:(bar))"));
expectedProcessedProjections.put(RestConstants.METADATA_FIELDS_PARAM, new HashSet<>(Arrays.asList("foo:(bar)", "bar:(baz)", "qux")));
expectedProcessedProjections.put(RestConstants.PAGING_FIELDS_PARAM, new HashSet<>(Arrays.asList("total", "count", "links:($*:(rel))")));
Assert.assertEquals(processedProjections.size(), expectedProcessedProjections.size(), "We must have the correct number of" + " expected projections!");
for (final Map.Entry<String, List<String>> entry : processedProjections.entrySet()) {
// Acceptable because these are always comma delimited
final Set<String> actualProjectionValueSet = new HashSet<>(Arrays.asList(entry.getValue().get(0).split(",")));
Assert.assertEquals(actualProjectionValueSet, expectedProcessedProjections.get(entry.getKey()), "The individual projection " + "for " + entry.getKey() + " does not match what is expected!");
}
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class RestSpecCodec method serializeTypeFields.
private void serializeTypeFields(DataMap data, PathSpec path) throws IOException {
for (Map.Entry<String, Object> entry : data.entrySet()) {
final PathSpec currentElement = new PathSpec(path.getPathComponents(), entry.getKey());
if (isPegasusTypeField(currentElement) && entry.getValue() instanceof DataMap) {
final String value = new String(_dataCodec.mapToBytes((DataMap) entry.getValue()), RestConstants.DEFAULT_CHARSET);
data.put(entry.getKey(), value);
} else if (entry.getValue() instanceof DataMap) {
serializeTypeFields((DataMap) entry.getValue(), currentElement);
} else if (entry.getValue() instanceof DataList) {
for (Object o : (DataList) entry.getValue()) {
if (o instanceof DataMap) {
serializeTypeFields((DataMap) o, currentElement);
}
}
}
}
}
use of com.linkedin.data.schema.PathSpec 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(actualEncodedMaskURI);
Assert.assertEquals(generatedMaskTree.getDataMap(), idLocationNameMap, "The actual encoded Mask URI should be correct");
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class TestScatterGather method testBuildSGGetEntityRequests.
private static void testBuildSGGetEntityRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> requests = buildScatterGatherGetEntityRequests(sg, ids);
Assert.assertEquals(requests.size(), numEndpoints);
Set<Set<String>> requestIdSets = new HashSet<>();
Set<Long> requestIds = new HashSet<>();
for (ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<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, EntityResponse<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 TestEmptyUnionValidation method testUnionEmptyWithProjection.
@Test
public void testUnionEmptyWithProjection() throws RemoteInvocationException {
ValidateEmptyUnion expected = new ValidateEmptyUnion();
expected.setFoo(new ValidateEmptyUnion.Foo());
List<PathSpec> spec = Collections.singletonList(ValidateEmptyUnion.fields().foo().Fuzz());
EmptyUnionRequestBuilders requestBuilders = new EmptyUnionRequestBuilders();
GetRequest<ValidateEmptyUnion> req = requestBuilders.get().id(1L).fields(spec.toArray(new PathSpec[spec.size()])).build();
ValidateEmptyUnion actual = getClient().sendRequest(req).getResponse().getEntity();
Assert.assertEquals(actual, expected);
}
Aggregations