use of com.linkedin.restli.common.ActionResponse in project rest.li by linkedin.
the class TestMockActionResponseFactory method testInference.
@Test
public void testInference() {
final RecordTemplateWithDefaultValue record = new RecordTemplateWithDefaultValue();
record.setId(42L);
record.setMessage("Lorem ipsum");
final ActionResponse<RecordTemplateWithDefaultValue> response = MockActionResponseFactory.create(RecordTemplateWithDefaultValue.class, record);
Assert.assertEquals(response.getValue(), record);
final RecordDataSchema schema = response.schema();
Assert.assertEquals(schema.getName(), ActionResponse.class.getSimpleName());
Assert.assertEquals(schema.getField(ActionResponse.VALUE_NAME).getType(), DataTemplateUtil.getSchema(RecordTemplateWithDefaultValue.class));
}
use of com.linkedin.restli.common.ActionResponse in project rest.li by linkedin.
the class MockActionResponseFactory method create.
/**
* Create an {@link ActionResponse} with specified value class and data.
*
* @param clazz value class of the response
* @param schema schema of the response
* @param value value data of the response
* @param <T> type of the value class
* @return mocked {@link ActionResponse}
*/
public static <T> ActionResponse<T> create(Class<T> clazz, DataSchema schema, T value) {
final FieldDef<T> fieldDef = new FieldDef<>(ActionResponse.VALUE_NAME, clazz, schema);
final RecordDataSchema entitySchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>singletonList(fieldDef));
return new ActionResponse<>(value, fieldDef, entitySchema);
}
use of com.linkedin.restli.common.ActionResponse in project rest.li by linkedin.
the class TestExamplesGenerator method validateActionResponse.
private <T extends RecordTemplate> ValidationResult validateActionResponse(RestResponse response, Class<T> recordClass, ValidationOptions options) throws IOException {
final DataMap respData = _codec.bytesToMap(response.getEntity().copyBytes());
final FieldDef<T> responseFieldDef = new FieldDef<>(ActionResponse.VALUE_NAME, recordClass, DataTemplateUtil.getSchema(recordClass));
final RecordDataSchema recordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>singletonList(responseFieldDef));
final ActionResponse<T> actionResp = new ActionResponse<>(respData, responseFieldDef, recordDataSchema);
final DataSchema recordSchema = DataTemplateUtil.getSchema(recordClass);
return ValidateDataAgainstSchema.validate(actionResp.getValue().data(), recordSchema, options);
}
use of com.linkedin.restli.common.ActionResponse in project rest.li by linkedin.
the class ActionResponseBuilder method buildRestLiResponseData.
/**
* {@inheritDoc}
*
* @param result The result for a Rest.li ACTION method. It can be the return value for the ACTION itself, or the
* return value wrapped in an {@link ActionResult}.
*/
@Override
public RestLiResponseData<ActionResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
final Object value;
final HttpStatus status;
if (result instanceof ActionResult) {
final ActionResult<?> actionResult = (ActionResult<?>) result;
value = actionResult.getValue();
status = actionResult.getStatus();
if (status == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null HttpStatus inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
}
// When it has an ActionResult<Void> type response, it should return null but not empty body record
if (routingResult.getResourceMethod().getActionReturnType() == Void.TYPE) {
return new RestLiResponseDataImpl<>(new ActionResponseEnvelope(status, null), headers, cookies);
}
} else {
// when value == null and return type is void, it is handled outside in RestLiResponseHandler
value = result;
status = HttpStatus.S_200_OK;
}
RecordDataSchema actionReturnRecordDataSchema = routingResult.getResourceMethod().getActionReturnRecordDataSchema();
final Object actionResponseValue;
if (value != null && RecordTemplate.class.isAssignableFrom(value.getClass()) && routingResult.getContext().isFillInDefaultsRequested()) {
RecordTemplate actionResponseRecordTemplate = (RecordTemplate) value;
DataMap dataMap = actionResponseRecordTemplate.data();
dataMap = (DataMap) ResponseUtils.fillInDataDefault(actionResponseRecordTemplate.schema(), dataMap);
Object valueWithDefault = null;
try {
valueWithDefault = (Object) value.getClass().getConstructor(DataMap.class).newInstance(dataMap);
} catch (Exception e) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected error encountered. Can not create return value class " + value.getClass().getSimpleName() + " with default filled inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
}
actionResponseValue = valueWithDefault;
} else {
actionResponseValue = value;
}
@SuppressWarnings("unchecked") FieldDef<Object> actionReturnFieldDef = (FieldDef<Object>) routingResult.getResourceMethod().getActionReturnFieldDef();
final ActionResponse<?> actionResponse = new ActionResponse<>(actionResponseValue, actionReturnFieldDef, actionReturnRecordDataSchema);
return new RestLiResponseDataImpl<>(new ActionResponseEnvelope(status, actionResponse), headers, cookies);
}
use of com.linkedin.restli.common.ActionResponse in project rest.li by linkedin.
the class ActionResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
final Object value;
final HttpStatus status;
if (result instanceof ActionResult) {
final ActionResult<?> actionResult = (ActionResult<?>) result;
value = actionResult.getValue();
status = actionResult.getStatus();
if (status == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null HttpStatus inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
}
} else {
value = result;
status = HttpStatus.S_200_OK;
}
RecordDataSchema actionReturnRecordDataSchema = routingResult.getResourceMethod().getActionReturnRecordDataSchema();
@SuppressWarnings("unchecked") FieldDef<Object> actionReturnFieldDef = (FieldDef<Object>) routingResult.getResourceMethod().getActionReturnFieldDef();
final ActionResponse<?> actionResponse = new ActionResponse<Object>(value, actionReturnFieldDef, actionReturnRecordDataSchema);
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
responseData.setResponseEnvelope(new ActionResponseEnvelope(actionResponse, responseData));
return responseData;
}
Aggregations