PartnerQueryBuilder extends AbstractQueryBuilder<Partner> { @Override protected BaseQueryBuilder build(final Partner context, final BaseQueryBuilder builder) { return builder .where() .openBracket() .addAndCondition(PARENT_CONDITION, context.getParent()) .addAndCondition(ABBRNAME_CONDITION, context.getAbbrName()) .addAndCondition(NAME_CONDITION, context.getName()) .addAndCondition(MANAGER_CONDITION, context.getManager()) .addAndCondition(PHONE_CONDITION, context.getPhone()) .addAndCondition(EMAIL_CONDITION, context.getEmail()) .addAndCondition(BEGDATE_CONDITION, context.getBegDate()) .addAndCondition(ENDDATE_CONDITION, context.getEndDate()) .closeBracket(); }    }
@Test public void testBuild() throws Exception { Partner context = new Partner(); String expected = "SELECT e FROM Partner e WHERE e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }
@Test public void testBuildWithDisabledSecurityConstraint() throws Exception { unit.setSecurity(false); Partner context = new Partner(); String expected = "SELECT e FROM Partner e WHERE e.status=:status "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }
@Test public void testBuildWithDisabledStatusConstraint() throws Exception { unit.setActive(false); Partner context = new Partner(); String expected = "SELECT e FROM Partner e WHERE e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }
@Test public void testBuildWithDisabledDefaultConstraints() throws Exception { unit.setActive(false); unit.setSecurity(false); Partner context = new Partner(); String expected = "SELECT e FROM Partner e "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }
@Test public void testBuildWithParameters() throws Exception { Partner parent = new Partner(); String abbrName = "abbr name"; String name = "fdsfds"; String manager = "fdsfsd"; String phone = "fdfds"; String email = "fdsfds"; Date begDate = new Date(); Date endDate = new Date(); Partner context = new Partner(); context.setAbbrName(abbrName); context.setParent(parent); context.setName(name); context.setManager(manager); context.setPhone(phone); context.setEmail(email); context.setBegDate(begDate); context.setEndDate(endDate); String expected = "SELECT e FROM Partner e WHERE ( e.parent = :parent AND e.abbrName LIKE CONCAT('%',:abbrName,'%') AND e.name LIKE CONCAT('%',:name,'%') AND e.manager LIKE CONCAT('%',:manager,'%') AND e.phone LIKE CONCAT('%',:phone,'%') AND e.email LIKE CONCAT('%',:email,'%') AND e.begDate <= :begDate AND e.endDate >= :endDate ) AND e.status=:status AND e.crtUserGroup IN (:userGroups) "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }
@Test public void testBuildWithParametersAndDisabledDefaultConstraint() throws Exception { unit.setActive(false); unit.setSecurity(false); Partner parent = new Partner(); String abbrName = "abbr name"; String name = "fdsfds"; String manager = "fdsfsd"; String phone = "fdfds"; String email = "fdsfds"; Date begDate = new Date(); Date endDate = new Date(); Partner context = new Partner(); context.setAbbrName(abbrName); context.setParent(parent); context.setName(name); context.setManager(manager); context.setPhone(phone); context.setEmail(email); context.setBegDate(begDate); context.setEndDate(endDate); String expected = "SELECT e FROM Partner e WHERE ( e.parent = :parent AND e.abbrName LIKE CONCAT('%',:abbrName,'%') AND e.name LIKE CONCAT('%',:name,'%') AND e.manager LIKE CONCAT('%',:manager,'%') AND e.phone LIKE CONCAT('%',:phone,'%') AND e.email LIKE CONCAT('%',:email,'%') AND e.begDate <= :begDate AND e.endDate >= :endDate ) "; MultiplePagedSearch<Partner> pagedSearch = new MultiplePagedSearch<>(); pagedSearch.setEntity(context); String actualQuery = unit.build(pagedSearch); assertEquals(expected, actualQuery); }