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;
}
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();
}
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);
}
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);
}
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);
}
Aggregations