@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); reader = new MongoItemReader<>(); sortOptions = new HashMap<>(); sortOptions.put("name", Sort.Direction.DESC); reader.setTemplate(template); reader.setTargetType(String.class); reader.setQuery("{ }"); reader.setSort(sortOptions); reader.afterPropertiesSet(); reader.setPageSize(50); }
reader.afterPropertiesSet(); fail("Template was not set but exception was not thrown."); } catch (IllegalStateException iae) { reader.afterPropertiesSet(); fail("type was not set but exception was not thrown."); } catch (IllegalStateException iae) { reader.afterPropertiesSet(); fail("Query was not set but exception was not thrown."); } catch (IllegalStateException iae) { reader.afterPropertiesSet(); fail("Sort was not set but exception was not thrown."); } catch (IllegalStateException iae) { reader.afterPropertiesSet();
@Test public void testQueryObject() throws Exception { reader = new MongoItemReader<>(); reader.setTemplate(template); Query query = new Query() .with(Sort.by(new Order(Sort.Direction.ASC, "_id"))); reader.setQuery(query); reader.setTargetType(String.class); reader.afterPropertiesSet(); ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class); when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>()); assertFalse(reader.doPageRead().hasNext()); Query actualQuery = queryContainer.getValue(); assertFalse(reader.doPageRead().hasNext()); assertEquals(10, actualQuery.getLimit()); assertEquals(0, actualQuery.getSkip()); }
@Test public void testQueryObjectWithCollection() throws Exception { reader = new MongoItemReader<>(); reader.setTemplate(template); Query query = new Query() .with(Sort.by(new Order(Sort.Direction.ASC, "_id"))); reader.setQuery(query); reader.setTargetType(String.class); reader.setCollection("collection"); reader.afterPropertiesSet(); ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class); ArgumentCaptor<String> stringContainer = ArgumentCaptor.forClass(String.class); when(template.find(queryContainer.capture(), eq(String.class), stringContainer.capture())).thenReturn(new ArrayList<>()); assertFalse(reader.doPageRead().hasNext()); Query actualQuery = queryContainer.getValue(); assertFalse(reader.doPageRead().hasNext()); assertEquals(10, actualQuery.getLimit()); assertEquals(0, actualQuery.getSkip()); assertEquals("collection", stringContainer.getValue()); } }
@Test public void testQueryObjectWithIgnoredPageSize() throws Exception { reader = new MongoItemReader<>(); reader.setTemplate(template); Query query = new Query() .with(Sort.by(new Order(Sort.Direction.ASC, "_id"))) .with(PageRequest.of(0, 50)); reader.setQuery(query); reader.setTargetType(String.class); reader.afterPropertiesSet(); ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class); when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>()); assertFalse(reader.doPageRead().hasNext()); Query actualQuery = queryContainer.getValue(); assertFalse(reader.doPageRead().hasNext()); assertEquals(10, actualQuery.getLimit()); assertEquals(0, actualQuery.getSkip()); }
@Test public void testQueryObjectWithPageSize() throws Exception { reader = new MongoItemReader<>(); reader.setTemplate(template); Query query = new Query() .with(Sort.by(new Order(Sort.Direction.ASC, "_id"))) .with(PageRequest.of(30, 50)); reader.setQuery(query); reader.setTargetType(String.class); reader.setPageSize(100); reader.afterPropertiesSet(); ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class); when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>()); assertFalse(reader.doPageRead().hasNext()); Query actualQuery = queryContainer.getValue(); assertFalse(reader.doPageRead().hasNext()); assertEquals(100, actualQuery.getLimit()); assertEquals(0, actualQuery.getSkip()); }
@Test public void testAfterPropertiesSetForQueryObject() throws Exception{ reader = new MongoItemReader<>(); reader.setTemplate(template); reader.setTargetType(String.class); Query query1 = new Query().with(Sort.by(new Order(Sort.Direction.ASC, "_id"))); reader.setQuery(query1); reader.afterPropertiesSet(); }