@Test
public void testAddPlaylist() throws Exception {

	Page page1 = pageService.createPage(new Page("p1"));
	Page page2 = pageService.createPage(new Page("p2"));
	Page page3 = pageService.createPage(new Page("p3"));
	Playlist playlist1 = new Playlist("d1");
	playlist1.addPageReference(new PageReference(page1));
	playlist1 = playlistService.createPlaylist(playlist1);
	assertEquals(1, playlistService.getPlaylist(playlist1.getId(), false, false, true).getPageReferences().size());
	Playlist playlist2 = new Playlist("d2");
	playlist2.addPageReference(new PageReference(page2));
	playlist2.addPageReference(new PageReference(page3));
	playlist2 = playlistService.createPlaylist(playlist2);
	assertEquals(2, playlistService.getPlaylist(playlist2.getId(), false, false, true).getPageReferences().size());
	playlist1 = playlistService.addPlaylist(playlist1.getId(), playlist2);
	List<PageReference> pageReferences = getPlaylist(playlist1.getId()).getPageReferences();
	assertEquals(2, pageReferences.size());
	assertEquals(PageRefType.PAGE, pageReferences.get(0).getRefType());
	assertEquals(PageRefType.PLAYLIST, pageReferences.get(1).getRefType());
	// expand must return all pages
	pageReferences = playlistService.getPlaylist(playlist1.getId(), false, true, true).getPageReferences();
	assertEquals(3, pageReferences.size());
	playlist1 = playlistService.addPlaylist(playlist1.getId(), playlist2);
	pageReferences = playlistService.getPlaylist(playlist1.getId(), false, true, true).getPageReferences();
	assertEquals(5, pageReferences.size());
	// recursion must be detected
	Playlist playlist3 = new Playlist("d3");
	playlist3.addPageReference(new PageReference(playlist1));
	playlist3 = playlistService.createPlaylist(playlist3);
	playlist1 = playlistService.addPlaylist(playlist1.getId(), playlist3);
	pageReferences = playlistService.getPlaylist(playlist1.getId(), false, true, true).getPageReferences();
	assertEquals(5, pageReferences.size());
	assertEquals("p1", pageReferences.get(0).getPage().getName());
	assertEquals("p2", pageReferences.get(1).getPage().getName());
	assertEquals("p3", pageReferences.get(2).getPage().getName());
	assertEquals("p2", pageReferences.get(3).getPage().getName());
	assertEquals("p3", pageReferences.get(4).getPage().getName());
	// make sure playlist overview can expand
	List<Playlist> playlists = getPlaylists(false);
	assertEquals(4, playlists.get(0).getPageReferences().size());
	assertEquals(2, playlists.get(1).getPageReferences().size());
	assertEquals(1, playlists.get(2).getPageReferences().size());
	playlists = getPlaylists(true);
	assertEquals(5, playlists.get(0).getPageReferences().size());
	assertEquals(2, playlists.get(1).getPageReferences().size());
	assertEquals(5, playlists.get(2).getPageReferences().size());
	// make sure screen API always expands
	Screen screen = new Screen("s1", playlist1);
	screen.setKey("sk1");
	screen = screenService.createScreen(screen);
	assertEquals(5, screen.getPlaylist().getPageReferences().size());
	assertEquals(5, screenService.getScreen(screen.getId(), false, false).getPlaylist().getPageReferences().size());
	assertEquals(5, screenService.getScreenByKey("sk1", false, false, false, null).getPlaylist().getPageReferences()
			.size());
	assertEquals(5, screenService.getScreens(null).get(0).getPlaylist().getPageReferences().size());
	// make sure update afterward expanding is not broken
	screen = screenService.addOwners(screen.getId(), new Owner[] { new Owner(new User("i1")) });
	assertEquals(5, screenService.getScreen(screen.getId(), false, false).getPlaylist().getPageReferences().size());
}