@Test
  public void testExportImportLayoutsPriorities() throws Exception {
    Layout layout1 = LayoutTestUtil.addLayout(group);
    Layout layout2 = LayoutTestUtil.addLayout(group);
    Layout layout3 = LayoutTestUtil.addLayout(group);

    int priority = layout1.getPriority();

    layout1.setPriority(layout3.getPriority());
    layout3.setPriority(priority);

    layout1 = LayoutLocalServiceUtil.updateLayout(layout1);
    layout3 = LayoutLocalServiceUtil.updateLayout(layout3);

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

    exportImportLayouts(layoutIds, getImportParameterMap());

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

    Assert.assertNotEquals(layout1.getPriority(), importedLayout1.getPriority());

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

    Assert.assertNotEquals(layout2.getPriority(), importedLayout2.getPriority());

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);

    layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);

    exportImportLayouts(layoutIds, getImportParameterMap());

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

    Assert.assertEquals(layout1.getPriority(), importedLayout1.getPriority());

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

    Assert.assertEquals(layout2.getPriority(), importedLayout2.getPriority());

    Layout importedLayout3 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout3.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertEquals(layout3.getPriority(), importedLayout3.getPriority());
  }
  protected Map<String, Layout> getLayoutsMap() throws Exception {
    Map<String, Layout> layouts = new LinkedHashMap<>(4);

    User user = TestPropsValues.getUser();

    layouts.put(_PRIVATE_LAYOUT, LayoutTestUtil.addLayout(group, true));
    layouts.put(_PRIVATE_USER_LAYOUT, LayoutTestUtil.addLayout(user.getGroupId(), true));
    layouts.put(_PUBLIC_LAYOUT, LayoutTestUtil.addLayout(group, false));
    layouts.put(_PUBLIC_USER_LAYOUT, LayoutTestUtil.addLayout(user.getGroupId(), false));

    return layouts;
  }
  @Test
  public void testGetSelectedLayoutsJSONSelectNoLayouts() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(_stagingGroup);

    LayoutTestUtil.addLayout(_stagingGroup, layout.getPlid());

    String selectedLayoutsJSON =
        ExportImportHelperUtil.getSelectedLayoutsJSON(
            _stagingGroup.getGroupId(), false, StringUtil.merge(new long[0]));

    JSONArray selectedLayoutsJSONArray = JSONFactoryUtil.createJSONArray(selectedLayoutsJSON);

    Assert.assertEquals(0, selectedLayoutsJSONArray.length());
  }
  @Before
  public void setUp() throws Exception {
    group = GroupTestUtil.addGroup();
    importedGroup = GroupTestUtil.addGroup();

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

    // Delete and readd to ensure a different layout ID (not ID or UUID).
    // See LPS-32132.

    LayoutLocalServiceUtil.deleteLayout(layout, true, new ServiceContext());

    layout = LayoutTestUtil.addLayout(group.getGroupId(), RandomTestUtil.randomString());
  }
  @Before
  public void setUp() throws Exception {
    _group = GroupTestUtil.addGroup();

    Map<Locale, String> nameMap = new HashMap<>();

    nameMap.put(LocaleUtil.GERMANY, "Zuhause1");
    nameMap.put(LocaleUtil.SPAIN, "Casa1");
    nameMap.put(LocaleUtil.US, "Home1");

    Map<Locale, String> friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.GERMANY, "/zuhause1");
    friendlyURLMap.put(LocaleUtil.SPAIN, "/casa1");
    friendlyURLMap.put(LocaleUtil.US, "/home1");

    _layout1 = LayoutTestUtil.addLayout(_group.getGroupId(), false, nameMap, friendlyURLMap);

    nameMap = new HashMap<>();

    nameMap.put(LocaleUtil.GERMANY, "Zuhause2");
    nameMap.put(LocaleUtil.SPAIN, "Casa2");
    nameMap.put(LocaleUtil.US, "Home2");

    friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.GERMANY, "/zuhause2");
    friendlyURLMap.put(LocaleUtil.SPAIN, "/casa2");
    friendlyURLMap.put(LocaleUtil.US, "/home2");

    _layout2 = LayoutTestUtil.addLayout(_group.getGroupId(), false, nameMap, friendlyURLMap);

    if (_defaultGroup == null) {
      _defaultGroup =
          GroupLocalServiceUtil.getGroup(
              TestPropsValues.getCompanyId(), PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);

      _defaultGrouplayout1 =
          LayoutLocalServiceUtil.fetchFirstLayout(
              _defaultGroup.getGroupId(), false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

      if (_defaultGrouplayout1 == null) {
        _defaultGrouplayout1 = LayoutTestUtil.addLayout(_defaultGroup);
      }

      _defaultGrouplayout2 = LayoutTestUtil.addLayout(_defaultGroup.getGroupId());
    }
  }
  @Test
  public void testFriendlyURLCollision() throws Exception {
    String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());

    Layout layoutA = LayoutTestUtil.addLayout(group);

    String friendlyURLA = layoutA.getFriendlyURL();

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(
            layoutA.getUserId(), layoutA.getPlid(), friendlyURLA + "-de", "de");

    Layout layoutB = LayoutTestUtil.addLayout(group);

    String friendlyURLB = layoutB.getFriendlyURL();

    layoutB =
        LayoutLocalServiceUtil.updateFriendlyURL(
            layoutB.getUserId(), layoutB.getPlid(), friendlyURLB + "-de", "de");

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

    exportImportLayouts(layoutIds, getImportParameterMap());

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

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

    layoutB =
        LayoutLocalServiceUtil.updateFriendlyURL(
            layoutB.getUserId(), layoutB.getPlid(), friendlyURLA, defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(
        layoutB.getUserId(), layoutB.getPlid(), friendlyURLA + "-de", "de");

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(
            layoutA.getUserId(), layoutA.getPlid(), friendlyURLB, defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(
        layoutA.getUserId(), layoutA.getPlid(), friendlyURLB + "-de", "de");

    exportImportLayouts(layoutIds, getImportParameterMap());
  }
  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);

    long[] layoutIds = new long[0];

    try {
      exportImportLayouts(layoutIds, getImportParameterMap());

      if (expectFailure) {
        Assert.fail();
      }
    } catch (LocaleException le) {
      if (!expectFailure) {
        Assert.fail();
      }
    }
  }
  @Override
  protected StagedModel addStagedModel(
      Group group, Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    _layoutSetPrototype = LayoutTestUtil.addLayoutSetPrototype(RandomTestUtil.randomString());

    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(
            _layoutSetPrototype.getGroupId(), true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

    Assert.assertEquals(1, layouts.size());

    Layout layout = layouts.get(0);

    addLayout(LayoutSetPrototype.class, layout);
    addLayoutFriendlyURLs(LayoutSetPrototype.class, layout.getPlid());

    LayoutPrototype layoutPrototype = addLayoutPrototype(dependentStagedModelsMap);

    Layout prototypedLayout =
        LayoutTestUtil.addLayout(_layoutSetPrototype.getGroupId(), true, layoutPrototype, true);

    addLayout(LayoutSetPrototype.class, prototypedLayout);
    addLayoutFriendlyURLs(LayoutSetPrototype.class, prototypedLayout.getPlid());

    return _layoutSetPrototype;
  }
  protected Group addScopeGroup() throws Exception {
    Group group = GroupTestUtil.addGroup();

    Layout layout = LayoutTestUtil.addLayout(group);

    Map<Locale, String> nameMap = new HashMap<>();

    String name = RandomTestUtil.randomString();

    nameMap.put(LocaleUtil.getDefault(), name);

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

    return GroupLocalServiceUtil.addGroup(
        TestPropsValues.getUserId(),
        group.getParentGroupId(),
        Layout.class.getName(),
        layout.getPlid(),
        GroupConstants.DEFAULT_LIVE_GROUP_ID,
        nameMap,
        RandomTestUtil.randomLocaleStringMap(),
        GroupConstants.TYPE_SITE_OPEN,
        true,
        GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
        StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name),
        false,
        true,
        serviceContext);
  }
  @Test
  public void testSuccessfulLayoutLocalPublishing() throws Exception {
    LayoutTestUtil.addLayout(_group, false);

    JournalTestUtil.addArticle(
        _group.getGroupId(), JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID);

    StagingUtil.publishLayouts(
        TestPropsValues.getUserId(),
        _group.getGroupId(),
        _liveGroup.getGroupId(),
        false,
        null,
        _parameterMap);

    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_LAYOUT_EXPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_LAYOUT_EXPORT_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PORTLET_EXPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PORTLET_EXPORT_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PORTLET_IMPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PORTLET_IMPORT_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PUBLICATION_LAYOUT_LOCAL_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PUBLICATION_LAYOUT_LOCAL_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_STAGED_MODEL_EXPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_STAGED_MODEL_EXPORT_SUCCEEDED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_STAGED_MODEL_IMPORT_STARTED));
    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_STAGED_MODEL_IMPORT_SUCCEEDED));
  }
  @Test
  public void testDeleteMissingLayouts() throws Exception {
    Layout layout1 = LayoutTestUtil.addLayout(group);
    Layout layout2 = LayoutTestUtil.addLayout(group);

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);

    long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);

    exportImportLayouts(layoutIds, getImportParameterMap());

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

    LayoutTestUtil.addLayout(importedGroup);

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

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

    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);
  }
  @Test
  public void testExportImportLayouts() throws Exception {
    LayoutTestUtil.addLayout(group);

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);

    exportImportLayouts(ExportImportHelperUtil.getLayoutIds(layouts), getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));
  }
  @Test
  public void testGetSelectedLayoutsJSONSelectChildLayout() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(_stagingGroup);

    Layout childLayout = LayoutTestUtil.addLayout(_stagingGroup, layout.getPlid());

    long[] selectedLayoutIds = new long[] {childLayout.getLayoutId()};

    String selectedLayoutsJSON =
        ExportImportHelperUtil.getSelectedLayoutsJSON(
            _stagingGroup.getGroupId(), false, StringUtil.merge(selectedLayoutIds));

    JSONArray selectedLayoutsJSONArray = JSONFactoryUtil.createJSONArray(selectedLayoutsJSON);

    Assert.assertEquals(1, selectedLayoutsJSONArray.length());

    JSONObject layoutJSONObject = selectedLayoutsJSONArray.getJSONObject(0);

    Assert.assertTrue(layoutJSONObject.getBoolean("includeChildren"));
    Assert.assertEquals(childLayout.getPlid(), layoutJSONObject.getLong("plid"));
  }
  @Ignore
  @Test
  public void testExportLinksToUserLayouts() throws Exception {
    User user = TestPropsValues.getUser();

    Group group = user.getGroup();

    Layout privateLayout = LayoutTestUtil.addLayout(group, true);
    Layout publicLayout = LayoutTestUtil.addLayout(group, false);

    PortletDataContext portletDataContextExport =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            group.getCompanyId(),
            group.getGroupId(),
            new HashMap<String, String[]>(),
            new Date(System.currentTimeMillis() - Time.HOUR),
            new Date(),
            new TestReaderWriter());

    JournalArticle journalArticle =
        JournalTestUtil.addArticle(
            group.getGroupId(), RandomTestUtil.randomString(), RandomTestUtil.randomString());

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

    rootElement.addElement("entry");

    String content =
        replaceLinksToLayoutsParameters(
            getContent("layout_links_user_group.txt"), privateLayout, publicLayout);

    content =
        ExportImportHelperUtil.replaceExportContentReferences(
            portletDataContextExport, journalArticle, content, true);

    assertLinksToLayouts(content, privateLayout, 0);
    assertLinksToLayouts(content, privateLayout, privateLayout.getGroupId());
    assertLinksToLayouts(content, publicLayout, 0);
    assertLinksToLayouts(content, publicLayout, publicLayout.getGroupId());
  }
  @Ignore
  @Test
  public void testImportLinksToLayoutsIdsReplacement() throws Exception {
    LayoutTestUtil.addLayout(_liveGroup, true);
    LayoutTestUtil.addLayout(_liveGroup, false);

    exportImportLayouts(true);
    exportImportLayouts(false);

    Layout importedPrivateLayout =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            _stagingPrivateLayout.getUuid(), _liveGroup.getGroupId(), true);
    Layout importedPublicLayout =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            _stagingPublicLayout.getUuid(), _liveGroup.getGroupId(), false);

    Map<Long, Long> layoutPlids =
        (Map<Long, Long>) _portletDataContextImport.getNewPrimaryKeysMap(Layout.class);

    layoutPlids.put(_stagingPrivateLayout.getPlid(), importedPrivateLayout.getPlid());
    layoutPlids.put(_stagingPublicLayout.getPlid(), importedPublicLayout.getPlid());

    String content = getContent("layout_links_ids_replacement.txt");

    String expectedContent =
        replaceLinksToLayoutsParameters(content, importedPrivateLayout, importedPublicLayout);

    content = replaceLinksToLayoutsParameters(content, _stagingPrivateLayout, _stagingPublicLayout);

    content =
        ExportImportHelperUtil.replaceExportContentReferences(
            _portletDataContextExport, _referrerStagedModel, content, true);

    String importedContent =
        ExportImportHelperUtil.replaceImportContentReferences(
            _portletDataContextImport, _referrerStagedModel, content);

    Assert.assertEquals(expectedContent, importedContent);
  }
  @Override
  protected Map<String, List<StagedModel>> addDependentStagedModelsMap(Group group)
      throws Exception {

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout parentLayout = LayoutTestUtil.addLayout(group);

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, parentLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, parentLayout);

    return dependentStagedModelsMap;
  }
  @Test
  public void testTypeLinkToLayout() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout linkedLayout = LayoutTestUtil.addLayout(stagingGroup);

    List<LayoutFriendlyURL> linkedLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(linkedLayout.getPlid());

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, linkedLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, linkedLayout);

    Layout layout =
        LayoutTestUtil.addTypeLinkToLayoutLayout(
            stagingGroup.getGroupId(), linkedLayout.getLayoutId());

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(layout.getPlid());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        linkedLayout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL linkedLayoutFriendlyURL = linkedLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        linkedLayoutFriendlyURL.getUuid(), liveGroup.getGroupId());

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        layout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        layoutFriendlyURL.getUuid(), liveGroup.getGroupId());
  }
  @Override
  protected StagedModel addStagedModel(
      Group group, Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    List<StagedModel> dependentStagedModels =
        dependentStagedModelsMap.get(Layout.class.getSimpleName());

    Layout parentLayout = (Layout) dependentStagedModels.get(0);

    Layout layout = LayoutTestUtil.addLayout(group, parentLayout.getPlid());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    return layout;
  }
  @Test
  public void testExportImportSelectedLayouts() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(group);

    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);
  }
  protected LayoutRevision getLayoutRevision() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(_group, false);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext();

    LayoutSetBranch layoutSetBranch =
        LayoutSetBranchLocalServiceUtil.addLayoutSetBranch(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            false,
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString(),
            true,
            0,
            serviceContext);

    LayoutBranch layoutBranch =
        LayoutBranchLocalServiceUtil.getMasterLayoutBranch(
            layoutSetBranch.getLayoutSetBranchId(), layout.getPlid());

    return LayoutRevisionLocalServiceUtil.getLayoutRevision(
        layoutSetBranch.getLayoutSetBranchId(), layoutBranch.getLayoutBranchId(), layout.getPlid());
  }
  @Before
  public void setUp() throws Exception {
    _liveGroup = GroupTestUtil.addGroup();
    _stagingGroup = GroupTestUtil.addGroup();

    ServiceContext serviceContext =
        ServiceContextTestUtil.getServiceContext(
            _stagingGroup.getGroupId(), TestPropsValues.getUserId());

    _fileEntry =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            _stagingGroup.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString() + ".txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    ThumbnailCapability thumbnailCapability =
        _fileEntry.getRepositoryCapability(ThumbnailCapability.class);

    _fileEntry = thumbnailCapability.setLargeImageId(_fileEntry, _fileEntry.getFileEntryId());

    TestReaderWriter testReaderWriter = new TestReaderWriter();

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

    Document document = SAXReaderUtil.createDocument();

    Element manifestRootElement = document.addElement("root");

    manifestRootElement.addElement("header");

    testReaderWriter.addEntry("/manifest.xml", document.asXML());

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

    _portletDataContextExport.setExportDataRootElement(rootElement);

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

    _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid());

    _portletDataContextImport =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            _liveGroup.getCompanyId(),
            _liveGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new TestUserIdStrategy(),
            testReaderWriter);

    _portletDataContextImport.setImportDataRootElement(rootElement);

    Element missingReferencesElement = rootElement.addElement("missing-references");

    _portletDataContextImport.setMissingReferencesElement(missingReferencesElement);

    _livePublicLayout = LayoutTestUtil.addLayout(_liveGroup, false);

    _portletDataContextImport.setPlid(_livePublicLayout.getPlid());

    _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId());

    rootElement.addElement("entry");

    _referrerStagedModel =
        JournalTestUtil.addArticle(
            _stagingGroup.getGroupId(),
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString());
  }
  @Test
  public void testTypeLinkToLayout() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout linkedLayout = LayoutTestUtil.addLayout(stagingGroup);

    List<LayoutFriendlyURL> linkedLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(linkedLayout.getPlid());

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, linkedLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, linkedLayout);

    Layout layout =
        LayoutTestUtil.addTypeLinkToLayoutLayout(
            stagingGroup.getGroupId(), linkedLayout.getLayoutId());

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(layout.getPlid());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
        ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_STARTED,
        ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
        PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);

    Layout exportedLinkedLayout = (Layout) readExportedStagedModel(linkedLayout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLinkedLayout);

    ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
        ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_SUCCEEDED,
        ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
        PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        linkedLayout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL linkedLayoutFriendlyURL = linkedLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        linkedLayoutFriendlyURL.getUuid(), liveGroup.getGroupId());

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        layout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        layoutFriendlyURL.getUuid(), liveGroup.getGroupId());
  }