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.util.DateUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.StagedModel;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.test.LayoutTestUtil;
028    import com.liferay.portlet.exportimport.lar.PortletDataHandlerBoolean;
029    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
030    
031    import java.io.File;
032    
033    import java.util.Date;
034    import java.util.LinkedHashMap;
035    import java.util.Map;
036    
037    import org.junit.After;
038    import org.junit.Assert;
039    import org.junit.Before;
040    
041    /**
042     * @author Eduardo Garcia
043     */
044    public class BaseExportImportTestCase {
045    
046            @Before
047            public void setUp() throws Exception {
048                    group = GroupTestUtil.addGroup();
049                    importedGroup = GroupTestUtil.addGroup();
050    
051                    layout = LayoutTestUtil.addLayout(group);
052    
053                    // Delete and readd to ensure a different layout ID (not ID or UUID).
054                    // See LPS-32132.
055    
056                    LayoutLocalServiceUtil.deleteLayout(layout, true, new ServiceContext());
057    
058                    layout = LayoutTestUtil.addLayout(group);
059            }
060    
061            @After
062            public void tearDown() throws Exception {
063                    if ((larFile != null) && larFile.exists()) {
064                            FileUtil.delete(larFile);
065                    }
066            }
067    
068            protected void addParameter(
069                    Map<String, String[]> parameterMap, String name, String value) {
070    
071                    parameterMap.put(name, new String[] {value});
072            }
073    
074            protected void addParameter(
075                    Map<String, String[]> parameterMap, String namespace, String name,
076                    boolean value) {
077    
078                    PortletDataHandlerBoolean portletDataHandlerBoolean =
079                            new PortletDataHandlerBoolean(namespace, name);
080    
081                    addParameter(
082                            parameterMap, portletDataHandlerBoolean.getNamespacedControlName(),
083                            String.valueOf(value));
084            }
085    
086            protected StagedModel addStagedModel(long groupId) throws Exception {
087                    return null;
088            }
089    
090            protected StagedModel addStagedModel(long groupId, Date createdDate)
091                    throws Exception {
092    
093                    return null;
094            }
095    
096            protected void deleteStagedModel(StagedModel stagedModel) throws Exception {
097            }
098    
099            protected Map<String, String[]> getExportParameterMap() throws Exception {
100                    Map<String, String[]> parameterMap = new LinkedHashMap<>();
101    
102                    parameterMap.put(
103                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
104                            new String[] {Boolean.TRUE.toString()});
105                    parameterMap.put(
106                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
107                            new String[] {Boolean.TRUE.toString()});
108                    parameterMap.put(
109                            PortletDataHandlerKeys.PORTLET_DATA,
110                            new String[] {Boolean.TRUE.toString()});
111                    parameterMap.put(
112                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
113                            new String[] {Boolean.TRUE.toString()});
114                    parameterMap.put(
115                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
116                            new String[] {Boolean.TRUE.toString()});
117    
118                    return parameterMap;
119            }
120    
121            protected Map<String, String[]> getImportParameterMap() throws Exception {
122                    Map<String, String[]> parameterMap = new LinkedHashMap<>();
123    
124                    parameterMap.put(
125                            PortletDataHandlerKeys.DATA_STRATEGY,
126                            new String[] {
127                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE
128                            });
129                    parameterMap.put(
130                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
131                            new String[] {Boolean.TRUE.toString()});
132                    parameterMap.put(
133                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
134                            new String[] {Boolean.TRUE.toString()});
135                    parameterMap.put(
136                            PortletDataHandlerKeys.PORTLET_DATA,
137                            new String[] {Boolean.TRUE.toString()});
138                    parameterMap.put(
139                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
140                            new String[] {Boolean.TRUE.toString()});
141                    parameterMap.put(
142                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
143                            new String[] {Boolean.TRUE.toString()});
144    
145                    return parameterMap;
146            }
147    
148            @SuppressWarnings("unused")
149            protected StagedModel getStagedModel(String uuid, long groupId)
150                    throws PortalException {
151    
152                    return null;
153            }
154    
155            @SuppressWarnings("unused")
156            protected String getStagedModelUuid(StagedModel stagedModel)
157                    throws PortalException {
158    
159                    return stagedModel.getUuid();
160            }
161    
162            protected void validateImportedStagedModel(
163                            StagedModel stagedModel, StagedModel importedStagedModel)
164                    throws Exception {
165    
166                    Assert.assertTrue(
167                            stagedModel.getCreateDate() + " " +
168                                    importedStagedModel.getCreateDate(),
169                            DateUtil.equals(
170                                    stagedModel.getCreateDate(),
171                                    importedStagedModel.getCreateDate()));
172                    Assert.assertTrue(
173                            stagedModel.getModifiedDate() + " " +
174                                    importedStagedModel.getModifiedDate(),
175                            DateUtil.equals(
176                                    stagedModel.getModifiedDate(),
177                                    importedStagedModel.getModifiedDate()));
178                    Assert.assertEquals(
179                            stagedModel.getUuid(), importedStagedModel.getUuid());
180            }
181    
182            @DeleteAfterTestRun
183            protected Group group;
184    
185            @DeleteAfterTestRun
186            protected Group importedGroup;
187    
188            protected Layout importedLayout;
189            protected File larFile;
190            protected Layout layout;
191    
192    }