/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.document.library.trash.test;
import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.document.library.kernel.model.DLFolder;
import com.liferay.document.library.kernel.model.DLFolderConstants;
import com.liferay.document.library.kernel.service.DLAppServiceUtil;
import com.liferay.document.library.kernel.service.DLFolderLocalServiceUtil;
import com.liferay.document.library.kernel.service.DLTrashServiceUtil;
import com.liferay.portal.kernel.model.BaseModel;
import com.liferay.portal.kernel.model.ClassedModel;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.repository.model.Folder;
import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.rule.Sync;
import com.liferay.portal.kernel.test.rule.SynchronousDestinationTestRule;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
import com.liferay.portal.kernel.test.util.TestPropsValues;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.security.permission.SimplePermissionChecker;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import com.liferay.registry.ServiceTracker;
import com.liferay.trash.TrashHelper;
import com.liferay.trash.test.util.BaseTrashHandlerTestCase;
import com.liferay.trash.test.util.DefaultWhenIsAssetable;
import com.liferay.trash.test.util.DefaultWhenIsIndexableBaseModel;
import com.liferay.trash.test.util.WhenCanBeDuplicatedInTrash;
import com.liferay.trash.test.util.WhenHasGrandParent;
import com.liferay.trash.test.util.WhenIsAssetable;
import com.liferay.trash.test.util.WhenIsAssetableBaseModel;
import com.liferay.trash.test.util.WhenIsAssetableParentModel;
import com.liferay.trash.test.util.WhenIsIndexableBaseModel;
import com.liferay.trash.test.util.WhenIsMoveableFromTrashBaseModel;
import com.liferay.trash.test.util.WhenIsRestorableBaseModel;
import com.liferay.trash.test.util.WhenIsUpdatableBaseModel;
import com.liferay.trash.test.util.WhenParentModelIsSameType;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runner.RunWith;
/**
* @author Alexander Chow
* @author Eudaldo Alonso
*/
@RunWith(Arquillian.class)
@Sync
public class DLFolderTrashHandlerTest
extends BaseTrashHandlerTestCase
implements WhenCanBeDuplicatedInTrash, WhenHasGrandParent,
WhenIsAssetableBaseModel, WhenIsAssetableParentModel,
WhenIsIndexableBaseModel, WhenIsMoveableFromTrashBaseModel,
WhenIsRestorableBaseModel, WhenIsUpdatableBaseModel,
WhenParentModelIsSameType {
@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new AggregateTestRule(
new LiferayIntegrationTestRule(),
SynchronousDestinationTestRule.INSTANCE);
@BeforeClass
public static void setUpClass() {
Registry registry = RegistryUtil.getRegistry();
_serviceTracker = registry.trackServices(TrashHelper.class.getName());
_serviceTracker.open();
}
@AfterClass
public static void tearDownClass() {
_serviceTracker.close();
}
@Override
public AssetEntry fetchAssetEntry(ClassedModel classedModel)
throws Exception {
return _whenIsAssetable.fetchAssetEntry(classedModel);
}
@Override
public String getBaseModelName(ClassedModel classedModel) {
DLFolder dlFolder = (DLFolder)classedModel;
return dlFolder.getName();
}
@Override
public String getParentBaseModelClassName() {
Class<DLFolder> dlFolderClass = DLFolder.class;
return dlFolderClass.getName();
}
@Override
public String getSearchKeywords() {
return _FOLDER_NAME;
}
@Override
public boolean isAssetEntryVisible(ClassedModel classedModel, long classPK)
throws Exception {
return _whenIsAssetable.isAssetEntryVisible(classedModel, classPK);
}
@Override
public BaseModel<?> moveBaseModelFromTrash(
ClassedModel classedModel, Group group,
ServiceContext serviceContext)
throws Exception {
BaseModel<?> parentBaseModel = getParentBaseModel(
group, serviceContext);
DLTrashServiceUtil.moveFolderFromTrash(
(Long)classedModel.getPrimaryKeyObj(),
(Long)parentBaseModel.getPrimaryKeyObj(), serviceContext);
return parentBaseModel;
}
@Override
public void moveParentBaseModelToTrash(long primaryKey) throws Exception {
DLTrashServiceUtil.moveFolderToTrash(primaryKey);
}
@Override
public int searchBaseModelsCount(Class<?> clazz, long groupId)
throws Exception {
return _whenIsIndexableBaseModel.searchBaseModelsCount(clazz, groupId);
}
@Override
public int searchTrashEntriesCount(
String keywords, ServiceContext serviceContext)
throws Exception {
return _whenIsIndexableBaseModel.searchTrashEntriesCount(
keywords, serviceContext);
}
@Before
@Override
public void setUp() throws Exception {
super.setUp();
_trashHelper = _serviceTracker.getService();
setUpPermissionThreadLocal();
setUpPrincipalThreadLocal();
}
@After
public void tearDown() {
PermissionThreadLocal.setPermissionChecker(_originalPermissionChecker);
PrincipalThreadLocal.setName(_originalName);
}
@Override
public BaseModel<?> updateBaseModel(
long primaryKey, ServiceContext serviceContext)
throws Exception {
DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(primaryKey);
if (serviceContext.getWorkflowAction() ==
WorkflowConstants.ACTION_SAVE_DRAFT) {
dlFolder = DLFolderLocalServiceUtil.updateStatus(
TestPropsValues.getUserId(), primaryKey,
WorkflowConstants.STATUS_DRAFT, null, serviceContext);
}
return dlFolder;
}
@Override
protected BaseModel<?> addBaseModelWithWorkflow(
BaseModel<?> parentBaseModel, ServiceContext serviceContext)
throws Exception {
DLFolder parentDLFolder = (DLFolder)parentBaseModel;
return addBaseModelWithWorkflow(
parentDLFolder.getGroupId(), parentDLFolder.getFolderId());
}
protected BaseModel<?> addBaseModelWithWorkflow(long groupId, long folderId)
throws Exception {
ServiceContext serviceContext =
ServiceContextTestUtil.getServiceContext(
groupId, TestPropsValues.getUserId());
Folder folder = DLAppServiceUtil.addFolder(
groupId, folderId, getSearchKeywords(),
RandomTestUtil.randomString(), serviceContext);
return (DLFolder)folder.getModel();
}
@Override
protected BaseModel<?> addBaseModelWithWorkflow(
ServiceContext serviceContext)
throws Exception {
return addBaseModelWithWorkflow(
serviceContext.getScopeGroupId(),
DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
}
@Override
protected void deleteParentBaseModel(
BaseModel<?> parentBaseModel, boolean includeTrashedEntries)
throws Exception {
DLFolder dlFolder = (DLFolder)parentBaseModel;
DLFolderLocalServiceUtil.deleteFolder(dlFolder.getFolderId(), false);
}
@Override
protected BaseModel<?> getBaseModel(long primaryKey) throws Exception {
return DLFolderLocalServiceUtil.getFolder(primaryKey);
}
@Override
protected Class<?> getBaseModelClass() {
return DLFolder.class;
}
@Override
protected int getNotInTrashBaseModelsCount(BaseModel<?> parentBaseModel)
throws Exception {
DLFolder parentDLFolder = (DLFolder)parentBaseModel;
return DLFolderLocalServiceUtil.getFoldersCount(
parentDLFolder.getGroupId(), parentDLFolder.getFolderId(),
WorkflowConstants.STATUS_APPROVED, false);
}
@Override
protected BaseModel<?> getParentBaseModel(
Group group, long parentBaseModelId, ServiceContext serviceContext)
throws Exception {
Folder folder = DLAppServiceUtil.addFolder(
group.getGroupId(), parentBaseModelId,
RandomTestUtil.randomString(_FOLDER_NAME_MAX_LENGTH),
RandomTestUtil.randomString(), serviceContext);
return (DLFolder)folder.getModel();
}
@Override
protected BaseModel<?> getParentBaseModel(
Group group, ServiceContext serviceContext)
throws Exception {
return getParentBaseModel(
group, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, serviceContext);
}
@Override
protected String getUniqueTitle(BaseModel<?> baseModel) {
DLFolder dlFolder = (DLFolder)baseModel;
String name = dlFolder.getName();
return _trashHelper.getOriginalTitle(name);
}
@Override
protected void moveBaseModelToTrash(long primaryKey) throws Exception {
DLTrashServiceUtil.moveFolderToTrash(primaryKey);
}
protected void setUpPermissionThreadLocal() throws Exception {
_originalPermissionChecker =
PermissionThreadLocal.getPermissionChecker();
PermissionThreadLocal.setPermissionChecker(
new SimplePermissionChecker() {
{
init(TestPropsValues.getUser());
}
@Override
public boolean hasOwnerPermission(
long companyId, String name, String primKey, long ownerId,
String actionId) {
return true;
}
});
}
protected void setUpPrincipalThreadLocal() throws Exception {
_originalName = PrincipalThreadLocal.getName();
PrincipalThreadLocal.setName(TestPropsValues.getUserId());
}
private static final String _FOLDER_NAME = RandomTestUtil.randomString(100);
private static final int _FOLDER_NAME_MAX_LENGTH = 100;
private static ServiceTracker<TrashHelper, TrashHelper> _serviceTracker;
private String _originalName;
private PermissionChecker _originalPermissionChecker;
private TrashHelper _trashHelper;
private final WhenIsAssetable _whenIsAssetable =
new DefaultWhenIsAssetable();
private final WhenIsIndexableBaseModel _whenIsIndexableBaseModel =
new DefaultWhenIsIndexableBaseModel();
}