@Test
public void addNewPage_existingPages() {
final String PAGE_NAME = "my new page";
final Long EXPECTED_RENDER_SEQUENCE = 2L;
List<Page> existingPages = new ArrayList<Page>();
existingPages.add(new Page());
Page expectedPage = new Page();
expectedPage.setName(PAGE_NAME);
expectedPage.setOwner(user);
expectedPage.setPageLayout(pageLayout);
expectedPage.setRenderSequence(EXPECTED_RENDER_SEQUENCE);
expectedPage.setRegions(createEmptyRegionList(pageLayout.getNumberOfRegions()));
expect(userService.getAuthenticatedUser()).andReturn(user);
expect(pageLayoutRepository.getByPageLayoutCode(PAGE_LAYOUT_CODE)).andReturn(pageLayout);
expect(pageRepository.save(expectedPage)).andReturn(expectedPage);
expect(pageRepository.getAllPages(user.getId())).andReturn(existingPages);
replay(userService);
replay(pageLayoutRepository);
replay(pageRepository);
Page newPage = pageService.addNewPage(PAGE_NAME, PAGE_LAYOUT_CODE);
assertThat(newPage.getRenderSequence(), is(EXPECTED_RENDER_SEQUENCE));
assertThat(newPage.getName(), is(PAGE_NAME));
assertThat(newPage.getRegions().size(), is(pageLayout.getNumberOfRegions().intValue()));
verify(userService);
verify(pageLayoutRepository);
verify(pageRepository);
}