Search in sources :

Example 21 with EmptyRecord

use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.

the class TestRestLiCallback method testOnSuccessWithExceptionBuildingResponseNoFilters.

@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithExceptionBuildingResponseNoFilters() throws Exception {
    String result = "foo";
    RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
    RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
    RestLiServiceException ex = new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY);
    Map<String, String> inputHeaders = Maps.newHashMap();
    inputHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(ex, new HashMap<String, String>(), Collections.<HttpCookie>emptyList());
    responseData.setResponseEnvelope(new GetResponseEnvelope(new EmptyRecord(), responseData));
    PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
    RestException restException = new RestException(new RestResponseBuilder().build());
    // Set up.
    // Throw an exception when we try to build the response data.
    when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
    when(_responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenThrow(ex);
    when(_restRequest.getHeaders()).thenReturn(inputHeaders);
    when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), eq(ex), anyMap(), anyList())).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    when(_responseHandler.buildRestException(ex, partialResponse)).thenReturn(restException);
    // invoke request filters so cursor is in correct place
    when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
    _twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
    // Invoke.
    _noFilterRestLiCallback.onSuccess(result, executionReport, responseAttachments);
    // Verify.
    verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
    verify(_responseHandler).buildRestException(ex, partialResponse);
    verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), eq(ex), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
    verify(_restRequest).getHeaders();
    verifyZeroInteractions(_routingResult);
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 22 with EmptyRecord

use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.

the class TestRestLiFilterResponseContextFactory method testFromThrowable.

@SuppressWarnings("unchecked")
@Test(dataProvider = "provideExceptionsAndStatuses")
public void testFromThrowable(Exception e, HttpStatus status) {
    RestLiServiceException serviceException;
    if (e instanceof RestLiServiceException) {
        serviceException = (RestLiServiceException) e;
    } else {
        serviceException = new RestLiServiceException(status, e);
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(serviceException, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    responseData.setResponseEnvelope(new GetResponseEnvelope(new EmptyRecord(), responseData));
    ArgumentCaptor<RestLiServiceException> exceptionArgumentCaptor = ArgumentCaptor.forClass(RestLiServiceException.class);
    // Setup.
    when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exceptionArgumentCaptor.capture(), anyMap(), anyList())).thenReturn(responseData);
    when(_restRequest.getHeaders()).thenReturn(null);
    // Invoke.
    FilterResponseContext responseContext = _filterResponseContextFactory.fromThrowable(e);
    // Verify.
    verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exceptionArgumentCaptor.capture(), anyMap(), anyList());
    verify(_restRequest).getHeaders();
    // RestLiCallback should pass the original exception to the response handler.
    RestLiServiceException exceptionArgument = exceptionArgumentCaptor.getValue();
    assertTrue(exceptionArgument.equals(e) || exceptionArgument.getCause().equals(e));
    assertEquals(exceptionArgument.getStatus(), status);
    // The end result should also contain the original exception.
    assertTrue(responseContext.getResponseData().isErrorResponse());
    assertTrue(responseContext.getResponseData().getServiceException().equals(e) || responseContext.getResponseData().getServiceException().getCause().equals(e));
    assertEquals(responseContext.getResponseData().getServiceException().getStatus(), status);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 23 with EmptyRecord

use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method testContextErrors.

@Test
public void testContextErrors() {
    BatchGetResponseBuilder builder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    ServerResourceContext context = EasyMock.createMock(ServerResourceContext.class);
    Map<Object, RestLiServiceException> errors = new HashMap<Object, RestLiServiceException>();
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_402_PAYMENT_REQUIRED);
    errors.put("foo", exception);
    EasyMock.expect(context.hasParameter("altkey")).andReturn(false);
    EasyMock.expect(context.getBatchKeyErrors()).andReturn(errors);
    EasyMock.replay(context);
    RoutingResult routingResult = new RoutingResult(context, null);
    RestLiResponseData responseData = builder.buildRestLiResponseData(null, routingResult, new BatchResult<Object, EmptyRecord>(Collections.<Object, EmptyRecord>emptyMap(), Collections.<Object, RestLiServiceException>emptyMap()), Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    Assert.assertEquals(responseData.getBatchResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
    Assert.assertEquals(responseData.getBatchResponseEnvelope().getBatchResponseMap().size(), 1);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

Example 24 with EmptyRecord

use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.

the class TestBatchPatchArgumentBuilder method failureData.

@DataProvider
private Object[][] failureData() {
    Object[] compoundKeys = new Object[] { new CompoundKey().append("string1", "apples").append("string2", "oranges"), new CompoundKey().append("string1", "XYZ").append("string2", "tea") };
    Object[] complexResourceKeys = new Object[] { new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("XYZ").setB(111L), new EmptyRecord()), new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("A2").setB(222L), new EmptyRecord()) };
    return new Object[][] { { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"10002\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", new Object[] { 10001, 99999 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"99999\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", new Object[] { 10001, 10002 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"10002\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", new Object[] { 10001, 10002, 10003 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"string1=apples&string2=oranges\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"string1=coffee&string2=tea\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"string1=apples&string2=oranges\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"string2=oranges&string1=apples\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"(string1:apples,string2:oranges)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"(string1:coffee,string2:tea)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"(string1:apples,string2:oranges)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"(string2:oranges,string1:apples)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=A1&b=111\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"a=A2&b=222\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", complexResourceKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=A2&b=222\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"b=222&a=A2\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", complexResourceKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"($params:(),a:A2,b:222)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}," + "\"($params:(),a:A1,b:111)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}}}", complexResourceKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"($params:(),a:A2,b:222)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}," + "\"($params:(),b:222,a:A2)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}}}", complexResourceKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) CompoundKey(com.linkedin.restli.common.CompoundKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) DataProvider(org.testng.annotations.DataProvider)

Example 25 with EmptyRecord

use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.

the class TestBatchPatchArgumentBuilder method argumentData.

@DataProvider(name = "argumentData")
private Object[][] argumentData() {
    Map<String, Object> aMap1 = new HashMap<String, Object>();
    aMap1.put("a", "someString");
    Map<String, Object> setMap1 = new HashMap<String, Object>();
    setMap1.put("$set", new DataMap(aMap1));
    Map<String, Object> patchMap1 = new HashMap<String, Object>();
    patchMap1.put("patch", new DataMap(setMap1));
    PatchRequest<MyComplexKey> patch1 = new PatchRequest<MyComplexKey>(new DataMap(patchMap1));
    Map<String, Object> aMap2 = new HashMap<String, Object>();
    aMap2.put("a", "someOtherString");
    Map<String, Object> setMap2 = new HashMap<String, Object>();
    setMap2.put("$set", new DataMap(aMap2));
    Map<String, Object> data2 = new HashMap<String, Object>();
    data2.put("patch", new DataMap(setMap2));
    PatchRequest<MyComplexKey> patch2 = new PatchRequest<MyComplexKey>(new DataMap(data2));
    @SuppressWarnings("rawtypes") PatchRequest[] patches = new PatchRequest[] { patch1, patch2 };
    Object[] simpleKeys = new Object[] { "simple", "(s:pe%cial)" };
    Object[] compoundKeys = new Object[] { new CompoundKey().append("string1", "apples").append("string2", "oranges"), new CompoundKey().append("string1", "simple").append("string2", "(s:pe%cial)") };
    Object[] complexResourceKeys = new Object[] { new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("simple").setB(111L), new EmptyRecord()), new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("(s:pe%cial)").setB(222L), new EmptyRecord()) };
    return new Object[][] { { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("stringKey", String.class, new StringDataSchema()), null, "{\"entities\":{\"simple\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"(s:pe%cial)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", simpleKeys, patches }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("stringKey", String.class, new StringDataSchema()), null, "{\"entities\":{\"simple\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"(s:pe%cial)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", simpleKeys, patches }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"string1=apples&string2=oranges\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"string1=simple&string2=(s:pe%25cial)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, patches }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"(string1:apples,string2:oranges)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"(string1:simple,string2:%28s%3Ape%25cial%29)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", compoundKeys, patches }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=simple&b=111\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}," + "\"a=(s:pe%25cial)&b=222\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}}}", complexResourceKeys, patches }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"($params:(),a:%28s%3Ape%25cial%29,b:222)\":{\"patch\":{\"$set\":{\"a\":\"someOtherString\"}}}," + "\"($params:(),a:simple,b:111)\":{\"patch\":{\"$set\":{\"a\":\"someString\"}}}}}", complexResourceKeys, patches } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) DataMap(com.linkedin.data.DataMap) StringDataSchema(com.linkedin.data.schema.StringDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) DataProvider(org.testng.annotations.DataProvider)

Aggregations

EmptyRecord (com.linkedin.restli.common.EmptyRecord)75 Test (org.testng.annotations.Test)62 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)33 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)15 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)12 RestException (com.linkedin.r2.message.rest.RestException)11 BeforeTest (org.testng.annotations.BeforeTest)10 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)9 CompoundKey (com.linkedin.restli.common.CompoundKey)9 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)9 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)9 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)9 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)8 CreateResponse (com.linkedin.restli.client.response.CreateResponse)7 HashMap (java.util.HashMap)7 RecordTemplate (com.linkedin.data.template.RecordTemplate)6 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)6 Key (com.linkedin.restli.server.Key)6 RoutingException (com.linkedin.restli.server.RoutingException)6 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)6