Search in sources :

Example 1 with MovieEntity

use of io.crnk.operations.model.MovieEntity in project crnk-framework by crnk-project.

the class AbstractOperationsTest method newMovie.

protected MovieEntity newMovie(String title) {
    MovieEntity movie = new MovieEntity();
    movie.setId(UUID.randomUUID());
    movie.setImdbId(title);
    movie.setTitle(title);
    return movie;
}
Also used : MovieEntity(io.crnk.operations.model.MovieEntity)

Example 2 with MovieEntity

use of io.crnk.operations.model.MovieEntity in project crnk-framework by crnk-project.

the class OperationsExceptionTest method testUnknownErrorTriggerInternalError.

@Test
public void testUnknownErrorTriggerInternalError() {
    operationsModule.addFilter(new OperationFilter() {

        @Override
        public List<OperationResponse> filter(OperationFilterContext context, OperationFilterChain chain) {
            throw new IllegalStateException("test");
        }
    });
    MovieEntity movie1 = newMovie("1");
    MovieEntity movie2 = newMovie("2");
    OperationsCall insertCall = operationsClient.createCall();
    insertCall.add(HttpMethod.POST, movie1);
    insertCall.add(HttpMethod.POST, movie2);
    try {
        insertCall.execute();
        Assert.fail();
    } catch (InternalServerErrorException e) {
    // ok
    }
}
Also used : MovieEntity(io.crnk.operations.model.MovieEntity) OperationFilterContext(io.crnk.operations.server.OperationFilterContext) OperationFilter(io.crnk.operations.server.OperationFilter) OperationFilterChain(io.crnk.operations.server.OperationFilterChain) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) List(java.util.List) OperationsCall(io.crnk.operations.client.OperationsCall) Test(org.junit.Test)

Example 3 with MovieEntity

use of io.crnk.operations.model.MovieEntity in project crnk-framework by crnk-project.

the class OperationsSingleEntityTest method testSingleEntityCrud.

@Test
public void testSingleEntityCrud() {
    MovieEntity movie = newMovie("test");
    // post
    OperationsCall call = operationsClient.createCall();
    call.add(HttpMethod.POST, movie);
    call.execute();
    // read
    ResourceList<MovieEntity> movies = movieRepo.findAll(new QuerySpec(MovieEntity.class));
    Assert.assertEquals(1, movies.size());
    movie = movies.get(0);
    // update
    movie.setTitle("NewTitle");
    call = operationsClient.createCall();
    call.add(HttpMethod.PATCH, movie);
    call.execute();
    movie = call.getResponseObject(0, MovieEntity.class);
    Assert.assertEquals("NewTitle", movie.getTitle());
    // delete
    call = operationsClient.createCall();
    call.add(HttpMethod.DELETE, movie);
    call.execute();
    movies = movieRepo.findAll(new QuerySpec(MovieEntity.class));
    Assert.assertEquals(0, movies.size());
}
Also used : MovieEntity(io.crnk.operations.model.MovieEntity) OperationsCall(io.crnk.operations.client.OperationsCall) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 4 with MovieEntity

use of io.crnk.operations.model.MovieEntity in project crnk-framework by crnk-project.

the class OperationsPostTest method testMultiplePost.

@Test
public void testMultiplePost() {
    ResourceRepositoryV2<PersonEntity, UUID> personRepo = client.getRepositoryForType(PersonEntity.class);
    PersonEntity person1 = newPerson("1");
    PersonEntity person2 = newPerson("2");
    MovieEntity movie = newMovie("test");
    movie.setDirectors(new HashSet<>(Arrays.asList(person1, person2)));
    // tag::client[]
    OperationsClient operationsClient = new OperationsClient(client);
    OperationsCall call = operationsClient.createCall();
    call.add(HttpMethod.POST, movie);
    call.add(HttpMethod.POST, person1);
    call.add(HttpMethod.POST, person2);
    call.execute();
    // end::client[]
    QuerySpec querySpec = new QuerySpec(PersonEntity.class);
    ResourceList<PersonEntity> persons = personRepo.findAll(querySpec);
    Assert.assertEquals(2, persons.size());
    querySpec = new QuerySpec(MovieEntity.class);
    querySpec.includeRelation(Arrays.asList("directors"));
    ResourceList<MovieEntity> movies = movieRepo.findAll(querySpec);
    Assert.assertEquals(1, movies.size());
    movie = movies.get(0);
    Assert.assertEquals(2, movie.getDirectors().size());
}
Also used : MovieEntity(io.crnk.operations.model.MovieEntity) OperationsClient(io.crnk.operations.client.OperationsClient) OperationsCall(io.crnk.operations.client.OperationsCall) PersonEntity(io.crnk.operations.model.PersonEntity) UUID(java.util.UUID) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 5 with MovieEntity

use of io.crnk.operations.model.MovieEntity in project crnk-framework by crnk-project.

the class OperationsDeleteRelationTest method testDeleteRelation.

@Test
public void testDeleteRelation() {
    PersonEntity person1 = newPerson("1");
    PersonEntity person2 = newPerson("2");
    MovieEntity movie = newMovie("test");
    movie.setDirectors(new HashSet<>(Arrays.asList(person1, person2)));
    OperationsCall insertCall = operationsClient.createCall();
    insertCall.add(HttpMethod.POST, movie);
    insertCall.add(HttpMethod.POST, person1);
    insertCall.add(HttpMethod.POST, person2);
    insertCall.execute();
    QuerySpec querySpec = new QuerySpec(MovieEntity.class);
    querySpec.includeRelation(Arrays.asList("directors"));
    MovieEntity updatedMovie = movieRepo.findOne(movie.getId(), querySpec);
    Set<PersonEntity> directors = updatedMovie.getDirectors();
    PersonEntity deletedDirector = directors.iterator().next();
    directors.remove(deletedDirector);
    OperationsCall call = operationsClient.createCall();
    call.add(HttpMethod.PATCH, updatedMovie);
    call.add(HttpMethod.DELETE, deletedDirector);
    call.execute();
    // check whether updated relationship is included in the response
    Resource movieResource = call.getResponse(0).getSingleData().get();
    Relationship directorsRelationship = movieResource.getRelationships().get("directors");
    List<ResourceIdentifier> directorIds = directorsRelationship.getCollectionData().get();
    Assert.assertEquals(1, directorIds.size());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) MovieEntity(io.crnk.operations.model.MovieEntity) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) OperationsCall(io.crnk.operations.client.OperationsCall) PersonEntity(io.crnk.operations.model.PersonEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Aggregations

MovieEntity (io.crnk.operations.model.MovieEntity)5 OperationsCall (io.crnk.operations.client.OperationsCall)4 Test (org.junit.Test)4 QuerySpec (io.crnk.core.queryspec.QuerySpec)3 PersonEntity (io.crnk.operations.model.PersonEntity)2 Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 OperationsClient (io.crnk.operations.client.OperationsClient)1 OperationFilter (io.crnk.operations.server.OperationFilter)1 OperationFilterChain (io.crnk.operations.server.OperationFilterChain)1 OperationFilterContext (io.crnk.operations.server.OperationFilterContext)1 List (java.util.List)1 UUID (java.util.UUID)1