Search in sources :

Example 16 with WebQuery

use of com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery in project stdlib by petergeneric.

the class DynamicQueryTest method testOrderDesc.

@Test
public void testOrderDesc() throws Exception {
    ParentEntity obj1 = new ParentEntity();
    obj1.setName("Name1");
    dao.save(obj1);
    ParentEntity obj2 = new ParentEntity();
    obj2.setName("Name2");
    dao.save(obj2);
    assertEquals(getIds(obj2, obj1), getIds(dao.findByUriQuery(new WebQuery().orderDesc("id")).getList()));
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test)

Example 17 with WebQuery

use of com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery in project stdlib by petergeneric.

the class DynamicQueryTest method testEntityWrappedId.

@Test
public void testEntityWrappedId() throws Exception {
    ParentEntity obj = new ParentEntity();
    obj.setName("Name");
    obj.setOtherObject(new ChildEntity());
    obj.getOtherObject().setName("Name");
    childDao.save(obj.getOtherObject());
    dao.save(obj);
    final ConstrainedResultSet<ParentEntity> results = dao.find(new WebQuery(), JPASearchStrategy.ENTITY_WRAPPED_ID);
    assertEquals(1, results.list.size());
    // should be populated
    assertNotNull(results.uniqueResult().getId());
    // should not be populated
    assertNull(results.uniqueResult().getName());
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test)

Example 18 with WebQuery

use of com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery in project stdlib by petergeneric.

the class DynamicQueryTest method testComputeSizeWithOrder.

/**
 * Tests that computing size while applying ordering and limiting to the resultset still works
 *
 * @throws Exception
 */
@Test
public void testComputeSizeWithOrder() throws Exception {
    {
        ParentEntity obj1 = new ParentEntity();
        obj1.setName("Name1");
        dao.save(obj1);
        ParentEntity obj2 = new ParentEntity();
        obj2.setName("Name2");
        dao.save(obj2);
        ParentEntity obj3 = new ParentEntity();
        obj3.setName("Name3");
        dao.save(obj3);
    }
    final WebQuery query = new WebQuery().orderDesc("name").limit(2);
    // First, test that the correct results are returned with computeSize=false
    {
        ConstrainedResultSet<ParentEntity> results = dao.findByUriQuery(query);
        // Must honour limit
        assertEquals("without computeSize; must only have returned 2 results", 2, results.getList().size());
        assertEquals("without computeSize; must have returned right 2 entities", Arrays.asList("Name3", "Name2"), results.getList().stream().map(e -> e.getName()).collect(Collectors.toList()));
    }
    // Next, test that we get the same results with computeSize=true (ensures the order is still present after size is computed)
    query.computeSize(true);
    {
        ConstrainedResultSet<ParentEntity> results = dao.findByUriQuery(query);
        // Must have correct total size
        assertEquals("with computeSize; must have computed correct total size", Long.valueOf(3), results.getTotal());
        // Must honour limit
        assertEquals("with computeSize; must only have returned 2 results", 2, results.getList().size());
        assertEquals("with computeSize; must have returned right 2 entities", Arrays.asList("Name3", "Name2"), results.getList().stream().map(e -> e.getName()).collect(Collectors.toList()));
    }
}
Also used : Arrays(java.util.Arrays) HibernateDao(com.peterphi.std.guice.hibernate.dao.HibernateDao) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Assert.assertNotNull(org.junit.Assert.assertNotNull) Inject(com.google.inject.Inject) JPASearchStrategy(com.peterphi.std.guice.hibernate.webquery.impl.jpa.JPASearchStrategy) DateTime(org.joda.time.DateTime) RunWith(org.junit.runner.RunWith) Transactional(com.peterphi.std.guice.database.annotation.Transactional) JPASearchExecutor(com.peterphi.std.guice.hibernate.webquery.impl.jpa.JPASearchExecutor) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) GuiceConfig(com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.GuiceConfig) Test(org.junit.Test) GuiceUnit(com.peterphi.std.guice.testing.GuiceUnit) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) QEntityFactory(com.peterphi.std.guice.hibernate.webquery.impl.QEntityFactory) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) After(org.junit.After) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test)

Example 19 with WebQuery

use of com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery in project stdlib by petergeneric.

the class DynamicQueryTest method testGetByRelationIdIsNull.

@Test
public void testGetByRelationIdIsNull() throws Exception {
    ParentEntity obj = new ParentEntity();
    obj.setName("Name");
    dao.save(obj);
    assertEquals(1, dao.findByUriQuery(new WebQuery().isNull("otherObject.id")).getList().size());
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test)

Example 20 with WebQuery

use of com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery in project stdlib by petergeneric.

the class DynamicQueryTest method testNestedAssociatorThatIsMadeUpDoesNotWork.

@Test(expected = IllegalArgumentException.class)
public void testNestedAssociatorThatIsMadeUpDoesNotWork() throws Exception {
    WebQuery query = new WebQuery().eq("otherObject.parent.fictionalfield.name", "Alice");
    // Nonsense field shouldn't work
    dao.findByUriQuery(query);
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test)

Aggregations

WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)24 Test (org.junit.Test)15 Transactional (com.peterphi.std.guice.database.annotation.Transactional)12 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)4 OAuthSessionEntity (com.peterphi.usermanager.db.entity.OAuthSessionEntity)2 RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)2 UserEntity (com.peterphi.usermanager.db.entity.UserEntity)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ResteasyUriInfo (org.jboss.resteasy.spi.ResteasyUriInfo)2 DateTime (org.joda.time.DateTime)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Inject (com.google.inject.Inject)1 ResourceInstanceDTO (com.peterphi.servicemanager.service.rest.resource.type.ResourceInstanceDTO)1 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)1 HibernateDao (com.peterphi.std.guice.hibernate.dao.HibernateDao)1 QEntityFactory (com.peterphi.std.guice.hibernate.webquery.impl.QEntityFactory)1 JPASearchExecutor (com.peterphi.std.guice.hibernate.webquery.impl.jpa.JPASearchExecutor)1 JPASearchStrategy (com.peterphi.std.guice.hibernate.webquery.impl.jpa.JPASearchStrategy)1 GuiceUnit (com.peterphi.std.guice.testing.GuiceUnit)1