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;
016    
017    import com.liferay.portal.LocaleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.lar.ExportImportClassedModelUtil;
020    import com.liferay.portal.kernel.lar.ExportImportDateUtil;
021    import com.liferay.portal.kernel.lar.PortletDataHandler;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
023    import com.liferay.portal.kernel.template.TemplateHandler;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Time;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.Portlet;
031    import com.liferay.portal.model.StagedModel;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.service.LayoutLocalServiceUtil;
034    import com.liferay.portal.service.PortletLocalServiceUtil;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.test.GroupTestUtil;
037    import com.liferay.portal.util.test.LayoutTestUtil;
038    import com.liferay.portal.util.test.RandomTestUtil;
039    import com.liferay.portal.util.test.TestPropsValues;
040    import com.liferay.portlet.PortletPreferencesFactoryUtil;
041    import com.liferay.portlet.asset.model.AssetEntry;
042    import com.liferay.portlet.asset.model.AssetLink;
043    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
044    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046    import com.liferay.portlet.dynamicdatamapping.util.test.DDMTemplateTestUtil;
047    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplate;
048    
049    import java.util.Date;
050    import java.util.HashMap;
051    import java.util.Iterator;
052    import java.util.LinkedHashMap;
053    import java.util.List;
054    import java.util.Locale;
055    import java.util.Map;
056    
057    import javax.portlet.PortletPreferences;
058    
059    import org.junit.Assert;
060    import org.junit.Test;
061    
062    /**
063     * @author Juan Fern??ndez
064     */
065    public abstract class BasePortletExportImportTestCase
066            extends BaseExportImportTestCase {
067    
068            public String getNamespace() {
069                    return null;
070            }
071    
072            public String getPortletId() throws Exception {
073                    return null;
074            }
075    
076            @Test
077            public void testExportImportAssetLinks() throws Exception {
078                    StagedModel stagedModel = addStagedModel(group.getGroupId());
079    
080                    StagedModel relatedStagedModel1 = addStagedModel(group.getGroupId());
081                    StagedModel relatedStagedModel2 = addStagedModel(group.getGroupId());
082    
083                    addAssetLink(stagedModel, relatedStagedModel1, 1);
084                    addAssetLink(stagedModel, relatedStagedModel2, 2);
085    
086                    exportImportPortlet(getPortletId());
087    
088                    StagedModel importedStagedModel = getStagedModel(
089                            getStagedModelUuid(stagedModel), importedGroup.getGroupId());
090    
091                    Assert.assertNotNull(importedStagedModel);
092    
093                    validateImportedLinks(stagedModel, importedStagedModel);
094            }
095    
096            @Test
097            public void testExportImportDeletions() throws Exception {
098                    StagedModel stagedModel = addStagedModel(group.getGroupId());
099    
100                    if (stagedModel == null) {
101                            return;
102                    }
103    
104                    String stagedModelUuid = getStagedModelUuid(stagedModel);
105    
106                    exportImportPortlet(getPortletId());
107    
108                    deleteStagedModel(stagedModel);
109    
110                    exportImportPortlet(getPortletId());
111    
112                    StagedModel importedStagedModel = getStagedModel(
113                            stagedModelUuid, importedGroup.getGroupId());
114    
115                    Assert.assertNotNull(importedStagedModel);
116    
117                    Map<String, String[]> exportParameterMap =
118                            new LinkedHashMap<String, String[]>();
119    
120                    exportParameterMap.put(
121                            PortletDataHandlerKeys.DELETIONS,
122                            new String[] {String.valueOf(true)});
123    
124                    exportImportPortlet(
125                            getPortletId(), exportParameterMap, getImportParameterMap());
126    
127                    importedStagedModel = getStagedModel(
128                            stagedModelUuid, importedGroup.getGroupId());
129    
130                    Assert.assertNotNull(importedStagedModel);
131    
132                    Map<String, String[]> importParameterMap =
133                            new LinkedHashMap<String, String[]>();
134    
135                    importParameterMap.put(
136                            PortletDataHandlerKeys.DELETIONS,
137                            new String[] {String.valueOf(true)});
138    
139                    exportImportPortlet(
140                            getPortletId(), exportParameterMap, importParameterMap);
141    
142                    try {
143                            importedStagedModel = getStagedModel(
144                                    stagedModelUuid, importedGroup.getGroupId());
145    
146                            Assert.assertNull(importedStagedModel);
147                    }
148                    catch (Exception e) {
149                    }
150            }
151    
152            @Test
153            public void testExportImportDisplayStyleFromCurrentGroup()
154                    throws Exception {
155    
156                    testExportImportDisplayStyle(group.getGroupId(), StringPool.BLANK);
157            }
158    
159            @Test
160            public void testExportImportDisplayStyleFromDifferentGroup()
161                    throws Exception {
162    
163                    Group group2 = GroupTestUtil.addGroup();
164    
165                    testExportImportDisplayStyle(group2.getGroupId(), StringPool.BLANK);
166            }
167    
168            @Test
169            public void testExportImportDisplayStyleFromGlobalScope() throws Exception {
170                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
171                            group.getCompanyId());
172    
173                    testExportImportDisplayStyle(companyGroup.getGroupId(), "company");
174            }
175    
176            @Test
177            public void testExportImportDisplayStyleFromLayoutScope() throws Exception {
178                    testExportImportDisplayStyle(group.getGroupId(), "layout");
179            }
180    
181            @Test
182            public void testExportImportInvalidAvailableLocales() throws Exception {
183                    testExportImportAvailableLocales(
184                            new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN},
185                            new Locale[] {LocaleUtil.US, LocaleUtil.GERMANY}, true);
186            }
187    
188            @Test
189            public void testExportImportValidAvailableLocales() throws Exception {
190                    testExportImportAvailableLocales(
191                            new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN},
192                            new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN, LocaleUtil.GERMANY},
193                            false);
194            }
195    
196            @Test
197            public void testUpdateLastPublishDate() throws Exception {
198                    Date lastPublishDate = new Date(System.currentTimeMillis() - Time.HOUR);
199    
200                    Date stagedModelCreationDate = new Date(
201                            lastPublishDate.getTime() + Time.MINUTE);
202    
203                    StagedModel stagedModel = addStagedModel(
204                            group.getGroupId(), stagedModelCreationDate);
205    
206                    if (stagedModel == null) {
207                            return;
208                    }
209    
210                    LayoutTestUtil.addPortletToLayout(
211                            TestPropsValues.getUserId(), layout, getPortletId(), "column-1",
212                            new HashMap<String, String[]>());
213    
214                    PortletPreferences portletPreferences =
215                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
216                                    layout, getPortletId());
217    
218                    portletPreferences.setValue(
219                            "last-publish-date", String.valueOf(lastPublishDate.getTime()));
220    
221                    portletPreferences.store();
222    
223                    Map<String, String[]> exportParameterMap =
224                            new LinkedHashMap<String, String[]>();
225    
226                    exportParameterMap.put(
227                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
228                            new String[] {String.valueOf(true)});
229                    exportParameterMap.put(
230                            "range",
231                            new String[] {ExportImportDateUtil.RANGE_FROM_LAST_PUBLISH_DATE});
232    
233                    Map<String, String[]> importParameterMap =
234                            new LinkedHashMap<String, String[]>();
235    
236                    Date startDate = new Date(
237                            stagedModelCreationDate.getTime() + Time.MINUTE);
238                    Date endDate = new Date();
239    
240                    exportImportPortlet(
241                            getPortletId(), exportParameterMap, importParameterMap, startDate,
242                            endDate);
243    
244                    portletPreferences =
245                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
246                                    layout, getPortletId());
247    
248                    lastPublishDate = ExportImportDateUtil.getLastPublishDate(
249                            portletPreferences);
250    
251                    Assert.assertEquals(endDate.getTime(), lastPublishDate.getTime());
252    
253                    StagedModel importedStagedModel = getStagedModel(
254                            getStagedModelUuid(stagedModel), importedGroup.getGroupId());
255    
256                    Assert.assertNotNull(importedStagedModel);
257            }
258    
259            protected AssetLink addAssetLink(
260                            StagedModel sourceStagedModel, StagedModel targetStagedModel,
261                            int weight)
262                    throws PortalException {
263    
264                    AssetEntry originAssetEntry = getAssetEntry(sourceStagedModel);
265                    AssetEntry targetAssetEntry = getAssetEntry(targetStagedModel);
266    
267                    return AssetLinkLocalServiceUtil.addLink(
268                            TestPropsValues.getUserId(), originAssetEntry.getEntryId(),
269                            targetAssetEntry.getEntryId(), 0, weight);
270            }
271    
272            protected void addParameter(
273                    Map<String, String[]> parameterMap, String name, boolean value) {
274    
275                    addParameter(parameterMap, getNamespace(), name, value);
276            }
277    
278            protected void exportImportPortlet(String portletId) throws Exception {
279                    exportImportPortlet(
280                            portletId, new LinkedHashMap<String, String[]>(),
281                            new LinkedHashMap<String, String[]>());
282            }
283    
284            protected void exportImportPortlet(
285                            String portletId, Map<String, String[]> exportParameterMap,
286                            Map<String, String[]> importParameterMap)
287                    throws Exception {
288    
289                    exportImportPortlet(
290                            portletId, exportParameterMap, importParameterMap, null, null);
291            }
292    
293            protected void exportImportPortlet(
294                            String portletId, Map<String, String[]> exportParameterMap,
295                            Map<String, String[]> importParameterMap, Date startDate,
296                            Date endDate)
297                    throws Exception {
298    
299                    MapUtil.merge(getExportParameterMap(), exportParameterMap);
300    
301                    larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
302                            layout.getPlid(), layout.getGroupId(), portletId,
303                            exportParameterMap, startDate, endDate);
304    
305                    importedLayout = LayoutTestUtil.addLayout(
306                            importedGroup.getGroupId(), RandomTestUtil.randomString());
307    
308                    MapUtil.merge(getImportParameterMap(), importParameterMap);
309    
310                    LayoutLocalServiceUtil.importPortletInfo(
311                            TestPropsValues.getUserId(), importedLayout.getPlid(),
312                            importedGroup.getGroupId(), portletId, importParameterMap, larFile);
313            }
314    
315            protected AssetEntry getAssetEntry(StagedModel stagedModel)
316                    throws PortalException {
317    
318                    return AssetEntryLocalServiceUtil.getEntry(
319                            ExportImportClassedModelUtil.getClassName(stagedModel),
320                            ExportImportClassedModelUtil.getClassPK(stagedModel));
321            }
322    
323            protected PortletPreferences getImportedPortletPreferences(
324                            Map<String, String[]> preferenceMap)
325                    throws Exception {
326    
327                    String portletId = LayoutTestUtil.addPortletToLayout(
328                            TestPropsValues.getUserId(), this.layout, getPortletId(),
329                            "column-1", preferenceMap);
330    
331                    exportImportPortlet(portletId);
332    
333                    return LayoutTestUtil.getPortletPreferences(importedLayout, portletId);
334            }
335    
336            protected void testExportImportAvailableLocales(
337                            Locale[] sourceAvailableLocales, Locale[] targetAvailableLocales,
338                            boolean expectFailure)
339                    throws Exception {
340    
341                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
342                            group.getCompanyId(), getPortletId());
343    
344                    if (portlet == null) {
345                            return;
346                    }
347    
348                    PortletDataHandler portletDataHandler =
349                            portlet.getPortletDataHandlerInstance();
350    
351                    if (!portletDataHandler.isDataLocalized()) {
352                            Assert.assertTrue("This test does not apply", true);
353    
354                            return;
355                    }
356    
357                    GroupTestUtil.updateDisplaySettings(
358                            group.getGroupId(), sourceAvailableLocales, null);
359                    GroupTestUtil.updateDisplaySettings(
360                            importedGroup.getGroupId(), targetAvailableLocales, null);
361    
362                    try {
363                            exportImportPortlet(getPortletId());
364    
365                            if (expectFailure) {
366                                    Assert.fail();
367                            }
368                    }
369                    catch (LocaleException le) {
370                            if (!expectFailure) {
371                                    Assert.fail();
372                            }
373                    }
374            }
375    
376            protected void testExportImportDisplayStyle(
377                            long displayStyleGroupId, String scopeType)
378                    throws Exception {
379    
380                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
381                            group.getCompanyId(), getPortletId());
382    
383                    if (portlet == null) {
384                            return;
385                    }
386    
387                    if (scopeType.equals("layout") && !portlet.isScopeable()) {
388                            Assert.assertTrue("This test does not apply", true);
389    
390                            return;
391                    }
392    
393                    TemplateHandler templateHandler = portlet.getTemplateHandlerInstance();
394    
395                    if (templateHandler == null) {
396                            Assert.assertTrue("This test does not apply", true);
397    
398                            return;
399                    }
400    
401                    String className = templateHandler.getClassName();
402    
403                    DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(
404                            displayStyleGroupId, PortalUtil.getClassNameId(className), 0);
405    
406                    Map<String, String[]> preferenceMap = new HashMap<String, String[]>();
407    
408                    String displayStyle =
409                            PortletDisplayTemplate.DISPLAY_STYLE_PREFIX + ddmTemplate.getUuid();
410    
411                    preferenceMap.put("displayStyle", new String[] {displayStyle});
412    
413                    preferenceMap.put(
414                            "displayStyleGroupId",
415                            new String[] {String.valueOf(ddmTemplate.getGroupId())});
416    
417                    if (scopeType.equals("layout")) {
418                            preferenceMap.put(
419                                    "lfrScopeLayoutUuid", new String[] {this.layout.getUuid()});
420                    }
421    
422                    preferenceMap.put("lfrScopeType", new String[] {scopeType});
423    
424                    PortletPreferences portletPreferences = getImportedPortletPreferences(
425                            preferenceMap);
426    
427                    String importedDisplayStyle = portletPreferences.getValue(
428                            "displayStyle", StringPool.BLANK);
429    
430                    Assert.assertEquals(displayStyle, importedDisplayStyle);
431    
432                    long importedDisplayStyleGroupId = GetterUtil.getLong(
433                            portletPreferences.getValue("displayStyleGroupId", null));
434    
435                    long expectedDisplayStyleGroupId = importedGroup.getGroupId();
436    
437                    if (scopeType.equals("company")) {
438                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
439                                    importedGroup.getCompanyId());
440    
441                            expectedDisplayStyleGroupId = companyGroup.getGroupId();
442                    }
443                    else if (displayStyleGroupId != group.getGroupId()) {
444                            expectedDisplayStyleGroupId = displayStyleGroupId;
445                    }
446    
447                    Assert.assertEquals(
448                            expectedDisplayStyleGroupId, importedDisplayStyleGroupId);
449            }
450    
451            protected void validateImportedLinks(
452                            StagedModel originalStagedModel, StagedModel importedStagedModel)
453                    throws PortalException {
454    
455                    AssetEntry originalAssetEntry = getAssetEntry(originalStagedModel);
456    
457                    List<AssetLink> originalAssetLinks = AssetLinkLocalServiceUtil.getLinks(
458                            originalAssetEntry.getEntryId());
459    
460                    AssetEntry importedAssetEntry = getAssetEntry(importedStagedModel);
461    
462                    List<AssetLink> importedAssetLinks = AssetLinkLocalServiceUtil.getLinks(
463                            importedAssetEntry.getEntryId());
464    
465                    Assert.assertEquals(
466                            originalAssetLinks.size(), importedAssetLinks.size());
467    
468                    for (AssetLink originalLink : originalAssetLinks) {
469                            AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.getEntry(
470                                    originalLink.getEntryId1());
471    
472                            AssetEntry targetAssetEntry = AssetEntryLocalServiceUtil.getEntry(
473                                    originalLink.getEntryId2());
474    
475                            Iterator<AssetLink> iterator = importedAssetLinks.iterator();
476    
477                            while (iterator.hasNext()) {
478                                    AssetLink importedLink = iterator.next();
479    
480                                    AssetEntry importedLinkSourceAssetEntry =
481                                            AssetEntryLocalServiceUtil.getEntry(
482                                                    importedLink.getEntryId1());
483                                    AssetEntry importedLinkTargetAssetEntry =
484                                            AssetEntryLocalServiceUtil.getEntry(
485                                                    importedLink.getEntryId2());
486    
487                                    if (!sourceAssetEntry.getClassUuid().equals(
488                                                    importedLinkSourceAssetEntry.getClassUuid())) {
489    
490                                            continue;
491                                    }
492    
493                                    if (!targetAssetEntry.getClassUuid().equals(
494                                                    importedLinkTargetAssetEntry.getClassUuid())) {
495    
496                                            continue;
497                                    }
498    
499                                    Assert.assertEquals(
500                                            originalLink.getWeight(), importedLink.getWeight());
501                                    Assert.assertEquals(
502                                            originalLink.getType(), importedLink.getType());
503    
504                                    iterator.remove();
505    
506                                    break;
507                            }
508                    }
509    
510                    Assert.assertEquals(0, importedAssetLinks.size());
511            }
512    
513    }