@Before public void setUp() { repo.deleteAll(); repo.save( Arrays.asList( POPULAR_AVAILABLE_PRODUCT, UNPOPULAR_AVAILABLE_PRODUCT, UNAVAILABLE_PRODUCT, NAMED_PRODUCT)); }
@Test public void testFindByAfter() { repo.deleteAll(); ProductBean modifiedMid2012 = createProductBean("2012", 5, true); modifiedMid2012.setLastModified(new DateTime(2012, 6, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); ProductBean modifiedMid2011 = createProductBean("2011", 5, true); modifiedMid2011.setLastModified(new DateTime(2011, 6, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); repo.save(Arrays.asList(modifiedMid2012, modifiedMid2011)); List<ProductBean> found = repo.findByLastModifiedAfter(new DateTime(2012, 1, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); Assert.assertEquals(1, found.size()); Assert.assertEquals(modifiedMid2012.getId(), found.get(0).getId()); }
@Test public void testWithBoost() { repo.deleteAll(); ProductBean beanWithName = createProductBean("1", 5, true, "stackoverflow"); beanWithName.setTitle(Arrays.asList("indexoutofbounds")); ProductBean beanWithTitle = createProductBean("2", 5, true, "indexoutofbounds"); beanWithTitle.setTitle(Arrays.asList("stackoverflow")); repo.save(Arrays.asList(beanWithName, beanWithTitle)); List<ProductBean> found = repo.findByNameStartsWithOrTitleStartsWith("indexoutofbounds", "indexoutofbounds"); Assert.assertEquals(2, found.size()); Assert.assertEquals(beanWithTitle.getId(), found.get(0).getId()); Assert.assertEquals(beanWithName.getId(), found.get(1).getId()); }
@Test public void testFindWithSortDesc() { repo.deleteAll(); List<ProductBean> values = new ArrayList<ProductBean>(); for (int i = 0; i < 10; i++) { values.add(createProductBean(Integer.toString(i), i, true)); } repo.save(values); List<ProductBean> found = repo.findByAvailableTrueOrderByPopularityDesc(); ProductBean prev = found.get(0); for (int i = 1; i < found.size(); i++) { ProductBean cur = found.get(i); Assert.assertTrue(Long.valueOf(cur.getPopularity()) < Long.valueOf(prev.getPopularity())); prev = cur; } }
@Test public void testFindWithSortDescInPageableForNamedQuery() { repo.deleteAll(); List<ProductBean> values = new ArrayList<ProductBean>(); for (int i = 0; i < 10; i++) { values.add(createProductBean(Integer.toString(i), i, true)); } repo.save(values); Page<ProductBean> found = repo.findByAvailableWithSort( true, new PageRequest(0, 30, new Sort(Direction.DESC, "popularity"))); ProductBean prev = found.getContent().get(0); for (int i = 1; i < found.getContent().size(); i++) { ProductBean cur = found.getContent().get(i); Assert.assertTrue(Long.valueOf(cur.getPopularity()) < Long.valueOf(prev.getPopularity())); prev = cur; } }
@After public void tearDown() { repo.deleteAll(); }