### Question:
MongoDbPageRepository implements PageRepository { @Override public List<PageUser> getPagesForUser(String userId, String pageType) { List<Page> pages = template.find(query(where("members").elemMatch(where("userId").is(userId)).andOperator(where("pageType").is(getString(pageType))))); List<PageUser> userList = Lists.newArrayList(); for(Page page : pages) { userList.add(findPageUser(userId, page)); } return sortUsers(userList, userId); }  @Override List<Page> getAllPagesForUserType(String userId, String pageType); @Override List<Page> getPagesForContextType(String contextId, String pageType); @Override int deletePages(String userId, String pageType); @Override Page createPageForUser(User user, PageTemplate pt); @Override boolean hasPersonPage(String userId); @Override boolean hasPage(String userId, String pageType); @Override List<PageUser> getPagesForUser(String userId, String pageType); @Override PageUser getSingleRecord(String userId, String pageId); @Override Class<? extends Page> getType(); @Override Page get(String  id); @Override Page save(Page item); @Override void delete(Page item); @Override List<Page> getAll(); @Override List<Page> getLimitedList(int offset, int pageSize); @Override int getCountAll(); MongoPageOperations getTemplate(); void setTemplate(MongoPageOperations template);  }### Answer:
@Test public void getPagesForUser(){ Page p = new PageImpl(); PageUser user1 = new PageUserImpl("2222L"); PageUser user3 = new PageUserImpl("2222L"); User user2 = new UserImpl("2222L"); user1.setUserId(user2.getId()); List<PageUser> pageUser = Lists.newArrayList(); pageUser.add(user1); pageUser.add(user3); p.setMembers(pageUser); Page p2 = new PageImpl(); PageUser user4 = new PageUserImpl("2222L"); PageUser user5 = new PageUserImpl("2222L"); User user6 = new UserImpl("2222L"); user4.setUserId(user6.getId()); List<PageUser> pageUser2 = Lists.newArrayList(); pageUser.add(user4); pageUser.add(user5); p2.setMembers(pageUser2); List<Page> pages = Lists.newArrayList(p); pages.add(p2); List<PageUser> result; String userId = "2222L"; expect(template.find(query(where("members").elemMatch(where("userId").is(userId)).andOperator(where("pageType").is("USER"))))).andReturn(pages); replay(template); result = repo.getPagesForUser(userId, PageType.USER.toString().toString()); assertThat(result.get(0).getUserId(), is(equalTo(user2.getId()))); assertThat(result.size(), is(equalTo(2))); assertThat(result.get(0).getUserId(), is(equalTo("2222L"))); }
@Test public void getPagesForUser_null(){ Page p = new PageImpl(); PageUser user1 = new PageUserImpl("1111L"); user1.setUserId("2222L"); List<PageUser> pageUser = Lists.newArrayList(); pageUser.add(user1); p.setMembers(pageUser); List<Page> pages = Lists.newArrayList(p); List<PageUser> result; String userId = "3333L"; expect(template.find(query(where("members").elemMatch(where("userId").is(userId)).andOperator(where("pageType").is("USER"))))).andReturn(pages); replay(template); result = repo.getPagesForUser(userId, PageType.USER.toString().toString()); assertThat(result.size(), is(equalTo(1))); }