@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());
  }
  protected void validatePrototypedLayouts(Class<?> clazz, long groupId) throws Exception {

    List<Layout> layouts = getLayouts(clazz);

    for (Layout layout : layouts) {
      Layout importedLayout =
          LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
              layout.getUuid(), groupId, layout.getPrivateLayout());

      Assert.assertNotNull(importedLayout);
      Assert.assertEquals(
          layout.getTypeSettingsProperty(
              LayoutSetPrototypeStagedModelDataHandlerTest.class.getName()),
          importedLayout.getTypeSettingsProperty(
              LayoutSetPrototypeStagedModelDataHandlerTest.class.getName()));
    }

    List<LayoutFriendlyURL> layoutFriendlyURLs = getLayoutFriendlyURLs(clazz);

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      LayoutFriendlyURL importedLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURLByUuidAndGroupId(
              layoutFriendlyURL.getUuid(), groupId);

      Assert.assertNotNull(importedLayoutFriendlyURL);
      Assert.assertEquals(
          layoutFriendlyURL.getFriendlyURL(), importedLayoutFriendlyURL.getFriendlyURL());
    }
  }
  protected LayoutFriendlyURL fetchExistingLayoutFriendlyURL(
      PortletDataContext portletDataContext, LayoutFriendlyURL layoutFriendlyURL, long plid) {

    LayoutFriendlyURL existingLayoutFriendlyURL =
        fetchStagedModelByUuidAndGroupId(
            layoutFriendlyURL.getUuid(), portletDataContext.getScopeGroupId());

    if (existingLayoutFriendlyURL == null) {
      existingLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
              plid, layoutFriendlyURL.getLanguageId(), false);
    }

    return existingLayoutFriendlyURL;
  }
  @Override
  protected void validateImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

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

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

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

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        parentLayout.getUuid(), group.getGroupId(), false);

    List<LayoutFriendlyURL> parentLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(parentLayout.getPlid());

    LayoutFriendlyURL parentLayoutFriendlyURL = parentLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        parentLayoutFriendlyURL.getUuid(), group.getGroupId());
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, LayoutFriendlyURL layoutFriendlyURL) throws Exception {

    long userId = portletDataContext.getUserId(layoutFriendlyURL.getUserUuid());

    Map<Long, Long> plids = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Layout.class);

    long plid = MapUtil.getLong(plids, layoutFriendlyURL.getPlid(), layoutFriendlyURL.getPlid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(layoutFriendlyURL);

    LayoutFriendlyURL importedLayoutFriendlyURL = null;

    if (portletDataContext.isDataStrategyMirror()) {
      LayoutFriendlyURL existingLayoutFriendlyURL =
          fetchExistingLayoutFriendlyURL(portletDataContext, layoutFriendlyURL, plid);

      layoutFriendlyURL =
          getUniqueLayoutFriendlyURL(
              portletDataContext, layoutFriendlyURL, existingLayoutFriendlyURL);

      if (existingLayoutFriendlyURL == null) {
        serviceContext.setUuid(layoutFriendlyURL.getUuid());

        importedLayoutFriendlyURL =
            LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
                userId,
                portletDataContext.getCompanyId(),
                portletDataContext.getScopeGroupId(),
                plid,
                portletDataContext.isPrivateLayout(),
                layoutFriendlyURL.getFriendlyURL(),
                layoutFriendlyURL.getLanguageId(),
                serviceContext);
      } else {
        importedLayoutFriendlyURL =
            LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURL(
                userId,
                portletDataContext.getCompanyId(),
                portletDataContext.getScopeGroupId(),
                plid,
                portletDataContext.isPrivateLayout(),
                layoutFriendlyURL.getFriendlyURL(),
                layoutFriendlyURL.getLanguageId(),
                serviceContext);
      }
    } else {
      layoutFriendlyURL = getUniqueLayoutFriendlyURL(portletDataContext, layoutFriendlyURL, null);

      importedLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
              userId,
              portletDataContext.getCompanyId(),
              portletDataContext.getScopeGroupId(),
              plid,
              portletDataContext.isPrivateLayout(),
              layoutFriendlyURL.getFriendlyURL(),
              layoutFriendlyURL.getLanguageId(),
              serviceContext);
    }

    portletDataContext.importClassedModel(layoutFriendlyURL, importedLayoutFriendlyURL);
  }