Search in sources :

Example 76 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchUpdateResponseBuilder method getMockResourceContext.

private static ResourceContext getMockResourceContext(ProtocolVersion protocolVersion, String altKeyName) {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.getBatchKeyErrors()).andReturn(Collections.<Object, RestLiServiceException>emptyMap()).once();
    EasyMock.expect(mockContext.getRestliProtocolVersion()).andReturn(protocolVersion).once();
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName != null).anyTimes();
    if (altKeyName != null) {
        EasyMock.expect(mockContext.getParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName).atLeastOnce();
    }
    EasyMock.replay(mockContext);
    return mockContext;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext)

Example 77 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method dataProvider.

@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
public Object[][] dataProvider() {
    Map<CompoundKey, Foo> results = new HashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
    CompoundKey c3 = new CompoundKey().append("a", "a3").append("b", 3);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    Foo projectedRecord1 = new Foo().setStringField("record1");
    Foo record2 = new Foo().setStringField("record2").setIntField(7);
    Foo projectedRecord2 = new Foo().setStringField("record2");
    results.put(c1, record1);
    results.put(c2, record2);
    DataMap projectionDataMap = new DataMap();
    projectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree maskTree = new MaskTree(projectionDataMap);
    Map<String, Foo> protocol1TransformedResults = new HashMap<String, Foo>();
    protocol1TransformedResults.put("a=a1&b=1", record1);
    protocol1TransformedResults.put("a=a2&b=2", record2);
    Map<String, Foo> protocol1TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol1TransformedResultsWithProjection.put("a=a1&b=1", projectedRecord1);
    protocol1TransformedResultsWithProjection.put("a=a2&b=2", projectedRecord2);
    Map<String, Foo> protocol2TransformedResults = new HashMap<String, Foo>();
    protocol2TransformedResults.put("(a:a1,b:1)", record1);
    protocol2TransformedResults.put("(a:a2,b:2)", record2);
    Map<String, Foo> protocol2TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol2TransformedResultsWithProjection.put("(a:a1,b:1)", projectedRecord1);
    protocol2TransformedResultsWithProjection.put("(a:a2,b:2)", projectedRecord2);
    Map<String, ErrorResponse> protocol1Errors = Collections.singletonMap("a=a3&b=3", new ErrorResponse().setStatus(404));
    Map<String, ErrorResponse> protocol2Errors = Collections.singletonMap("(a:a3,b:3)", new ErrorResponse().setStatus(404));
    Map<CompoundKey, HttpStatus> statuses = new HashMap<CompoundKey, HttpStatus>();
    statuses.put(c1, HttpStatus.S_200_OK);
    statuses.put(c2, HttpStatus.S_200_OK);
    Map<CompoundKey, RestLiServiceException> exceptions = new HashMap<CompoundKey, RestLiServiceException>();
    exceptions.put(c3, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
    BatchResult<CompoundKey, Foo> batchResult = new BatchResult<CompoundKey, Foo>(results, statuses, exceptions);
    Map<Object, RestLiServiceException> exceptionsWithUntypedKey = new HashMap<Object, RestLiServiceException>(exceptions);
    ProtocolVersion protocolVersion1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    ProtocolVersion protocolVersion2 = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    return new Object[][] { // automatic projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, // manual projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, // manual projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, // automatic projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { results, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto } };
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) HttpStatus(com.linkedin.restli.common.HttpStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchResult(com.linkedin.restli.server.BatchResult) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) DataProvider(org.testng.annotations.DataProvider)

Example 78 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method unsupportedNullKeyMapData.

/* Note that we use java.util.concurrent.ConcurrentHashMap when possible. This is because rest.li checks
  * for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
  * is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
  * java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
  * way that doesn't throw an NullPointerException.
  */
@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "unsupportedNullKeyMapData")
public Object[][] unsupportedNullKeyMapData() {
    Map<CompoundKey, Foo> results = new ConcurrentHashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    results.put(c1, record1);
    Map<CompoundKey, HttpStatus> statuses = new ConcurrentHashMap<CompoundKey, HttpStatus>();
    statuses.put(c1, HttpStatus.S_200_OK);
    final BatchResult<CompoundKey, Foo> batchResult = new BatchResult<CompoundKey, Foo>(results, statuses, new ConcurrentHashMap<CompoundKey, RestLiServiceException>());
    final Map<String, Foo> protocol1TransformedResults = new ConcurrentHashMap<String, Foo>();
    protocol1TransformedResults.put("a=a1&b=1", record1);
    final Map<String, Foo> protocol2TransformedResults = new ConcurrentHashMap<String, Foo>();
    protocol2TransformedResults.put("(a:a1,b:1)", record1);
    ProtocolVersion protocolVersion1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    ProtocolVersion protocolVersion2 = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    return new Object[][] { { results, protocolVersion1, protocol1TransformedResults }, { results, protocolVersion2, protocol2TransformedResults }, { batchResult, protocolVersion1, protocol1TransformedResults }, { batchResult, protocolVersion2, protocol2TransformedResults } };
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) HttpStatus(com.linkedin.restli.common.HttpStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchResult(com.linkedin.restli.server.BatchResult) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DataProvider(org.testng.annotations.DataProvider)

Example 79 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestRestLiResponseEnvelope method testSetNewEnvelopeData.

@Test(dataProvider = "envelopeResourceMethodDataProvider")
public void testSetNewEnvelopeData(RestLiResponseEnvelope responseEnvelope, ResourceMethod resourceMethod) {
    ResponseType responseType = ResponseType.fromMethodType(resourceMethod);
    switch(responseType) {
        case SINGLE_ENTITY:
            RecordResponseEnvelope recordResponseEnvelope = (RecordResponseEnvelope) responseEnvelope;
            RecordTemplate oldRecord = recordResponseEnvelope.getRecord();
            RecordTemplate newRecord = new AnyRecord(new DataMap());
            newRecord.data().put("test", "testing");
            recordResponseEnvelope.setRecord(newRecord, HttpStatus.S_200_OK);
            Assert.assertNotEquals(recordResponseEnvelope.getRecord(), oldRecord);
            break;
        case GET_COLLECTION:
            CollectionResponseEnvelope collectionResponseEnvelope = (CollectionResponseEnvelope) responseEnvelope;
            List<? extends RecordTemplate> oldResponses = collectionResponseEnvelope.getCollectionResponse();
            RecordTemplate oldResponseMetadata = collectionResponseEnvelope.getCollectionResponseCustomMetadata();
            CollectionMetadata oldPagingMetadata = collectionResponseEnvelope.getCollectionResponsePaging();
            RecordTemplate newResponseMetadata = new AnyRecord(new DataMap());
            newResponseMetadata.data().put("test", "testing");
            CollectionMetadata newResponsesPaging = new CollectionMetadata();
            List<? extends RecordTemplate> newResponses = Arrays.asList(new AnyRecord(new DataMap()));
            collectionResponseEnvelope.setCollectionResponse(newResponses, newResponsesPaging, newResponseMetadata, HttpStatus.S_200_OK);
            Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponse(), oldResponses);
            Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), oldResponseMetadata);
            Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponsePaging(), oldPagingMetadata);
            Assert.assertEquals(collectionResponseEnvelope.getCollectionResponse(), newResponses);
            Assert.assertEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), newResponseMetadata);
            Assert.assertEquals(collectionResponseEnvelope.getCollectionResponsePaging(), newResponsesPaging);
            break;
        case CREATE_COLLECTION:
            BatchCreateResponseEnvelope batchCreateResponseEnvelope = (BatchCreateResponseEnvelope) responseEnvelope;
            List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> oldCreateResponses = batchCreateResponseEnvelope.getCreateResponses();
            CreateIdStatus<String> newCreateIdStatus = new CreateIdStatus<String>(new DataMap(), "key");
            RestLiServiceException newException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
            BatchCreateResponseEnvelope.CollectionCreateResponseItem successCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newCreateIdStatus);
            BatchCreateResponseEnvelope.CollectionCreateResponseItem exceptionCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newException, "id2");
            List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> newCreateResponses = Arrays.asList(successCreateItem, exceptionCreateItem);
            batchCreateResponseEnvelope.setCreateResponse(newCreateResponses, HttpStatus.S_200_OK);
            Assert.assertNotEquals(batchCreateResponseEnvelope.getCreateResponses(), oldCreateResponses);
            Assert.assertEquals(batchCreateResponseEnvelope.getCreateResponses(), newCreateResponses);
            BatchCreateResponseEnvelope.CollectionCreateResponseItem firstItem = batchCreateResponseEnvelope.getCreateResponses().get(0);
            Assert.assertNull(firstItem.getId());
            Assert.assertEquals(firstItem.getRecord(), newCreateIdStatus);
            Assert.assertFalse(firstItem.isErrorResponse());
            Assert.assertNull(firstItem.getException());
            BatchCreateResponseEnvelope.CollectionCreateResponseItem secondItem = batchCreateResponseEnvelope.getCreateResponses().get(1);
            Assert.assertEquals(secondItem.getId(), "id2");
            Assert.assertNull(secondItem.getRecord());
            Assert.assertTrue(secondItem.isErrorResponse());
            Assert.assertEquals(secondItem.getException(), newException);
            break;
        case BATCH_ENTITIES:
            BatchResponseEnvelope batchResponseEnvelope = (BatchResponseEnvelope) responseEnvelope;
            Map<?, BatchResponseEnvelope.BatchResponseEntry> oldBatchResponses = batchResponseEnvelope.getBatchResponseMap();
            RecordTemplate newResponseRecord = new EmptyRecord();
            RestLiServiceException newResponseException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
            Map<String, BatchResponseEnvelope.BatchResponseEntry> newBatchResponses = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
            newBatchResponses.put("id1", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, newResponseRecord));
            newBatchResponses.put("id2", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_500_INTERNAL_SERVER_ERROR, newResponseException));
            batchResponseEnvelope.setBatchResponseMap(newBatchResponses, HttpStatus.S_200_OK);
            Map<?, BatchResponseEnvelope.BatchResponseEntry> envelopeMap = batchResponseEnvelope.getBatchResponseMap();
            Assert.assertNotEquals(envelopeMap, oldBatchResponses);
            Assert.assertEquals(envelopeMap, newBatchResponses);
            BatchResponseEnvelope.BatchResponseEntry id1Entry = envelopeMap.get("id1");
            Assert.assertEquals(id1Entry.getStatus(), HttpStatus.S_200_OK);
            Assert.assertEquals(id1Entry.getRecord(), newResponseRecord);
            Assert.assertFalse(id1Entry.hasException());
            Assert.assertNull(id1Entry.getException());
            BatchResponseEnvelope.BatchResponseEntry id2Entry = envelopeMap.get("id2");
            Assert.assertEquals(id2Entry.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
            Assert.assertNull(id2Entry.getRecord());
            Assert.assertTrue(id2Entry.hasException());
            Assert.assertEquals(id2Entry.getException(), newResponseException);
            break;
        case STATUS_ONLY:
            // status only envelopes are blank by default since they have no data fields
            break;
        default:
            throw new IllegalStateException();
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HashMap(java.util.HashMap) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ResponseType(com.linkedin.restli.internal.server.ResponseType) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) Test(org.testng.annotations.Test)

Example 80 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method testBuilderExceptions.

@Test(dataProvider = "exceptionTestData")
public void testBuilderExceptions(Object results, String expectedErrorMessage) {
    // Protocol version doesn't matter here
    ResourceContext mockContext = getMockResourceContext(null, Collections.<Object, RestLiServiceException>emptyMap(), null, null, null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    try {
        responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
        Assert.fail("buildRestLiResponseData should have failed because of null elements!");
    } catch (RestLiServiceException e) {
        Assert.assertTrue(e.getMessage().contains(expectedErrorMessage));
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)93 Test (org.testng.annotations.Test)36 HashMap (java.util.HashMap)31 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)17 RoutingException (com.linkedin.restli.server.RoutingException)17 RestException (com.linkedin.r2.message.rest.RestException)16 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)16 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)16 BeforeTest (org.testng.annotations.BeforeTest)16 DataMap (com.linkedin.data.DataMap)14 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)13 Map (java.util.Map)13 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)12 EmptyRecord (com.linkedin.restli.common.EmptyRecord)12 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)12 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)11 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)11 RecordTemplate (com.linkedin.data.template.RecordTemplate)10