Search in sources :

Example 1 with ActionResponse

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));
}
Also used : RecordTemplateWithDefaultValue(com.linkedin.restli.test.RecordTemplateWithDefaultValue) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ActionResponse(com.linkedin.restli.common.ActionResponse) Test(org.testng.annotations.Test)

Example 2 with ActionResponse

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);
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ActionResponse(com.linkedin.restli.common.ActionResponse)

Example 3 with ActionResponse

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);
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) FieldDef(com.linkedin.data.template.FieldDef) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ActionResponse(com.linkedin.restli.common.ActionResponse) DataMap(com.linkedin.data.DataMap)

Example 4 with ActionResponse

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);
}
Also used : HttpStatus(com.linkedin.restli.common.HttpStatus) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ActionResponse(com.linkedin.restli.common.ActionResponse) DataMap(com.linkedin.data.DataMap) FieldDef(com.linkedin.data.template.FieldDef) ActionResult(com.linkedin.restli.server.ActionResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 5 with ActionResponse

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;
}
Also used : HttpStatus(com.linkedin.restli.common.HttpStatus) ActionResponse(com.linkedin.restli.common.ActionResponse) FieldDef(com.linkedin.data.template.FieldDef) ActionResult(com.linkedin.restli.server.ActionResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Aggregations

ActionResponse (com.linkedin.restli.common.ActionResponse)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 FieldDef (com.linkedin.data.template.FieldDef)4 DataMap (com.linkedin.data.DataMap)3 HttpStatus (com.linkedin.restli.common.HttpStatus)2 ActionResult (com.linkedin.restli.server.ActionResult)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 RecordTemplateWithDefaultValue (com.linkedin.restli.test.RecordTemplateWithDefaultValue)2 Test (org.testng.annotations.Test)2 DataSchema (com.linkedin.data.schema.DataSchema)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 CollectionResponse (com.linkedin.restli.common.CollectionResponse)1