use of com.linkedin.restli.common.IdResponse 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, String>(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<V>(status, headers, cookies, _entity, _restLiResponseException);
response.setAttachmentReader(_restLiAttachmentReader);
return response;
}
use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.
the class TestGroupsClient method testCollectionCreateGetUpdateDeleteId.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionCreateGetUpdateDeleteId(RestliRequestOptions requestOptions) throws RemoteInvocationException {
// find with optional params
Group group = new Group();
String name = "test";
int memberID = 1;
group.setName(name);
group.setOwner(buildGroupMembership(memberID, "a@a.a", "f", "l"));
GroupMembershipParam param = new GroupMembershipParam();
param.setIntParameter(1);
param.setStringParameter("String");
final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions);
final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions);
// Create
Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse();
Assert.assertEquals(response.getStatus(), 201);
Integer createdId = response.getEntity().getId();
Assert.assertNotNull(createdId);
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(createdId.intValue(), Integer.parseInt(stringId));
// Get newly created group and verify name
Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), name);
// Partial update - change name
String newName = "new name";
group.setName(newName);
PatchRequest<Group> patch = PatchGenerator.diffEmpty(group);
ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
// Get updated group and verify name
Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), newName);
// Delete
responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
// Verify deleted
try {
getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse();
Assert.fail("Expected RestLiResponseException");
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), 404);
}
// Cleanup - delete the owner's membership that was created along with the group
responseFuture = getClient().sendRequest(membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
}
use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.
the class TestGroupsClient method testCollectionCreateGetUpdateDelete.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionCreateGetUpdateDelete(RestliRequestOptions requestOptions) throws RemoteInvocationException {
// find with optional params
Group group = new Group();
String name = "test";
int memberID = 1;
group.setName(name);
group.setOwner(buildGroupMembership(memberID, "a@a.a", "f", "l"));
GroupMembershipParam param = new GroupMembershipParam();
param.setIntParameter(1);
param.setStringParameter("String");
final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions);
final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions);
// Create
Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse();
Assert.assertEquals(response.getStatus(), 201);
@SuppressWarnings("unchecked") IdResponse<Integer> createResponse = response.getEntity();
Assert.assertNotNull(createResponse.getId());
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(createResponse.getId().intValue(), Integer.parseInt(stringId));
// Get newly created group and verify name
Integer createdId = createResponse.getId();
Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createResponse.getId()).build()).getResponse().getEntity().getName(), name);
// Partial update - change name
String newName = "new name";
group.setName(newName);
PatchRequest<Group> patch = PatchGenerator.diffEmpty(group);
ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
// Get updated group and verify name
Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), newName);
// Delete
responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
// Verify deleted
try {
getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse();
Assert.fail("Expected RestLiResponseException");
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), 404);
}
// Cleanup - delete the owner's membership that was created along with the group
responseFuture = getClient().sendRequest(membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build());
Assert.assertEquals(204, responseFuture.getResponse().getStatus());
}
use of com.linkedin.restli.common.IdResponse 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 {@link 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.common.IdResponse in project rest.li by linkedin.
the class TestGreetingClientContentTypes method testCreateId.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "buildersClientDataDataProvider")
public void testCreateId(RestClient restClient, RestliRequestOptions requestOptions) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("Hello there!");
greeting.setTone(Tone.FRIENDLY);
final GreetingsRequestBuilders builders = new GreetingsRequestBuilders(requestOptions);
CreateIdRequest<Long, Greeting> createRequest = builders.create().input(greeting).build();
Response<IdResponse<Long>> response = restClient.sendRequest(createRequest).getResponse();
Assert.assertNull(response.getHeader(RestConstants.HEADER_CONTENT_TYPE));
@SuppressWarnings("unchecked") long id = response.getEntity().getId();
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(id, Long.parseLong(stringId));
Request<Greeting> getRequest = builders.get().id(id).build();
Response<Greeting> getResponse = restClient.sendRequest(getRequest).getResponse();
Greeting responseGreeting = getResponse.getEntity();
Assert.assertEquals(responseGreeting.getMessage(), greeting.getMessage());
Assert.assertEquals(responseGreeting.getTone(), greeting.getTone());
}
Aggregations