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.portal.kernel.test.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.Group;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.service.GroupLocalServiceUtil;
021    import com.liferay.portal.service.ServiceContext;
022    
023    /**
024     * @author Manuel de la Pe??a
025     */
026    public class ServiceContextTestUtil {
027    
028            public static ServiceContext getServiceContext() throws PortalException {
029                    return getServiceContext(TestPropsValues.getGroupId());
030            }
031    
032            public static ServiceContext getServiceContext(Group group, long userId) {
033                    return getServiceContext(
034                            group.getCompanyId(), group.getGroupId(), userId);
035            }
036    
037            public static ServiceContext getServiceContext(long groupId)
038                    throws PortalException {
039    
040                    if (groupId == TestPropsValues.getGroupId()) {
041                            return getServiceContext(groupId, TestPropsValues.getUserId());
042                    }
043                    else {
044                            Group group = GroupLocalServiceUtil.getGroup(groupId);
045    
046                            User user = UserTestUtil.getAdminUser(group.getCompanyId());
047    
048                            return getServiceContext(group, user.getUserId());
049                    }
050            }
051    
052            public static ServiceContext getServiceContext(long groupId, long userId)
053                    throws PortalException {
054    
055                    if (groupId == TestPropsValues.getGroupId()) {
056                            return getServiceContext(
057                                    TestPropsValues.getCompanyId(), groupId, userId);
058                    }
059                    else {
060                            Group group = GroupLocalServiceUtil.getGroup(groupId);
061    
062                            return getServiceContext(
063                                    group.getCompanyId(), group.getGroupId(), userId);
064                    }
065            }
066    
067            public static ServiceContext getServiceContext(
068                    long companyId, long groupId, long userId) {
069    
070                    ServiceContext serviceContext = new ServiceContext();
071    
072                    serviceContext.setAddGroupPermissions(true);
073                    serviceContext.setAddGuestPermissions(true);
074                    serviceContext.setCompanyId(companyId);
075                    serviceContext.setScopeGroupId(groupId);
076                    serviceContext.setUserId(userId);
077    
078                    return serviceContext;
079            }
080    
081    }