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.LocaleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateManager;
020    import com.liferay.portal.kernel.template.TemplateHandler;
021    import com.liferay.portal.kernel.test.util.GroupTestUtil;
022    import com.liferay.portal.kernel.test.util.TestPropsValues;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Time;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.Portlet;
032    import com.liferay.portal.model.StagedModel;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.service.PortletLocalServiceUtil;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.test.LayoutTestUtil;
039    import com.liferay.portlet.PortletPreferencesFactoryUtil;
040    import com.liferay.portlet.asset.model.AssetEntry;
041    import com.liferay.portlet.asset.model.AssetLink;
042    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
043    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatamapping.DDMTemplate;
045    import com.liferay.portlet.dynamicdatamapping.util.test.DDMTemplateTestUtil;
046    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
047    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationSettingsMapFactory;
048    import com.liferay.portlet.exportimport.lar.ExportImportDateUtil;
049    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
050    import com.liferay.portlet.exportimport.lar.PortletDataHandler;
051    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
052    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
053    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
054    import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
055    
056    import java.io.Serializable;
057    
058    import java.util.Arrays;
059    import java.util.Collection;
060    import java.util.Date;
061    import java.util.HashMap;
062    import java.util.Iterator;
063    import java.util.LinkedHashMap;
064    import java.util.List;
065    import java.util.Locale;
066    import java.util.Map;
067    
068    import javax.portlet.PortletPreferences;
069    
070    import org.junit.Assert;
071    import org.junit.Test;
072    
073    /**
074     * @author Juan Fern??ndez
075     */
076    public abstract class BasePortletExportImportTestCase
077            extends BaseExportImportTestCase {
078    
079            public String getNamespace() {
080                    return null;
081            }
082    
083            public String getPortletId() throws Exception {
084                    return null;
085            }
086    
087            @Test
088            public void testExportImportAssetLinks() throws Exception {
089                    StagedModel stagedModel = addStagedModel(group.getGroupId());
090    
091                    StagedModel relatedStagedModel1 = addStagedModel(group.getGroupId());
092                    StagedModel relatedStagedModel2 = addStagedModel(group.getGroupId());
093    
094                    addAssetLink(stagedModel, relatedStagedModel1, 1);
095                    addAssetLink(stagedModel, relatedStagedModel2, 2);
096    
097                    exportImportPortlet(getPortletId());
098    
099                    StagedModel importedStagedModel = getStagedModel(
100                            getStagedModelUuid(stagedModel), importedGroup.getGroupId());
101    
102                    Assert.assertNotNull(importedStagedModel);
103    
104                    validateImportedLinks(stagedModel, importedStagedModel);
105            }
106    
107            @Test
108            public void testExportImportDeletions() throws Exception {
109                    StagedModel stagedModel = addStagedModel(group.getGroupId());
110    
111                    if (stagedModel == null) {
112                            return;
113                    }
114    
115                    String stagedModelUuid = getStagedModelUuid(stagedModel);
116    
117                    exportImportPortlet(getPortletId());
118    
119                    deleteStagedModel(stagedModel);
120    
121                    exportImportPortlet(getPortletId());
122    
123                    StagedModel importedStagedModel = getStagedModel(
124                            stagedModelUuid, importedGroup.getGroupId());
125    
126                    Assert.assertNotNull(importedStagedModel);
127    
128                    Map<String, String[]> exportParameterMap = new LinkedHashMap<>();
129    
130                    exportParameterMap.put(
131                            PortletDataHandlerKeys.DELETIONS,
132                            new String[] {String.valueOf(true)});
133    
134                    exportImportPortlet(
135                            getPortletId(), exportParameterMap, getImportParameterMap());
136    
137                    importedStagedModel = getStagedModel(
138                            stagedModelUuid, importedGroup.getGroupId());
139    
140                    Assert.assertNotNull(importedStagedModel);
141    
142                    Map<String, String[]> importParameterMap = new LinkedHashMap<>();
143    
144                    importParameterMap.put(
145                            PortletDataHandlerKeys.DELETIONS,
146                            new String[] {String.valueOf(true)});
147    
148                    exportImportPortlet(
149                            getPortletId(), exportParameterMap, importParameterMap);
150    
151                    try {
152                            importedStagedModel = getStagedModel(
153                                    stagedModelUuid, importedGroup.getGroupId());
154    
155                            Assert.assertNull(importedStagedModel);
156                    }
157                    catch (Exception e) {
158                    }
159            }
160    
161            @Test
162            public void testExportImportDisplayStyleFromCurrentGroup()
163                    throws Exception {
164    
165                    testExportImportDisplayStyle(group.getGroupId(), StringPool.BLANK);
166            }
167    
168            @Test
169            public void testExportImportDisplayStyleFromDifferentGroup()
170                    throws Exception {
171    
172                    Group group2 = GroupTestUtil.addGroup();
173    
174                    testExportImportDisplayStyle(group2.getGroupId(), StringPool.BLANK);
175            }
176    
177            @Test
178            public void testExportImportDisplayStyleFromGlobalScope() throws Exception {
179                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
180                            group.getCompanyId());
181    
182                    testExportImportDisplayStyle(companyGroup.getGroupId(), "company");
183            }
184    
185            @Test
186            public void testExportImportDisplayStyleFromLayoutScope() throws Exception {
187                    testExportImportDisplayStyle(group.getGroupId(), "layout");
188            }
189    
190            @Test
191            public void testExportImportInvalidAvailableLocales() throws Exception {
192                    testExportImportAvailableLocales(
193                            Arrays.asList(LocaleUtil.US, LocaleUtil.SPAIN),
194                            Arrays.asList(LocaleUtil.US, LocaleUtil.GERMANY), true);
195            }
196    
197            @Test
198            public void testExportImportValidAvailableLocales() throws Exception {
199                    testExportImportAvailableLocales(
200                            Arrays.asList(LocaleUtil.US, LocaleUtil.SPAIN),
201                            Arrays.asList(LocaleUtil.US, LocaleUtil.SPAIN, LocaleUtil.GERMANY),
202                            false);
203            }
204    
205            @Test
206            public void testUpdateLastPublishDate() throws Exception {
207                    Date lastPublishDate = new Date(System.currentTimeMillis() - Time.HOUR);
208    
209                    Date stagedModelCreationDate = new Date(
210                            lastPublishDate.getTime() + Time.MINUTE);
211    
212                    StagedModel stagedModel = addStagedModel(
213                            group.getGroupId(), stagedModelCreationDate);
214    
215                    if (stagedModel == null) {
216                            return;
217                    }
218    
219                    LayoutTestUtil.addPortletToLayout(
220                            TestPropsValues.getUserId(), layout, getPortletId(), "column-1",
221                            new HashMap<String, String[]>());
222    
223                    PortletPreferences portletPreferences =
224                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
225                                    layout, getPortletId());
226    
227                    portletPreferences.setValue(
228                            "last-publish-date", String.valueOf(lastPublishDate.getTime()));
229    
230                    portletPreferences.store();
231    
232                    Map<String, String[]> exportParameterMap = new LinkedHashMap<>();
233    
234                    exportParameterMap.put(
235                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
236                            new String[] {String.valueOf(true)});
237                    exportParameterMap.put(
238                            "range",
239                            new String[] {ExportImportDateUtil.RANGE_FROM_LAST_PUBLISH_DATE});
240    
241                    Map<String, String[]> importParameterMap = new LinkedHashMap<>();
242    
243                    portletPreferences =
244                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
245                                    layout, getPortletId());
246    
247                    Date oldLastPublishDate = ExportImportDateUtil.getLastPublishDate(
248                            portletPreferences);
249    
250                    exportImportPortlet(
251                            getPortletId(), exportParameterMap, importParameterMap);
252    
253                    portletPreferences =
254                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
255                                    layout, getPortletId());
256    
257                    Date newLastPublishDate = ExportImportDateUtil.getLastPublishDate(
258                            portletPreferences);
259    
260                    Assert.assertTrue(newLastPublishDate.after(oldLastPublishDate));
261    
262                    StagedModel importedStagedModel = getStagedModel(
263                            getStagedModelUuid(stagedModel), importedGroup.getGroupId());
264    
265                    Assert.assertNotNull(importedStagedModel);
266            }
267    
268            @Test
269            public void testVersioning1() throws Exception {
270                    if (!isVersioningEnabled()) {
271                            return;
272                    }
273    
274                    StagedModel stagedModel = addStagedModel(group.getGroupId());
275    
276                    addVersion(stagedModel);
277    
278                    exportImportPortlet(getPortletId());
279    
280                    validateVersions();
281            }
282    
283            @Test
284            public void testVersioning2() throws Exception {
285                    if (!isVersioningEnabled()) {
286                            return;
287                    }
288    
289                    StagedModel stagedModel = addStagedModel(group.getGroupId());
290    
291                    Thread.sleep(4000);
292    
293                    exportImportPortlet(getPortletId());
294    
295                    validateVersions();
296    
297                    addVersion(stagedModel);
298    
299                    exportImportPortlet(getPortletId());
300    
301                    validateVersions();
302            }
303    
304            @Test
305            public void testVersioningDeleteFirst() throws Exception {
306                    if (!isVersioningEnabled()) {
307                            return;
308                    }
309    
310                    StagedModel stagedModel = addStagedModel(group.getGroupId());
311    
312                    stagedModel = addVersion(stagedModel);
313    
314                    exportImportPortlet(getPortletId());
315    
316                    validateVersions();
317    
318                    deleteFirstVersion(stagedModel);
319    
320                    exportImportPortlet(getPortletId());
321    
322                    validateVersions();
323            }
324    
325            @Test
326            public void testVersioningDeleteLatest() throws Exception {
327                    if (!isVersioningEnabled()) {
328                            return;
329                    }
330    
331                    StagedModel stagedModel = addStagedModel(group.getGroupId());
332    
333                    stagedModel = addVersion(stagedModel);
334    
335                    exportImportPortlet(getPortletId());
336    
337                    validateVersions();
338    
339                    deleteLatestVersion(stagedModel);
340    
341                    exportImportPortlet(getPortletId());
342    
343                    validateVersions();
344            }
345    
346            @Test
347            public void testVersioningExportImportTwice() throws Exception {
348                    if (!isVersioningEnabled()) {
349                            return;
350                    }
351    
352                    StagedModel stagedModel = addStagedModel(group.getGroupId());
353    
354                    addVersion(stagedModel);
355    
356                    exportImportPortlet(getPortletId());
357    
358                    validateVersions();
359    
360                    exportImportPortlet(getPortletId());
361    
362                    validateVersions();
363            }
364    
365            protected void addParameter(
366                    Map<String, String[]> parameterMap, String name, boolean value) {
367    
368                    addParameter(parameterMap, getNamespace(), name, value);
369            }
370    
371            protected StagedModel addVersion(StagedModel stagedModel) throws Exception {
372                    return null;
373            }
374    
375            protected void deleteFirstVersion(StagedModel stagedModel)
376                    throws Exception {
377            }
378    
379            protected void deleteLatestVersion(StagedModel stagedModel)
380                    throws Exception {
381            }
382    
383            protected void exportImportPortlet(String portletId) throws Exception {
384                    exportImportPortlet(
385                            portletId, new LinkedHashMap<String, String[]>(),
386                            new LinkedHashMap<String, String[]>());
387            }
388    
389            protected void exportImportPortlet(
390                            String portletId, Map<String, String[]> exportParameterMap,
391                            Map<String, String[]> importParameterMap)
392                    throws Exception {
393    
394                    User user = TestPropsValues.getUser();
395    
396                    MapUtil.merge(getExportParameterMap(), exportParameterMap);
397    
398                    Map<String, Serializable> settingsMap =
399                            ExportImportConfigurationSettingsMapFactory.
400                                    buildExportPortletSettingsMap(
401                                            user, layout.getPlid(), layout.getGroupId(), portletId,
402                                            exportParameterMap, StringPool.BLANK);
403    
404                    ExportImportConfiguration exportImportConfiguration =
405                            ExportImportConfigurationLocalServiceUtil.
406                                    addDraftExportImportConfiguration(
407                                            user.getUserId(),
408                                            ExportImportConfigurationConstants.TYPE_EXPORT_PORTLET,
409                                            settingsMap);
410    
411                    ExportImportThreadLocal.setPortletStagingInProcess(true);
412    
413                    try {
414                            larFile = ExportImportLocalServiceUtil.exportPortletInfoAsFile(
415                                    exportImportConfiguration);
416    
417                            importedLayout = LayoutTestUtil.addLayout(importedGroup);
418    
419                            MapUtil.merge(getImportParameterMap(), importParameterMap);
420    
421                            settingsMap =
422                                    ExportImportConfigurationSettingsMapFactory.
423                                            buildImportPortletSettingsMap(
424                                                    user, importedLayout.getPlid(),
425                                                    importedGroup.getGroupId(), portletId,
426                                                    importParameterMap);
427    
428                            exportImportConfiguration =
429                                    ExportImportConfigurationLocalServiceUtil.
430                                            addExportImportConfiguration(
431                                                    user.getUserId(), importedGroup.getGroupId(),
432                                                    StringPool.BLANK, StringPool.BLANK,
433                                                    ExportImportConfigurationConstants.TYPE_IMPORT_PORTLET,
434                                                    settingsMap, WorkflowConstants.STATUS_DRAFT,
435                                                    new ServiceContext());
436    
437                            ExportImportLocalServiceUtil.importPortletDataDeletions(
438                                    exportImportConfiguration, larFile);
439    
440                            ExportImportLocalServiceUtil.importPortletInfo(
441                                    exportImportConfiguration, larFile);
442                    }
443                    finally {
444                            ExportImportThreadLocal.setPortletStagingInProcess(false);
445                    }
446            }
447    
448            protected PortletPreferences getImportedPortletPreferences(
449                            Map<String, String[]> preferenceMap)
450                    throws Exception {
451    
452                    String portletId = LayoutTestUtil.addPortletToLayout(
453                            TestPropsValues.getUserId(), this.layout, getPortletId(),
454                            "column-1", preferenceMap);
455    
456                    exportImportPortlet(portletId);
457    
458                    return LayoutTestUtil.getPortletPreferences(importedLayout, portletId);
459            }
460    
461            protected boolean isVersioningEnabled() {
462                    return false;
463            }
464    
465            protected void testExportImportAvailableLocales(
466                            Collection<Locale> sourceAvailableLocales,
467                            Collection<Locale> targetAvailableLocales, boolean expectFailure)
468                    throws Exception {
469    
470                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
471                            group.getCompanyId(), getPortletId());
472    
473                    if (portlet == null) {
474                            return;
475                    }
476    
477                    PortletDataHandler portletDataHandler =
478                            portlet.getPortletDataHandlerInstance();
479    
480                    if (!portletDataHandler.isDataLocalized()) {
481                            Assert.assertTrue("This test does not apply", true);
482    
483                            return;
484                    }
485    
486                    GroupTestUtil.updateDisplaySettings(
487                            group.getGroupId(), sourceAvailableLocales, null);
488                    GroupTestUtil.updateDisplaySettings(
489                            importedGroup.getGroupId(), targetAvailableLocales, null);
490    
491                    try {
492                            exportImportPortlet(getPortletId());
493    
494                            Assert.assertFalse(expectFailure);
495                    }
496                    catch (LocaleException le) {
497                            Assert.assertTrue(expectFailure);
498                    }
499            }
500    
501            protected void testExportImportDisplayStyle(
502                            long displayStyleGroupId, String scopeType)
503                    throws Exception {
504    
505                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
506                            group.getCompanyId(), getPortletId());
507    
508                    if (portlet == null) {
509                            return;
510                    }
511    
512                    if (scopeType.equals("layout") && !portlet.isScopeable()) {
513                            Assert.assertTrue("This test does not apply", true);
514    
515                            return;
516                    }
517    
518                    TemplateHandler templateHandler = portlet.getTemplateHandlerInstance();
519    
520                    if (templateHandler == null) {
521                            Assert.assertTrue("This test does not apply", true);
522    
523                            return;
524                    }
525    
526                    String className = templateHandler.getClassName();
527                    long resourceClassNameId = PortalUtil.getClassNameId(
528                            "com.liferay.portlet.display.template.PortletDisplayTemplate");
529    
530                    DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(
531                            displayStyleGroupId, PortalUtil.getClassNameId(className), 0,
532                            resourceClassNameId);
533    
534                    Map<String, String[]> preferenceMap = new HashMap<>();
535    
536                    String displayStyle =
537                            PortletDisplayTemplateManager.DISPLAY_STYLE_PREFIX +
538                                    ddmTemplate.getTemplateKey();
539    
540                    preferenceMap.put("displayStyle", new String[] {displayStyle});
541    
542                    preferenceMap.put(
543                            "displayStyleGroupId",
544                            new String[] {String.valueOf(ddmTemplate.getGroupId())});
545    
546                    if (scopeType.equals("layout")) {
547                            preferenceMap.put(
548                                    "lfrScopeLayoutUuid", new String[] {this.layout.getUuid()});
549                    }
550    
551                    preferenceMap.put("lfrScopeType", new String[] {scopeType});
552    
553                    PortletPreferences portletPreferences = getImportedPortletPreferences(
554                            preferenceMap);
555    
556                    String importedDisplayStyle = portletPreferences.getValue(
557                            "displayStyle", StringPool.BLANK);
558    
559                    Assert.assertEquals(displayStyle, importedDisplayStyle);
560    
561                    long importedDisplayStyleGroupId = GetterUtil.getLong(
562                            portletPreferences.getValue("displayStyleGroupId", null));
563    
564                    long expectedDisplayStyleGroupId = importedGroup.getGroupId();
565    
566                    if (scopeType.equals("company")) {
567                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
568                                    importedGroup.getCompanyId());
569    
570                            expectedDisplayStyleGroupId = companyGroup.getGroupId();
571                    }
572                    else if (displayStyleGroupId != group.getGroupId()) {
573                            expectedDisplayStyleGroupId = displayStyleGroupId;
574                    }
575    
576                    Assert.assertEquals(
577                            expectedDisplayStyleGroupId, importedDisplayStyleGroupId);
578            }
579    
580            protected void validateImportedLinks(
581                            StagedModel originalStagedModel, StagedModel importedStagedModel)
582                    throws PortalException {
583    
584                    AssetEntry originalAssetEntry = getAssetEntry(originalStagedModel);
585    
586                    List<AssetLink> originalAssetLinks = AssetLinkLocalServiceUtil.getLinks(
587                            originalAssetEntry.getEntryId());
588    
589                    AssetEntry importedAssetEntry = getAssetEntry(importedStagedModel);
590    
591                    List<AssetLink> importedAssetLinks = AssetLinkLocalServiceUtil.getLinks(
592                            importedAssetEntry.getEntryId());
593    
594                    Assert.assertEquals(
595                            originalAssetLinks.size(), importedAssetLinks.size());
596    
597                    for (AssetLink originalLink : originalAssetLinks) {
598                            AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.getEntry(
599                                    originalLink.getEntryId1());
600    
601                            AssetEntry targetAssetEntry = AssetEntryLocalServiceUtil.getEntry(
602                                    originalLink.getEntryId2());
603    
604                            Iterator<AssetLink> iterator = importedAssetLinks.iterator();
605    
606                            while (iterator.hasNext()) {
607                                    AssetLink importedLink = iterator.next();
608    
609                                    AssetEntry importedLinkSourceAssetEntry =
610                                            AssetEntryLocalServiceUtil.getEntry(
611                                                    importedLink.getEntryId1());
612                                    AssetEntry importedLinkTargetAssetEntry =
613                                            AssetEntryLocalServiceUtil.getEntry(
614                                                    importedLink.getEntryId2());
615    
616                                    if (!Validator.equals(
617                                                    sourceAssetEntry.getClassUuid(),
618                                                    importedLinkSourceAssetEntry.getClassUuid())) {
619    
620                                            continue;
621                                    }
622    
623                                    if (!Validator.equals(
624                                                    targetAssetEntry.getClassUuid(),
625                                                    importedLinkTargetAssetEntry.getClassUuid())) {
626    
627                                            continue;
628                                    }
629    
630                                    Assert.assertEquals(
631                                            originalLink.getWeight(), importedLink.getWeight());
632                                    Assert.assertEquals(
633                                            originalLink.getType(), importedLink.getType());
634    
635                                    iterator.remove();
636    
637                                    break;
638                            }
639                    }
640    
641                    Assert.assertEquals(0, importedAssetLinks.size());
642            }
643    
644            protected void validateVersions() throws Exception {
645            }
646    
647    }