001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar.test;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
019    import com.liferay.portal.kernel.test.util.GroupTestUtil;
020    import com.liferay.portal.kernel.test.util.TestPropsValues;
021    import com.liferay.portal.kernel.util.DateUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.StagedModel;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.test.LayoutTestUtil;
032    import com.liferay.portlet.asset.model.AssetEntry;
033    import com.liferay.portlet.asset.model.AssetLink;
034    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
035    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
036    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
037    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationSettingsMapFactory;
038    import com.liferay.portlet.exportimport.lar.ExportImportClassedModelUtil;
039    import com.liferay.portlet.exportimport.lar.PortletDataHandlerBoolean;
040    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
041    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
042    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
043    import com.liferay.portlet.exportimport.service.ExportImportServiceUtil;
044    
045    import java.io.File;
046    import java.io.Serializable;
047    
048    import java.util.Date;
049    import java.util.LinkedHashMap;
050    import java.util.Map;
051    
052    import org.junit.After;
053    import org.junit.Assert;
054    import org.junit.Before;
055    
056    /**
057     * @author Eduardo Garcia
058     */
059    public class BaseExportImportTestCase {
060    
061            public void importLayouts(Map<String, String[]> parameterMap)
062                    throws Exception {
063    
064                    User user = TestPropsValues.getUser();
065    
066                    Map<String, Serializable> importLayoutSettingsMap =
067                            ExportImportConfigurationSettingsMapFactory.
068                                    buildImportLayoutSettingsMap(
069                                            user, importedGroup.getGroupId(), false, null,
070                                            parameterMap);
071    
072                    ExportImportConfiguration exportImportConfiguration =
073                            ExportImportConfigurationLocalServiceUtil.
074                                    addExportImportConfiguration(
075                                            user.getUserId(), importedGroup.getGroupId(),
076                                            StringPool.BLANK, StringPool.BLANK,
077                                            ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
078                                            importLayoutSettingsMap, WorkflowConstants.STATUS_DRAFT,
079                                            new ServiceContext());
080    
081                    ExportImportServiceUtil.importLayouts(
082                            exportImportConfiguration, larFile);
083            }
084    
085            @Before
086            public void setUp() throws Exception {
087                    group = GroupTestUtil.addGroup();
088                    importedGroup = GroupTestUtil.addGroup();
089    
090                    layout = LayoutTestUtil.addLayout(group);
091    
092                    // Delete and readd to ensure a different layout ID (not ID or UUID).
093                    // See LPS-32132.
094    
095                    LayoutLocalServiceUtil.deleteLayout(layout, true, new ServiceContext());
096    
097                    layout = LayoutTestUtil.addLayout(group);
098            }
099    
100            @After
101            public void tearDown() throws Exception {
102                    if ((larFile != null) && larFile.exists()) {
103                            FileUtil.delete(larFile);
104                    }
105            }
106    
107            protected AssetLink addAssetLink(
108                            StagedModel sourceStagedModel, StagedModel targetStagedModel,
109                            int weight)
110                    throws PortalException {
111    
112                    AssetEntry originAssetEntry = getAssetEntry(sourceStagedModel);
113                    AssetEntry targetAssetEntry = getAssetEntry(targetStagedModel);
114    
115                    return AssetLinkLocalServiceUtil.addLink(
116                            TestPropsValues.getUserId(), originAssetEntry.getEntryId(),
117                            targetAssetEntry.getEntryId(), 0, weight);
118            }
119    
120            protected void addParameter(
121                    Map<String, String[]> parameterMap, String name, String value) {
122    
123                    parameterMap.put(name, new String[] {value});
124            }
125    
126            protected void addParameter(
127                    Map<String, String[]> parameterMap, String namespace, String name,
128                    boolean value) {
129    
130                    PortletDataHandlerBoolean portletDataHandlerBoolean =
131                            new PortletDataHandlerBoolean(namespace, name);
132    
133                    addParameter(
134                            parameterMap, portletDataHandlerBoolean.getNamespacedControlName(),
135                            String.valueOf(value));
136            }
137    
138            protected StagedModel addStagedModel(long groupId) throws Exception {
139                    return null;
140            }
141    
142            protected StagedModel addStagedModel(long groupId, Date createdDate)
143                    throws Exception {
144    
145                    return null;
146            }
147    
148            protected void deleteStagedModel(StagedModel stagedModel) throws Exception {
149            }
150    
151            protected void exportImportLayouts(
152                            long[] layoutIds, Map<String, String[]> parameterMap)
153                    throws Exception {
154    
155                    exportLayouts(layoutIds, getExportParameterMap());
156    
157                    importLayouts(parameterMap);
158            }
159    
160            protected void exportLayouts(
161                            long[] layoutIds, Map<String, String[]> parameterMap)
162                    throws Exception {
163    
164                    User user = TestPropsValues.getUser();
165    
166                    Map<String, Serializable> exportLayoutSettingsMap =
167                            ExportImportConfigurationSettingsMapFactory.
168                                    buildExportLayoutSettingsMap(
169                                            user, group.getGroupId(), false, layoutIds, parameterMap);
170    
171                    ExportImportConfiguration exportImportConfiguration =
172                            ExportImportConfigurationLocalServiceUtil.
173                                    addDraftExportImportConfiguration(
174                                            user.getUserId(),
175                                            ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
176                                            exportLayoutSettingsMap);
177    
178                    larFile = ExportImportServiceUtil.exportLayoutsAsFile(
179                            exportImportConfiguration);
180            }
181    
182            protected AssetEntry getAssetEntry(StagedModel stagedModel)
183                    throws PortalException {
184    
185                    return AssetEntryLocalServiceUtil.getEntry(
186                            ExportImportClassedModelUtil.getClassName(stagedModel),
187                            ExportImportClassedModelUtil.getClassPK(stagedModel));
188            }
189    
190            protected Map<String, String[]> getExportParameterMap() throws Exception {
191                    Map<String, String[]> parameterMap = new LinkedHashMap<>();
192    
193                    parameterMap.put(
194                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
195                            new String[] {Boolean.TRUE.toString()});
196                    parameterMap.put(
197                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
198                            new String[] {Boolean.TRUE.toString()});
199                    parameterMap.put(
200                            PortletDataHandlerKeys.PORTLET_DATA,
201                            new String[] {Boolean.TRUE.toString()});
202                    parameterMap.put(
203                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
204                            new String[] {Boolean.TRUE.toString()});
205                    parameterMap.put(
206                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
207                            new String[] {Boolean.TRUE.toString()});
208    
209                    return parameterMap;
210            }
211    
212            protected Map<String, String[]> getImportParameterMap() throws Exception {
213                    Map<String, String[]> parameterMap = new LinkedHashMap<>();
214    
215                    parameterMap.put(
216                            PortletDataHandlerKeys.DATA_STRATEGY,
217                            new String[] {
218                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE
219                            });
220                    parameterMap.put(
221                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
222                            new String[] {Boolean.TRUE.toString()});
223                    parameterMap.put(
224                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
225                            new String[] {Boolean.TRUE.toString()});
226                    parameterMap.put(
227                            PortletDataHandlerKeys.PORTLET_DATA,
228                            new String[] {Boolean.TRUE.toString()});
229                    parameterMap.put(
230                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
231                            new String[] {Boolean.TRUE.toString()});
232                    parameterMap.put(
233                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
234                            new String[] {Boolean.TRUE.toString()});
235    
236                    return parameterMap;
237            }
238    
239            @SuppressWarnings("unused")
240            protected StagedModel getStagedModel(String uuid, long groupId)
241                    throws PortalException {
242    
243                    return null;
244            }
245    
246            @SuppressWarnings("unused")
247            protected String getStagedModelUuid(StagedModel stagedModel)
248                    throws PortalException {
249    
250                    return stagedModel.getUuid();
251            }
252    
253            protected void validateImportedStagedModel(
254                            StagedModel stagedModel, StagedModel importedStagedModel)
255                    throws Exception {
256    
257                    Assert.assertTrue(
258                            stagedModel.getCreateDate() + " " +
259                                    importedStagedModel.getCreateDate(),
260                            DateUtil.equals(
261                                    stagedModel.getCreateDate(),
262                                    importedStagedModel.getCreateDate()));
263                    Assert.assertTrue(
264                            stagedModel.getModifiedDate() + " " +
265                                    importedStagedModel.getModifiedDate(),
266                            DateUtil.equals(
267                                    stagedModel.getModifiedDate(),
268                                    importedStagedModel.getModifiedDate()));
269                    Assert.assertEquals(
270                            stagedModel.getUuid(), importedStagedModel.getUuid());
271            }
272    
273            @DeleteAfterTestRun
274            protected Group group;
275    
276            @DeleteAfterTestRun
277            protected Group importedGroup;
278    
279            protected Layout importedLayout;
280            protected File larFile;
281            protected Layout layout;
282    
283    }