From d3dae0d288032e332695c02458c4699c55defa22 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 10 Jun 2016 01:00:44 +0200 Subject: [PATCH] Add some helpers and refactor tests --- .../datatables/mapping/DataTablesInput.java | 53 ++++++++ .../datatables/mapping/DataTablesOutput.java | 18 +-- .../datatables/parameter/ColumnParameter.java | 9 ++ .../jpa/datatables/model/BillRepository.java | 7 + .../jpa/datatables/model/GameRepository.java | 6 + .../{repository => model}/HomeRepository.java | 3 +- .../jpa/datatables/model/UserRepository.java | 7 + .../datatables/repository/BillRepository.java | 7 - .../repository/BillRepositoryTest.java | 75 +++-------- .../datatables/repository/GameRepository.java | 6 - .../repository/GameRepositoryTest.java | 51 ++----- .../repository/LessonRepositoryTest.java | 39 ++---- .../datatables/repository/RepositoryTest.java | 2 + .../datatables/repository/UserRepository.java | 7 - .../repository/UserRepositoryTest.java | 124 ++++++------------ src/test/resources/init.sql | 60 +++++++++ 16 files changed, 233 insertions(+), 241 deletions(-) create mode 100644 src/test/java/org/springframework/data/jpa/datatables/model/BillRepository.java create mode 100644 src/test/java/org/springframework/data/jpa/datatables/model/GameRepository.java rename src/test/java/org/springframework/data/jpa/datatables/{repository => model}/HomeRepository.java (53%) create mode 100644 src/test/java/org/springframework/data/jpa/datatables/model/UserRepository.java delete mode 100644 src/test/java/org/springframework/data/jpa/datatables/repository/BillRepository.java delete mode 100644 src/test/java/org/springframework/data/jpa/datatables/repository/GameRepository.java delete mode 100644 src/test/java/org/springframework/data/jpa/datatables/repository/UserRepository.java diff --git a/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java b/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java index b5aa39e..ba784a9 100644 --- a/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java +++ b/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java @@ -62,6 +62,9 @@ public class DataTablesInput { private List columns; public DataTablesInput() { + this.draw = 1; + this.start = 0; + this.length = 10; this.search = new SearchParameter(); this.order = new ArrayList(); this.columns = new ArrayList(); @@ -127,6 +130,56 @@ public Map getColumnsAsMap() { return map; } + /** + * Find a column by its name + * + * @param columnName the name of the column + * @return the given Column, or null if not found + */ + public ColumnParameter getColumn(String columnName) { + if (columnName == null) { + return null; + } + for (ColumnParameter column : columns) { + if (columnName.equals(column.getData())) { + return column; + } + } + return null; + } + + /** + * Add a new column + * + * @param columnName the name of the column + * @param searchable whether the column is searchable or not + * @param orderable whether the column is orderable or not + * @param searchValue if any, the search value to apply + */ + public void addColumn(String columnName, boolean searchable, boolean orderable, + String searchValue) { + this.columns.add(new ColumnParameter(columnName, "", searchable, orderable, + new SearchParameter(searchValue, false))); + } + + /** + * Add an order on the given column + * + * @param columnName the name of the column + * @param ascending whether the sorting is ascending or descending + */ + public void addOrder(String columnName, boolean ascending) { + if (columnName == null) { + return; + } + for (int i = 0; i < columns.size(); i++) { + if (!columnName.equals(columns.get(i).getData())) { + continue; + } + order.add(new OrderParameter(i, ascending ? "asc" : "desc")); + } + } + @Override public String toString() { return "DataTablesInput [draw=" + draw + ", start=" + start + ", length=" + length + ", search=" diff --git a/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java b/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java index 14e37a8..a9055cb 100644 --- a/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java +++ b/src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java @@ -14,20 +14,20 @@ public class DataTablesOutput { * parameter, in order to prevent Cross Site Scripting (XSS) attacks. */ @JsonView(View.class) - private Integer draw; + private int draw; /** * Total records, before filtering (i.e. the total number of records in the database) */ @JsonView(View.class) - private Long recordsTotal = 0L; + private long recordsTotal = 0L; /** * Total records, after filtering (i.e. the total number of records after filtering has been * applied - not just the number of records being returned for this page of data). */ @JsonView(View.class) - private Long recordsFiltered = 0L; + private long recordsFiltered = 0L; /** * The data to be displayed in the table. This is an array of data source objects, one for each @@ -48,27 +48,27 @@ public class DataTablesOutput { public interface View { } - public Integer getDraw() { + public int getDraw() { return draw; } - public void setDraw(Integer draw) { + public void setDraw(int draw) { this.draw = draw; } - public Long getRecordsTotal() { + public long getRecordsTotal() { return recordsTotal; } - public void setRecordsTotal(Long recordsTotal) { + public void setRecordsTotal(long recordsTotal) { this.recordsTotal = recordsTotal; } - public Long getRecordsFiltered() { + public long getRecordsFiltered() { return recordsFiltered; } - public void setRecordsFiltered(Long recordsFiltered) { + public void setRecordsFiltered(long recordsFiltered) { this.recordsFiltered = recordsFiltered; } diff --git a/src/main/java/org/springframework/data/jpa/datatables/parameter/ColumnParameter.java b/src/main/java/org/springframework/data/jpa/datatables/parameter/ColumnParameter.java index b82dda0..621485d 100644 --- a/src/main/java/org/springframework/data/jpa/datatables/parameter/ColumnParameter.java +++ b/src/main/java/org/springframework/data/jpa/datatables/parameter/ColumnParameter.java @@ -95,6 +95,15 @@ public void setSearch(SearchParameter search) { this.search = search; } + /** + * Set the search value to apply to this column + * + * @param searchValue if any, the search value to apply + */ + public void setSearchValue(String searchValue) { + this.search.setValue(searchValue); + } + @Override public String toString() { return "ColumnParameter [data=" + data + ", name=" + name + ", searchable=" + searchable diff --git a/src/test/java/org/springframework/data/jpa/datatables/model/BillRepository.java b/src/test/java/org/springframework/data/jpa/datatables/model/BillRepository.java new file mode 100644 index 0000000..320a307 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/datatables/model/BillRepository.java @@ -0,0 +1,7 @@ +package org.springframework.data.jpa.datatables.model; + +import org.springframework.data.jpa.datatables.repository.DataTablesRepository; + +public interface BillRepository extends DataTablesRepository { + +} diff --git a/src/test/java/org/springframework/data/jpa/datatables/model/GameRepository.java b/src/test/java/org/springframework/data/jpa/datatables/model/GameRepository.java new file mode 100644 index 0000000..9b2bfa5 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/datatables/model/GameRepository.java @@ -0,0 +1,6 @@ +package org.springframework.data.jpa.datatables.model; + +import org.springframework.data.jpa.datatables.repository.DataTablesRepository; + +public interface GameRepository extends DataTablesRepository { +} diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/HomeRepository.java b/src/test/java/org/springframework/data/jpa/datatables/model/HomeRepository.java similarity index 53% rename from src/test/java/org/springframework/data/jpa/datatables/repository/HomeRepository.java rename to src/test/java/org/springframework/data/jpa/datatables/model/HomeRepository.java index 769f6bb..faa2664 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/HomeRepository.java +++ b/src/test/java/org/springframework/data/jpa/datatables/model/HomeRepository.java @@ -1,6 +1,5 @@ -package org.springframework.data.jpa.datatables.repository; +package org.springframework.data.jpa.datatables.model; -import org.springframework.data.jpa.datatables.model.Home; import org.springframework.data.repository.CrudRepository; public interface HomeRepository extends CrudRepository { diff --git a/src/test/java/org/springframework/data/jpa/datatables/model/UserRepository.java b/src/test/java/org/springframework/data/jpa/datatables/model/UserRepository.java new file mode 100644 index 0000000..cdfd940 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/datatables/model/UserRepository.java @@ -0,0 +1,7 @@ +package org.springframework.data.jpa.datatables.model; + +import org.springframework.data.jpa.datatables.repository.DataTablesRepository; + +public interface UserRepository extends DataTablesRepository { + +} diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepository.java b/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepository.java deleted file mode 100644 index 8e055fc..0000000 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.springframework.data.jpa.datatables.repository; - -import org.springframework.data.jpa.datatables.model.Bill; - -public interface BillRepository extends DataTablesRepository { - -} diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java b/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java index 5c14c05..6d14db5 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java +++ b/src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java @@ -4,9 +4,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import java.util.ArrayList; - -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,9 +11,7 @@ import org.springframework.data.jpa.datatables.mapping.DataTablesInput; import org.springframework.data.jpa.datatables.mapping.DataTablesOutput; import org.springframework.data.jpa.datatables.model.Bill; -import org.springframework.data.jpa.datatables.parameter.ColumnParameter; -import org.springframework.data.jpa.datatables.parameter.OrderParameter; -import org.springframework.data.jpa.datatables.parameter.SearchParameter; +import org.springframework.data.jpa.datatables.model.BillRepository; import org.springframework.data.jpa.datatables.specification.PreFilteringSpecification; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -28,28 +23,6 @@ public class BillRepositoryTest { @Autowired private BillRepository billRepository; - /** - * Insert sample data at the beginning of all tests - */ - @Before - public void setUp() { - if (billRepository.count() > 0) - return; - for (int i = 0; i < 12; i++) { - Bill bill = new Bill(); - bill.setHasBeenPayed(i % 2 == 0); - bill.setAmount((i + 1) * 100); - if (i == 0) { - bill.setDescription("foo%"); - } else if (i == 1) { - bill.setDescription("foo_"); - } else { - bill.setDescription("foo" + i); - } - billRepository.save(bill); - } - } - @Test public void testWithoutFilter() { DataTablesInput input = getBasicInput(); @@ -57,45 +30,45 @@ public void testWithoutFilter() { DataTablesOutput output = billRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(12, (long) output.getRecordsFiltered()); - assertEquals(12, (long) output.getRecordsTotal()); + assertEquals(12, output.getRecordsFiltered()); + assertEquals(12, output.getRecordsTotal()); } @Test public void testBooleanFilter() { DataTablesInput input = getBasicInput(); - input.getColumns().get(2).getSearch().setValue("TRUE"); + input.getColumn("hasBeenPayed").setSearchValue("TRUE"); DataTablesOutput output = billRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(6, (long) output.getRecordsFiltered()); + assertEquals(6, output.getRecordsFiltered()); } @Test public void testBooleanFilter2() { DataTablesInput input = getBasicInput(); - input.getColumns().get(2).getSearch().setValue("TRUE+FALSE"); + input.getColumn("hasBeenPayed").setSearchValue("TRUE+FALSE"); DataTablesOutput output = billRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(12, (long) output.getRecordsFiltered()); + assertEquals(12, output.getRecordsFiltered()); } @Test public void testEscapeCharacter() { DataTablesInput input = getBasicInput(); - input.getColumns().get(3).getSearch().setValue("foo%"); + input.getColumn("description").setSearchValue("foo%"); DataTablesOutput output = billRepository.findAll(input); assertNotNull(output); - assertEquals(1, (long) output.getRecordsFiltered()); + assertEquals(1, output.getRecordsFiltered()); - input.getColumns().get(3).getSearch().setValue("foo_"); + input.getColumn("description").setSearchValue("foo_"); output = billRepository.findAll(input); assertNotNull(output); - assertEquals(1, (long) output.getRecordsFiltered()); + assertEquals(1, output.getRecordsFiltered()); } @Test @@ -105,8 +78,8 @@ public void testWithPreFiltering() { DataTablesOutput output = billRepository.findAll(input, null, new PreFilteringSpecification()); assertNotNull(output); - assertEquals(6, (long) output.getRecordsFiltered()); - assertEquals(6, (long) output.getRecordsTotal()); + assertEquals(6, output.getRecordsFiltered()); + assertEquals(6, output.getRecordsTotal()); } /** @@ -115,23 +88,11 @@ public void testWithPreFiltering() { */ private static DataTablesInput getBasicInput() { DataTablesInput input = new DataTablesInput(); - input.setDraw(1); - input.setStart(0); - input.setLength(10); - input.setSearch(new SearchParameter("", false)); - input.setOrder(new ArrayList()); - input.getOrder().add(new OrderParameter(0, "asc")); - - input.setColumns(new ArrayList()); - input.getColumns() - .add(new ColumnParameter("id", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("amount", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("hasBeenPayed", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("description", "", true, true, new SearchParameter("", false))); - + input.addColumn("id", true, true, ""); + input.addColumn("amount", true, true, ""); + input.addColumn("hasBeenPayed", true, true, ""); + input.addColumn("description", true, true, ""); + input.addOrder("id", true); return input; } } diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepository.java b/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepository.java deleted file mode 100644 index 84cb50f..0000000 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepository.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.springframework.data.jpa.datatables.repository; - -import org.springframework.data.jpa.datatables.model.Game; - -public interface GameRepository extends DataTablesRepository { -} diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepositoryTest.java b/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepositoryTest.java index 0ddc7ac..346d346 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepositoryTest.java +++ b/src/test/java/org/springframework/data/jpa/datatables/repository/GameRepositoryTest.java @@ -4,9 +4,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import java.util.ArrayList; - -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,10 +11,7 @@ import org.springframework.data.jpa.datatables.mapping.DataTablesInput; import org.springframework.data.jpa.datatables.mapping.DataTablesOutput; import org.springframework.data.jpa.datatables.model.Game; -import org.springframework.data.jpa.datatables.model.Prize; -import org.springframework.data.jpa.datatables.parameter.ColumnParameter; -import org.springframework.data.jpa.datatables.parameter.OrderParameter; -import org.springframework.data.jpa.datatables.parameter.SearchParameter; +import org.springframework.data.jpa.datatables.model.GameRepository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -28,19 +22,6 @@ public class GameRepositoryTest { @Autowired private GameRepository gameRepository; - /** - * Insert sample data at the beginning of all tests - */ - @Before - public void setUp() { - if (gameRepository.count() > 0) - return; - for (int i = 0; i < 12; i++) { - Game game = new Game(new Prize("prize" + i)); - gameRepository.save(game); - } - } - @Test public void testWithoutFilter() { DataTablesInput input = getBasicInput(); @@ -48,31 +29,31 @@ public void testWithoutFilter() { DataTablesOutput output = gameRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(12, (long) output.getRecordsFiltered()); - assertEquals(12, (long) output.getRecordsTotal()); + assertEquals(12, output.getRecordsFiltered()); + assertEquals(12, output.getRecordsTotal()); } @Test public void testSearchOnEmbeddedProperty() { DataTablesInput input = getBasicInput(); - input.getColumns().get(1).getSearch().setValue("prize1"); + input.getColumn("prize.name").setSearchValue("prize1"); DataTablesOutput output = gameRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(3, (long) output.getRecordsFiltered()); + assertEquals(3, output.getRecordsFiltered()); } @Test public void testOrderOnEmbeddedProperty() { DataTablesInput input = getBasicInput(); - input.setOrder(new ArrayList()); - input.getOrder().add(new OrderParameter(1, "desc")); + input.getOrder().clear(); + input.addOrder("prize.name", false); DataTablesOutput output = gameRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(12, (long) output.getRecordsFiltered()); + assertEquals(12, output.getRecordsFiltered()); assertEquals("prize9", output.getData().get(0).getPrize().getName()); } @@ -82,19 +63,9 @@ public void testOrderOnEmbeddedProperty() { */ private static DataTablesInput getBasicInput() { DataTablesInput input = new DataTablesInput(); - input.setDraw(1); - input.setStart(0); - input.setLength(10); - input.setSearch(new SearchParameter("", false)); - input.setOrder(new ArrayList()); - input.getOrder().add(new OrderParameter(0, "asc")); - - input.setColumns(new ArrayList()); - input.getColumns() - .add(new ColumnParameter("id", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("prize.name", "", true, true, new SearchParameter("", false))); - + input.addColumn("id", true, true, ""); + input.addColumn("prize.name", true, true, ""); + input.addOrder("id", true); return input; } } diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/LessonRepositoryTest.java b/src/test/java/org/springframework/data/jpa/datatables/repository/LessonRepositoryTest.java index 9396280..0425f7a 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/LessonRepositoryTest.java +++ b/src/test/java/org/springframework/data/jpa/datatables/repository/LessonRepositoryTest.java @@ -4,8 +4,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import java.util.ArrayList; - import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,9 +12,6 @@ import org.springframework.data.jpa.datatables.mapping.DataTablesOutput; import org.springframework.data.jpa.datatables.model.Lesson; import org.springframework.data.jpa.datatables.model.LessonRepository; -import org.springframework.data.jpa.datatables.parameter.ColumnParameter; -import org.springframework.data.jpa.datatables.parameter.OrderParameter; -import org.springframework.data.jpa.datatables.parameter.SearchParameter; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -31,19 +26,19 @@ public class LessonRepositoryTest { public void testThroughTwoManyToOneRelationships() { DataTablesInput input = getBasicInput(); - input.getColumns().get(3).getSearch().setValue("CourseTypeA"); + input.getColumn("course.type.name").setSearchValue("CourseTypeA"); DataTablesOutput output = lessonRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(5, (long) output.getRecordsFiltered()); - assertEquals(7, (long) output.getRecordsTotal()); + assertEquals(5, output.getRecordsFiltered()); + assertEquals(7, output.getRecordsTotal()); - input.getColumns().get(2).getSearch().setValue("CourseA-2"); + input.getColumn("course.name").setSearchValue("CourseA-2"); output = lessonRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(2, (long) output.getRecordsFiltered()); - assertEquals(7, (long) output.getRecordsTotal()); + assertEquals(2, output.getRecordsFiltered()); + assertEquals(7, output.getRecordsTotal()); } /** @@ -52,23 +47,11 @@ public void testThroughTwoManyToOneRelationships() { */ private static DataTablesInput getBasicInput() { DataTablesInput input = new DataTablesInput(); - input.setDraw(1); - input.setStart(0); - input.setLength(10); - input.setSearch(new SearchParameter("", false)); - input.setOrder(new ArrayList()); - input.getOrder().add(new OrderParameter(0, "asc")); - - input.setColumns(new ArrayList()); - input.getColumns() - .add(new ColumnParameter("id", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("name", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("course.name", "", true, true, new SearchParameter("", false))); - input.getColumns().add( - new ColumnParameter("course.type.name", "", true, true, new SearchParameter("", false))); - + input.addColumn("id", true, true, ""); + input.addColumn("name", true, true, ""); + input.addColumn("course.name", true, true, ""); + input.addColumn("course.type.name", true, true, ""); + input.addOrder("id", true); return input; } } diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/RepositoryTest.java b/src/test/java/org/springframework/data/jpa/datatables/repository/RepositoryTest.java index 06819c3..8f828be 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/RepositoryTest.java +++ b/src/test/java/org/springframework/data/jpa/datatables/repository/RepositoryTest.java @@ -7,6 +7,8 @@ import org.springframework.aop.framework.Advised; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.datatables.Config; +import org.springframework.data.jpa.datatables.model.HomeRepository; +import org.springframework.data.jpa.datatables.model.UserRepository; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepository.java b/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepository.java deleted file mode 100644 index ea21c5e..0000000 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.springframework.data.jpa.datatables.repository; - -import org.springframework.data.jpa.datatables.model.User; - -public interface UserRepository extends DataTablesRepository { - -} diff --git a/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepositoryTest.java b/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepositoryTest.java index 4009aad..300b5e2 100644 --- a/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepositoryTest.java +++ b/src/test/java/org/springframework/data/jpa/datatables/repository/UserRepositoryTest.java @@ -4,23 +4,16 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import java.util.ArrayList; import java.util.List; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.datatables.Config; import org.springframework.data.jpa.datatables.mapping.DataTablesInput; import org.springframework.data.jpa.datatables.mapping.DataTablesOutput; -import org.springframework.data.jpa.datatables.model.Home; import org.springframework.data.jpa.datatables.model.User; -import org.springframework.data.jpa.datatables.model.User.UserRole; -import org.springframework.data.jpa.datatables.model.User.UserStatus; -import org.springframework.data.jpa.datatables.parameter.ColumnParameter; -import org.springframework.data.jpa.datatables.parameter.OrderParameter; -import org.springframework.data.jpa.datatables.parameter.SearchParameter; +import org.springframework.data.jpa.datatables.model.UserRepository; import org.springframework.data.jpa.datatables.specification.TestSpecification; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -32,35 +25,6 @@ public class UserRepositoryTest { @Autowired private UserRepository userRepository; - @Autowired - private HomeRepository homeRepository; - - /** - * Insert sample data at the beginning of all tests - */ - @Before - public void setUp() { - if (userRepository.count() > 0) - return; - List homes = new ArrayList(); - for (int i = 0; i < 4; i++) { - Home home = new Home(); - home.setTown("town" + i); - home = homeRepository.save(home); - homes.add(home); - } - for (int i = 0; i < 24; i++) { - User user = new User(); - user.setUsername("john" + i); - user.setRole(UserRole.values()[i % 3]); - user.setStatus(UserStatus.values()[i % 2]); - if (i > 3) - user.setHome(homes.get(i % 4)); - user.setVisible(i % 2 == 0); - userRepository.save(user); - } - } - @Test public void testSort() { DataTablesInput input = getBasicInput(); @@ -70,8 +34,8 @@ public void testSort() { assertNotNull(output); assertEquals(1, (int) output.getDraw()); assertNull(output.getError()); - assertEquals(24, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(24, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); List users = output.getData(); assertNotNull(users); @@ -84,13 +48,14 @@ public void testSort() { // sorting by id desc input.setDraw(2); - input.getOrder().get(0).setDir("desc"); + input.getOrder().clear(); + input.addOrder("id", false); output = userRepository.findAll(input); assertNotNull(output); assertEquals(2, (int) output.getDraw()); assertNull(output.getError()); - assertEquals(24, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(24, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); users = output.getData(); assertNotNull(users); @@ -106,7 +71,9 @@ public void testSort() { public void testMultipleSort() { DataTablesInput input = getBasicInput(); - input.getOrder().add(0, new OrderParameter(3, "desc")); + input.getOrder().clear(); + input.addOrder("status", false); + input.addOrder("id", true); // sorting by id asc and status desc DataTablesOutput output = userRepository.findAll(input); @@ -140,10 +107,10 @@ public void testFilterGlobal() { DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(1, (int) output.getDraw()); + assertEquals(1, output.getDraw()); assertNull(output.getError()); - assertEquals(11, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(11, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); List users = output.getData(); assertNotNull(users); @@ -163,33 +130,33 @@ public void testFilterGlobalIgnoreCase() { DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(11, (long) output.getRecordsFiltered()); + assertEquals(11, output.getRecordsFiltered()); } @Test public void testFilterOnOneColumnIgnoreCase() { DataTablesInput input = getBasicInput(); - input.getColumns().get(1).getSearch().setValue("OhN1"); + input.getColumn("username").setSearchValue("OhN1"); DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(11, (long) output.getRecordsFiltered()); + assertEquals(11, output.getRecordsFiltered()); } @Test public void testFilterOnSeveralColumns() { DataTablesInput input = getBasicInput(); - input.getColumns().get(2).getSearch().setValue("ADMIN"); - input.getColumns().get(3).getSearch().setValue("ACTIVE"); + input.getColumn("role").setSearchValue("ADMIN"); + input.getColumn("status").setSearchValue("ACTIVE"); DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(1, (int) output.getDraw()); + assertEquals(1, output.getDraw()); assertNull(output.getError()); - assertEquals(4, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(4, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); List users = output.getData(); assertNotNull(users); @@ -209,14 +176,14 @@ public void testFilterOnSeveralColumns() { public void testMultiFilterOnSameColumn() { DataTablesInput input = getBasicInput(); - input.getColumns().get(2).getSearch().setValue("ADMIN+USER"); + input.getColumn("role").setSearchValue("ADMIN+USER"); DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(1, (int) output.getDraw()); + assertEquals(1, output.getDraw()); assertNull(output.getError()); - assertEquals(16, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(16, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); List users = output.getData(); assertNotNull(users); @@ -234,14 +201,14 @@ public void testMultiFilterOnSameColumn() { public void testFilterOnManyToOneRelationship() { DataTablesInput input = getBasicInput(); - input.getColumns().get(4).getSearch().setValue("town0"); + input.getColumn("home.town").setSearchValue("town0"); DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); - assertEquals(1, (int) output.getDraw()); + assertEquals(1, output.getDraw()); assertNull(output.getError()); - assertEquals(5, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(5, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); List users = output.getData(); assertNotNull(users); @@ -260,8 +227,8 @@ public void testWithAdditionalSpecification() { DataTablesOutput output = userRepository.findAll(input, new TestSpecification()); assertNotNull(output); assertNull(output.getError()); - assertEquals(12, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(12, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); } @@ -284,8 +251,8 @@ public void testWithNegativeLength() { DataTablesOutput output = userRepository.findAll(input); assertNotNull(output); assertNull(output.getError()); - assertEquals(24, (long) output.getRecordsFiltered()); - assertEquals(24, (long) output.getRecordsTotal()); + assertEquals(24, output.getRecordsFiltered()); + assertEquals(24, output.getRecordsTotal()); } /** @@ -294,25 +261,12 @@ public void testWithNegativeLength() { */ private static DataTablesInput getBasicInput() { DataTablesInput input = new DataTablesInput(); - input.setDraw(1); - input.setStart(0); - input.setLength(10); - input.setSearch(new SearchParameter("", false)); - input.setOrder(new ArrayList()); - input.getOrder().add(new OrderParameter(0, "asc")); - - input.setColumns(new ArrayList()); - input.getColumns() - .add(new ColumnParameter("id", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("username", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("role", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("status", "", true, true, new SearchParameter("", false))); - input.getColumns() - .add(new ColumnParameter("home.town", "", true, true, new SearchParameter("", false))); - + input.addColumn("id", true, true, ""); + input.addColumn("username", true, true, ""); + input.addColumn("role", true, true, ""); + input.addColumn("status", true, true, ""); + input.addColumn("home.town", true, true, ""); + input.addOrder("id", true); return input; } } diff --git a/src/test/resources/init.sql b/src/test/resources/init.sql index 740228b..5e74d17 100644 --- a/src/test/resources/init.sql +++ b/src/test/resources/init.sql @@ -17,3 +17,63 @@ INSERT INTO lesson (id, name, course_id) VALUES (5, 'LessonA-2-b', 2), (6, 'LessonB-1-a', 4), (7, 'LessonB-1-b', 4); + +INSERT INTO bill (id, description, amount, hasBeenPayed) VALUES + (1, 'foo%', 100, false), + (2, 'foo_', 200, true), + (3, 'foo3', 300, false), + (4, 'foo4', 400, true), + (5, 'foo5', 500, false), + (6, 'foo6', 600, true), + (7, 'foo7', 700, false), + (8, 'foo8', 800, true), + (9, 'foo9', 900, false), + (10, 'foo10', 1000, true), + (11, 'foo11', 1100, false), + (12, 'foo12', 1200, true); + +INSERT INTO game (id, prize_name) VALUES + (1, 'prize0'), + (2, 'prize1'), + (3, 'prize2'), + (4, 'prize3'), + (5, 'prize4'), + (6, 'prize5'), + (7, 'prize6'), + (8, 'prize7'), + (9, 'prize8'), + (10, 'prize9'), + (11, 'prize10'), + (12, 'prize11'); + +INSERT INTO home (id, town) VALUES + (1, 'town0'), + (2, 'town1'), + (3, 'town2'), + (4, 'town3'); + +INSERT INTO user (id, username, role, status, id_home, visible) VALUES + (1, 'john0', 'ADMIN', 'ACTIVE', null, true), + (2, 'john1', 'AUTHOR', 'BLOCKED', null, false), + (3, 'john2', 'USER', 'ACTIVE', null, true), + (4, 'john3', 'ADMIN', 'BLOCKED', null, false), + (5, 'john4', 'AUTHOR', 'ACTIVE', 1, true), + (6, 'john5', 'USER', 'BLOCKED', 2, false), + (7, 'john6', 'ADMIN', 'ACTIVE', 3, true), + (8, 'john7', 'AUTHOR', 'BLOCKED', 4, false), + (9, 'john8', 'USER', 'ACTIVE', 1, true), + (10, 'john9', 'ADMIN', 'BLOCKED', 2, false), + (11, 'john10', 'AUTHOR', 'ACTIVE', 3, true), + (12, 'john11', 'USER', 'BLOCKED', 4, false), + (13, 'john12', 'ADMIN', 'ACTIVE', 1, true), + (14, 'john13', 'AUTHOR', 'BLOCKED', 2, false), + (15, 'john14', 'USER', 'ACTIVE', 3, true), + (16, 'john15', 'ADMIN', 'BLOCKED', 4, false), + (17, 'john16', 'AUTHOR', 'ACTIVE', 1, true), + (18, 'john17', 'USER', 'BLOCKED', 2, false), + (19, 'john18', 'ADMIN', 'ACTIVE', 3, true), + (20, 'john19', 'AUTHOR', 'BLOCKED', 4, false), + (21, 'john20', 'USER', 'ACTIVE', 1, true), + (22, 'john21', 'ADMIN', 'BLOCKED', 2, false), + (23, 'john22', 'AUTHOR', 'ACTIVE', 3, true), + (24, 'john23', 'USER', 'BLOCKED', 4, false);