Specifications { public static <T> Specification<T> ifParamNotNull(final SingleParamSpecification<T> singleParamSpecification) { return new Specification<T>() { public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { return singleParamSpecification.isParamNull() ? null : singleParamSpecification.toPredicate(root, query, builder); } }; }  static Specification<T> ifParamNotNull(final SingleParamSpecification<T> singleParamSpecification); static Specification<T> distinct(Specification<T> spec);  }
@Test public void testIfParamNotNull() throws RepositoryNameAlreadyUsedException { long numberOfRepositories = repositoryRepository.count(); long numberOfDeletedRepositories = findAllDeleted().size(); Repository repository1 = repositoryService.createRepository(testIdWatcher.getEntityName("repository1")); Repository repository2 = repositoryService.createRepository(testIdWatcher.getEntityName("repository2")); repository2.setDeleted(true); List<Repository> repositories = Arrays.asList(repository1, repository2); repositoryRepository.saveAll(repositories); long expectedNumberOfRepositories = numberOfRepositories + repositories.size(); long expectedNumberOfNotDeletedRepositories = numberOfRepositories - numberOfDeletedRepositories + 1; List<Repository> findAll = findAll(null, null); Assert.assertEquals(expectedNumberOfRepositories, findAll.size()); findAll = findAll(null, repository1.getName()); Assert.assertEquals(1, findAll.size()); Assert.assertEquals(repository1.getName(), findAll.get(0).getName()); findAll = findAll(repository2.getName(), null); Assert.assertEquals(1, findAll.size()); Assert.assertEquals(repository2.getName(), findAll.get(0).getName()); findAll = findAll(repository1.getName(), repository2.getName()); Assert.assertEquals(0, findAll.size()); findAll = repositoryRepository.findByDeletedFalseOrderByNameAsc(); Assert.assertEquals(expectedNumberOfNotDeletedRepositories, findAll.size()); findAll = findIfNotDeleted(repository1.getName()); Assert.assertEquals(1, findAll.size()); Assert.assertEquals(repository1.getName(), findAll.get(0).getName()); findAll = findIfNotDeleted(repository2.getName()); Assert.assertEquals(0, findAll.size()); findAll = findIfNotDeleted(null); Assert.assertEquals(expectedNumberOfNotDeletedRepositories, findAll.size()); }