1   package org.marketchangers.liferay;
2   
3   import java.util.Collections;
4   
5   import junit.framework.TestCase;
6   
7   import org.marketchangers.PortalUserManager;
8   
9   import com.liferay.portal.ejb.RoleManager;
10  import com.liferay.portal.ejb.UserRemoteManager;
11  import com.liferay.portal.model.Role;
12  import com.mockobjects.dynamic.C;
13  import com.mockobjects.dynamic.Mock;
14  
15  /***
16   * @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
17   */
18  public class LiferayUserManagerTest extends TestCase {
19  	private PortalUserManager manager;
20  	private Role role;
21  
22  	private Mock userManager;
23  	private Mock roleManager;
24  
25  	// --------------------------------------------- SetUp and TearDown Methods
26  	public void setUp() {
27  		userManager = new Mock(UserRemoteManager.class);
28  		roleManager = new Mock(RoleManager.class);
29  
30  		role = new Role("FOOSBALL QUEEN");
31  
32  		manager =
33  			new LiferayUserManager(
34  				(UserRemoteManager)userManager.proxy(),
35  				(RoleManager)roleManager.proxy());
36  	}
37  
38  	public void tearDown() {
39  		userManager.verify();
40  		roleManager.verify();
41  	}
42  
43  	// ----------------------------------------------------------- Test Methods
44  	public void testAddRole() {
45  		roleManager.expectAndReturn("getRoleByName", "FOOSBALL QUEEN", role);
46  
47  		userManager.expectAndReturn(
48  			"addRole",
49  			C.args(C.eq("THELMA LOU"), C.eq(role)),
50  			true);
51  
52  		assertTrue(manager.addRole("THELMA LOU", "FOOSBALL QUEEN"));
53  	}
54  
55  	public void testIsUserInRoleReturnsFalseAsAppropriate() {
56  		roleManager.expectAndReturn("getRoleByName", "FOOSBALL QUEEN", role);
57  
58  		userManager.expectAndReturn(
59  			"getRoles",
60  			"THELMA LOU",
61  			Collections.EMPTY_LIST);
62  
63  		assertFalse(manager.isUserInRole("THELMA LOU", "FOOSBALL QUEEN"));
64  	}
65  
66  	public void testIsUserInRoleReturnsTrueAsAppropriate() {
67  		roleManager.expectAndReturn("getRoleByName", "FOOSBALL QUEEN", role);
68  
69  		userManager.expectAndReturn(
70  			"getRoles",
71  			"THELMA LOU",
72  			Collections.singletonList(role));
73  
74  		assertTrue(manager.isUserInRole("THELMA LOU", "FOOSBALL QUEEN"));
75  	}
76  
77  	public void testLookupUserById() {
78  		//TODO - cannot unit test at the moment due mainly to User.getAddresses() makes
79  		//an ejb call (getInitialContext is called).  Need way to either mock the 
80  		//User.getAddresses() call or ???
81  
82  		/* - below is a valid test that works if the User.getAddresses() method is not called
83  		String userid = "thelma@wc-group.com";
84  		
85  		Address address = new Address("ADDRESS ID");
86  		address.setDescription("MY COMPANY");
87  		address.setUserId(userid);
88  		List addressList = new LinkedList();
89  		addressList.add(address);
90  		User user = new User(userid);
91  		user.setFirstName("THELMA");
92  		user.setMiddleName("M.");
93  		user.setLastName("LOU");
94  		
95  		userManager.expectAndReturn("getUserById", userid, user);
96  		
97  		PortalUser portalUser = manager.lookupUserById(userid);
98  		assertEquals(portalUser.getUserId(), user.getUserId());
99  		assertEquals(portalUser.getFullName(), user.getFullName());
100 		*/
101 	}
102 
103 	public void testRemoveRole() {
104 		roleManager.expectAndReturn("getRoleByName", "FOOSBALL QUEEN", role);
105 
106 		userManager.expectAndReturn(
107 			"deleteRole",
108 			C.args(C.eq("THELMA LOU"), C.eq(role)),
109 			true);
110 
111 		assertTrue(manager.removeRole("THELMA LOU", "FOOSBALL QUEEN"));
112 	}
113 
114 	public void testLookupUsersByRole() {
115 		//TODO - cannot unit test at the moment due mainly to User.getAddresses() makes
116 		//an ejb call (getInitialContext is called).  Need way to either mock the 
117 		//User.getAddresses() call or ???
118 
119 		/* - below is a valid test that works if the User.getAddresses() method is not called
120 				Role role = new Role("roleid");
121 				role.setName("intercessor");
122 				
123 				User user = new User("userid");
124 				List users = Collections.singletonList(user);
125 				
126 				roleManager.expectAndReturn("getRoleByName","intercessor",role);
127 				roleManager.expectAndReturn("getUsers",role.getRoleId(),users);
128 				
129 				List portalUsers = manager.lookupUsersByRole("intercessor");
130 				assertEquals(((PortalUser)portalUsers.get(0)).getUserId(),user.getUserId());
131 			*/
132 	}
133 }