public void addNewSubPage_existingPages() {
final String PAGE_NAME = "my new page";
final String PARENT_PAGE_NAME = "my parent page";
final Long EXPECTED_RENDER_SEQUENCE = 2L;
List<Page> existingPages = new ArrayList<Page>();
existingPages.add(new PageImpl());
Page expectedPage = new PageImpl();
expectedPage.setName(PAGE_NAME);
expectedPage.setOwnerId(user.getId());
expectedPage.setPageLayout(pageLayout);
expectedPage.setRegions(createEmptyRegionList(pageLayout.getNumberOfRegions()));
Page parentPage = new PageImpl();
parentPage.setName(PARENT_PAGE_NAME);
parentPage.setOwnerId(user.getId());
parentPage.setPageLayout(pageLayout);
parentPage.setRegions(createEmptyRegionList(pageLayout.getNumberOfRegions()));
parentPage.setSubPages(existingPages);
expect(userService.getAuthenticatedUser()).andReturn(user);
expect(pageLayoutRepository.getByPageLayoutCode(PAGE_LAYOUT_CODE)).andReturn(pageLayout);
expect(pageRepository.save(expectedPage)).andReturn(expectedPage);
replay(userService, pageLayoutRepository, pageRepository);
Page newPage = pageService.addNewSubPage(PAGE_NAME, PAGE_LAYOUT_CODE, parentPage);
assertThat(newPage.getMembers().get(0).getRenderSequence(), is(EXPECTED_RENDER_SEQUENCE));
assertThat(newPage.getName(), is(PAGE_NAME));
assertThat(newPage.getRegions().size(), is(pageLayout.getNumberOfRegions().intValue()));
assertThat(newPage.getPageType(), is(PageType.SUB_PAGE));
assertThat(newPage.getParentPage(), is(parentPage));
assertTrue(parentPage.getSubPages().contains(newPage));
verify(userService, pageLayoutRepository, pageRepository);
}