use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class MockBatchEntityResponseFactory method buildDataMap.
private static <K, V extends RecordTemplate> DataMap buildDataMap(Map<K, V> recordTemplates, Map<K, HttpStatus> statuses, Map<K, ErrorResponse> errorResponses, ProtocolVersion version) {
Set<K> mergedKeys = new HashSet<K>();
mergedKeys.addAll(recordTemplates.keySet());
mergedKeys.addAll(statuses.keySet());
mergedKeys.addAll(errorResponses.keySet());
DataMap batchResponseDataMap = new DataMap();
DataMap rawBatchData = new DataMap();
for (K key : mergedKeys) {
DataMap entityResponseData = new DataMap();
RecordTemplate recordTemplate = recordTemplates.get(key);
if (recordTemplate != null) {
entityResponseData.put(EntityResponse.ENTITY, recordTemplate.data());
}
HttpStatus status = statuses.get(key);
if (status != null) {
entityResponseData.put(EntityResponse.STATUS, status.getCode());
}
ErrorResponse errorResponse = errorResponses.get(key);
if (errorResponse != null) {
entityResponseData.put(EntityResponse.ERROR, errorResponse.data());
}
String stringKey = URIParamUtils.encodeKeyForBody(key, false, version);
rawBatchData.put(stringKey, entityResponseData);
}
batchResponseDataMap.put(BatchResponse.RESULTS, rawBatchData);
DataMap rawErrorData = new DataMap();
for (Map.Entry<K, ErrorResponse> errorResponse : errorResponses.entrySet()) {
rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
}
batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
return batchResponseDataMap;
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestClientBuilders method testComplexKeyGetRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "complexKeyAndParam")
public void testComplexKeyGetRequestBuilder(URIDetails expectedURIDetails) throws Exception {
GetRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new GetRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
ComplexResourceKey<TestRecord, TestRecord> id = buildComplexKey(1L, "KeyMessage", 10L, "ParamMessage");
RecordTemplate param1 = buildComplexParam(123, "ParamMessage");
GetRequest<TestRecord> request = builder.id(id).setParam("testParam", param1).build();
Assert.assertTrue(request.isSafe());
Assert.assertTrue(request.isIdempotent());
checkBasicRequest(request, expectedURIDetails, ResourceMethod.GET, null, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestClientBuilders method testComplexKeyDeleteRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "complexKeyAndParam")
public void testComplexKeyDeleteRequestBuilder(URIDetails expectedURIDetails) throws Exception {
DeleteRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new DeleteRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
ComplexResourceKey<TestRecord, TestRecord> id = buildComplexKey(1L, "KeyMessage", 10L, "ParamMessage");
RecordTemplate param = buildComplexParam(123, "ParamMessage");
DeleteRequest<TestRecord> request = builder.id(id).setParam("testParam", param).build();
Assert.assertFalse(request.isSafe());
Assert.assertTrue(request.isIdempotent());
checkBasicRequest(request, expectedURIDetails, ResourceMethod.DELETE, null, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestClientBuilders method checkKeyValueMapIsReadOnly.
private void checkKeyValueMapIsReadOnly(ComplexResourceKey<TestRecord, TestRecord> id1, ComplexResourceKey<TestRecord, TestRecord> id2, RecordTemplate t1, RecordTemplate t2, Class<? extends RecordTemplate> valueClass, Map<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> entries) {
for (Map.Entry<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> entry : entries.entrySet()) {
ComplexResourceKey<TestRecord, TestRecord> generatedKey = entry.getKey();
if (generatedKey.equals(id1)) {
checkComplexKeyIsReadOnly(id1, generatedKey);
} else {
checkComplexKeyIsReadOnly(id2, generatedKey);
}
Assert.assertTrue(generatedKey.isReadOnly());
RecordTemplate value = entry.getValue();
if (value.equals(t1)) {
if (t1.data().isMadeReadOnly()) {
Assert.assertSame(t1, value);
} else {
Assert.assertNotSame(t1, value);
}
} else {
if (t2.data().isReadOnly()) {
Assert.assertSame(t2, value);
} else {
Assert.assertNotSame(t2, value);
}
}
Assert.assertTrue(value.data().isMadeReadOnly());
}
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestRestClientRequestBuilder method clientGeneratedStreamRequest.
//This is similar to clientGeneratedRestRequest above except that it will generate a StreamRequest instead
//of a RestRequest. Note that this will ONLY happen if either acceptResponseAttachments below is 'true' OR
//streamingAttachmentDataSources below is non-null with a size greater then 0. If both of these do not hold,
//then a StreamRequest will not be generated by the RestClient.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> StreamRequest clientGeneratedStreamRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient, List<Object> streamingAttachmentDataSources, boolean acceptResponseAttachments) throws URISyntaxException {
// massive setup...
Client mockClient = EasyMock.createMock(Client.class);
@SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
@SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
//If there is a desire to receive response attachments, then we must use request options.
if (!acceptContentTypePerClient || acceptResponseAttachments) {
requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, acceptResponseAttachments);
}
setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
if (streamingAttachmentDataSources != null && streamingAttachmentDataSources.size() > 0) {
EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(streamingAttachmentDataSources).times(2);
} else {
EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
}
setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
Capture<StreamRequest> streamRequestCapture = new Capture<StreamRequest>();
EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
mockClient.streamRequest(EasyMock.capture(streamRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<StreamResponse>) EasyMock.anyObject());
EasyMock.expectLastCall().once();
EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
// do work!
RestClient restClient;
if (acceptContentTypePerClient) {
// configuration per client
restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
} else {
// configuration per request
restClient = new RestClient(mockClient, HOST);
}
restClient.sendRequest(mockRequest);
return streamRequestCapture.getValue();
}
Aggregations