Search in sources :

Example 11 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class RestLiArgumentBuilderTestHelper method getMockResourceModel.

static ResourceModel getMockResourceModel(Class<? extends RecordTemplate> valueClass, Key key, Key[] associationKeys, Set<Object> batchKeys) {
    ResourceModel model = createMock(ResourceModel.class);
    if (valueClass != null) {
        expect((Class) model.getValueClass()).andReturn(valueClass);
    }
    // This conditional block to set the mock expectations doesn't explicitly take care of Alternate Key types yet.
    if (key != null) {
        expect(model.getPrimaryKey()).andReturn(key).anyTimes();
        if (CompoundKey.class.equals(key.getType())) {
            Set<Key> assocKeys = new HashSet<>();
            Collections.addAll(assocKeys, associationKeys);
            expect(model.getKeys()).andReturn(assocKeys).anyTimes();
        } else if (ComplexResourceKey.class.equals(key.getType())) {
            if (batchKeys != null && batchKeys.size() > 0) {
                ComplexResourceKey<? extends RecordTemplate, ? extends RecordTemplate> complexKey = (ComplexResourceKey<? extends RecordTemplate, ? extends RecordTemplate>) batchKeys.toArray()[0];
                expect((Class) model.getKeyKeyClass()).andReturn(complexKey.getKey().getClass()).anyTimes();
                expect((Class) model.getKeyParamsClass()).andReturn(complexKey.getParams().getClass()).anyTimes();
            }
        }
    }
    replay(model);
    return model;
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) HashSet(java.util.HashSet)

Example 12 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class CollectionResponseBuilder method buildResponse.

@Override
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
    CollectionResponseEnvelope response = responseData.getCollectionResponseEnvelope();
    PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
    CollectionResponse<AnyRecord> collectionResponse = new CollectionResponse<AnyRecord>(AnyRecord.class);
    collectionResponse.setPaging(response.getCollectionResponsePaging());
    DataList elementsMap = (DataList) collectionResponse.data().get(CollectionResponse.ELEMENTS);
    for (RecordTemplate entry : response.getCollectionResponse()) {
        CheckedUtil.addWithoutChecking(elementsMap, entry.data());
    }
    if (response.getCollectionResponseCustomMetadata() != null) {
        collectionResponse.setMetadataRaw(response.getCollectionResponseCustomMetadata().data());
    }
    builder.entity(collectionResponse);
    return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : DataList(com.linkedin.data.DataList) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RecordTemplate(com.linkedin.data.template.RecordTemplate)

Example 13 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class TestRestUtils method testRecursiveBasic.

@Test
public void testRecursiveBasic() throws CloneNotSupportedException {
    LinkedListNode node1 = new LinkedListNode();
    node1.setIntField(1);
    LinkedListNode node2 = new LinkedListNode();
    node2.setIntField(2);
    node1.setNext(node2);
    RecordTemplate expected = node1.copy();
    // Introduce bad elements.
    node1.data().put("foo", "bar");
    node2.data().put("foo", "bar");
    Assert.assertEquals(node1.getIntField().intValue(), 1);
    Assert.assertEquals(node1.getNext(), node2);
    Assert.assertEquals(node1.data().size(), 3);
    Assert.assertEquals(node2.getIntField().intValue(), 2);
    Assert.assertEquals(node2.getNext(), null);
    Assert.assertEquals(node2.data().size(), 2);
    RestUtils.trimRecordTemplate(node1, false);
    Assert.assertEquals(node1, expected);
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) LinkedListNode(com.linkedin.restli.server.LinkedListNode) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 14 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class TestRestLiResponseData method testCollectionResponseEnvelopeUpdates.

@Test(dataProvider = "collectionResponseEnvelopesProvider")
@SuppressWarnings("unchecked")
public void testCollectionResponseEnvelopeUpdates(RestLiResponseDataImpl responseData) {
    CollectionResponseEnvelope responseEnvelope = responseData.getCollectionResponseEnvelope();
    Assert.assertFalse(responseData.isErrorResponse());
    Assert.assertEquals(responseEnvelope.getCollectionResponse(), Collections.<EmptyRecord>emptyList());
    Assert.assertEquals(responseEnvelope.getCollectionResponsePaging(), new CollectionMetadata());
    Assert.assertEquals(responseEnvelope.getCollectionResponseCustomMetadata(), new EmptyRecord());
    // Swap to exception
    responseData.setException(exception500);
    Assert.assertNull(responseEnvelope.getCollectionResponse());
    Assert.assertNull(responseEnvelope.getCollectionResponseCustomMetadata());
    Assert.assertNull(responseEnvelope.getCollectionResponsePaging());
    Assert.assertEquals(responseData.getServiceException(), exception500);
    Assert.assertEquals(responseData.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    // Swap back
    responseEnvelope.setCollectionResponse(new ArrayList<RecordTemplate>(), new CollectionMetadata(), new EmptyRecord(), HttpStatus.S_200_OK);
    Assert.assertFalse(responseData.isErrorResponse());
    Assert.assertEquals(responseEnvelope.getCollectionResponse(), Collections.<EmptyRecord>emptyList());
    Assert.assertEquals(responseEnvelope.getCollectionResponsePaging(), new CollectionMetadata());
    Assert.assertEquals(responseEnvelope.getCollectionResponseCustomMetadata(), new EmptyRecord());
    // Check mutability when available
    List<EmptyRecord> temp = (List<EmptyRecord>) responseEnvelope.getCollectionResponse();
    temp.add(new EmptyRecord());
    Assert.assertEquals(responseEnvelope.getCollectionResponse().size(), 1);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) RecordTemplate(com.linkedin.data.template.RecordTemplate) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 15 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class TestAbstractRequestBuilder method testParametersAreReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testParametersAreReadOnly() {
    final AbstractRequestBuilder<?, ?, ?> builder = new DummyAbstractRequestBuilder();
    TestRecord testRecord = new TestRecord();
    builder.setParam("abc", testRecord);
    Map<String, Object> parameters = builder.buildReadOnlyQueryParameters();
    Assert.assertNotSame(parameters.get("abc"), testRecord);
    Assert.assertTrue(((RecordTemplate) parameters.get("abc")).data().isMadeReadOnly());
    testRecord.data().makeReadOnly();
    parameters = builder.buildReadOnlyQueryParameters();
    Assert.assertSame(parameters.get("abc"), testRecord);
    TestRecord testRecord2 = new TestRecord();
    builder.addParam("abc2", testRecord2);
    parameters = builder.buildReadOnlyQueryParameters();
    List<Object> collectionParam = (List<Object>) parameters.get("abc2");
    Assert.assertNotSame(collectionParam.get(0), testRecord2);
    Assert.assertTrue(((RecordTemplate) collectionParam.get(0)).data().isMadeReadOnly());
    testRecord2.data().makeReadOnly();
    parameters = builder.buildReadOnlyQueryParameters();
    collectionParam = (List<Object>) parameters.get("abc2");
    Assert.assertSame(collectionParam.get(0), testRecord2);
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) ArrayList(java.util.ArrayList) List(java.util.List) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

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