@Test public void queryWithOrderingAndSearchFilter() throws IOException, NoSuchMethodException { ServiceLocator locator = container; ArticleRepository repository = locator.resolve(ArticleRepository.class); Random random = new Random(); int projectID = random.nextInt(); Article article1 = new Article() .setProjectID(projectID) .setSku(UUID.randomUUID().toString().substring(0, 10)) .setTitle("Article A"); Article article2 = new Article() .setProjectID(projectID) .setSku(UUID.randomUUID().toString().substring(0, 10)) .setTitle("Article Z"); repository.insert(Arrays.asList(article1, article2)); ArticleGridRepository gridRepository = locator.resolve(ArticleGridRepository.class); Specification<ArticleGrid> spec = new ArticleGrid.filterSearch().setProjectID(projectID).setFilter("article"); JinqMetaModel jmm = locator.resolve(JinqMetaModel.class); Method method = ArticleGrid.class.getMethod("getTitle"); Query.Compare<ArticleGrid, ?> order = jmm.findGetter(method); List<ArticleGrid> list = gridRepository.query(spec).sortedDescendingBy(order).list(); Assert.assertEquals(2, list.size()); Assert.assertEquals(article2.getID(), list.get(0).getID()); Assert.assertEquals(article1.getID(), list.get(1).getID()); }
@Test public void testTimestampQueryingAndOrder() throws IOException { ServiceLocator locator = container; SearchByTimestampAndOrderByTimestampRepository analysisGridRepository = locator.resolve(SearchByTimestampAndOrderByTimestampRepository.class); String marker = UUID.randomUUID().toString(); OffsetDateTime dt1 = OffsetDateTime.now(); OffsetDateTime dt2 = dt1.plusDays(1); OffsetDateTime dt3 = dt1.plusDays(2); OffsetDateTime dt4 = dt1.plusDays(3); SearchByTimestampAndOrderByTimestamp[] arr = new SearchByTimestampAndOrderByTimestamp[] { new SearchByTimestampAndOrderByTimestamp().setOndate(dt1).setMarker(marker), new SearchByTimestampAndOrderByTimestamp().setOndate(dt2).setMarker(marker), new SearchByTimestampAndOrderByTimestamp().setOndate(dt3).setMarker(marker), new SearchByTimestampAndOrderByTimestamp().setOndate(dt4).setMarker(marker) }; analysisGridRepository.insert(arr); List<SearchByTimestampAndOrderByTimestamp> queryResults = analysisGridRepository .query(it -> it.getMarker().equals(marker) && it.getOndate().isAfter(dt2)) .sortedDescendingBy(SearchByTimestampAndOrderByTimestamp::getOndate) .list(); Assert.assertEquals(queryResults.size(), arr.length - 2); Assert.assertTrue(queryResults.get(0).getOndate().isEqual(dt4)); }
@Test public void stringOrderingViaLambda() throws IOException { ServiceLocator locator = container; InfoRepository infoRepository = locator.resolve(InfoRepository.class); String id = UUID.randomUUID().toString(); List<Info> infos = new ArrayList<Info>(); for (char letter = 'A'; letter < 'Z'; letter++) { infos.add( new Info() .setCode(String.format("code %s %c", id, letter)) .setName(String.format("name %s %c", id, letter))); } infoRepository.insert(infos); Specification<Info> filter = it -> it.getCode().startsWith("code " + id); List<Info> found = infoRepository .search(filter) .stream() .sorted((a, b) -> a.getName().compareTo(b.getName())) .collect(Collectors.toList()); List<Info> infosAscByName = infoRepository.query().filter(filter).sortedBy(Info::getName).list(); Assert.assertEquals(found, infosAscByName); List<Info> infosDescByName = infoRepository.query().filter(filter).sortedDescendingBy(Info::getName).list(); Collections.reverse(infosDescByName); Assert.assertEquals(found, infosDescByName); }
@Test public void searchWithFilter() throws IOException { ServiceLocator locator = container; NextRepository repository = locator.resolve(NextRepository.class); String uri = repository.insert(new Next()); int id = Integer.parseInt(uri); List<Next> found = repository.search(next -> next.getID() == id); Assert.assertEquals(1, found.size()); Assert.assertEquals(id, found.get(0).getID()); }
@Test public void willUseRewrittenSpecificationInSearch() throws IOException { ServiceLocator locator = container; DataContext db = locator.resolve(DataContext.class); LocalDate ld = LocalDate.now().plusDays(15); Clicked cl = new Clicked().setDate(ld); db.submit(cl); List<ClickedList> found = db.search(ClickedList.class, new ClickedList.FindAt(ld)); Assert.assertTrue(found.size() > 0); }
@Test public void simpleQuery() throws IOException { ServiceLocator locator = container; NextRepository repository = locator.resolve(NextRepository.class); repository.insert(new Next()); List<Next> search = repository.search(); Query<Next> stream = repository.query(); List<Next> list = stream.list(); Assert.assertEquals(search.size(), list.size()); }
@Test public void canUseQueryDirectly() throws IOException, ReflectiveOperationException { ServiceLocator locator = container; DataContext db = locator.resolve(DataContext.class); LocalDate ld = LocalDate.now().plusDays(15); Clicked cl = new Clicked().setDate(ld); db.submit(cl); List<ClickedList> found = locator.resolve(Query.class, ClickedList.class).filter(new ClickedList.FindAt(ld)).list(); Assert.assertTrue(found.size() > 0); }
@Test public void queryWithLimit() throws IOException { ServiceLocator locator = container; NextRepository repository = locator.resolve(NextRepository.class); String[] uris = repository.insert(Arrays.asList(new Next(), new Next())); Assert.assertEquals(2, uris.length); int id1 = Integer.parseInt(uris[0]); int id2 = Integer.parseInt(uris[1]); List<Next> found = repository.query(next -> next.getID() == id1 || next.getID() == id2).limit(1).list(); Assert.assertEquals(1, found.size()); }
@Test public void toStringAndValueOf() throws IOException { ServiceLocator locator = container; NextRepository repository = locator.resolve(NextRepository.class); String uri = repository.insert(new Next()); int id = Integer.parseInt(uri); List<Next> found = repository.search( it -> String.valueOf(it.getID()).equals(uri) && id == Integer.valueOf(it.getURI())); Assert.assertEquals(1, found.size()); Assert.assertEquals(id, found.get(0).getID()); }
@Test public void collectionContainsQuery() throws IOException { ServiceLocator locator = container; NextRepository repository = locator.resolve(NextRepository.class); String uri = repository.insert(new Next()); int id = Integer.parseInt(uri); List<Integer> numbers = Arrays.asList(-1, 0, id); List<Next> found = repository.query().filter(it -> numbers.contains(it.getID())).limit(2).list(); Assert.assertEquals(1, found.size()); Assert.assertEquals(id, found.get(0).getID()); }
@Test public void substringMethods() throws IOException { ServiceLocator locator = container; CompositeRepository repository = locator.resolve(CompositeRepository.class); UUID id = UUID.randomUUID(); repository.insert( new Composite().setId(id).setSimple(new Simple().setText("xxx" + id + "yyy"))); List<Composite> found = repository .query() .filter(it -> it.getId().equals(id)) .filter(it -> it.getSimple().getText().substring(3).equals(id.toString() + "yyy")) .filter(it -> it.getSimple().getText().substring(0, 3).equals("xxx")) .list(); Assert.assertEquals(1, found.size()); Assert.assertEquals(id, found.get(0).getId()); }
@Test public void testSQLMapping() throws IOException { ServiceLocator locator = container; CoverageUpdateRepository covUpdateRepo = locator.resolve(CoverageUpdateRepository.class); CoverageVersions1Repository covVersionsRepo = locator.resolve(CoverageVersions1Repository.class); String suppID = UUID.randomUUID().toString(); covUpdateRepo.submit(new CoverageUpdate().setSupplierID(suppID)); try { Thread.sleep(100); } catch (InterruptedException ignore) { } covUpdateRepo.submit(new CoverageUpdate().setSupplierID(suppID)); List<CoverageVersions1> items = covVersionsRepo.query(it -> it.getSupplierID().equals(suppID)).list(); Assert.assertEquals(items.size(), 2); Assert.assertNotEquals( items.get(0).getProcessedAt().isEqual(items.get(1).getProcessedAt()), true); }
@Test public void queryWithNullSearchFilter() throws IOException { ServiceLocator locator = container; ArticleRepository repository = locator.resolve(ArticleRepository.class); Random random = new Random(); Article article = new Article() .setProjectID(random.nextInt()) .setSku(UUID.randomUUID().toString().substring(0, 10)) .setTitle(UUID.randomUUID().toString().substring(0, 25)); repository.insert(article); ArticleGridRepository gridRepository = locator.resolve(ArticleGridRepository.class); Specification<ArticleGrid> spec = new ArticleGrid.filterSearch().setProjectID(article.getProjectID()).setFilter(null); List<ArticleGrid> list = gridRepository.query(spec).list(); Assert.assertEquals(1, list.size()); Assert.assertEquals(article.getID(), list.get(0).getID()); }
@Test public void stringOrderingViaPropertyName() throws IOException, NoSuchMethodException { ServiceLocator locator = container; InfoRepository infoRepository = locator.resolve(InfoRepository.class); String id = UUID.randomUUID().toString(); List<Info> infos = new ArrayList<Info>(); for (char letter = 'A'; letter < 'Z'; letter++) { infos.add( new Info() .setCode(String.format("code %s %c", id, letter)) .setName(String.format("name %s %c", id, letter))); } JinqMetaModel jinqMetaModel = locator.resolve(JinqMetaModel.class); Method method = Info.class.getMethod("getName"); Query.Compare<Info, ?> nameOrder = jinqMetaModel.findGetter(method); Specification<Info> filter = it -> it.getCode().startsWith("code " + id); infoRepository.insert(infos); List<Info> found = infoRepository .search(filter) .stream() .sorted((a, b) -> a.getName().compareTo(b.getName())) .collect(Collectors.toList()); List<Info> infosAscByName = infoRepository.query(filter).sortedBy(nameOrder).list(); Assert.assertEquals(found, infosAscByName); List<Info> infosDescByName = infoRepository.query(filter).sortedDescendingBy(nameOrder).list(); Collections.reverse(infosDescByName); Assert.assertEquals(found, infosDescByName); }