|
| 1 | +package org.springframework.data.jpa.datatables.repository; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | + |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.data.jpa.datatables.Config; |
| 14 | +import org.springframework.data.jpa.datatables.mapping.DataTablesInput; |
| 15 | +import org.springframework.data.jpa.datatables.mapping.DataTablesOutput; |
| 16 | +import org.springframework.data.jpa.datatables.model.Game; |
| 17 | +import org.springframework.data.jpa.datatables.model.Prize; |
| 18 | +import org.springframework.data.jpa.datatables.parameter.ColumnParameter; |
| 19 | +import org.springframework.data.jpa.datatables.parameter.OrderParameter; |
| 20 | +import org.springframework.data.jpa.datatables.parameter.SearchParameter; |
| 21 | +import org.springframework.test.context.ContextConfiguration; |
| 22 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@ContextConfiguration(classes = Config.class) |
| 26 | +public class GameRepositoryTest { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private GameRepository gameRepository; |
| 30 | + |
| 31 | + /** |
| 32 | + * Insert sample data at the beginning of all tests |
| 33 | + */ |
| 34 | + @Before |
| 35 | + public void setUp() { |
| 36 | + if (gameRepository.count() > 0) |
| 37 | + return; |
| 38 | + for (int i = 0; i < 12; i++) { |
| 39 | + Game game = new Game(new Prize("prize" + i)); |
| 40 | + gameRepository.save(game); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testWithoutFilter() { |
| 46 | + DataTablesInput input = getBasicInput(); |
| 47 | + |
| 48 | + DataTablesOutput<Game> output = gameRepository.findAll(input); |
| 49 | + assertNotNull(output); |
| 50 | + assertNull(output.getError()); |
| 51 | + assertEquals(12, (long) output.getRecordsFiltered()); |
| 52 | + assertEquals(12, (long) output.getRecordsTotal()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testSearchOnEmbeddedProperty() { |
| 57 | + DataTablesInput input = getBasicInput(); |
| 58 | + |
| 59 | + input.getColumns().get(1).getSearch().setValue("prize1"); |
| 60 | + DataTablesOutput<Game> output = gameRepository.findAll(input); |
| 61 | + assertNotNull(output); |
| 62 | + assertNull(output.getError()); |
| 63 | + assertEquals(3, (long) output.getRecordsFiltered()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testOrderOnEmbeddedProperty() { |
| 68 | + DataTablesInput input = getBasicInput(); |
| 69 | + input.setOrder(new ArrayList<OrderParameter>()); |
| 70 | + input.getOrder().add(new OrderParameter(1, "desc")); |
| 71 | + |
| 72 | + DataTablesOutput<Game> output = gameRepository.findAll(input); |
| 73 | + assertNotNull(output); |
| 74 | + assertNull(output.getError()); |
| 75 | + assertEquals(12, (long) output.getRecordsFiltered()); |
| 76 | + assertEquals("prize9", output.getData().get(0).getPrize().getName()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * |
| 81 | + * @return basic input parameters |
| 82 | + */ |
| 83 | + private static DataTablesInput getBasicInput() { |
| 84 | + DataTablesInput input = new DataTablesInput(); |
| 85 | + input.setDraw(1); |
| 86 | + input.setStart(0); |
| 87 | + input.setLength(10); |
| 88 | + input.setSearch(new SearchParameter("", false)); |
| 89 | + input.setOrder(new ArrayList<OrderParameter>()); |
| 90 | + input.getOrder().add(new OrderParameter(0, "asc")); |
| 91 | + |
| 92 | + input.setColumns(new ArrayList<ColumnParameter>()); |
| 93 | + input.getColumns() |
| 94 | + .add(new ColumnParameter("id", "", true, true, new SearchParameter("", false))); |
| 95 | + input.getColumns() |
| 96 | + .add(new ColumnParameter("prize.name", "", true, true, new SearchParameter("", false))); |
| 97 | + |
| 98 | + return input; |
| 99 | + } |
| 100 | +} |
0 commit comments