|
| 1 | +package org.baeldung.persistence.query; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.collection.IsIn.isIn; |
| 5 | +import static org.hamcrest.core.IsNot.not; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.baeldung.persistence.dao.UserRepository; |
| 10 | +import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor; |
| 11 | +import org.baeldung.persistence.model.User; |
| 12 | +import org.baeldung.spring.PersistenceConfig; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.data.jpa.domain.Specification; |
| 18 | +import org.springframework.test.context.ContextConfiguration; |
| 19 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 20 | +import org.springframework.test.context.transaction.TransactionConfiguration; |
| 21 | +import org.springframework.transaction.annotation.Transactional; |
| 22 | + |
| 23 | +import cz.jirutka.rsql.parser.RSQLParser; |
| 24 | +import cz.jirutka.rsql.parser.ast.Node; |
| 25 | + |
| 26 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 27 | +@ContextConfiguration(classes = { PersistenceConfig.class }) |
| 28 | +@Transactional |
| 29 | +@TransactionConfiguration |
| 30 | +public class RsqlTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private UserRepository repository; |
| 34 | + |
| 35 | + private User userJohn; |
| 36 | + |
| 37 | + private User userTom; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void init() { |
| 41 | + userJohn = new User(); |
| 42 | + userJohn.setFirstName("john"); |
| 43 | + userJohn.setLastName("doe"); |
| 44 | + userJohn.setEmail("john@doe.com"); |
| 45 | + userJohn.setAge(22); |
| 46 | + repository.save(userJohn); |
| 47 | + |
| 48 | + userTom = new User(); |
| 49 | + userTom.setFirstName("tom"); |
| 50 | + userTom.setLastName("doe"); |
| 51 | + userTom.setEmail("tom@doe.com"); |
| 52 | + userTom.setAge(26); |
| 53 | + repository.save(userTom); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { |
| 58 | + final Node rootNode = new RSQLParser().parse("firstName==john;lastName==doe"); |
| 59 | + final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>()); |
| 60 | + final List<User> results = repository.findAll(spec); |
| 61 | + |
| 62 | + assertThat(userJohn, isIn(results)); |
| 63 | + assertThat(userTom, not(isIn(results))); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() { |
| 68 | + final Node rootNode = new RSQLParser().parse("firstName!=john"); |
| 69 | + final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>()); |
| 70 | + final List<User> results = repository.findAll(spec); |
| 71 | + |
| 72 | + assertThat(userTom, isIn(results)); |
| 73 | + assertThat(userJohn, not(isIn(results))); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenMinAge_whenGettingListOfUsers_thenCorrect() { |
| 78 | + final Node rootNode = new RSQLParser().parse("age>25"); |
| 79 | + final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>()); |
| 80 | + final List<User> results = repository.findAll(spec); |
| 81 | + |
| 82 | + assertThat(userTom, isIn(results)); |
| 83 | + assertThat(userJohn, not(isIn(results))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() { |
| 88 | + final Node rootNode = new RSQLParser().parse("firstName==jo*"); |
| 89 | + final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>()); |
| 90 | + final List<User> results = repository.findAll(spec); |
| 91 | + |
| 92 | + assertThat(userJohn, isIn(results)); |
| 93 | + assertThat(userTom, not(isIn(results))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void givenListOfFirstName_whenGettingListOfUsers_thenCorrect() { |
| 98 | + final Node rootNode = new RSQLParser().parse("firstName=in=(john,jack)"); |
| 99 | + final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>()); |
| 100 | + final List<User> results = repository.findAll(spec); |
| 101 | + |
| 102 | + assertThat(userJohn, isIn(results)); |
| 103 | + assertThat(userTom, not(isIn(results))); |
| 104 | + } |
| 105 | +} |
0 commit comments