use of io.crnk.core.exception.ResourceNotFoundException in project crnk-framework by crnk-project.
the class TaskResourceRepositoryTest method completeTask.
@Test
public void completeTask() {
QuerySpec querySpec = new QuerySpec(ApproveTask.class);
ApproveTask resource = taskRepository.findOne(task.getId(), querySpec);
Assert.assertFalse(resource.isCompleted());
resource.setCompleted(true);
ApproveTask updatedResource = taskRepository.save(resource);
Assert.assertTrue(updatedResource.isCompleted());
try {
taskRepository.findOne(task.getId(), querySpec);
Assert.fail();
} catch (ResourceNotFoundException e) {
// ok
}
}
use of io.crnk.core.exception.ResourceNotFoundException in project crnk-framework by crnk-project.
the class CollectionGet method handle.
@Override
@SuppressWarnings("unchecked")
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getElementName();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
if (registryEntry == null) {
throw new ResourceNotFoundException(resourceName);
}
Document responseDocument;
ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
JsonApiResponse entities;
if (jsonPath.getIds() == null || jsonPath.getIds().getIds().isEmpty()) {
entities = resourceRepository.findAll(queryAdapter);
} else {
Class<? extends Serializable> idType = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
Iterable<? extends Serializable> parsedIds = typeParser.parse((Iterable<String>) jsonPath.getIds().getIds(), idType);
entities = resourceRepository.findAll(parsedIds, queryAdapter);
}
responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
return new Response(responseDocument, 200);
}
use of io.crnk.core.exception.ResourceNotFoundException in project crnk-framework by crnk-project.
the class ResourceDelete method handle.
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getElementName();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
if (registryEntry == null) {
// TODO: Add JsonPath toString and provide to exception?
throw new ResourceNotFoundException(resourceName);
}
for (String id : resourceIds.getIds()) {
Serializable castedId = registryEntry.getResourceInformation().parseIdString(id);
// noinspection unchecked
registryEntry.getResourceRepository(parameterProvider).delete(castedId, queryAdapter);
}
return new Response(null, 204);
}
use of io.crnk.core.exception.ResourceNotFoundException in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithErrorsButInvalidContentType.
@Test
public void checkBodyWithErrorsButInvalidContentType() throws IOException {
Document document = new Document();
ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
document.setErrors(Arrays.asList(errorData));
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn("not json api");
Mockito.when(response.code()).thenReturn(404);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ResourceNotFoundException);
Assert.assertNull(exception.getMessage());
}
use of io.crnk.core.exception.ResourceNotFoundException in project crnk-framework by crnk-project.
the class ClientStubBaseTest method check404AndNoBodyGivesNotFoundException.
@Test
public void check404AndNoBodyGivesNotFoundException() throws IOException {
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn("");
Mockito.when(response.code()).thenReturn(404);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ResourceNotFoundException);
}
Aggregations