001
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.template.TemplateHandler;
020 import com.liferay.portal.kernel.test.util.GroupTestUtil;
021 import com.liferay.portal.kernel.test.util.TestPropsValues;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.LocaleUtil;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Time;
027 import com.liferay.portal.kernel.workflow.WorkflowConstants;
028 import com.liferay.portal.model.Group;
029 import com.liferay.portal.model.Portlet;
030 import com.liferay.portal.model.StagedModel;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.service.GroupLocalServiceUtil;
033 import com.liferay.portal.service.PortletLocalServiceUtil;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.test.LayoutTestUtil;
037 import com.liferay.portlet.PortletPreferencesFactoryUtil;
038 import com.liferay.portlet.asset.model.AssetEntry;
039 import com.liferay.portlet.asset.model.AssetLink;
040 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
041 import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
042 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
043 import com.liferay.portlet.dynamicdatamapping.util.test.DDMTemplateTestUtil;
044 import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
045 import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationSettingsMapFactory;
046 import com.liferay.portlet.exportimport.lar.ExportImportClassedModelUtil;
047 import com.liferay.portlet.exportimport.lar.ExportImportDateUtil;
048 import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
049 import com.liferay.portlet.exportimport.lar.PortletDataHandler;
050 import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
051 import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
052 import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
053 import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
054 import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplateUtil;
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
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 protected AssetLink addAssetLink(
269 StagedModel sourceStagedModel, StagedModel targetStagedModel,
270 int weight)
271 throws PortalException {
272
273 AssetEntry originAssetEntry = getAssetEntry(sourceStagedModel);
274 AssetEntry targetAssetEntry = getAssetEntry(targetStagedModel);
275
276 return AssetLinkLocalServiceUtil.addLink(
277 TestPropsValues.getUserId(), originAssetEntry.getEntryId(),
278 targetAssetEntry.getEntryId(), 0, weight);
279 }
280
281 protected void addParameter(
282 Map<String, String[]> parameterMap, String name, boolean value) {
283
284 addParameter(parameterMap, getNamespace(), name, value);
285 }
286
287 protected void exportImportPortlet(String portletId) throws Exception {
288 exportImportPortlet(
289 portletId, new LinkedHashMap<String, String[]>(),
290 new LinkedHashMap<String, String[]>());
291 }
292
293 protected void exportImportPortlet(
294 String portletId, Map<String, String[]> exportParameterMap,
295 Map<String, String[]> importParameterMap)
296 throws Exception {
297
298 User user = TestPropsValues.getUser();
299
300 MapUtil.merge(getExportParameterMap(), exportParameterMap);
301
302 Map<String, Serializable> settingsMap =
303 ExportImportConfigurationSettingsMapFactory.buildExportSettingsMap(
304 user.getUserId(), layout.getPlid(), layout.getGroupId(),
305 portletId, exportParameterMap, StringPool.BLANK,
306 user.getLocale(), user.getTimeZone(), StringPool.BLANK);
307
308 ExportImportConfiguration exportImportConfiguration =
309 ExportImportConfigurationLocalServiceUtil.
310 addExportImportConfiguration(
311 user.getUserId(), layout.getGroupId(), StringPool.BLANK,
312 StringPool.BLANK,
313 ExportImportConfigurationConstants.TYPE_EXPORT_PORTLET,
314 settingsMap, WorkflowConstants.STATUS_DRAFT,
315 new ServiceContext());
316
317 ExportImportThreadLocal.setPortletStagingInProcess(true);
318
319 try {
320 larFile = ExportImportLocalServiceUtil.exportPortletInfoAsFile(
321 exportImportConfiguration);
322
323 importedLayout = LayoutTestUtil.addLayout(importedGroup);
324
325 MapUtil.merge(getImportParameterMap(), importParameterMap);
326
327 settingsMap =
328 ExportImportConfigurationSettingsMapFactory.
329 buildImportSettingsMap(
330 user.getUserId(), importedLayout.getPlid(),
331 importedGroup.getGroupId(), portletId,
332 importParameterMap, StringPool.BLANK, user.getLocale(),
333 user.getTimeZone(), StringPool.BLANK);
334
335 exportImportConfiguration =
336 ExportImportConfigurationLocalServiceUtil.
337 addExportImportConfiguration(
338 user.getUserId(), importedGroup.getGroupId(),
339 StringPool.BLANK, StringPool.BLANK,
340 ExportImportConfigurationConstants.TYPE_IMPORT_PORTLET,
341 settingsMap, WorkflowConstants.STATUS_DRAFT,
342 new ServiceContext());
343
344 ExportImportLocalServiceUtil.importPortletDataDeletions(
345 exportImportConfiguration, larFile);
346
347 ExportImportLocalServiceUtil.importPortletInfo(
348 exportImportConfiguration, larFile);
349 }
350 finally {
351 ExportImportThreadLocal.setPortletStagingInProcess(false);
352 }
353 }
354
355 protected AssetEntry getAssetEntry(StagedModel stagedModel)
356 throws PortalException {
357
358 return AssetEntryLocalServiceUtil.getEntry(
359 ExportImportClassedModelUtil.getClassName(stagedModel),
360 ExportImportClassedModelUtil.getClassPK(stagedModel));
361 }
362
363 protected PortletPreferences getImportedPortletPreferences(
364 Map<String, String[]> preferenceMap)
365 throws Exception {
366
367 String portletId = LayoutTestUtil.addPortletToLayout(
368 TestPropsValues.getUserId(), this.layout, getPortletId(),
369 "column-1", preferenceMap);
370
371 exportImportPortlet(portletId);
372
373 return LayoutTestUtil.getPortletPreferences(importedLayout, portletId);
374 }
375
376 protected void testExportImportAvailableLocales(
377 Collection<Locale> sourceAvailableLocales,
378 Collection<Locale> targetAvailableLocales, boolean expectFailure)
379 throws Exception {
380
381 Portlet portlet = PortletLocalServiceUtil.getPortletById(
382 group.getCompanyId(), getPortletId());
383
384 if (portlet == null) {
385 return;
386 }
387
388 PortletDataHandler portletDataHandler =
389 portlet.getPortletDataHandlerInstance();
390
391 if (!portletDataHandler.isDataLocalized()) {
392 Assert.assertTrue("This test does not apply", true);
393
394 return;
395 }
396
397 GroupTestUtil.updateDisplaySettings(
398 group.getGroupId(), sourceAvailableLocales, null);
399 GroupTestUtil.updateDisplaySettings(
400 importedGroup.getGroupId(), targetAvailableLocales, null);
401
402 try {
403 exportImportPortlet(getPortletId());
404
405 Assert.assertFalse(expectFailure);
406 }
407 catch (LocaleException le) {
408 Assert.assertTrue(expectFailure);
409 }
410 }
411
412 protected void testExportImportDisplayStyle(
413 long displayStyleGroupId, String scopeType)
414 throws Exception {
415
416 Portlet portlet = PortletLocalServiceUtil.getPortletById(
417 group.getCompanyId(), getPortletId());
418
419 if (portlet == null) {
420 return;
421 }
422
423 if (scopeType.equals("layout") && !portlet.isScopeable()) {
424 Assert.assertTrue("This test does not apply", true);
425
426 return;
427 }
428
429 TemplateHandler templateHandler = portlet.getTemplateHandlerInstance();
430
431 if (templateHandler == null) {
432 Assert.assertTrue("This test does not apply", true);
433
434 return;
435 }
436
437 String className = templateHandler.getClassName();
438
439 DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(
440 displayStyleGroupId, PortalUtil.getClassNameId(className), 0);
441
442 Map<String, String[]> preferenceMap = new HashMap<>();
443
444 String displayStyle = PortletDisplayTemplateUtil.getDisplayStyle(
445 ddmTemplate.getTemplateKey());
446
447 preferenceMap.put("displayStyle", new String[] {displayStyle});
448
449 preferenceMap.put(
450 "displayStyleGroupId",
451 new String[] {String.valueOf(ddmTemplate.getGroupId())});
452
453 if (scopeType.equals("layout")) {
454 preferenceMap.put(
455 "lfrScopeLayoutUuid", new String[] {this.layout.getUuid()});
456 }
457
458 preferenceMap.put("lfrScopeType", new String[] {scopeType});
459
460 PortletPreferences portletPreferences = getImportedPortletPreferences(
461 preferenceMap);
462
463 String importedDisplayStyle = portletPreferences.getValue(
464 "displayStyle", StringPool.BLANK);
465
466 Assert.assertEquals(displayStyle, importedDisplayStyle);
467
468 long importedDisplayStyleGroupId = GetterUtil.getLong(
469 portletPreferences.getValue("displayStyleGroupId", null));
470
471 long expectedDisplayStyleGroupId = importedGroup.getGroupId();
472
473 if (scopeType.equals("company")) {
474 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
475 importedGroup.getCompanyId());
476
477 expectedDisplayStyleGroupId = companyGroup.getGroupId();
478 }
479 else if (displayStyleGroupId != group.getGroupId()) {
480 expectedDisplayStyleGroupId = displayStyleGroupId;
481 }
482
483 Assert.assertEquals(
484 expectedDisplayStyleGroupId, importedDisplayStyleGroupId);
485 }
486
487 protected void validateImportedLinks(
488 StagedModel originalStagedModel, StagedModel importedStagedModel)
489 throws PortalException {
490
491 AssetEntry originalAssetEntry = getAssetEntry(originalStagedModel);
492
493 List<AssetLink> originalAssetLinks = AssetLinkLocalServiceUtil.getLinks(
494 originalAssetEntry.getEntryId());
495
496 AssetEntry importedAssetEntry = getAssetEntry(importedStagedModel);
497
498 List<AssetLink> importedAssetLinks = AssetLinkLocalServiceUtil.getLinks(
499 importedAssetEntry.getEntryId());
500
501 Assert.assertEquals(
502 originalAssetLinks.size(), importedAssetLinks.size());
503
504 for (AssetLink originalLink : originalAssetLinks) {
505 AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.getEntry(
506 originalLink.getEntryId1());
507
508 AssetEntry targetAssetEntry = AssetEntryLocalServiceUtil.getEntry(
509 originalLink.getEntryId2());
510
511 Iterator<AssetLink> iterator = importedAssetLinks.iterator();
512
513 while (iterator.hasNext()) {
514 AssetLink importedLink = iterator.next();
515
516 AssetEntry importedLinkSourceAssetEntry =
517 AssetEntryLocalServiceUtil.getEntry(
518 importedLink.getEntryId1());
519 AssetEntry importedLinkTargetAssetEntry =
520 AssetEntryLocalServiceUtil.getEntry(
521 importedLink.getEntryId2());
522
523 if (!sourceAssetEntry.getClassUuid().equals(
524 importedLinkSourceAssetEntry.getClassUuid())) {
525
526 continue;
527 }
528
529 if (!targetAssetEntry.getClassUuid().equals(
530 importedLinkTargetAssetEntry.getClassUuid())) {
531
532 continue;
533 }
534
535 Assert.assertEquals(
536 originalLink.getWeight(), importedLink.getWeight());
537 Assert.assertEquals(
538 originalLink.getType(), importedLink.getType());
539
540 iterator.remove();
541
542 break;
543 }
544 }
545
546 Assert.assertEquals(0, importedAssetLinks.size());
547 }
548
549 }