Skip to content

Commit

Permalink
Added tests for the new list-based methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Apr 25, 2024
1 parent c2f6794 commit 7ed4562
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 261 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.spring.data.deployment;

import org.springframework.data.repository.ListCrudRepository;

public interface BookListCrudRepository extends ListCrudRepository<Book, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.spring.data.deployment;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BookListCrudRepositoryTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource("import_books.sql", "import.sql")
.addClasses(Book.class, BookListCrudRepository.class))
.withConfigurationResource("application.properties");

@Inject
BookListCrudRepository repo;

@Test
@Order(1)
@Transactional
public void shouldListAllBooks() {
List<Book> all = repo.findAll();
assertThat(all).isNotEmpty();
assertThat(all).hasSize(3);
assertThat(all.stream().map(Book::getName)).containsExactlyInAnyOrder("Talking to Strangers", "The Ascent of Money",
"A Short History of Everything");
}

@Test
@Order(2)
@Transactional
public void shouldListBooksWithIds() {
List<Integer> ids = Arrays.asList(1, 2);
List<Book> all = repo.findAllById(ids);
assertThat(all).isNotEmpty();
assertThat(all).hasSize(2);
assertThat(all.stream().map(Book::getName)).containsExactlyInAnyOrder("Talking to Strangers", "The Ascent of Money");
}

@Test
@Order(3)
@Transactional
public void shouldSaveBooks() {
Book harryPotterAndTheChamberOfSecrets = populateBook(4, "Harry Potter and the Chamber of Secrets");
Book harryPotterAndThePrisonerOfAzkaban = populateBook(5, "Harry Potter and the Prisoner of Azkaban");
Book harryPotterAndTheGlobetOfFire = populateBook(6, "Harry Potter and the Globet of Fire");
List<Book> books = Arrays.asList(harryPotterAndTheChamberOfSecrets, harryPotterAndThePrisonerOfAzkaban,
harryPotterAndTheGlobetOfFire);
List<Book> all = repo.saveAll(books);
assertThat(all).isNotEmpty();
assertThat(all).hasSize(3);
assertThat(all.stream().map(Book::getName)).containsExactlyInAnyOrder("Harry Potter and the Chamber of Secrets",
"Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Globet of Fire");
}

private Book populateBook(Integer id, String title) {
Book book = new Book();
book.setBid(id);
book.setName(title);
return book;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.spring.data.deployment;

import org.springframework.data.repository.ListPagingAndSortingRepository;

public interface BookListPagingAndSortingRepository extends ListPagingAndSortingRepository<Book, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.quarkus.spring.data.deployment;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import io.quarkus.test.QuarkusUnitTest;

public class BookPagingAndSortingRepositoryTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource("import_hp_books.sql", "import.sql")
.addClasses(Book.class, BookListPagingAndSortingRepository.class))
.withConfigurationResource("application.properties");

@Inject
BookListPagingAndSortingRepository repo;

@Test
// @Order(1)
@Transactional
public void shouldReturnFirstPageOfTwoBooks() {
Pageable pageRequest = PageRequest.of(0, 2);
Page<Book> result = repo.findAll(pageRequest);

assertThat(result).isNotEmpty();
assertThat(result).hasSize(2);
assertThat(result.stream().map(Book::getBid)).containsExactly(1, 2);
}

@Test
@Transactional
public void shouldReturnSecondPageOfSizeTwoBooks() {
Pageable pageRequest = PageRequest.of(1, 2);
Page<Book> result = repo.findAll(pageRequest);

assertThat(result).isNotEmpty();
assertThat(result).hasSize(2);
assertThat(result.stream().map(Book::getBid)).containsExactly(3, 4);
}

@Test
@Transactional
public void shouldReturnLastPage() {
Pageable pageRequest = PageRequest.of(2, 2);
Page<Book> result = repo.findAll(pageRequest);

assertThat(result).isNotEmpty();
assertThat(result).hasSize(2);
assertThat(result.stream().map(Book::getBid)).containsExactly(5, 6);
}

@Test
@Transactional
void shouldReturnSortedByNameAscAndPagedResult() {
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));

Page<Book> result = repo.findAll(pageRequest);
assertThat(result).isNotEmpty();
assertThat(result).hasSize(3);
assertThat(result.stream().map(Book::getBid)).containsExactly(2, 7, 4);

}

@Test
@Transactional
void shouldReturnSortedByNameDescAndPagedResult() {
Pageable pageRequest = PageRequest.of(0, 5, Sort.by("name").descending());

Page<Book> result = repo.findAll(pageRequest);
assertThat(result).isNotEmpty();
assertThat(result).hasSize(5);
assertThat(result.stream().map(Book::getBid)).containsExactly(3, 1, 5, 6, 4);

}

@Test
@Transactional
void shouldReturnAllBooksSortedByNameDescResult() {
List<Book> result = repo.findAll(Sort.by("name").descending());
assertThat(result).isNotEmpty();
assertThat(result).hasSize(7);
assertThat(result.stream().map(Book::getBid)).containsExactly(3, 1, 5, 6, 4, 7, 2);

}

@Test
@Transactional
void shouldReturnAllBooksSortedByNameAscResult() {
List<Book> result = repo.findAll(Sort.by("name"));
assertThat(result).isNotEmpty();
assertThat(result).hasSize(7);
assertThat(result.stream().map(Book::getBid)).containsExactly(2, 7, 4, 6, 5, 1, 3);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
INSERT INTO book(bid, name) VALUES (1, 'Harry Potter and the Philosophers Stone');
INSERT INTO book(bid, name) VALUES (2, 'Harry Potter and the Chamber of Secrets');
INSERT INTO book(bid, name) VALUES (3, 'Harry Potter and the Prisoner of Azkaban');
INSERT INTO book(bid, name) VALUES (4, 'Harry Potter and the Goblet of Fire');
INSERT INTO book(bid, name) VALUES (5, 'Harry Potter and the Order of the Phoenix');
INSERT INTO book(bid, name) VALUES (6, 'Harry Potter and the Half-Blood Prince');
INSERT INTO book(bid, name) VALUES (7, 'Harry Potter and the Deathly Hallows');
Loading

0 comments on commit 7ed4562

Please sign in to comment.