InMemoryQueryEngine implements QueryEngine<T> { @Override public List<T> query( Query query ) { validateQuery( query ); List<T> list = runQuery( query ); list = runSorter( query, list ); return PagerUtils.pageCollection( list, query.getFirstResult(), query.getMaxResults() ); } @Autowired  InMemoryQueryEngine( SchemaService schemaService, AclService aclService, CurrentUserService currentUserService ); @Override List<T> query( Query query ); @Override int count( Query query );  }
@Test public void getAllQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); assertEquals( 6, queryEngine.query( query ).size() ); }
@Test public void getMinMaxQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.setFirstResult( 2 ); query.setMaxResults( 10 ); assertEquals( 4, queryEngine.query( query ).size() ); query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.setFirstResult( 2 ); query.setMaxResults( 2 ); assertEquals( 2, queryEngine.query( query ).size() ); }
@Test public void getEqQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.eq( "id", "deabcdefghA" ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 1, objects.size() ); assertEquals( "deabcdefghA", objects.get( 0 ).getUid() ); }
@Test public void getEqQueryEnum() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.eq( "valueType", ValueType.INTEGER ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void getNeQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.ne( "id", "deabcdefghA" ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 5, objects.size() ); assertFalse( collectionContainsUid( objects, "deabcdefghA" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghE" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void getLikeQueryAnywhere() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.like( "name", "F", MatchMode.ANYWHERE ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 1, objects.size() ); assertEquals( "deabcdefghF", objects.get( 0 ).getUid() ); }
@Test public void getLikeQueryStart() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.like( "name", "Data", MatchMode.START ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertEquals( "deabcdefghA", objects.get( 0 ).getUid() ); assertEquals( "deabcdefghB", objects.get( 1 ).getUid() ); assertEquals( "deabcdefghC", objects.get( 2 ).getUid() ); assertEquals( "deabcdefghD", objects.get( 3 ).getUid() ); assertEquals( "deabcdefghE", objects.get( 4 ).getUid() ); assertEquals( "deabcdefghF", objects.get( 5 ).getUid() ); }
@Test public void getLikeQueryEnd() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.like( "name", "ElementE", MatchMode.END ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 1, objects.size() ); assertEquals( "deabcdefghE", objects.get( 0 ).getUid() ); }
@Test public void getGtQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.gt( "created", Year.parseYear( "2003" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 3, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghE" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void getLtQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.lt( "created", Year.parseYear( "2003" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghA" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); }
@Test public void getGeQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.ge( "created", Year.parseYear( "2003" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 4, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghE" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void getLeQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.le( "created", Year.parseYear( "2003" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 3, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghA" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); }
@Test public void getBetweenQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.between( "created", Year.parseYear( "2003" ).getStart(), Year.parseYear( "2005" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 3, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghE" ) ); }
@Test public void getInQuery() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.in( "id", Lists.newArrayList( "deabcdefghD", "deabcdefghF" ) ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void testDateRange() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.ge( "created", Year.parseYear( "2002" ).getStart() ) ); query.add( Restrictions.le( "created", Year.parseYear( "2004" ).getStart() ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 3, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); }
@Test public void testIsNotNull() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.isNotNull( "categoryCombo" ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghA" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghE" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void testIsNull() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); query.add( Restrictions.isNull( "categoryCombo" ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 0, objects.size() ); }
@Test public void testDateRangeWithConjunction() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); Conjunction conjunction = query.conjunction(); conjunction.add( Restrictions.ge( "created", Year.parseYear( "2002" ).getStart() ) ); conjunction.add( Restrictions.le( "created", Year.parseYear( "2004" ).getStart() ) ); query.add( conjunction ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 3, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghB" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghC" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); }
@Test public void testDoubleEqConjunction() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); Conjunction conjunction = query.conjunction(); conjunction.add( Restrictions.eq( "id", "deabcdefghD" ) ); conjunction.add( Restrictions.eq( "id", "deabcdefghF" ) ); query.add( conjunction ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 0, objects.size() ); }
@Test public void testDoubleEqDisjunction() { Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) ); query.setObjects( dataElements ); Disjunction disjunction = query.disjunction(); disjunction.add( Restrictions.eq( "id", "deabcdefghD" ) ); disjunction.add( Restrictions.eq( "id", "deabcdefghF" ) ); query.add( disjunction ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); assertTrue( collectionContainsUid( objects, "deabcdefghD" ) ); assertTrue( collectionContainsUid( objects, "deabcdefghF" ) ); }
@Test public void sortNameDesc() { Schema schema = schemaService.getDynamicSchema( DataElement.class ); Query query = Query.from( schema ); query.setObjects( dataElements ); query.addOrder( new Order( schema.getProperty( "name" ), Direction.DESCENDING ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertEquals( "deabcdefghF", objects.get( 0 ).getUid() ); assertEquals( "deabcdefghE", objects.get( 1 ).getUid() ); assertEquals( "deabcdefghD", objects.get( 2 ).getUid() ); assertEquals( "deabcdefghC", objects.get( 3 ).getUid() ); assertEquals( "deabcdefghB", objects.get( 4 ).getUid() ); assertEquals( "deabcdefghA", objects.get( 5 ).getUid() ); }
@Test public void sortNameAsc() { Schema schema = schemaService.getDynamicSchema( DataElement.class ); Query query = Query.from( schema ); query.setObjects( dataElements ); query.addOrder( new Order( schema.getProperty( "name" ), Direction.ASCENDING ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertEquals( "deabcdefghA", objects.get( 0 ).getUid() ); assertEquals( "deabcdefghB", objects.get( 1 ).getUid() ); assertEquals( "deabcdefghC", objects.get( 2 ).getUid() ); assertEquals( "deabcdefghD", objects.get( 3 ).getUid() ); assertEquals( "deabcdefghE", objects.get( 4 ).getUid() ); assertEquals( "deabcdefghF", objects.get( 5 ).getUid() ); }
@Test public void sortCreatedDesc() { Schema schema = schemaService.getDynamicSchema( DataElement.class ); Query query = Query.from( schema ); query.setObjects( dataElements ); query.addOrder( new Order( schema.getProperty( "created" ), Direction.DESCENDING ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertEquals( "deabcdefghF", objects.get( 0 ).getUid() ); assertEquals( "deabcdefghE", objects.get( 1 ).getUid() ); assertEquals( "deabcdefghD", objects.get( 2 ).getUid() ); assertEquals( "deabcdefghC", objects.get( 3 ).getUid() ); assertEquals( "deabcdefghB", objects.get( 4 ).getUid() ); assertEquals( "deabcdefghA", objects.get( 5 ).getUid() ); }
@Test public void sortCreatedAsc() { Schema schema = schemaService.getDynamicSchema( DataElement.class ); Query query = Query.from( schema ); query.setObjects( dataElements ); query.addOrder( new Order( schema.getProperty( "created" ), Direction.ASCENDING ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 6, objects.size() ); assertEquals( "deabcdefghA", objects.get( 0 ).getUid() ); assertEquals( "deabcdefghB", objects.get( 1 ).getUid() ); assertEquals( "deabcdefghC", objects.get( 2 ).getUid() ); assertEquals( "deabcdefghD", objects.get( 3 ).getUid() ); assertEquals( "deabcdefghE", objects.get( 4 ).getUid() ); assertEquals( "deabcdefghF", objects.get( 5 ).getUid() ); }
@Test( expected = QueryException.class ) public void testInvalidDeepPath() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); query.add( Restrictions.eq( "dataElements.abc", "deabcdefghA" ) ); queryEngine.query( query ); }
@Test public void testEqIdDeepPath() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); query.add( Restrictions.eq( "dataElements.id", "deabcdefghA" ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 1, objects.size() ); assertEquals( "abcdefghijA", objects.get( 0 ).getUid() ); }
@Test public void testLikeNameDeepPath() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); query.add( Restrictions.like( "dataElements.name", "ElementD", MatchMode.END ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 1, objects.size() ); assertEquals( "abcdefghijB", objects.get( 0 ).getUid() ); }
@Test public void testLikeNamesDeepPath() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); Disjunction disjunction = query.disjunction(); disjunction.add( Restrictions.like( "dataElements.name", "ElementD", MatchMode.END ) ); disjunction.add( Restrictions.like( "dataElements.name", "ElementA", MatchMode.END ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); assertTrue( collectionContainsUid( objects, "abcdefghijA" ) ); assertTrue( collectionContainsUid( objects, "abcdefghijB" ) ); }
@Test public void testCollectionDeep() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); query.add( Restrictions.like( "dataElements.dataElementGroups.name", "A", MatchMode.END ) ); queryEngine.query( query ); }
@Test public void testCollectionEqSize() { Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) ); query.setObjects( dataElementGroups ); query.add( Restrictions.eq( "dataElements", 3 ) ); List<? extends IdentifiableObject> objects = queryEngine.query( query ); assertEquals( 2, objects.size() ); }