com.liferay.document.library.trash.test.DLFolderTrashHandlerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.document.library.trash.test.DLFolderTrashHandlerTest.java

Source

/**
 * 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.service.ServiceContext;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
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.test.rule.Inject;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.portal.test.rule.PermissionCheckerTestRule;
import com.liferay.trash.TrashHelper;
import com.liferay.trash.exception.RestoreEntryException;
import com.liferay.trash.exception.TrashEntryException;
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.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author Alexander Chow
 * @author Eudaldo Alonso
 */
@RunWith(Arquillian.class)
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(), PermissionCheckerTestRule.INSTANCE);

    @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);
    }

    @Override
    @Test(expected = TrashEntryException.class)
    public void testTrashParentAndBaseModel() throws Exception {
        try {
            super.testTrashParentAndBaseModel();
        } catch (com.liferay.trash.kernel.exception.TrashEntryException tee) {
            throw new TrashEntryException();
        }
    }

    @Override
    @Test(expected = RestoreEntryException.class)
    public void testTrashParentAndRestoreParentAndBaseModel() throws Exception {
        try {
            super.testTrashParentAndRestoreParentAndBaseModel();
        } catch (com.liferay.trash.kernel.exception.RestoreEntryException ree) {
            throw new RestoreEntryException();
        }
    }

    @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);
    }

    private static final String _FOLDER_NAME = RandomTestUtil.randomString(100);

    private static final int _FOLDER_NAME_MAX_LENGTH = 100;

    @Inject
    private TrashHelper _trashHelper;

    private final WhenIsAssetable _whenIsAssetable = new DefaultWhenIsAssetable();
    private final WhenIsIndexableBaseModel _whenIsIndexableBaseModel = new DefaultWhenIsIndexableBaseModel();

}