Search in sources :

Example 41 with RestLiServiceException

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

the class TestErrorResponseValidationFilter method testErrorResponseValidation.

/**
 * Ensures that the validation filter correctly validates outgoing error response
 * to have predefined http status code, service error code and error details.
 */
@Test(dataProvider = "errorResponseValidationDataProvider")
public void testErrorResponseValidation(List<ServiceError> resourceServiceErrors, List<ServiceError> methodServiceErrors, HttpStatus httpStatus, String serviceErrorCode, RecordTemplate errorDetails, HttpStatus expectedHttpStatus, String expectedErrorCode, RecordTemplate expectedErrorDetails) {
    try {
        when(filterRequestContext.getFilterResourceModel()).thenReturn(resourceModel);
        when(resourceModel.getServiceErrors()).thenReturn(resourceServiceErrors);
        when(filterRequestContext.getMethodServiceErrors()).thenReturn(methodServiceErrors);
        ErrorResponseValidationFilter validationFilter = new ErrorResponseValidationFilter();
        RestLiServiceException restLiServiceException = new RestLiServiceException(httpStatus);
        restLiServiceException.setCode(serviceErrorCode);
        restLiServiceException.setErrorDetails(errorDetails);
        CompletableFuture<Void> future = validationFilter.onError(restLiServiceException, filterRequestContext, filterResponseContext);
        Assert.assertTrue(future.isCompletedExceptionally());
        future.get();
    } catch (Exception exception) {
        if (exception.getCause() != null && exception.getCause() instanceof RestLiServiceException) {
            RestLiServiceException restLiServiceException = (RestLiServiceException) exception.getCause();
            Assert.assertEquals(restLiServiceException.getStatus(), expectedHttpStatus);
            Assert.assertEquals(restLiServiceException.getCode(), expectedErrorCode);
            Assert.assertEquals(restLiServiceException.getErrorDetailsRecord(), expectedErrorDetails);
        } else {
            Assert.fail("Expected to get only RestLiServiceException.");
        }
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Test(org.testng.annotations.Test)

Example 42 with RestLiServiceException

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

the class PhotoResource method searchPhotos.

@BatchFinder(value = "searchPhotos", batchParam = "criteria")
public BatchFinderResult<PhotoCriteria, Photo, NoMetadata> searchPhotos(@PagingContextParam PagingContext pagingContext, @QueryParam("criteria") PhotoCriteria[] criteria, @QueryParam("exif") @Optional EXIF exif) {
    BatchFinderResult<PhotoCriteria, Photo, NoMetadata> batchFinderResult = new BatchFinderResult<>();
    for (PhotoCriteria currentCriteria : criteria) {
        if (currentCriteria.getTitle() != null) {
            // on success
            final List<Photo> photos = new ArrayList<>();
            int index = 0;
            final int begin = pagingContext.getStart();
            final int end = begin + pagingContext.getCount();
            final Collection<Photo> dbPhotos = _db.getData().values();
            for (Photo p : dbPhotos) {
                if (index == end) {
                    break;
                } else if (index >= begin) {
                    if (p.getTitle().equalsIgnoreCase(currentCriteria.getTitle())) {
                        if (currentCriteria.getFormat() == null || currentCriteria.getFormat() == p.getFormat()) {
                            photos.add(p);
                        }
                    }
                }
                index++;
            }
            CollectionResult<Photo, NoMetadata> cr = new CollectionResult<>(photos, photos.size());
            batchFinderResult.putResult(currentCriteria, cr);
        } else {
            // on error: to construct error response for test
            batchFinderResult.putError(currentCriteria, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Failed to find Photo!"));
        }
    }
    return batchFinderResult;
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) NoMetadata(com.linkedin.restli.server.NoMetadata) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ArrayList(java.util.ArrayList) Photo(com.linkedin.restli.example.Photo) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) PhotoCriteria(com.linkedin.restli.example.PhotoCriteria) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder)

Example 43 with RestLiServiceException

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

the class AlbumEntryResource method update.

/**
 * Add the specified photo to the specified album.
 * If a matching pair of IDs already exists, this changes the add date.
 */
@Override
@SuccessResponse(statuses = { HttpStatus.S_204_NO_CONTENT })
@ServiceErrors(INVALID_PERMISSIONS)
@ParamError(code = INVALID_ID, parameterNames = { "albumEntryId" })
public UpdateResponse update(CompoundKey key, AlbumEntry entity) {
    long photoId = (Long) key.getPart("photoId");
    long albumId = (Long) key.getPart("albumId");
    // make sure photo and album exist
    if (!_photoDb.getData().containsKey(photoId))
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent photo ID: " + photoId);
    if (!_albumDb.getData().containsKey(albumId))
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent album ID: " + albumId);
    // disallow changing entity ID
    if (entity.hasAlbumId() || entity.hasPhotoId())
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo/album ID are not acceptable in request");
    // make sure the ID in the entity is consistent with the key in the database
    entity.setPhotoId(photoId);
    entity.setAlbumId(albumId);
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) SuccessResponse(com.linkedin.restli.server.annotations.SuccessResponse) ServiceErrors(com.linkedin.restli.server.annotations.ServiceErrors) ParamError(com.linkedin.restli.server.annotations.ParamError)

Example 44 with RestLiServiceException

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

the class GroupMembershipsResource2 method batchGet.

/**
 * @see GroupMembershipsResource2#batchGet(Set)
 */
@Override
public BatchResult<CompoundKey, GroupMembership> batchGet(Set<CompoundKey> ids) {
    Map<CompoundKey, GroupMembership> result = new HashMap<>(ids.size());
    Map<CompoundKey, RestLiServiceException> errors = new HashMap<>();
    Iterator<CompoundKey> iterator = ids.iterator();
    while (iterator.hasNext()) {
        CompoundKey key = iterator.next();
        GroupMembership membership = _app.getMembershipMgr().get(key);
        if (membership != null) {
            result.put(key, membership);
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<>(result, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) BatchResult(com.linkedin.restli.server.BatchResult)

Example 45 with RestLiServiceException

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

the class GroupMembershipsResource3 method create.

/**
 * @see com.linkedin.restli.server.resources.ComplexKeyResourceTemplate#create(com.linkedin.data.template.RecordTemplate)
 */
@Override
public CreateResponse create(ComplexKeyGroupMembership groupMembership) {
    // object
    if (!groupMembership.getId().hasMemberID() || !groupMembership.getId().hasGroupID()) {
        throw new RestLiServiceException(S_400_BAD_REQUEST, "groupID and memberID fields must be set while creating a ComplexKeyGroupMembership.");
    }
    GroupMembershipKey groupMembershipKey = new GroupMembershipKey();
    groupMembershipKey.setMemberID(groupMembership.getId().getMemberID());
    groupMembershipKey.setGroupID(groupMembership.getId().getGroupID());
    ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexResourceKey = new ComplexResourceKey<>(groupMembershipKey, new GroupMembershipParam());
    groupMembership.setId(complexResourceKey.getKey());
    _app.getMembershipMgr().save(toGroupMembership(groupMembership));
    return new CreateResponse(complexResourceKey, HttpStatus.S_201_CREATED);
}
Also used : GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) CreateResponse(com.linkedin.restli.server.CreateResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)145 Test (org.testng.annotations.Test)55 HashMap (java.util.HashMap)39 DataMap (com.linkedin.data.DataMap)29 RecordTemplate (com.linkedin.data.template.RecordTemplate)21 Map (java.util.Map)21 RoutingException (com.linkedin.restli.server.RoutingException)20 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)18 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)17 BeforeTest (org.testng.annotations.BeforeTest)17 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)14 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)13 RestRequest (com.linkedin.r2.message.rest.RestRequest)12 ByteString (com.linkedin.data.ByteString)11 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 BatchResult (com.linkedin.restli.server.BatchResult)11