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.portlet.mobiledevicerules.util.test;
016    
017    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManagerUtil;
018    import com.liferay.portal.kernel.mobile.device.rulegroup.RuleGroupProcessorUtil;
019    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
020    import com.liferay.portal.kernel.mobile.device.rulegroup.rule.RuleHandler;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.util.test.LayoutTestUtil;
023    import com.liferay.portal.util.test.RandomTestUtil;
024    import com.liferay.portal.util.test.ServiceContextTestUtil;
025    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
026    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
027    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
028    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
029    import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
030    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
031    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
032    import com.liferay.portlet.mobiledevicerules.service.MDRRuleLocalServiceUtil;
033    
034    import java.util.Collection;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Mate Thurzo
040     */
041    public class MDRTestUtil {
042    
043            public static MDRAction addAction(long ruleGroupInstanceId)
044                    throws Exception {
045    
046                    Collection<ActionHandler> actionHandlers =
047                            ActionHandlerManagerUtil.getActionHandlers();
048    
049                    Object[] actionHandlersArray = actionHandlers.toArray();
050    
051                    ActionHandler actionHandler = (ActionHandler)actionHandlersArray[0];
052    
053                    return addAction(
054                            ruleGroupInstanceId, RandomTestUtil.randomLocaleStringMap(),
055                            RandomTestUtil.randomLocaleStringMap(), actionHandler.getType(),
056                            null);
057            }
058    
059            public static MDRAction addAction(
060                            long ruleGroupInstanceId, Map<Locale, String> nameMap,
061                            Map<Locale, String> descriptionMap, String type,
062                            String typeSettings)
063                    throws Exception {
064    
065                    return MDRActionLocalServiceUtil.addAction(
066                            ruleGroupInstanceId, nameMap, descriptionMap, type, typeSettings,
067                            ServiceContextTestUtil.getServiceContext());
068            }
069    
070            public static MDRRule addRule(long ruleGroupId) throws Exception {
071                    Collection<RuleHandler> ruleHandlers =
072                            RuleGroupProcessorUtil.getRuleHandlers();
073    
074                    Object[] ruleHandlersArray = ruleHandlers.toArray();
075    
076                    RuleHandler ruleHandler = (RuleHandler)ruleHandlersArray[0];
077    
078                    return addRule(
079                            ruleGroupId, RandomTestUtil.randomLocaleStringMap(),
080                            RandomTestUtil.randomLocaleStringMap(), ruleHandler.getType(),
081                            null);
082            }
083    
084            public static MDRRule addRule(
085                            long ruleGroupId, Map<Locale, String> nameMap,
086                            Map<Locale, String> descriptionMap, String type,
087                            String typeSettings)
088                    throws Exception {
089    
090                    return MDRRuleLocalServiceUtil.addRule(
091                            ruleGroupId, nameMap, descriptionMap, type, typeSettings,
092                            ServiceContextTestUtil.getServiceContext());
093            }
094    
095            public static MDRRuleGroup addRuleGroup(long groupId) throws Exception {
096                    return addRuleGroup(
097                            groupId, RandomTestUtil.randomLocaleStringMap(),
098                            RandomTestUtil.randomLocaleStringMap());
099            }
100    
101            public static MDRRuleGroup addRuleGroup(
102                            long groupId, Map<Locale, String> nameMap,
103                            Map<Locale, String> descriptionMap)
104                    throws Exception {
105    
106                    return MDRRuleGroupLocalServiceUtil.addRuleGroup(
107                            groupId, nameMap, descriptionMap,
108                            ServiceContextTestUtil.getServiceContext(groupId));
109            }
110    
111            public static MDRRuleGroupInstance addRuleGroupInstance(
112                            long groupId, long ruleGroupId)
113                    throws Exception {
114    
115                    Layout layout = LayoutTestUtil.addLayout(
116                            groupId, RandomTestUtil.randomString());
117    
118                    return addRuleGroupInstance(
119                            groupId, Layout.class.getName(), layout.getPlid(), ruleGroupId);
120            }
121    
122            public static MDRRuleGroupInstance addRuleGroupInstance(
123                            long groupId, String className, long classPK, long ruleGroupId)
124                    throws Exception {
125    
126                    return MDRRuleGroupInstanceLocalServiceUtil.addRuleGroupInstance(
127                            groupId, className, classPK, ruleGroupId,
128                            ServiceContextTestUtil.getServiceContext(groupId));
129            }
130    
131    }