@Test
public void addNewUserPage_noExistingPages_and_have_template() {
final String PAGE_NAME = "my new page";
final Long EXPECTED_RENDER_SEQUENCE = 1L;
PageTemplate pageTemplate = new PageTemplateImpl();
Page userPage = new PageImpl();
userPage.setName("Page Template");
userPage.setRegions(createEmptyRegionList(pageLayout.getNumberOfRegions()));
Page expectedPage = new PageImpl();
expectedPage.setName(PAGE_NAME);
expectedPage.setOwnerId(user.getId());
expectedPage.setPageLayout(pageLayout);
expectedPage.setRegions(createEmptyRegionList(pageLayout.getNumberOfRegions()));
expectedPage.setPageType(PageType.USER);
PageUser lPageUser = new PageUserImpl(user.getId(), expectedPage, EXPECTED_RENDER_SEQUENCE);
List<PageUser> members = new ArrayList<PageUser>();
members.add(lPageUser);
userPage.setMembers(members);
expect(userService.getAuthenticatedUser()).andReturn(user);
expect(pageLayoutRepository.getByPageLayoutCode(PAGE_LAYOUT_CODE)).andReturn(pageLayout);
expect(pageRepository.getAllPages(user.getId(), PageType.USER)).andReturn(new ArrayList<Page>());
expect(pageRepository.createPageForUser(user, pageTemplate)).andReturn(userPage);
expect(pageTemplateRepository.getDefaultPage(PageType.USER)).andReturn(pageTemplate);
replay(userService, pageLayoutRepository, pageRepository, pageTemplateRepository);
Page newPage = pageService.addNewUserPage(PAGE_NAME, PAGE_LAYOUT_CODE);
assertThat(newPage.getMembers().get(0).getRenderSequence(), is(EXPECTED_RENDER_SEQUENCE));
assertThat(newPage.getName(), is("Page Template"));
assertThat(newPage.getRegions().size(), is(pageLayout.getNumberOfRegions().intValue()));
verify(userService, pageLayoutRepository, pageRepository, pageTemplateRepository);
}