Search in sources :

Example 26 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsCriteriaFilterTest method testQueryCriteriaFilterWithParamsOr.

@Test
public void testQueryCriteriaFilterWithParamsOr() {
    QueryFilter qf = new QueryFilter();
    qf.setField("lastname");
    qf.setOperation(FilterOperation.EQ);
    qf.setValue("Turner");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q, (p, cb, r) -> cb.or(p, cb.equal(r.get("firstname"), "Antonio")));
    Assert.assertNotNull(users);
    Assert.assertEquals(4, users.size());
    Assert.assertNotNull(users.get(0));
    Assert.assertEquals("Antonio", users.get(0).getFirstname());
    Assert.assertEquals("Mills", users.get(0).getLastname());
    Assert.assertNotNull(users.get(1));
    Assert.assertEquals("Antonio", users.get(1).getFirstname());
    Assert.assertEquals("Carroll", users.get(1).getLastname());
    Assert.assertNotNull(users.get(2));
    Assert.assertEquals("Antonio", users.get(2).getFirstname());
    Assert.assertEquals("Turner", users.get(2).getLastname());
    Assert.assertNotNull(users.get(3));
    Assert.assertEquals("Bonnie", users.get(3).getFirstname());
    Assert.assertEquals("Turner", users.get(3).getLastname());
    Long usersCount = JPAUtils.queryEntitiesCount(em, User.class, q, (p, cb, r) -> cb.or(p, cb.equal(r.get("firstname"), "Antonio")));
    Assert.assertNotNull(usersCount);
    Assert.assertEquals(4, usersCount.longValue());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 27 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testInic.

@Test
public void testInic() {
    QueryFilter qf = new QueryFilter();
    qf.setField("firstname");
    qf.setOperation(FilterOperation.INIC);
    qf.getValues().add("sArAH");
    qf.getValues().add("ricHArd");
    qf.getValues().add("jACk");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(4, users.size());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 28 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testSingleFilter.

@Test
public void testSingleFilter() {
    QueryFilter qf = new QueryFilter();
    qf.setField("firstname");
    qf.setOperation(FilterOperation.EQ);
    qf.setValue("Sandra");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(1, users.size());
    Assert.assertNotNull(users.get(0).getFirstname());
    Assert.assertEquals("Sandra", users.get(0).getFirstname());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 29 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testGte.

@Test
public void testGte() {
    QueryFilter qf = new QueryFilter();
    qf.setField("role");
    qf.setOperation(FilterOperation.GTE);
    qf.setValue("1");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(47, users.size());
    Date d = Date.from(ZonedDateTime.parse("2014-11-26T11:15:08Z").toInstant());
    qf = new QueryFilter();
    qf.setField("createdAt");
    qf.setOperation(FilterOperation.GTE);
    qf.setDateValue(d);
    q = new QueryParameters();
    q.getFilters().add(qf);
    users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(40, users.size());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Date(java.util.Date) Test(org.junit.Test)

Example 30 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testLte.

@Test
public void testLte() {
    QueryFilter qf = new QueryFilter();
    qf.setField("role");
    qf.setOperation(FilterOperation.LTE);
    qf.setValue("0");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(53, users.size());
    Date d = Date.from(ZonedDateTime.parse("2014-11-26T11:15:08Z").toInstant());
    qf = new QueryFilter();
    qf.setField("createdAt");
    qf.setOperation(FilterOperation.LTE);
    qf.setDateValue(d);
    q = new QueryParameters();
    q.getFilters().add(qf);
    users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(61, users.size());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Date(java.util.Date) Test(org.junit.Test)

Aggregations

QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)49 Test (org.junit.Test)47 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)44 User (com.github.tfaga.lynx.test.entities.User)28 Project (com.github.tfaga.lynx.test.entities.Project)10 Date (java.util.Date)6 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)3 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 java.util (java.util)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)1 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)1 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)1 FilterOperation (com.github.tfaga.lynx.enums.FilterOperation)1 QueryFormatError (com.github.tfaga.lynx.enums.QueryFormatError)1 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)1 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)1