use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.
the class MockResponseBuilder method build.
/**
* Builds a {@link Response} that has been constructed using the setters in this class.
*
* @return the constructed {@link Response}
*/
public Response<V> build() {
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
if (_headers != null) {
headers.putAll(_headers);
}
ProtocolVersion protocolVersion = (_protocolVersion == null) ? AllProtocolVersions.BASELINE_PROTOCOL_VERSION : _protocolVersion;
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
int status = (_status == 0) ? DEFAULT_HTTP_STATUS : _status;
if (_entity instanceof CreateResponse || _entity instanceof IdResponse) {
final K id;
if (_entity instanceof CreateResponse) {
@SuppressWarnings("unchecked") final CreateResponse<K> createResponse = (CreateResponse<K>) _entity;
id = createResponse.getId();
} else {
@SuppressWarnings("unchecked") final IdResponse<K> idResponse = (IdResponse<K>) _entity;
id = idResponse.getId();
}
headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForBody(id, false, protocolVersion));
}
List<HttpCookie> cookies = _cookies == null ? Collections.<HttpCookie>emptyList() : _cookies;
final ResponseImpl<V> response = new ResponseImpl<>(status, headers, cookies, _entity, _restLiResponseException);
response.setAttachmentReader(_restLiAttachmentReader);
return response;
}
use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.
the class TestDebugRequestHandlers method createNewGreetingOnTheServer.
private Long createNewGreetingOnTheServer(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Greeting newGreeting = new Greeting().setMessage("New Greeting!").setTone(Tone.FRIENDLY);
RootBuilderWrapper.MethodBuilderWrapper<Long, Greeting, EmptyRecord> createBuilderWrapper = builders.create();
Long createdId;
if (createBuilderWrapper.isRestLi2Builder()) {
Object objBuilder = createBuilderWrapper.getBuilder();
@SuppressWarnings("unchecked") CreateIdRequestBuilder<Long, Greeting> createIdRequestBuilder = (CreateIdRequestBuilder<Long, Greeting>) objBuilder;
CreateIdRequest<Long, Greeting> request = createIdRequestBuilder.input(newGreeting).build();
Response<IdResponse<Long>> response = getClient().sendRequest(request).getResponse();
createdId = response.getEntity().getId();
} else {
Request<EmptyRecord> request = createBuilderWrapper.input(newGreeting).build();
Response<EmptyRecord> response = getClient().sendRequest(request).getResponse();
@SuppressWarnings("unchecked") CreateResponse<Long> createResponse = (CreateResponse<Long>) response.getEntity();
createdId = createResponse.getId();
}
return createdId;
}
use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.
the class TestSimpleResourceHierarchy method testSubCollectionCreate.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testSubCollectionCreate(RestliRequestOptions requestOptions) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("Hello there!");
greeting.setTone(Tone.FRIENDLY);
final SubgreetingsBuilders builders = new SubgreetingsBuilders(requestOptions);
// POST
Request<EmptyRecord> createRequest = builders.create().input(greeting).build();
Response<EmptyRecord> response = getClient().sendRequest(createRequest).getResponse();
Assert.assertNull(response.getHeader(RestConstants.HEADER_CONTENT_TYPE));
@SuppressWarnings("unchecked") CreateResponse<Long> createResponse = (CreateResponse<Long>) response.getEntity();
long id = createResponse.getId();
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(id, Long.parseLong(stringId));
// GET again to verify that the create has worked.
Request<Greeting> getRequest = builders.get().id(id).build();
Response<Greeting> getResponse = getClient().sendRequest(getRequest).getResponse();
Greeting responseGreeting = getResponse.getEntity();
Assert.assertEquals(responseGreeting.getMessage(), greeting.getMessage());
Assert.assertEquals(responseGreeting.getTone(), greeting.getTone());
}
use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.
the class ResponseImpl method getId.
/**
* Specific getter for the 'X-LinkedIn-Id' header
*
* @throws UnsupportedOperationException if the entity returned is a {@link CreateResponse} or {@link IdResponse}
* and the key is a {@link ComplexResourceKey} or {@link CompoundKey}.
*
* @deprecated
* @see com.linkedin.restli.client.Response#getId()
*/
@Override
@Deprecated
public String getId() {
if (_entity instanceof CreateResponse<?> || _entity instanceof IdResponse<?> || _entity instanceof IdEntityResponse<?, ?>) {
final Object id = checkAndReturnId();
final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(_headers);
return URIParamUtils.encodeKeyForHeader(id, protocolVersion);
} else {
return null;
}
}
use of com.linkedin.restli.client.response.CreateResponse in project rest.li by linkedin.
the class TestComplexKeysResource method testCreateMainOldBuilders.
private void testCreateMainOldBuilders(CreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> createRequestBuilder, GetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequestBuilder) throws RemoteInvocationException {
final String messageText = "newMessage";
Message message = new Message();
message.setMessage(messageText);
Request<EmptyRecord> request = createRequestBuilder.input(message).build();
ResponseFuture<EmptyRecord> future = getClient().sendRequest(request);
Response<EmptyRecord> response = future.getResponse();
Assert.assertEquals(response.getStatus(), 201);
ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey = getComplexKey(messageText, messageText);
try {
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.fail("getId() should throw an exception for complex resource keys!");
} catch (UnsupportedOperationException e) {
// expected
}
@SuppressWarnings("unchecked") CreateResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>> createResponse = (CreateResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>>) response.getEntity();
Assert.assertEquals(createResponse.getId(), expectedComplexKey);
// attempt to get the record you just created
@SuppressWarnings("unchecked") Request<Message> getRequest = getRequestBuilder.id(expectedComplexKey).build();
ResponseFuture<Message> getFuture = getClient().sendRequest(getRequest);
Response<Message> getResponse = getFuture.getResponse();
Assert.assertEquals(getResponse.getEntity().getMessage(), messageText);
}
Aggregations