Search in sources :

Example 56 with RecordTemplate

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);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceLevel(com.linkedin.restli.server.ResourceLevel) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) StringArray(com.linkedin.data.template.StringArray) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 57 with RecordTemplate

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();
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HashMap(java.util.HashMap) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ResponseType(com.linkedin.restli.internal.server.ResponseType) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) Test(org.testng.annotations.Test)

Example 58 with RecordTemplate

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);
}
Also used : TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest) RecordTemplate(com.linkedin.data.template.RecordTemplate) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 59 with RecordTemplate

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);
}
Also used : DataList(com.linkedin.data.DataList) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordBar(com.linkedin.pegasus.generator.test.RecordBar) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Aggregations

RecordTemplate (com.linkedin.data.template.RecordTemplate)59 DataMap (com.linkedin.data.DataMap)23 Test (org.testng.annotations.Test)23 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)9 DataList (com.linkedin.data.DataList)8 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)8 ArrayList (java.util.ArrayList)8 PathSpec (com.linkedin.data.schema.PathSpec)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)7 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)7 BeforeTest (org.testng.annotations.BeforeTest)7 ByteString (com.linkedin.data.ByteString)6 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)6 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)6 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)6 RoutingException (com.linkedin.restli.server.RoutingException)6