/** * 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.content.targeting.rule.role; import com.liferay.content.targeting.anonymous.users.model.AnonymousUser; import com.liferay.content.targeting.anonymous.users.service.AnonymousUserLocalService; import com.liferay.content.targeting.api.model.Rule; import com.liferay.content.targeting.api.model.RulesRegistry; import com.liferay.content.targeting.model.RuleInstance; import com.liferay.content.targeting.service.RuleInstanceLocalService; import com.liferay.content.targeting.service.test.service.ServiceTestUtil; import com.liferay.content.targeting.service.test.util.TestPropsValues; import com.liferay.osgi.util.service.ServiceTrackerUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.model.Role; import com.liferay.portal.model.RoleConstants; import com.liferay.portal.service.RoleLocalServiceUtil; import com.liferay.portal.service.ServiceContext; import java.util.List; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; /** * @author Eudaldo Alonso */ @RunWith(Arquillian.class) public class RegularRoleRuleTest { @Before public void setUp() { try { _bundle.start(); } catch (BundleException e) { e.printStackTrace(); } _anonymousUserLocalService = ServiceTrackerUtil.getService( AnonymousUserLocalService.class, _bundle.getBundleContext()); _ruleInstanceLocalService = ServiceTrackerUtil.getService( RuleInstanceLocalService.class, _bundle.getBundleContext()); _rulesRegistry = ServiceTrackerUtil.getService( RulesRegistry.class, _bundle.getBundleContext()); } @Test public void testRegularRoleRule() throws Exception { ServiceContext serviceContext = ServiceTestUtil.getServiceContext(); AnonymousUser anonymousUser = _anonymousUserLocalService.addAnonymousUser( TestPropsValues.getUserId(), "127.0.0.1", StringPool.BLANK, serviceContext); List<Role> roles = RoleLocalServiceUtil.getRoles( TestPropsValues.getCompanyId(), new int[] {RoleConstants.TYPE_REGULAR}); Role role = roles.get(0); Rule rule = _rulesRegistry.getRule("RegularRoleRule"); RuleInstance ruleInstance = _ruleInstanceLocalService.addRuleInstance( TestPropsValues.getUserId(), rule.getRuleKey(), 0, String.valueOf(role.getRoleId()), serviceContext); RoleLocalServiceUtil.addUserRole( TestPropsValues.getUserId(), role.getRoleId()); Assert.assertTrue(rule.evaluate(null, ruleInstance, anonymousUser)); } private AnonymousUserLocalService _anonymousUserLocalService; @ArquillianResource private Bundle _bundle; private RuleInstanceLocalService _ruleInstanceLocalService; private RulesRegistry _rulesRegistry; }