From 153e1d22a4fc5774a0f6ec98863fc9384217796f Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Wed, 18 Sep 2024 20:49:15 +0100 Subject: [PATCH] feat: update structure Signed-off-by: Otavio Santana --- .../demoee/AuthorWithBooksRepository.java | 8 +- .../demoee/BookWithAuthorRepository.java | 7 +- .../java/org/jnosql/demoee/Bookstore.java | 15 +-- .../org/jnosql/demoee/PageableResource.java | 12 +- .../org/jnosql/demoee/AuthorResourceTest.java | 89 -------------- .../org/jnosql/demoee/BookResourceTest.java | 113 ------------------ .../java/org/jnosql/demoee/DogRepository.java | 5 +- .../java/org/jnosql/demoee/DogResource.java | 9 +- .../java/org/jnosql/demoee/FishService.java | 3 +- .../src/main/java/org/soujava/Animal.java | 4 +- .../java/org/soujava/AnimalRepository.java | 6 +- .../java/org/soujava/ObjectIdConverter.java | 4 +- 12 files changed, 35 insertions(+), 240 deletions(-) delete mode 100644 quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/AuthorResourceTest.java delete mode 100644 quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/BookResourceTest.java diff --git a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/AuthorWithBooksRepository.java b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/AuthorWithBooksRepository.java index a213187..742905b 100644 --- a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/AuthorWithBooksRepository.java +++ b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/AuthorWithBooksRepository.java @@ -1,12 +1,12 @@ package org.jnosql.demoee; -import jakarta.data.page.Pageable; -import jakarta.data.repository.PageableRepository; +import jakarta.data.page.PageRequest; +import jakarta.data.repository.BasicRepository; import jakarta.data.repository.Repository; import java.util.stream.Stream; @Repository -public interface AuthorWithBooksRepository extends PageableRepository { - Stream findByName(String name, Pageable pageable); +public interface AuthorWithBooksRepository extends BasicRepository { + Stream findByName(String name, PageRequest pageable); } diff --git a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/BookWithAuthorRepository.java b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/BookWithAuthorRepository.java index f0473b6..42e3af7 100644 --- a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/BookWithAuthorRepository.java +++ b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/BookWithAuthorRepository.java @@ -1,15 +1,14 @@ package org.jnosql.demoee; -import jakarta.data.page.Pageable; +import jakarta.data.page.PageRequest; import jakarta.data.repository.*; -import jakarta.ws.rs.QueryParam; import java.util.stream.Stream; @Repository -public interface BookWithAuthorRepository extends PageableRepository { +public interface BookWithAuthorRepository extends BasicRepository { @Query("select * from BookWithAuthor where author._id = @authorId") Stream listBooksByAuthorId(@Param("authorId") String id); - Stream findByTitleLike(String title, Pageable pageable); + Stream findByTitleLike(String title, PageRequest pageable); } diff --git a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/Bookstore.java b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/Bookstore.java index 6e0c5bb..db71016 100644 --- a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/Bookstore.java +++ b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/Bookstore.java @@ -1,6 +1,7 @@ package org.jnosql.demoee; -import jakarta.data.page.Pageable; +import jakarta.data.Order; +import jakarta.data.page.PageRequest; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -36,12 +37,12 @@ public AuthorWithBooks save(AuthorWithBooks author) { return author; } - public Stream findAuthorsByName(String name, Pageable pageable) { + public Stream findAuthorsByName(String name, PageRequest pageable) { return authorWithBooksRepository.findByName(name, pageable); } - public Stream listAuthors(Pageable pageable) { - return authorWithBooksRepository.findAll(pageable).stream(); + public Stream listAuthors(PageRequest pageable) { + return authorWithBooksRepository.findAll(pageable, Order.by()).stream(); } public Optional findAuthorById(String authorId) { @@ -58,11 +59,11 @@ public BookWithAuthor save(BookWithAuthor bookWithAuthor) { return bookWithAuthor; } - public Stream findBooksByTitle(String title, Pageable pageable) { + public Stream findBooksByTitle(String title, PageRequest pageable) { return bookWithAuthorRepository.findByTitleLike(title, pageable); } - public Stream listBooks(Pageable pageable) { - return bookWithAuthorRepository.findAll(pageable).stream(); + public Stream listBooks(PageRequest pageable) { + return bookWithAuthorRepository.findAll(pageable, Order.by()).stream(); } } diff --git a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/PageableResource.java b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/PageableResource.java index 08243c0..2266122 100644 --- a/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/PageableResource.java +++ b/quarkus-jnosql-lite/src/main/java/org/jnosql/demoee/PageableResource.java @@ -2,21 +2,15 @@ import jakarta.data.Direction; -import jakarta.data.page.Pageable; +import jakarta.data.page.PageRequest; import jakarta.data.Sort; -import java.util.Arrays; -import java.util.Objects; public interface PageableResource { - default Pageable createPageable(String orderBy, int page, int pageSize) { - return Pageable.ofSize(pageSize).page(page) - .sortBy(Arrays.stream(orderBy.split(",")) - .map(this::convertToSort) - .filter(Objects::nonNull) - .toList()); + default PageRequest createPageable(String orderBy, int page, int pageSize) { + return PageRequest.ofPage(page).size(pageSize); } diff --git a/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/AuthorResourceTest.java b/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/AuthorResourceTest.java deleted file mode 100644 index 224fdbe..0000000 --- a/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/AuthorResourceTest.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.jnosql.demoee; - -import com.github.javafaker.Faker; -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.common.mapper.TypeRef; -import io.restassured.http.ContentType; -import jakarta.inject.Inject; -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.*; -import static org.hamcrest.MatcherAssert.*; - -@QuarkusTest -@Disabled -public class AuthorResourceTest { - - static final Faker faker = new Faker(); - - @Inject - Bookstore bookstore; - @Inject - AuthorWithBooksRepository authorWithBooksRepository; - @Inject - BookWithAuthorRepository bookWithAuthorRepository; - - @BeforeEach - @AfterEach - void cleanUpDatabase() throws InterruptedException { - authorWithBooksRepository.deleteAll(); - bookWithAuthorRepository.deleteAll(); - TimeUnit.SECONDS.sleep(2); - } - - @Test - public void postNewAuthor() { - record NewAuthor(String name) { - } - - NewAuthor request = new NewAuthor(faker.name().fullName()); - - given(). - contentType(ContentType.JSON). - body(request). - when(). - post("/authors"). - then(). - statusCode(HttpStatus.SC_OK). - body( - "id", notNullValue(), - "name", is(request.name()) - ); - } - - @Test - public void getAllAuthors() { - - AuthorWithBooks[] authors = new AuthorWithBooks[]{ - AuthorWithBooks.of(faker.name().fullName()), - AuthorWithBooks.of(faker.name().fullName()), - AuthorWithBooks.of(faker.name().fullName())}; - - Arrays.stream(authors).forEach(bookstore::save); - - List persistedAuthors = given(). - accept(ContentType.JSON). - when(). - get("/authors"). - then(). - statusCode(HttpStatus.SC_OK). - body("$", hasSize(3)). - and(). - extract().as(new TypeRef>() { - }); - - assertThat(persistedAuthors, hasItems(authors)); - - } - -} \ No newline at end of file diff --git a/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/BookResourceTest.java b/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/BookResourceTest.java deleted file mode 100644 index f8147ff..0000000 --- a/quarkus-jnosql-lite/src/test/java/org/jnosql/demoee/BookResourceTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.jnosql.demoee; - -import com.github.javafaker.Faker; -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.common.mapper.TypeRef; -import io.restassured.http.ContentType; -import jakarta.inject.Inject; -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; - -@QuarkusTest -@Disabled -public class BookResourceTest { - - static final Faker faker = new Faker(); - - @Inject - Bookstore bookstore; - @Inject - AuthorWithBooksRepository authorWithBooksRepository; - @Inject - BookWithAuthorRepository bookWithAuthorRepository; - - @BeforeEach - @AfterEach - void cleanUpDatabase() throws InterruptedException { - authorWithBooksRepository.deleteAll(); - bookWithAuthorRepository.deleteAll(); - TimeUnit.SECONDS.sleep(2); - } - - @Test - public void postNewBook() { - - record NewBook(String title, Author author) { - } - - var book = faker.book(); - - var authorWithBooks = bookstore.save(AuthorWithBooks.of(Author.of(book.author()))); - - NewBook request = new NewBook(book.title(), authorWithBooks.getAuthor()); - - given().log().everything(). - contentType(ContentType.JSON). - body(request). - when(). - post("/books"). - then().log().everything(). - statusCode(HttpStatus.SC_OK). - body( - "id", notNullValue(), - "title", is(request.title()), - "author.id", is(request.author().getId()), - "author.name", is(request.author().getName()) - ); - } - - @Test - public void getAllBooks() { - var book = faker.book(); - - Book book1 = Book.of(book.title()); - Book book2 = Book.of(book.title()); - Book book3 = Book.of(book.title()); - var authorWithBooks1 = bookstore.save(AuthorWithBooks.of(book.author(), - book1, book2, book3)); - - Book book4 = Book.of(book.title()); - Book book5 = Book.of(book.title()); - var authorWithBooks2 = bookstore.save(AuthorWithBooks.of(book.author(), - book4, book5)); - - Book book6 = Book.of(book.title()); - var authorWithBooks3 = bookstore.save(AuthorWithBooks.of(book.author(), - book6)); - - - BookWithAuthor[] books = new BookWithAuthor[]{ - BookWithAuthor.of(book1,authorWithBooks1), - BookWithAuthor.of(book2,authorWithBooks1), - BookWithAuthor.of(book3,authorWithBooks1), - BookWithAuthor.of(book4,authorWithBooks2), - BookWithAuthor.of(book5,authorWithBooks2), - BookWithAuthor.of(book6,authorWithBooks3)}; - - List persistedAuthors = given(). - log().everything(). - accept(ContentType.JSON). - when(). - get("/books"). - then(). - log().everything(). - statusCode(HttpStatus.SC_OK). - extract().as(new TypeRef>() { - }); - - assertThat(persistedAuthors, hasItems(books)); - - } - -} \ No newline at end of file diff --git a/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogRepository.java b/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogRepository.java index 3028c96..8628797 100644 --- a/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogRepository.java +++ b/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogRepository.java @@ -1,8 +1,9 @@ package org.jnosql.demoee; -import jakarta.data.repository.PageableRepository; + +import jakarta.data.repository.BasicRepository; import jakarta.data.repository.Repository; @Repository -public interface DogRepository extends PageableRepository { +public interface DogRepository extends BasicRepository { } diff --git a/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogResource.java b/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogResource.java index 29f8b9a..aed70a1 100644 --- a/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogResource.java +++ b/quarkus-mongodb/src/main/java/org/jnosql/demoee/DogResource.java @@ -3,8 +3,9 @@ import com.github.javafaker.Faker; -import jakarta.data.page.Pageable; +import jakarta.data.Order; import jakarta.data.Sort; +import jakarta.data.page.PageRequest; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.DELETE; @@ -53,8 +54,10 @@ public List random(){ @GET public List findAll(@QueryParam("page") @DefaultValue("1") long page, @QueryParam("size") @DefaultValue("10") int size){ - Pageable pageable = Pageable.ofPage(page).size(size).sortBy(Sort.asc("name")); - return this.repository.findAll(pageable).content(); + PageRequest pageable = PageRequest.ofPage(page).size(size); + + Order order = Order.by(Sort.asc("name")); + return this.repository.findAll(pageable, order).content(); } @POST diff --git a/quarkus-mongodb/src/main/java/org/jnosql/demoee/FishService.java b/quarkus-mongodb/src/main/java/org/jnosql/demoee/FishService.java index 50a60fe..de768dd 100644 --- a/quarkus-mongodb/src/main/java/org/jnosql/demoee/FishService.java +++ b/quarkus-mongodb/src/main/java/org/jnosql/demoee/FishService.java @@ -5,10 +5,9 @@ import com.github.javafaker.Faker; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import jakarta.nosql.document.DocumentTemplate; - import org.bson.types.ObjectId; +import org.eclipse.jnosql.mapping.document.DocumentTemplate; import java.util.List; import java.util.Optional; diff --git a/tomee-jnosql-sample/src/main/java/org/soujava/Animal.java b/tomee-jnosql-sample/src/main/java/org/soujava/Animal.java index 5475785..fe6b74f 100644 --- a/tomee-jnosql-sample/src/main/java/org/soujava/Animal.java +++ b/tomee-jnosql-sample/src/main/java/org/soujava/Animal.java @@ -1,9 +1,9 @@ package org.soujava; import jakarta.nosql.Column; +import jakarta.nosql.Convert; import jakarta.nosql.Entity; import jakarta.nosql.Id; -import org.eclipse.jnosql.mapping.Convert; import java.util.Objects; @@ -57,4 +57,4 @@ public String toString() { ", name='" + name + '\'' + '}'; } -} \ No newline at end of file +} diff --git a/tomee-jnosql-sample/src/main/java/org/soujava/AnimalRepository.java b/tomee-jnosql-sample/src/main/java/org/soujava/AnimalRepository.java index c035b27..9179855 100644 --- a/tomee-jnosql-sample/src/main/java/org/soujava/AnimalRepository.java +++ b/tomee-jnosql-sample/src/main/java/org/soujava/AnimalRepository.java @@ -1,9 +1,9 @@ package org.soujava; -import jakarta.data.repository.PageableRepository; +import jakarta.data.repository.BasicRepository; import jakarta.data.repository.Repository; @Repository -public interface AnimalRepository extends PageableRepository { -} \ No newline at end of file +public interface AnimalRepository extends BasicRepository { +} diff --git a/tomee-jnosql-sample/src/main/java/org/soujava/ObjectIdConverter.java b/tomee-jnosql-sample/src/main/java/org/soujava/ObjectIdConverter.java index 1a5103b..e7c5752 100644 --- a/tomee-jnosql-sample/src/main/java/org/soujava/ObjectIdConverter.java +++ b/tomee-jnosql-sample/src/main/java/org/soujava/ObjectIdConverter.java @@ -1,7 +1,7 @@ package org.soujava; +import jakarta.nosql.AttributeConverter; import org.bson.types.ObjectId; -import org.eclipse.jnosql.mapping.AttributeConverter; public class ObjectIdConverter implements AttributeConverter { @@ -20,4 +20,4 @@ public String convertToEntityAttribute(ObjectId dbData) { } return null; } -} \ No newline at end of file +}