com.liferay.message.boards.trash.test.MBCategoryTrashHandlerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.message.boards.trash.test.MBCategoryTrashHandlerTest.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.message.boards.trash.test;

import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.message.boards.kernel.model.MBCategory;
import com.liferay.message.boards.kernel.model.MBCategoryConstants;
import com.liferay.message.boards.kernel.service.MBCategoryLocalServiceUtil;
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.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.TestPropsValues;
import com.liferay.portal.kernel.util.PortalRunMode;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.service.test.ServiceTestUtil;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.portlet.trash.test.BaseTrashHandlerTestCase;
import com.liferay.portlet.trash.test.WhenHasGrandParent;
import com.liferay.portlet.trash.test.WhenIsMoveableFromTrashBaseModel;
import com.liferay.portlet.trash.test.WhenIsRestorableBaseModel;
import com.liferay.portlet.trash.test.WhenIsUpdatableBaseModel;

import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runner.RunWith;

/**
 * @author Eduardo Garcia
 */
@RunWith(Arquillian.class)
@Sync
public class MBCategoryTrashHandlerTest extends BaseTrashHandlerTestCase implements WhenHasGrandParent,
        WhenIsMoveableFromTrashBaseModel, WhenIsRestorableBaseModel, WhenIsUpdatableBaseModel {

    @ClassRule
    @Rule
    public static final AggregateTestRule aggregateTestRule = new AggregateTestRule(
            new LiferayIntegrationTestRule(), SynchronousDestinationTestRule.INSTANCE);

    @Override
    public String getParentBaseModelClassName() {
        return getBaseModelClass().getName();
    }

    @Override
    public BaseModel<?> moveBaseModelFromTrash(ClassedModel classedModel, Group group,
            ServiceContext serviceContext) throws Exception {

        BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

        MBCategoryLocalServiceUtil.moveCategoryFromTrash(TestPropsValues.getUserId(),
                (Long) classedModel.getPrimaryKeyObj(), (Long) parentBaseModel.getPrimaryKeyObj());

        return parentBaseModel;
    }

    @Override
    public void moveParentBaseModelToTrash(long primaryKey) throws Exception {
        MBCategoryLocalServiceUtil.moveCategoryToTrash(TestPropsValues.getUserId(), primaryKey);
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();

        ServiceTestUtil.setUser(TestPropsValues.getUser());

        _testMode = PortalRunMode.isTestMode();

        PortalRunMode.setTestMode(true);
    }

    @After
    public void tearDown() throws Exception {
        PortalRunMode.setTestMode(_testMode);
    }

    @Override
    public BaseModel<?> updateBaseModel(long primaryKey, ServiceContext serviceContext) throws Exception {

        MBCategory category = MBCategoryLocalServiceUtil.getCategory(primaryKey);

        if (serviceContext.getWorkflowAction() == WorkflowConstants.ACTION_SAVE_DRAFT) {

            category = MBCategoryLocalServiceUtil.updateStatus(TestPropsValues.getUserId(), primaryKey,
                    WorkflowConstants.STATUS_DRAFT);
        }

        return category;
    }

    @Override
    protected BaseModel<?> addBaseModelWithWorkflow(BaseModel<?> parentBaseModel, ServiceContext serviceContext)
            throws Exception {

        MBCategory parentCategory = (MBCategory) parentBaseModel;

        return MBCategoryLocalServiceUtil.addCategory(TestPropsValues.getUserId(), parentCategory.getCategoryId(),
                _TITLE, StringPool.BLANK, serviceContext);
    }

    @Override
    protected BaseModel<?> addBaseModelWithWorkflow(ServiceContext serviceContext) throws Exception {

        return MBCategoryLocalServiceUtil.addCategory(TestPropsValues.getUserId(),
                MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, _TITLE, StringPool.BLANK, serviceContext);
    }

    @Override
    protected void deleteParentBaseModel(BaseModel<?> parentBaseModel, boolean includeTrashedEntries)
            throws Exception {

        MBCategory parentCategory = (MBCategory) parentBaseModel;

        MBCategoryLocalServiceUtil.deleteCategory(parentCategory, false);
    }

    @Override
    protected BaseModel<?> getBaseModel(long primaryKey) throws Exception {
        return MBCategoryLocalServiceUtil.getCategory(primaryKey);
    }

    @Override
    protected Class<?> getBaseModelClass() {
        return MBCategory.class;
    }

    @Override
    protected int getNotInTrashBaseModelsCount(BaseModel<?> parentBaseModel) throws Exception {

        MBCategory parentCategory = (MBCategory) parentBaseModel;

        return MBCategoryLocalServiceUtil.getCategoriesCount(parentCategory.getGroupId(),
                parentCategory.getCategoryId(), WorkflowConstants.STATUS_APPROVED);
    }

    @Override
    protected BaseModel<?> getParentBaseModel(Group group, long parentBaseModelId, ServiceContext serviceContext)
            throws Exception {

        return MBCategoryLocalServiceUtil.addCategory(TestPropsValues.getUserId(), parentBaseModelId, _TITLE,
                StringPool.BLANK, serviceContext);
    }

    @Override
    protected BaseModel<?> getParentBaseModel(Group group, ServiceContext serviceContext) throws Exception {

        return getParentBaseModel(group, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, serviceContext);
    }

    @Override
    protected String getUniqueTitle(BaseModel<?> baseModel) {
        return null;
    }

    @Override
    protected void moveBaseModelToTrash(long primaryKey) throws Exception {
        MBCategoryLocalServiceUtil.moveCategoryToTrash(TestPropsValues.getUserId(), primaryKey);
    }

    private static final String _TITLE = "Title";

    private boolean _testMode;

}