@Test
  public void testSyncMapTemplateToUserMap() {
    // //////////////////////
    // start create a template
    final PortfolioStructure templateEl =
        epStructureManager.createPortfolioMapTemplate(
            ident1, "sync-template-1", "symc-map-template-1");
    epStructureManager.savePortfolioStructure(templateEl);
    // create five pages
    final List<PortfolioStructure> pageRefs = new ArrayList<PortfolioStructure>();
    final List<PortfolioStructure> elementRefs = new ArrayList<PortfolioStructure>();

    for (int i = 0; i < 5; i++) {
      final PortfolioStructure page =
          epFrontendManager.createAndPersistPortfolioPage(
              templateEl, "sync-template-page-" + i, "sync-template-page-" + i);
      pageRefs.add(page);

      for (int j = 0; j < 5; j++) {
        final PortfolioStructure struct =
            epFrontendManager.createAndPersistPortfolioStructureElement(
                page, "template-structure-" + i + "." + j, "template-structure-" + i + "." + j);
        epStructureManager.addCollectRestriction(struct, "Forum", "minimum", 2);
        epStructureManager.savePortfolioStructure(struct);
        elementRefs.add(struct);
      }
    }
    // save the template
    dbInstance.commitAndCloseSession();
    // end create template
    // ////////////////////

    // make the copy
    final PortfolioStructureMap map =
        epFrontendManager.assignStructuredMapToUser(
            ident2, (EPStructuredMapTemplate) templateEl, null, null, null, null);
    dbInstance.commitAndCloseSession();
    assertNotNull(map);

    // ////////////////////////////////
    // shuffle the pages and delete one
    final PortfolioStructure retrievedTemplateEl =
        epFrontendManager.loadPortfolioStructureByKey(templateEl.getKey());
    final List<PortfolioStructure> pages =
        epFrontendManager.loadStructureChildren(retrievedTemplateEl);
    epStructureManager.moveUp(retrievedTemplateEl, pages.get(1));
    epStructureManager.moveDown(retrievedTemplateEl, pages.get(2));
    epStructureManager.removeStructure(retrievedTemplateEl, pages.get(3));
    epStructureManager.savePortfolioStructure(retrievedTemplateEl);
    // shuffle a page
    final List<PortfolioStructure> page1Children =
        epFrontendManager.loadStructureChildren(pages.get(1));
    epStructureManager.moveUp(pages.get(1), page1Children.get(3));
    epStructureManager.moveUp(pages.get(1), page1Children.get(2));
    epStructureManager.moveUp(pages.get(1), page1Children.get(4));
    // and add an element and sub-elements
    final PortfolioStructure newStruct =
        epFrontendManager.createAndPersistPortfolioStructureElement(
            pages.get(1), "new-template-structure-1.6", "template-structure-1.6");
    epStructureManager.addCollectRestriction(pages.get(1), "Forum", "minimum", 2);
    epStructureManager.savePortfolioStructure(newStruct);
    epStructureManager.savePortfolioStructure(pages.get(1));
    for (int k = 0; k < 5; k++) {
      final PortfolioStructure struct =
          epFrontendManager.createAndPersistPortfolioStructureElement(
              newStruct, "new-template-structure-2." + k, "template-structure-2." + k);
      epStructureManager.addCollectRestriction(struct, "bc", "minimum", 2);
      epStructureManager.savePortfolioStructure(struct);
      elementRefs.add(struct);
    }
    dbInstance.commitAndCloseSession();
    // end shuffle the pages
    // ////////////////////////////////

    // //////////////////
    // check the template
    final PortfolioStructure retrievedTemplate2El =
        epFrontendManager.loadPortfolioStructureByKey(templateEl.getKey());
    assertNotNull(retrievedTemplate2El);
    assertTrue(retrievedTemplate2El instanceof EPStructuredMapTemplate);
    final List<PortfolioStructure> retrievedPages2 =
        epFrontendManager.loadStructureChildren(retrievedTemplate2El);
    assertEquals(4, retrievedPages2.size());
    assertEquals(4, ((EPStructuredMapTemplate) retrievedTemplate2El).getInternalChildren().size());
    // check the shuffled pages
    assertEquals(pageRefs.get(1).getKey(), retrievedPages2.get(0).getKey());
    assertEquals(pageRefs.get(0).getKey(), retrievedPages2.get(1).getKey());
    assertEquals(pageRefs.get(2).getKey(), retrievedPages2.get(2).getKey());
    assertEquals(pageRefs.get(4).getKey(), retrievedPages2.get(3).getKey());
    // check added element
    final List<PortfolioStructure> retrievedChildren2 =
        epFrontendManager.loadStructureChildren(retrievedPages2.get(0));
    assertEquals(6, retrievedChildren2.size());

    dbInstance.commitAndCloseSession();
    // check the template
    // //////////////////

    // sync the map
    epFrontendManager.synchronizeStructuredMapToUserCopy(map);
    dbInstance.commitAndCloseSession();

    // ///////////////
    // check the sync
    final PortfolioStructure synchedMap =
        epFrontendManager.loadPortfolioStructureByKey(map.getKey());
    assertNotNull(synchedMap);
    assertTrue(synchedMap instanceof EPStructuredMap);
    final List<PortfolioStructure> synchedPages =
        epFrontendManager.loadStructureChildren(synchedMap);
    assertNotNull(synchedPages);
    assertEquals(4, synchedPages.size());
    assertEquals(
        ((EPStructureElement) pageRefs.get(1)).getTitle(),
        ((EPStructureElement) synchedPages.get(0)).getTitle());
    assertEquals(
        ((EPStructureElement) pageRefs.get(0)).getTitle(),
        ((EPStructureElement) synchedPages.get(1)).getTitle());
    assertEquals(
        ((EPStructureElement) pageRefs.get(2)).getTitle(),
        ((EPStructureElement) synchedPages.get(2)).getTitle());
    assertEquals(
        ((EPStructureElement) pageRefs.get(4)).getTitle(),
        ((EPStructureElement) synchedPages.get(3)).getTitle());

    // check synched key
    assertEquals(
        ((EPStructureElement) pageRefs.get(1)).getKey(),
        ((EPStructureElement) synchedPages.get(0)).getStructureElSource());
    assertEquals(
        ((EPStructureElement) pageRefs.get(0)).getKey(),
        ((EPStructureElement) synchedPages.get(1)).getStructureElSource());
    assertEquals(
        ((EPStructureElement) pageRefs.get(2)).getKey(),
        ((EPStructureElement) synchedPages.get(2)).getStructureElSource());
    assertEquals(
        ((EPStructureElement) pageRefs.get(4)).getKey(),
        ((EPStructureElement) synchedPages.get(3)).getStructureElSource());

    // check the new elements
    final List<PortfolioStructure> retrievedPage1Children =
        epFrontendManager.loadStructureChildren(synchedPages.get(0));
    assertEquals(6, retrievedPage1Children.size());
    final PortfolioStructure retrievedNewStruct = retrievedPage1Children.get(5);
    assertEquals(
        "new-template-structure-1.6", ((EPStructureElement) retrievedNewStruct).getTitle());
    final List<PortfolioStructure> retrievedNewStructChildren =
        epFrontendManager.loadStructureChildren(retrievedNewStruct);
    assertNotNull(retrievedNewStructChildren);
    assertEquals(5, retrievedNewStructChildren.size());
    for (int k = 0; k < 5; k++) {
      assertEquals(
          "new-template-structure-2." + k,
          ((EPStructureElement) retrievedNewStructChildren.get(k)).getTitle());
    }
    // end check the sync
    // //////////////////
  }
  @Test
  public void testAssignMapTemplateToUser() {
    // create a template
    // test save parent and child
    final PortfolioStructure templateEl =
        epStructureManager.createPortfolioMapTemplate(ident1, "template-1", "map-template-1");
    epStructureManager.savePortfolioStructure(templateEl);
    // first page
    final PortfolioStructure page1 =
        epFrontendManager.createAndPersistPortfolioPage(
            templateEl, "template-page-1", "template-page-1");
    // structure element 1 from page 1
    final PortfolioStructure struct11 =
        epFrontendManager.createAndPersistPortfolioStructureElement(
            page1, "template-structure-1.1", "template-structure-1.1");
    epStructureManager.addCollectRestriction(struct11, "Forum", "minimum", 2);
    epStructureManager.savePortfolioStructure(struct11);
    // structure element 2 from page 1
    final PortfolioStructure struct12 =
        epFrontendManager.createAndPersistPortfolioStructureElement(
            page1, "template-structure-1.2", "template-structure-1.2");
    // first page
    final PortfolioStructure page2 =
        epFrontendManager.createAndPersistPortfolioPage(
            templateEl, "template-page-2", "template-page-2");
    // structure element 1 from page 2
    final PortfolioStructure struct21 =
        epFrontendManager.createAndPersistPortfolioStructureElement(
            page2, "template-structure-2.1", "template-structure-2.1");
    epStructureManager.addCollectRestriction(struct21, "bc", "maximum", 4);
    epStructureManager.savePortfolioStructure(struct21);
    // save the template
    dbInstance.commitAndCloseSession();

    // make the copy
    final PortfolioStructureMap map =
        epFrontendManager.assignStructuredMapToUser(
            ident2, (EPStructuredMapTemplate) templateEl, null, null, null, null);
    dbInstance.commitAndCloseSession();
    assertNotNull(map);

    // check the copy
    final PortfolioStructure retrievedMap =
        epFrontendManager.loadPortfolioStructureByKey(map.getKey());
    assertNotNull(retrievedMap);
    assertTrue(retrievedMap instanceof EPStructuredMap);
    assertNotNull(((EPStructuredMap) retrievedMap).getStructuredMapSource());
    assertEquals(
        templateEl.getKey(), ((EPStructuredMap) retrievedMap).getStructuredMapSource().getKey());

    // check pages of the copied map
    final List<PortfolioStructure> pages = epFrontendManager.loadStructureChildren(retrievedMap);
    assertNotNull(pages);
    assertEquals(2, pages.size());
    assertTrue(pages.get(0) instanceof EPPage);
    assertTrue(pages.get(1) instanceof EPPage);
    assertEquals("template-page-1", ((EPStructureElement) pages.get(0)).getTitle());
    assertEquals("template-page-2", ((EPStructureElement) pages.get(1)).getTitle());
    // check root
    assertNotNull(((EPStructureElement) pages.get(0)).getRoot());
    assertEquals(retrievedMap.getKey(), ((EPStructureElement) pages.get(0)).getRoot().getKey());

    // check children of the pages
    final List<PortfolioStructure> structs1 = epFrontendManager.loadStructureChildren(pages.get(0));
    assertNotNull(structs1);
    assertEquals(2, structs1.size());
    assertTrue(structs1.get(0) instanceof EPStructureElement);
    assertTrue(structs1.get(1) instanceof EPStructureElement);
    final EPStructureElement struct11El = (EPStructureElement) structs1.get(0);
    assertEquals("template-structure-1.1", struct11El.getTitle());
    assertEquals("template-structure-1.2", ((EPStructureElement) structs1.get(1)).getTitle());
    // check root
    assertNotNull(((EPStructureElement) structs1.get(0)).getRoot());
    assertEquals(retrievedMap.getKey(), ((EPStructureElement) structs1.get(0)).getRoot().getKey());
    assertNotNull(((EPStructureElement) structs1.get(1)).getRoot());
    assertEquals(retrievedMap.getKey(), ((EPStructureElement) structs1.get(1)).getRoot().getKey());
    // check collect restriction
    assertNotNull(struct11El.getCollectRestrictions());
    assertEquals("Forum", struct11El.getCollectRestrictions().get(0).getArtefactType());
    assertEquals("minimum", struct11El.getCollectRestrictions().get(0).getRestriction());
    assertEquals(2, struct11El.getCollectRestrictions().get(0).getAmount());

    final List<PortfolioStructure> structs2 = epFrontendManager.loadStructureChildren(pages.get(1));
    assertNotNull(structs2);
    assertEquals(1, structs2.size());
    assertTrue(structs2.get(0) instanceof EPStructureElement);
    final EPStructureElement struct21El = (EPStructureElement) structs2.get(0);
    assertEquals("template-structure-2.1", struct21El.getTitle());
    // check root
    assertNotNull(struct21El.getRoot());
    assertEquals(retrievedMap.getKey(), struct21El.getRoot().getKey());
    // check collect restriction
    assertNotNull(struct21El.getCollectRestrictions());
    assertEquals("bc", struct21El.getCollectRestrictions().get(0).getArtefactType());
    assertEquals("maximum", struct21El.getCollectRestrictions().get(0).getRestriction());
    assertEquals(4, struct21El.getCollectRestrictions().get(0).getAmount());
  }