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.");
}
}
}
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;
}
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);
}
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);
}
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);
}
Aggregations