Search in sources :

Example 11 with ResourceNotFoundException

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
    }
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ApproveTask(io.crnk.activiti.example.model.ApproveTask) Test(org.junit.Test)

Example 12 with ResourceNotFoundException

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);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 13 with ResourceNotFoundException

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);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 14 with ResourceNotFoundException

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());
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Document(io.crnk.core.engine.document.Document) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 15 with ResourceNotFoundException

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);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) Test(org.junit.Test)

Aggregations

ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)18 Test (org.junit.Test)7 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 Document (io.crnk.core.engine.document.Document)4 Serializable (java.io.Serializable)4 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)3 Response (io.crnk.core.engine.dispatcher.Response)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)3 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)3 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)3 ApproveTask (io.crnk.activiti.example.model.ApproveTask)2 ErrorData (io.crnk.core.engine.document.ErrorData)2 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)2 Resource (io.crnk.core.engine.document.Resource)2 MetaLookup (io.crnk.meta.MetaLookup)2 MetaElement (io.crnk.meta.model.MetaElement)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ScheduleApprovalProcessInstance (io.crnk.activiti.example.model.ScheduleApprovalProcessInstance)1 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)1