@Before
  public void setUp() throws Exception {
    FinderCacheUtil.clearCache();

    ServiceContextThreadLocal.pushServiceContext(ServiceTestUtil.getServiceContext());

    // Group

    group = GroupTestUtil.addGroup();

    // Global scope article

    Company company = CompanyUtil.fetchByPrimaryKey(group.getCompanyId());

    globalGroupId = company.getGroupId();

    globalJournalArticle =
        JournalTestUtil.addArticle(globalGroupId, "Global Article", "Global Content");

    // Layout prototype

    layoutPrototype = LayoutTestUtil.addLayoutPrototype(ServiceTestUtil.randomString());

    layoutPrototypeLayout = layoutPrototype.getLayout();

    LayoutTestUtil.updateLayoutTemplateId(layoutPrototypeLayout, initialLayoutTemplateId);

    doSetUp();
  }
  @Test
  public void testFriendlyURLCollision() throws Exception {
    String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());

    Layout layoutA = LayoutTestUtil.addLayout(group.getGroupId(), "layoutA");

    layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutA-de", "de");

    Layout layoutB = LayoutTestUtil.addLayout(group.getGroupId(), "layoutB");

    layoutB = LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutB-de", "de");

    long[] layoutIds = {layoutA.getLayoutId(), layoutB.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp", defaultLanguageId);

    layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp-de", "de");

    layoutB =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA", defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA-de", "de");

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB", defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB-de", "de");

    exportImportLayouts(layoutIds, getImportParameterMap());
  }
  @Test
  @Transactional
  public void testExportImportLayoutSetPrototypeInvalidLARType() throws Exception {

    // Import LayoutSetPrototype to LayoutSet

    LayoutSetPrototype layoutSetPrototype =
        LayoutTestUtil.addLayoutSetPrototype(ServiceTestUtil.randomString());

    group = layoutSetPrototype.getGroup();
    importedGroup = GroupTestUtil.addGroup();

    long[] layoutIds = new long[0];

    try {
      exportImportLayouts(layoutIds, getImportParameterMap());

      Assert.fail();
    } catch (LARTypeException lte) {
    }

    // Import LayoutSetPrototype to LayoutPrototyope

    LayoutPrototype layoutPrototype =
        LayoutTestUtil.addLayoutPrototype(ServiceTestUtil.randomString());

    importedGroup = layoutPrototype.getGroup();

    try {
      exportImportLayouts(layoutIds, getImportParameterMap());

      Assert.fail();
    } catch (LARTypeException lte) {
    }
  }
  protected void doTestPortletPreferencesPropagation(boolean linkEnabled, boolean globalScope)
      throws Exception {

    setLinkEnabled(linkEnabled);

    PortletPreferences layoutSetPrototypePortletPreferences =
        LayoutTestUtil.getPortletPreferences(prototypeLayout, journalContentPortletId);

    MergeLayoutPrototypesThreadLocal.clearMergeComplete();

    layoutSetPrototypePortletPreferences.setValue("articleId", StringPool.BLANK);

    layoutSetPrototypePortletPreferences.setValue("showAvailableLocales", Boolean.FALSE.toString());

    if (globalScope) {
      layoutSetPrototypePortletPreferences.setValue("groupId", String.valueOf(globalGroupId));
      layoutSetPrototypePortletPreferences.setValue("lfrScopeType", "company");
    }

    layoutSetPrototypePortletPreferences.store();

    layout = propagateChanges(layout);

    PortletPreferences portletPreferences =
        LayoutTestUtil.getPortletPreferences(layout, journalContentPortletId);

    if (linkEnabled) {
      if (globalScope) {
        Assert.assertEquals(
            StringPool.BLANK, portletPreferences.getValue("articleId", StringPool.BLANK));
      } else {

        // Changes in preferences of local ids are not propagated

        Assert.assertEquals(
            journalArticle.getArticleId(),
            portletPreferences.getValue("articleId", StringPool.BLANK));
      }

      Assert.assertEquals(
          Boolean.FALSE.toString(),
          portletPreferences.getValue("showAvailableLocales", StringPool.BLANK));
    } else {
      Assert.assertEquals(
          journalArticle.getArticleId(),
          portletPreferences.getValue("articleId", StringPool.BLANK));
    }
  }
  protected Group addScopeGroup() throws Exception {
    Group group = GroupTestUtil.addGroup();

    Layout layout = LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());

    String name = ServiceTestUtil.randomString();

    ServiceContext serviceContext = ServiceTestUtil.getServiceContext(group.getGroupId());

    Group scopeGroup =
        GroupLocalServiceUtil.addGroup(
            TestPropsValues.getUserId(),
            group.getParentGroupId(),
            Layout.class.getName(),
            layout.getPlid(),
            GroupConstants.DEFAULT_LIVE_GROUP_ID,
            name,
            ServiceTestUtil.randomString(),
            GroupConstants.TYPE_SITE_OPEN,
            true,
            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name),
            false,
            true,
            serviceContext);

    return scopeGroup;
  }
  protected void testAvailableLocales(
      Locale[] sourceAvailableLocales, Locale[] targetAvailableLocales, boolean expectFailure)
      throws Exception {

    group = GroupTestUtil.updateDisplaySettings(group.getGroupId(), sourceAvailableLocales, null);
    importedGroup =
        GroupTestUtil.updateDisplaySettings(
            importedGroup.getGroupId(), targetAvailableLocales, null);

    LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());

    long[] layoutIds = new long[0];

    try {
      exportImportLayouts(layoutIds, getImportParameterMap());

      if (expectFailure) {
        Assert.fail();
      }
    } catch (LocaleException le) {
      if (!expectFailure) {
        Assert.fail();
      }
    }
  }
  protected String addJournalContentPortletToLayout(
      long userId, Layout layout, JournalArticle journalArticle, String columnId) throws Exception {

    Map<String, String[]> parameterMap = new HashMap<String, String[]>();

    parameterMap.put("articleId", new String[] {journalArticle.getArticleId()});
    parameterMap.put("groupId", new String[] {String.valueOf(journalArticle.getGroupId())});
    parameterMap.put("showAvailableLocales", new String[] {Boolean.TRUE.toString()});

    return LayoutTestUtil.addPortletToLayout(
        userId, layout, PortletKeys.JOURNAL_CONTENT, columnId, parameterMap);
  }
  @Test
  public void testExportImportLayouts() throws Exception {
    LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());

    long[] layoutIds = new long[0];

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));
  }
  @Test
  public void testGetPortletDataHandlerPortlets() throws Exception {
    LayoutTestUtil.addPortletToLayout(layout, PortletKeys.BOOKMARKS);
    LayoutTestUtil.addPortletToLayout(layout, PortletKeys.LOGIN);

    ServiceContext serviceContext = ServiceTestUtil.getServiceContext(group.getGroupId());

    serviceContext.setAttribute(
        StagingConstants.STAGED_PORTLET + PortletKeys.BOOKMARKS, Boolean.TRUE);

    ServiceContextThreadLocal.pushServiceContext(serviceContext);

    StagingLocalServiceUtil.enableLocalStaging(
        TestPropsValues.getUserId(), group, false, false, serviceContext);

    Group stagingGroup = group.getStagingGroup();

    Layout stagingLayout =
        LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
            layout.getUuid(), stagingGroup.getGroupId(), layout.isPrivateLayout());

    List<Layout> layouts = new ArrayList<Layout>();

    layouts.add(stagingLayout);

    List<Portlet> portlets = LayoutExporter.getPortletDataHandlerPortlets(layouts);

    Assert.assertEquals(2, portlets.size());

    for (Portlet portlet : portlets) {
      String portletId = portlet.getPortletId();

      if (!portletId.equals(PortletKeys.BOOKMARKS) && !portletId.equals(PortletKeys.LOGIN)) {

        Assert.fail();
      }
    }

    ServiceContextThreadLocal.popServiceContext();
  }
  @Test
  public void testDeleteMissingLayouts() throws Exception {
    Layout layout1 = LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());
    Layout layout2 = LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());

    exportImportLayouts(null, getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    LayoutTestUtil.addLayout(importedGroup.getGroupId(), ServiceTestUtil.randomString());

    Map<String, String[]> parameterMap = getImportParameterMap();

    parameterMap.put(
        PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS, new String[] {Boolean.TRUE.toString()});

    long[] layoutIds = new long[] {layout1.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    Layout importedLayout1 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout1.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout1);

    Layout importedLayout2 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout2.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout2);
  }
  @Before
  public void setUp() throws Exception {

    // Create site template

    FinderCacheUtil.clearCache();

    LayoutSetPrototype layoutSetPrototype =
        LayoutTestUtil.addLayoutSetPrototype(ServiceTestUtil.randomString());

    _layoutSetPrototypeGroup = layoutSetPrototype.getGroup();

    _layoutSetPrototypeLayout =
        LayoutTestUtil.addLayout(
            _layoutSetPrototypeGroup.getGroupId(), ServiceTestUtil.randomString(), true);

    updateLayoutTemplateId(_layoutSetPrototypeLayout, "1_column");

    _layoutSetPrototypeJournalArticle =
        addJournalArticle(_layoutSetPrototypeGroup.getGroupId(), 0, "Test Article", "Test Content");

    _layoutSetPrototypeJournalContentPortletId =
        addJournalContentPortletToLayout(
            TestPropsValues.getUserId(),
            _layoutSetPrototypeLayout,
            _layoutSetPrototypeJournalArticle,
            "column-1");

    // Create site from site template

    _group = GroupTestUtil.addGroup();

    SitesUtil.updateLayoutSetPrototypesLinks(
        _group, layoutSetPrototype.getLayoutSetPrototypeId(), 0, true, true);

    propagateChanges(_group);
  }
  @Test
  public void testExportImportSelectedLayouts() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(group.getGroupId(), ServiceTestUtil.randomString());

    long[] layoutIds = new long[] {layout.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        layoutIds.length, LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    importedLayout =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout);
  }
  @Override
  public void setUp() throws Exception {
    super.setUp();

    layout = LayoutTestUtil.addLayout(stagingGroup.getGroupId(), ServiceTestUtil.randomString());

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setUuid(layout.getUuid());

    LayoutLocalServiceUtil.addLayout(
        TestPropsValues.getUserId(),
        liveGroup.getGroupId(),
        layout.getPrivateLayout(),
        layout.getParentLayoutId(),
        layout.getName(),
        layout.getTitle(),
        layout.getDescription(),
        layout.getType(),
        layout.getHidden(),
        layout.getFriendlyURL(),
        serviceContext);
  }
  @Before
  public void setUp() throws Exception {
    FinderCacheUtil.clearCache();

    _liveGroup = GroupTestUtil.addGroup();
    _stagingGroup = GroupTestUtil.addGroup();

    _fileEntry =
        DLAppTestUtil.addFileEntry(
            _stagingGroup.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            ServiceTestUtil.randomString() + ".txt",
            ServiceTestUtil.randomString(),
            true);

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) _fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    dlFileEntry.setLargeImageId(dlFileEntry.getFileEntryId());

    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    TestReaderWriter testReaderWriter = new TestReaderWriter();

    _portletDataContextExport =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            _stagingGroup.getCompanyId(),
            _stagingGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new Date(System.currentTimeMillis() - Time.HOUR),
            new Date(),
            testReaderWriter);

    Element rootElement = SAXReaderUtil.createElement("root");

    _portletDataContextExport.setExportDataRootElement(rootElement);

    _stagingPrivateLayout =
        LayoutTestUtil.addLayout(_stagingGroup.getGroupId(), ServiceTestUtil.randomString(), true);
    _stagingPublicLayout =
        LayoutTestUtil.addLayout(_stagingGroup.getGroupId(), ServiceTestUtil.randomString(), false);

    _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid());

    _portletDataContextImport =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            _stagingGroup.getCompanyId(),
            _stagingGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new CurrentUserIdStrategy(TestPropsValues.getUser()),
            testReaderWriter);

    _portletDataContextImport.setImportDataRootElement(rootElement);

    _livePublicLayout =
        LayoutTestUtil.addLayout(_liveGroup.getGroupId(), ServiceTestUtil.randomString(), false);

    _portletDataContextImport.setPlid(_livePublicLayout.getPlid());

    _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId());

    rootElement.addElement("entry");

    _referrerStagedModel =
        JournalTestUtil.addArticle(
            _stagingGroup.getGroupId(),
            ServiceTestUtil.randomString(),
            ServiceTestUtil.randomString());
  }
  protected void doTestLayoutTypePropagation(boolean linkEnabled) throws Exception {

    setLinkEnabled(linkEnabled);

    int initialPortletCount = LayoutTestUtil.getPortlets(layout).size();

    prototypeLayout = LayoutTestUtil.updateLayoutTemplateId(prototypeLayout, "1_column");

    LayoutTestUtil.updateLayoutColumnCustomizable(prototypeLayout, "column-1", true);

    addJournalContentPortletToLayout(
        TestPropsValues.getUserId(), prototypeLayout, globalJournalArticle, "column-1");

    if (linkEnabled) {
      Assert.assertEquals(initialLayoutTemplateId, LayoutTestUtil.getLayoutTemplateId(layout));

      Assert.assertFalse(LayoutTestUtil.isLayoutColumnCustomizable(layout, "column-1"));

      Assert.assertEquals(initialPortletCount, LayoutTestUtil.getPortlets(layout).size());
    }

    layout = propagateChanges(layout);

    if (linkEnabled) {
      Assert.assertEquals("1_column", LayoutTestUtil.getLayoutTemplateId(layout));

      Assert.assertTrue(LayoutTestUtil.isLayoutColumnCustomizable(layout, "column-1"));

      Assert.assertEquals(initialPortletCount + 1, LayoutTestUtil.getPortlets(layout).size());
    } else {
      Assert.assertEquals(initialLayoutTemplateId, LayoutTestUtil.getLayoutTemplateId(layout));

      Assert.assertFalse(LayoutTestUtil.isLayoutColumnCustomizable(layout, "column-1"));

      Assert.assertEquals(initialPortletCount, LayoutTestUtil.getPortlets(layout).size());
    }
  }