use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class RestLiResourceRelationship method findDataModels.
private void findDataModels() {
final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {
@Override
public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
final String schema = resourceSchema.getSchema();
// ActionSet resources do not have a schema
if (schema != null) {
final NamedDataSchema schemaSchema = extractSchema(schema);
if (schemaSchema != null) {
connectSchemaToResource(visitContext, schemaSchema);
}
}
}
@Override
public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
final IdentifierSchema id = collectionSchema.getIdentifier();
final NamedDataSchema typeSchema = extractSchema(id.getType());
if (typeSchema != null) {
connectSchemaToResource(visitContext, typeSchema);
}
final String params = id.getParams();
if (params != null) {
final NamedDataSchema paramsSchema = extractSchema(params);
if (paramsSchema != null) {
connectSchemaToResource(visitContext, paramsSchema);
}
}
}
@Override
public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
for (AssocKeySchema key : associationSchema.getAssocKeys()) {
final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
if (keyTypeSchema != null) {
connectSchemaToResource(visitContext, keyTypeSchema);
}
}
}
@Override
public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
String parameterTypeString = parameterSchema.getType();
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(parameterTypeString)) {
visitInlineSchema(visitContext, parameterTypeString);
} else {
final NamedDataSchema schema;
// grab the schema name from it
if (parameterSchema.hasItems()) {
schema = extractSchema(parameterSchema.getItems());
} else // the only remaining possibility is that the type field contains the name of a data schema
{
schema = extractSchema(parameterTypeString);
}
if (schema != null) {
connectSchemaToResource(visitContext, schema);
}
}
}
@Override
public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
final MetadataSchema metadata = finderSchema.getMetadata();
if (metadata != null) {
final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
if (metadataTypeSchema != null) {
connectSchemaToResource(visitContext, metadataTypeSchema);
}
}
}
@Override
public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
final String returns = actionSchema.getReturns();
if (returns != null) {
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(returns)) {
visitInlineSchema(visitContext, returns);
} else // otherwise the type field contains the name of a data schema
{
final NamedDataSchema returnsSchema = extractSchema(returns);
if (returnsSchema != null) {
connectSchemaToResource(visitContext, returnsSchema);
}
}
}
final StringArray throwsArray = actionSchema.getThrows();
if (throwsArray != null) {
for (String errorName : throwsArray) {
final NamedDataSchema errorSchema = extractSchema(errorName);
if (errorSchema != null) {
connectSchemaToResource(visitContext, errorSchema);
}
}
}
}
private boolean isInlineSchema(String schemaString) {
return schemaString.startsWith("{");
}
private void visitInlineSchema(VisitContext visitContext, String schemaString) {
DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver);
if (schema instanceof ArrayDataSchema) {
DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
if (itemSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
}
}
if (schema instanceof MapDataSchema) {
DataSchema valueSchema = ((MapDataSchema) schema).getValues();
if (valueSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
}
}
}
private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
_dataModels.put(schema.getFullName(), schema);
final DataSchemaTraverse traveler = new DataSchemaTraverse();
traveler.traverse(schema, new DataSchemaTraverse.Callback() {
@Override
public void callback(List<String> path, DataSchema nestedSchema) {
if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
_dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
schemaNode.addAdjacentNode(node);
}
}
});
final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
resourceNode.addAdjacentNode(schemaNode);
schemaNode.addAdjacentNode(resourceNode);
}
};
ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestRestLiResponseEnvelope method testSetNewEnvelopeData.
@Test(dataProvider = "envelopeResourceMethodDataProvider")
public void testSetNewEnvelopeData(RestLiResponseEnvelope responseEnvelope, ResourceMethod resourceMethod) {
ResponseType responseType = ResponseType.fromMethodType(resourceMethod);
switch(responseType) {
case SINGLE_ENTITY:
RecordResponseEnvelope recordResponseEnvelope = (RecordResponseEnvelope) responseEnvelope;
RecordTemplate oldRecord = recordResponseEnvelope.getRecord();
RecordTemplate newRecord = new AnyRecord(new DataMap());
newRecord.data().put("test", "testing");
recordResponseEnvelope.setRecord(newRecord, HttpStatus.S_200_OK);
Assert.assertNotEquals(recordResponseEnvelope.getRecord(), oldRecord);
break;
case GET_COLLECTION:
CollectionResponseEnvelope collectionResponseEnvelope = (CollectionResponseEnvelope) responseEnvelope;
List<? extends RecordTemplate> oldResponses = collectionResponseEnvelope.getCollectionResponse();
RecordTemplate oldResponseMetadata = collectionResponseEnvelope.getCollectionResponseCustomMetadata();
CollectionMetadata oldPagingMetadata = collectionResponseEnvelope.getCollectionResponsePaging();
RecordTemplate newResponseMetadata = new AnyRecord(new DataMap());
newResponseMetadata.data().put("test", "testing");
CollectionMetadata newResponsesPaging = new CollectionMetadata();
List<? extends RecordTemplate> newResponses = Arrays.asList(new AnyRecord(new DataMap()));
collectionResponseEnvelope.setCollectionResponse(newResponses, newResponsesPaging, newResponseMetadata, HttpStatus.S_200_OK);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponse(), oldResponses);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), oldResponseMetadata);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponsePaging(), oldPagingMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponse(), newResponses);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), newResponseMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponsePaging(), newResponsesPaging);
break;
case CREATE_COLLECTION:
BatchCreateResponseEnvelope batchCreateResponseEnvelope = (BatchCreateResponseEnvelope) responseEnvelope;
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> oldCreateResponses = batchCreateResponseEnvelope.getCreateResponses();
CreateIdStatus<String> newCreateIdStatus = new CreateIdStatus<String>(new DataMap(), "key");
RestLiServiceException newException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
BatchCreateResponseEnvelope.CollectionCreateResponseItem successCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newCreateIdStatus);
BatchCreateResponseEnvelope.CollectionCreateResponseItem exceptionCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newException, "id2");
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> newCreateResponses = Arrays.asList(successCreateItem, exceptionCreateItem);
batchCreateResponseEnvelope.setCreateResponse(newCreateResponses, HttpStatus.S_200_OK);
Assert.assertNotEquals(batchCreateResponseEnvelope.getCreateResponses(), oldCreateResponses);
Assert.assertEquals(batchCreateResponseEnvelope.getCreateResponses(), newCreateResponses);
BatchCreateResponseEnvelope.CollectionCreateResponseItem firstItem = batchCreateResponseEnvelope.getCreateResponses().get(0);
Assert.assertNull(firstItem.getId());
Assert.assertEquals(firstItem.getRecord(), newCreateIdStatus);
Assert.assertFalse(firstItem.isErrorResponse());
Assert.assertNull(firstItem.getException());
BatchCreateResponseEnvelope.CollectionCreateResponseItem secondItem = batchCreateResponseEnvelope.getCreateResponses().get(1);
Assert.assertEquals(secondItem.getId(), "id2");
Assert.assertNull(secondItem.getRecord());
Assert.assertTrue(secondItem.isErrorResponse());
Assert.assertEquals(secondItem.getException(), newException);
break;
case BATCH_ENTITIES:
BatchResponseEnvelope batchResponseEnvelope = (BatchResponseEnvelope) responseEnvelope;
Map<?, BatchResponseEnvelope.BatchResponseEntry> oldBatchResponses = batchResponseEnvelope.getBatchResponseMap();
RecordTemplate newResponseRecord = new EmptyRecord();
RestLiServiceException newResponseException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<String, BatchResponseEnvelope.BatchResponseEntry> newBatchResponses = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
newBatchResponses.put("id1", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, newResponseRecord));
newBatchResponses.put("id2", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_500_INTERNAL_SERVER_ERROR, newResponseException));
batchResponseEnvelope.setBatchResponseMap(newBatchResponses, HttpStatus.S_200_OK);
Map<?, BatchResponseEnvelope.BatchResponseEntry> envelopeMap = batchResponseEnvelope.getBatchResponseMap();
Assert.assertNotEquals(envelopeMap, oldBatchResponses);
Assert.assertEquals(envelopeMap, newBatchResponses);
BatchResponseEnvelope.BatchResponseEntry id1Entry = envelopeMap.get("id1");
Assert.assertEquals(id1Entry.getStatus(), HttpStatus.S_200_OK);
Assert.assertEquals(id1Entry.getRecord(), newResponseRecord);
Assert.assertFalse(id1Entry.hasException());
Assert.assertNull(id1Entry.getException());
BatchResponseEnvelope.BatchResponseEntry id2Entry = envelopeMap.get("id2");
Assert.assertEquals(id2Entry.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Assert.assertNull(id2Entry.getRecord());
Assert.assertTrue(id2Entry.hasException());
Assert.assertEquals(id2Entry.getException(), newResponseException);
break;
case STATUS_ONLY:
// status only envelopes are blank by default since they have no data fields
break;
default:
throw new IllegalStateException();
}
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestRestUtils method testRefTrim.
@Test
public void testRefTrim() throws CloneNotSupportedException {
TyperefTest test = new TyperefTest();
test.setImportRef(1.0);
test.setImportRef2(2.0);
RecordTemplate expected = test.copy();
// Introduce bad elements
test.data().put("troublemaker", "boo");
Assert.assertEquals(test.data().size(), 3);
RestUtils.trimRecordTemplate(test, false);
Assert.assertEquals(test, expected);
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestRestUtils method testTrimmerWithPrimitivesRecordsUnionsMix.
@Test
public void testTrimmerWithPrimitivesRecordsUnionsMix() throws CloneNotSupportedException {
TyperefTest recordTemplate = new TyperefTest();
recordTemplate.setBoolean(true);
RecordBar foo = new RecordBar();
foo.setLocation("foo");
recordTemplate.setBar1(foo);
TyperefTest.Union5 union = new TyperefTest.Union5();
union.setIntRef(5);
recordTemplate.setUnion5(union);
RecordTemplate expected = recordTemplate.copy();
// Introduce bad elements
recordTemplate.getBar1().data().put("troublemaker", "foo");
((DataMap) recordTemplate.getUnion5().data()).put("troublemaker", "foo");
recordTemplate.data().put("foo", "bar");
DataList list = new DataList();
list.add(1);
DataMap map = new DataMap();
map.put("foo", 666);
recordTemplate.data().put("keyFoo", list);
recordTemplate.data().put("keyBar", map);
// Pre filtering
Assert.assertEquals(recordTemplate.data().size(), 6);
Assert.assertEquals(recordTemplate.getBar1().data().size(), 2);
RestUtils.trimRecordTemplate(recordTemplate, false);
// Post filtering
Assert.assertEquals(recordTemplate, expected);
}
Aggregations