use of com.linkedin.restli.common.IdResponse 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.common.IdResponse in project rest.li by linkedin.
the class TestTyperefKeysResource method testCreateId.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCreateId(RestliRequestOptions requestOptions) throws RemoteInvocationException {
Greeting greeting = new Greeting().setId(1L).setMessage("Foo").setTone(Tone.FRIENDLY);
CreateIdRequest<Long, Greeting> req = new TyperefKeysRequestBuilders(requestOptions).create().input(greeting).build();
Response<IdResponse<Long>> resp = getClient().sendRequest(req).getResponse();
Assert.assertEquals(resp.getEntity().getId(), new Long(1L));
}
use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.
the class TestCreateResponseBuilder method testBuilder.
@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion, Object expectedId, String expectedLocation, String expectedHeaderId, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap) throws URISyntaxException {
CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
CreateResponse createResponse = new CreateResponse(compoundKey);
IdResponse<?> expectedIdResponse = new IdResponse<Object>(expectedId);
RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
// the headers passed in are modified
Map<String, String> expectedHeaders = new HashMap<String, String>(headers);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
RestLiResponseData responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.<HttpCookie>emptyList());
Assert.assertFalse(responseData.getCreateResponseEnvelope().isGetAfterCreate());
PartialRestResponse partialRestResponse = createResponseBuilder.buildResponse(routingResult, responseData);
expectedHeaders.put(RestConstants.HEADER_LOCATION, expectedLocation);
if (protocolVersion.equals(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion())) {
expectedHeaders.put(RestConstants.HEADER_ID, expectedHeaderId);
} else {
expectedHeaders.put(RestConstants.HEADER_RESTLI_ID, expectedHeaderId);
}
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(partialRestResponse, expectedHeaders);
Assert.assertEquals(partialRestResponse.getStatus(), HttpStatus.S_201_CREATED);
Assert.assertEquals(partialRestResponse.getEntity(), expectedIdResponse);
}
use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.
the class CreateResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
CreateResponse createResponse = (CreateResponse) result;
if (createResponse.hasError()) {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
return responseData;
}
Object id = null;
if (createResponse.hasId()) {
id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
uribuilder.path(stringKey);
if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
// add altkey param to location URI
uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
}
headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
}
//Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
if (createResponse.getStatus() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
CreateResponseEnvelope responseEnvelope;
if (createResponse instanceof CreateKVResponse) {
final ResourceContext resourceContext = routingResult.getContext();
DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
} else //Instance of idResponse
{
IdResponse<?> idResponse = new IdResponse<Object>(id);
responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
}
responseData.setResponseEnvelope(responseEnvelope);
return responseData;
}
use of com.linkedin.restli.common.IdResponse in project rest.li by linkedin.
the class TestReturnEntityWithCreate method testCreateId.
/**
* Test for backward compatibility of create id request.
* @param restClient
* @param expectedContentType
* @param builders
* @throws RemoteInvocationException
*/
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "newBuildersClientDataDataProvider")
public void testCreateId(RestClient restClient, String expectedContentType, CreateGreetingRequestBuilders builders) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("first time!");
greeting.setTone(Tone.FRIENDLY);
CreateIdRequest<Long, Greeting> createIdRequest = builders.create().input(greeting).build();
Response<IdResponse<Long>> response = restClient.sendRequest(createIdRequest).getResponse();
Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), expectedContentType);
long id = response.getEntity().getId();
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(id, Long.parseLong(stringId));
}
Aggregations