protected File[] exportLayouts(long userGroupId, Map<String, String[]> parameterMap)
      throws PortalException {

    File[] files = new File[2];

    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(userGroupId);

    User user = userLocalService.getUser(GetterUtil.getLong(PrincipalThreadLocal.getName()));

    Group group = userGroup.getGroup();

    if (userGroup.hasPrivateLayouts()) {
      Map<String, Serializable> exportLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildExportLayoutSettingsMap(
              user,
              group.getGroupId(),
              true,
              ExportImportHelperUtil.getAllLayoutIds(group.getGroupId(), true),
              parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
              exportLayoutSettingsMap);

      files[0] = exportImportLocalService.exportLayoutsAsFile(exportImportConfiguration);
    }

    if (userGroup.hasPublicLayouts()) {
      Map<String, Serializable> exportLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildExportLayoutSettingsMap(
              user,
              group.getGroupId(),
              false,
              ExportImportHelperUtil.getAllLayoutIds(group.getGroupId(), false),
              parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
              exportLayoutSettingsMap);

      files[1] = exportImportLocalService.exportLayoutsAsFile(exportImportConfiguration);
    }

    return files;
  }
  protected void exportImportLayouts(boolean privateLayout) throws Exception {
    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(_stagingGroup.getGroupId(), privateLayout);

    User user = TestPropsValues.getUser();

    Map<String, Serializable> publishLayoutLocalSettingsMap =
        ExportImportConfigurationSettingsMapFactory.buildPublishLayoutLocalSettingsMap(
            user,
            _stagingGroup.getGroupId(),
            _liveGroup.getGroupId(),
            privateLayout,
            ExportImportHelperUtil.getLayoutIds(layouts),
            new HashMap<String, String[]>());

    ExportImportConfiguration exportImportConfiguration =
        ExportImportConfigurationLocalServiceUtil.addDraftExportImportConfiguration(
            user.getUserId(),
            ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL,
            publishLayoutLocalSettingsMap);

    File larFile = ExportImportLocalServiceUtil.exportLayoutsAsFile(exportImportConfiguration);

    ExportImportLocalServiceUtil.importLayouts(exportImportConfiguration, larFile);
  }
  protected void importLayouts(
      long userId,
      Map<String, String[]> parameterMap,
      File privateLayoutsFile,
      File publicLayoutsFile)
      throws PortalException {

    User user = userPersistence.findByPrimaryKey(userId);

    long groupId = user.getGroupId();

    if (privateLayoutsFile != null) {
      Map<String, Serializable> importLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildImportLayoutSettingsMap(
              user, groupId, true, null, parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
              importLayoutSettingsMap);

      exportImportLocalService.importLayouts(exportImportConfiguration, privateLayoutsFile);
    }

    if (publicLayoutsFile != null) {
      Map<String, Serializable> importLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildImportLayoutSettingsMap(
              user, groupId, false, null, parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
              importLayoutSettingsMap);

      exportImportLocalService.importLayouts(exportImportConfiguration, publicLayoutsFile);
    }
  }
  @Test
  public void testFailedPortletExport() throws Exception {
    PortletExporter portletExporter = PortletExporter.getInstance();

    long plid = RandomTestUtil.nextLong();

    Map<String, Serializable> settingsMap =
        ExportImportConfigurationSettingsMapFactory.buildExportSettingsMap(
            TestPropsValues.getUserId(),
            plid,
            _group.getGroupId(),
            StringPool.BLANK,
            _parameterMap,
            StringPool.BLANK,
            Locale.US,
            TimeZoneUtil.GMT,
            StringPool.BLANK);

    ExportImportConfiguration exportImportConfiguration =
        ExportImportConfigurationLocalServiceUtil.addExportImportConfiguration(
            TestPropsValues.getUserId(),
            0,
            RandomTestUtil.randomString(),
            StringPool.BLANK,
            ExportImportConfigurationConstants.TYPE_EXPORT_PORTLET,
            settingsMap,
            WorkflowConstants.STATUS_DRAFT,
            ServiceContextTestUtil.getServiceContext());

    try {
      portletExporter.exportPortletInfoAsFile(exportImportConfiguration);

      Assert.fail();
    } catch (NoSuchLayoutException nsle) {
      Assert.assertEquals("No Layout exists with the primary key " + plid, nsle.getMessage());
    }

    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_PORTLET_EXPORT_FAILED));
  }
  @Test
  public void testFailedLayoutExport() throws Exception {
    LayoutExporter layoutExporter = LayoutExporter.getInstance();

    Map<String, Serializable> settingsMap =
        ExportImportConfigurationSettingsMapFactory.buildSettingsMap(
            TestPropsValues.getUserId(),
            0,
            false,
            new long[0],
            _parameterMap,
            Locale.US,
            TimeZoneUtil.GMT);

    ExportImportConfiguration exportImportConfiguration =
        ExportImportConfigurationLocalServiceUtil.addExportImportConfiguration(
            TestPropsValues.getUserId(),
            0,
            RandomTestUtil.randomString(),
            StringPool.BLANK,
            ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
            settingsMap,
            WorkflowConstants.STATUS_DRAFT,
            ServiceContextTestUtil.getServiceContext());

    try {
      layoutExporter.exportLayoutsAsFile(exportImportConfiguration);

      Assert.fail();
    } catch (NoSuchGroupException nsge) {
      Assert.assertEquals("No Group exists with the primary key 0", nsge.getMessage());
    }

    Assert.assertTrue(
        _firedExportImportLifecycleEventsMap.containsKey(
            ExportImportLifecycleConstants.EVENT_LAYOUT_EXPORT_FAILED));
  }