1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.junit.Test;
20 import org.kuali.rice.kew.api.exception.WorkflowException;
21 import org.kuali.rice.kim.api.KimConstants.PermissionNames;
22 import org.kuali.rice.kim.api.identity.Person;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kns.authorization.AuthorizationConstants;
25 import org.kuali.rice.krad.UserSession;
26 import org.kuali.rice.krad.document.Document;
27 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
28 import org.kuali.rice.krad.document.authorization.PessimisticLock;
29 import org.kuali.rice.krad.exception.AuthorizationException;
30 import org.kuali.rice.krad.service.impl.PessimisticLockServiceImpl;
31 import org.kuali.rice.krad.test.document.AccountRequestDocument;
32 import org.kuali.rice.krad.test.document.AccountRequestDocument2;
33 import org.kuali.rice.krad.util.GlobalVariables;
34 import org.kuali.rice.krad.util.KRADConstants;
35 import org.kuali.rice.krad.util.KRADPropertyConstants;
36 import org.kuali.rice.maintainable.AccountType2MaintainableImpl;
37 import org.kuali.rice.test.BaselineTestCase;
38 import org.kuali.rice.test.data.UnitTestData;
39 import org.kuali.rice.test.data.UnitTestSql;
40 import org.kuali.test.KRADTestCase;
41
42 import java.io.Serializable;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Set;
51
52 import static org.junit.Assert.*;
53
54
55
56
57
58
59
60 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
61 public class PessimisticLockServiceTest extends KRADTestCase {
62
63 String sessionId = "ad4d6c83-4d0f-4309-a528-c2f81ec00396";
64
65 @Override
66 public void setUp() throws Exception {
67 super.setUp();
68 GlobalVariables.setUserSession(new UserSession("quickstart"));
69 GlobalVariables.getUserSession().setKualiSessionId(sessionId);
70 }
71
72
73
74
75
76
77
78 @UnitTestData(
79 sqlStatements = {
80 @UnitTestSql("DELETE FROM KRNS_PESSIMISTIC_LOCK_T"),
81 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1111, '4f6bc9e2-7df8-102c-97b6-ed716fdaf540', 0, NULL, '1234', {d '2007-07-01'}, 'employee', 'aa5d6c83-4d0f-4309-a528-c2f81ec00396')"),
82 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1112, '5add9cba-7df8-102c-97b6-ed716fdaf540', 0, NULL, '1235', {d '2007-10-01'}, 'frank', 'dd4d6c83-4d0f-4309-a528-c2f81ec00396')"),
83 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1113, '69e42b8e-7df8-102c-97b6-ed716fdaf540', 0, NULL, '1236', {d '2007-08-01'}, 'fred', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')"),
84 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1114, '76504650-7df8-102c-97b6-ed716fdaf540', 0, NULL, '1237', {d '2007-08-01'}, 'fred', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')")
85 }
86 )
87 @Test
88 public void testDeleteLocks() throws Exception {
89
90 List<PessimisticLock> locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
91 assertEquals("Should be 4 locks in DB", 4, locks.size());
92
93 String userId = "employee";
94 String[] lockIdsToVerify = new String[]{"1112", "1113"};
95 assertFalse("User " + userId + " should not be member of pessimistic lock admin permission", KimApiServiceLocator.getPermissionService().isAuthorized(new UserSession(userId).getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING,
96 Collections.<String, String>emptyMap() ) );
97 verifyDelete(userId, Arrays.asList(lockIdsToVerify), AuthorizationException.class, true);
98 userId = "frank";
99 lockIdsToVerify = new String[]{"1111", "1113"};
100 assertFalse("User " + userId + " should not be member of pessimistic lock admin permission", KimApiServiceLocator.getPermissionService().isAuthorized(new UserSession(userId).getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING, Collections.<String, String>emptyMap() ) );
101 verifyDelete(userId, Arrays.asList(lockIdsToVerify), AuthorizationException.class, true);
102 userId = "fred";
103 lockIdsToVerify = new String[]{"1111", "1112"};
104 assertFalse("User " + userId + " should not be member of pessimistic lock admin permission", KimApiServiceLocator.getPermissionService().isAuthorized(new UserSession(userId).getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING, Collections.<String, String>emptyMap() ) );
105 verifyDelete(userId, Arrays.asList(lockIdsToVerify), AuthorizationException.class, true);
106
107 verifyDelete("employee", Arrays.asList(new String[]{"1111"}), null, false);
108 verifyDelete("frank", Arrays.asList(new String[]{"1112"}), null, false);
109 verifyDelete("fred", Arrays.asList(new String[]{"1113"}), null, false);
110 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
111 assertEquals("Should be 1 lock left in DB", 1, locks.size());
112
113
114 userId = "fran";
115 assertTrue("User " + userId + " should be member of pessimistic lock admin permission", KimApiServiceLocator.getPermissionService().isAuthorized(new UserSession(userId).getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING, Collections.<String, String>emptyMap() ) );
116 userId = "admin";
117 assertTrue("User " + userId + " should be member of pessimistic lock admin permission", KimApiServiceLocator.getPermissionService().isAuthorized(new UserSession(userId).getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE, PermissionNames.ADMIN_PESSIMISTIC_LOCKING, Collections.<String, String>emptyMap() ) );
118 verifyDelete(userId, Arrays.asList(new String[]{"1114"}), null, false);
119 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
120 assertEquals("Should be 0 locks left in DB", 0, locks.size());
121 }
122
123 private void verifyDelete(String userId, List<String> lockIds, Class expectedException, boolean expectException) throws WorkflowException {
124 GlobalVariables.setUserSession(new UserSession(userId));
125 for (String lockId : lockIds) {
126 try {
127 KRADServiceLocatorWeb.getPessimisticLockService().delete(lockId);
128 if (expectException) {
129 fail("Expected exception when deleting lock with id '" + lockId + "' for user '" + userId + "'");
130 }
131 } catch (Exception e) {
132 if (!expectException) {
133 fail("Did not expect exception when deleting lock with id '" + lockId + "' for user '" + userId + "' but got exception of type '" + e.getClass().getName() + "'");
134 }
135 if (expectedException != null) {
136
137 if (!expectedException.isAssignableFrom(e.getClass())) {
138 fail("Expected exception of type '" + expectedException.getName() + "' when deleting lock with id '" + lockId + "' for user '" + userId + "' but got exception of type '" + e.getClass().getName() + "'");
139 }
140 }
141 }
142 }
143 }
144
145
146
147
148
149
150 @Test
151 public void testGenerateNewLocks() throws Exception {
152 PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
153
154
155 String documentNumber = "1243";
156 PessimisticLock lock = lockService.generateNewLock(documentNumber);
157 assertNotNull("Generated lock should have id", lock.getId());
158 assertEquals("Document Number should match", documentNumber, lock.getDocumentNumber());
159 assertNotNull("Generated lock should have a generated timestamp ", lock.getGeneratedTimestamp());
160 assertEquals("Generated lock should have default lock descriptor", PessimisticLock.DEFAULT_LOCK_DESCRIPTOR, lock.getLockDescriptor());
161 assertEquals("Generated lock should be owned by current user", GlobalVariables.getUserSession().getPerson().getPrincipalName(), lock.getOwnedByUser().getPrincipalName());
162 Map primaryKeys = new HashMap();
163 primaryKeys.put(KRADPropertyConstants.ID, lock.getId());
164 lock = null;
165 lock = (PessimisticLock) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PessimisticLock.class, primaryKeys);
166 assertNotNull("Generated lock should be available from BO Service", lock);
167 assertNotNull("Generated lock should have id", lock.getId());
168 assertEquals("Document Number should match", documentNumber, lock.getDocumentNumber());
169 assertNotNull("Generated lock should have a generated timestamp ", lock.getGeneratedTimestamp());
170 assertEquals("Generated lock should have default lock descriptor", PessimisticLock.DEFAULT_LOCK_DESCRIPTOR, lock.getLockDescriptor());
171 assertEquals("Generated lock should be owned by current user", GlobalVariables.getUserSession().getPerson().getPrincipalName(), lock.getOwnedByUser().getPrincipalName());
172
173
174 lock = null;
175 documentNumber = "4321";
176 String lockDescriptor = "this is a test lock descriptor";
177 lock = lockService.generateNewLock(documentNumber, lockDescriptor);
178 assertNotNull("Generated lock should have id", lock.getId());
179 assertEquals("Document Number should match", documentNumber, lock.getDocumentNumber());
180 assertNotNull("Generated lock should have a generated timestamp ", lock.getGeneratedTimestamp());
181 assertEquals("Generated lock should have lock descriptor set", lockDescriptor, lock.getLockDescriptor());
182 assertEquals("Generated lock should be owned by current user", GlobalVariables.getUserSession().getPerson().getPrincipalName(), lock.getOwnedByUser().getPrincipalName());
183 primaryKeys = new HashMap();
184 primaryKeys.put(KRADPropertyConstants.ID, lock.getId());
185 lock = null;
186 lock = (PessimisticLock) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PessimisticLock.class, primaryKeys);
187 assertNotNull("Generated lock should be available from BO Service", lock);
188 assertNotNull("Generated lock should have id", lock.getId());
189 assertEquals("Document Number should match", documentNumber, lock.getDocumentNumber());
190 assertNotNull("Generated lock should have a generated timestamp ", lock.getGeneratedTimestamp());
191 assertEquals("Generated lock should have lock descriptor set", lockDescriptor, lock.getLockDescriptor());
192 assertEquals("Generated lock should be owned by current user", GlobalVariables.getUserSession().getPerson().getPrincipalName(), lock.getOwnedByUser().getPrincipalName());
193 }
194
195
196
197
198
199
200 @UnitTestData(
201 sqlStatements = {
202 @UnitTestSql("DELETE FROM KRNS_PESSIMISTIC_LOCK_T"),
203 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1111, 'fbcb0362-7dfb-102c-97b6-ed716fdaf540', 0, NULL, '1234', {d '2007-07-01'}, 'fran', 'aa5d6c83-4d0f-4309-a528-c2f81ec00396')"),
204 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1112, '055bef4a-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1237', {d '2007-10-01'}, 'frank', 'dd5d6c83-4d0f-4309-a528-c2f81ec00396')"),
205 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1113, '0e0144ec-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1236', {d '2007-10-01'}, 'frank', 'dd5d6c83-4d0f-4309-a528-c2f81ec00396')"),
206 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1114, '1891526c-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1237', {d '2007-08-01'}, 'fred', 'ab4d6c83-4d0f-4309-a528-c2f81ec00396')")
207 }
208 )
209 @Test
210 public void testGetPessimisticLocksForDocument() throws Exception {
211 PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
212 String docId = "1234";
213 assertEquals("Document " + docId + " expected lock count incorrect", 1, lockService.getPessimisticLocksForDocument(docId).size());
214 docId = "1237";
215 assertEquals("Document " + docId + " expected lock count incorrect", 2, lockService.getPessimisticLocksForDocument(docId).size());
216 docId = "1236";
217 assertEquals("Document " + docId + " expected lock count incorrect", 1, lockService.getPessimisticLocksForDocument(docId).size());
218 docId = "3948";
219 assertEquals("Document " + docId + " expected lock count incorrect", 0, lockService.getPessimisticLocksForDocument(docId).size());
220 }
221
222
223
224
225
226
227 @UnitTestData(
228 sqlStatements = {
229 @UnitTestSql("DELETE FROM KRNS_PESSIMISTIC_LOCK_T"),
230 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1111, '24c40cd2-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1234', {d '2007-07-01'}, 'fran', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')"),
231 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1112, '32602e8e-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1235', {d '2007-10-01'}, 'fran', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')"),
232 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1113, '3acfc1ce-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1236', {d '2007-10-01'}, 'fran', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')"),
233 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1114, '463cc642-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1237', {d '2007-08-01'}, 'fran', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')")
234 }
235 )
236 @Test
237 public void testGetPessimisticLocksForSession() throws Exception {
238 List<PessimisticLock> locks = KRADServiceLocatorWeb.getPessimisticLockService().getPessimisticLocksForSession(sessionId);
239 assertEquals("Should return 2 locks for session " + sessionId, 2, locks.size());
240
241 ArrayList<String> documentNumbers = new ArrayList<String>();
242 for (PessimisticLock lock : locks) {
243 documentNumbers.add(lock.getDocumentNumber());
244 }
245 assertTrue("Locks should contain a lock for document number 1234 but contained " + documentNumbers, documentNumbers.contains("1234"));
246 assertTrue("Locks should contain a lock for document number 1236 but contained " + documentNumbers, documentNumbers.contains("1236"));
247 }
248
249
250
251
252
253
254 @UnitTestData(
255 sqlStatements = {
256 @UnitTestSql("DELETE FROM KRNS_PESSIMISTIC_LOCK_T"),
257 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1111, '24c40cd2-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1234', {d '2007-07-01'}, 'fran', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')"),
258 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1112, '32602e8e-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1235', {d '2007-10-01'}, 'frank', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')"),
259 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1113, '3acfc1ce-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1236', {d '2007-10-01'}, 'frank', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')"),
260 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1114, '463cc642-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1237', {d '2007-08-01'}, 'fred', 'dd5d6c66-4d0f-4309-a528-c2f81ec00396')"),
261 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1115, '4e66c4b2-7dfc-102c-97b6-ed716fdaf540', 0, 'Temporary Lock', '1234', {d '2007-07-01'}, 'fran', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')"),
262 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1116, '55d99b02-7dfc-102c-97b6-ed716fdaf540', 0, 'Temporary Lock', '1235', {d '2007-10-01'}, 'frank', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')"),
263 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1117, '5e47fb26-7dfc-102c-97b6-ed716fdaf540', 0, 'Temporary Lock', '1236', {d '2007-10-01'}, 'frank', 'bc5d6c66-4d0f-4309-a528-c2f81ec00396')"),
264 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1118, '65c366d8-7dfc-102c-97b6-ed716fdaf540', 0, 'Temporary Lock', '1237', {d '2007-08-01'}, 'fred', 'dd5d6c66-4d0f-4309-a528-c2f81ec00396')")
265 }
266 )
267 @Test
268 public void testReleaseAllLocksForUser() throws Exception {
269 String lockDescriptor = "Temporary Lock";
270 List<PessimisticLock> locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
271 assertEquals("Should be 8 manually inserted locks", 8, locks.size());
272
273 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("fran"), lockDescriptor);
274 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
275 assertEquals("Should be 7 locks left after releasing locks for fran using lock descriptor " + lockDescriptor, 7, locks.size());
276
277 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("frank"), lockDescriptor);
278 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
279 assertEquals("Should be 5 locks left after releasing locks for fran and frank using lock descriptor " + lockDescriptor, 5, locks.size());
280
281 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("fred"), lockDescriptor);
282 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
283 assertEquals("Should be 4 locks left after releasing locks for fran, frank, and fred using lock descriptor " + lockDescriptor, 4, locks.size());
284
285 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("fran"));
286 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
287 assertEquals("Should be 3 locks left after releasing locks for fran with no lock descriptor", 3, locks.size());
288
289 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("frank"));
290 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
291 assertEquals("Should be 1 lock left after releasing locks for fran and frank with no lock descriptor", 1, locks.size());
292
293 KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, KimApiServiceLocator.getPersonService().getPerson("fred"));
294 locks = (List<PessimisticLock>) KRADServiceLocator.getBusinessObjectService().findAll(PessimisticLock.class);
295 assertEquals("Should be no locks left after releasing locks for fran, frank, and fred with no lock descriptor", 0, locks.size());
296 }
297
298
299
300
301
302
303 @UnitTestData(
304 sqlStatements = {
305 @UnitTestSql("DELETE FROM KRNS_PESSIMISTIC_LOCK_T"),
306 @UnitTestSql("INSERT INTO KRNS_PESSIMISTIC_LOCK_T (PESSIMISTIC_LOCK_ID,OBJ_ID,VER_NBR,LOCK_DESC_TXT,DOC_HDR_ID,GNRT_DT,PRNCPL_ID,SESN_ID) VALUES (1111, '73f340de-7dfc-102c-97b6-ed716fdaf540', 0, NULL, '1234', {d '2007-07-01'}, 'fran', 'ad4d6c83-4d0f-4309-a528-c2f81ec00396')")
307 }
308 )
309 @Test
310 public void testSaveLock() throws Exception {
311 String lockDescriptor = "new test lock descriptor";
312
313 Map primaryKeys = new HashMap();
314 primaryKeys.put(KRADPropertyConstants.ID, Long.valueOf("1111"));
315 PessimisticLock lock = (PessimisticLock) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PessimisticLock.class, primaryKeys);
316 lock.setLockDescriptor(lockDescriptor);
317 KRADServiceLocatorWeb.getPessimisticLockService().save(lock);
318
319
320 PessimisticLock savedLock = (PessimisticLock) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PessimisticLock.class, primaryKeys);
321 assertEquals("Lock descriptor is not correct from lock that was saved", lockDescriptor, savedLock.getLockDescriptor());
322 }
323
324
325
326
327
328
329 @Test
330 public void testEstablishLocks() throws Exception {
331 PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
332 AccountRequestDocument accountDoc = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
333
334 assertTrue("The AccountRequestDocument should be using pessimistic locking",
335 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDocumentEntry(accountDoc.getClass().getName()).getUsePessimisticLocking());
336
337
338 UserSession quickstartSession = new UserSession("quickstart");
339 Person[] quickstartPerson = { quickstartSession.getPerson() };
340 Map<String,String> editMode = new HashMap<String,String>();
341 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
342 Map <?,?> finalModes = lockService.establishLocks(accountDoc, editMode, quickstartSession.getPerson());
343
344
345 assertCorrectLocksAreInPlace(true, finalModes, 1, accountDoc.getPessimisticLocks(), quickstartPerson, null);
346
347
348 UserSession adminSession = new UserSession("admin");
349 Set<String> documentActions = new HashSet<String>(Arrays.asList(new String[] { KRADConstants.KUALI_ACTION_CAN_CANCEL,
350 KRADConstants.KUALI_ACTION_CAN_SAVE, KRADConstants.KUALI_ACTION_CAN_ROUTE, KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE }));
351 Set<?> finalActions = lockService.getDocumentActions(accountDoc, adminSession.getPerson(), documentActions);
352 assertFalse("'admin' should not be able to cancel the locked document", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_CANCEL));
353 assertFalse("'admin' should not be able to save the locked document", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_SAVE));
354 assertFalse("'admin' should not be able to route the locked document", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_ROUTE));
355 assertFalse("'admin' should not be able to blanket approve the locked document", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE));
356
357
358 documentActions = new HashSet<String>(Arrays.asList(new String[] {
359 KRADConstants.KUALI_ACTION_CAN_CANCEL, KRADConstants.KUALI_ACTION_CAN_SAVE, KRADConstants.KUALI_ACTION_CAN_ROUTE }));
360 finalActions = lockService.getDocumentActions(accountDoc, quickstartSession.getPerson(), documentActions);
361 assertTrue("'quickstart' should have had cancel privileges", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_CANCEL));
362 assertTrue("'quickstart' should have had save privileges", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_SAVE));
363 assertTrue("'quickstart' should have had route privileges", finalActions.contains(KRADConstants.KUALI_ACTION_CAN_ROUTE));
364
365
366 editMode = new HashMap<String,String>();
367 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
368 finalModes = lockService.establishLocks(accountDoc, editMode, adminSession.getPerson());
369 assertCorrectLocksAreInPlace(false, finalModes, 1, accountDoc.getPessimisticLocks(), quickstartPerson, null);
370
371
372 editMode = new HashMap<String,String>();
373 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
374 finalModes = lockService.establishLocks(accountDoc, editMode, quickstartSession.getPerson());
375 assertCorrectLocksAreInPlace(true, finalModes, 1, accountDoc.getPessimisticLocks(), quickstartPerson, null);
376 }
377
378
379
380
381
382
383 @Test
384 public void testWorkflowPessimisticLocking() throws Exception {
385 PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
386 AccountRequestDocument accountDoc = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
387 assertTrue("The AccountRequestDocument should be using pessimistic locking",
388 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDocumentEntry(accountDoc.getClass().getName()).getUsePessimisticLocking());
389
390
391 UserSession systemSession = new UserSession(KRADConstants.SYSTEM_USER);
392 Person[] systemPerson = { systemSession.getPerson() };
393 lockService.establishWorkflowPessimisticLocking(accountDoc);
394 assertCorrectLocksAreInPlace(false, null, 1, accountDoc.getPessimisticLocks(), systemPerson, null);
395
396
397 UserSession adminSession = new UserSession("admin");
398 Map<String,String> editMode = new HashMap<String,String>();
399 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
400 Map<?,?> finalModes = lockService.establishLocks(accountDoc, editMode, adminSession.getPerson());
401 assertCorrectLocksAreInPlace(false, finalModes, 1, accountDoc.getPessimisticLocks(), systemPerson, null);
402
403
404 lockService.releaseWorkflowPessimisticLocking(accountDoc);
405 assertTrue("There should not be any pessimistic locks present on the document", accountDoc.getPessimisticLocks().isEmpty());
406 }
407
408
409
410
411
412
413 @Test
414 public void testPessimisticLockingWithCustomDocumentLockDescriptors() throws Exception {
415 AccountRequestDocument2 accountDoc2 = (AccountRequestDocument2) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest2");
416 assertTrue("The AccountRequestDocument2 should be using pessimistic locking", KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary(
417 ).getDocumentEntry(accountDoc2.getClass().getName()).getUsePessimisticLocking());
418 assertTrue("The AccountRequestDocument2 should be using custom lock descriptors", accountDoc2.useCustomLockDescriptors());
419
420
421 assertCustomLockDescriptorsAreWorking(accountDoc2, AccountRequestDocument2.ACCT_REQ_DOC_2_EDITABLE_FIELDS,
422 AccountRequestDocument2.EDIT_ALL_BUT_REASONS, AccountRequestDocument2.EDIT_REASONS_ONLY);
423 }
424
425
426
427
428
429
430
431 @Test
432 public void testPessimisticLockingWithCustomMaintainableLockDescriptors() throws Exception {
433 MaintenanceDocument maintDoc = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountType2MaintenanceDocument");
434 assertTrue("The AccountType2MaintenanceDocument should be using pessimistic locking", KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary(
435 ).getDocumentEntry(maintDoc.getNewMaintainableObject().getDataObjectClass().getSimpleName() + "MaintenanceDocument").getUsePessimisticLocking());
436 assertTrue("The AccountType2MaintenanceDocument should be using custom lock descriptors", maintDoc.useCustomLockDescriptors());
437 assertTrue("The AccountType2MaintenanceDocument's new maintainable uses the wrong class",
438 maintDoc.getNewMaintainableObject() instanceof AccountType2MaintainableImpl);
439 AccountType2MaintainableImpl newMaint = (AccountType2MaintainableImpl) maintDoc.getNewMaintainableObject();
440 assertTrue("The AccountType2MaintainableImpl should be using custom lock descriptors", newMaint.useCustomLockDescriptors());
441
442
443 assertCustomLockDescriptorsAreWorking(maintDoc, AccountType2MaintainableImpl.ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT,
444 AccountType2MaintainableImpl.EDIT_CODE_ONLY, AccountType2MaintainableImpl.EDIT_NAME_ONLY);
445 }
446
447
448
449
450
451
452
453
454
455
456
457 private void assertCustomLockDescriptorsAreWorking(Document testDoc, final String LOCK_KEY, final Serializable LOCK_VALUE1,
458 final Serializable LOCK_VALUE2) throws Exception {
459 PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
460
461
462 UserSession quickstartSession = new UserSession("quickstart");
463 Person[] allPersons = { quickstartSession.getPerson(), null };
464 Map<String,String> editMode = new HashMap<String,String>();
465 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
466 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
467 String[] allDescriptors = { testDoc.getCustomLockDescriptor(quickstartSession.getPerson()), null };
468 assertNotNull("The document should have generated a custom lock descriptor", allDescriptors[0]);
469 Map <?,?> finalModes = lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
470
471
472 assertCorrectLocksAreInPlace(true, finalModes, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
473
474
475 editMode = new HashMap<String,String>();
476 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
477 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
478 lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
479 assertCorrectLocksAreInPlace(false, null, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
480
481
482 UserSession adminSession = new UserSession("admin");
483 editMode = new HashMap<String,String>();
484 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
485 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
486 assertEquals("The document should have generated the same lock descriptors for both 'quickstart' and 'admin'",
487 allDescriptors[0], testDoc.getCustomLockDescriptor(adminSession.getPerson()));
488 finalModes = lockService.establishLocks(testDoc, editMode, adminSession.getPerson());
489 assertCorrectLocksAreInPlace(false, finalModes, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
490
491
492 allPersons[1] = adminSession.getPerson();
493 editMode = new HashMap<String,String>();
494 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
495 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
496 allDescriptors[1] = testDoc.getCustomLockDescriptor(adminSession.getPerson());
497 assertNotNull("The document should have generated a custom lock descriptor", allDescriptors[1]);
498 assertNotSame("'quickstart' and 'admin' should have different custom lock descriptors now", allDescriptors[0], allDescriptors[1]);
499 finalModes = lockService.establishLocks(testDoc, editMode, adminSession.getPerson());
500 assertCorrectLocksAreInPlace(true, finalModes, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
501
502
503 editMode = new HashMap<String,String>();
504 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
505 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
506 lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
507 assertCorrectLocksAreInPlace(false, null, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
508
509
510 lockService.releaseAllLocksForUser(testDoc.getPessimisticLocks(), allPersons[1], allDescriptors[1]);
511 testDoc.refreshPessimisticLocks();
512 assertCorrectLocksAreInPlace(false, null, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
513 allPersons[1] = allPersons[0];
514 editMode = new HashMap<String,String>();
515 editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
516 GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
517 finalModes = lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
518 assertCorrectLocksAreInPlace(true, finalModes, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
519
520
521 GlobalVariables.getUserSession().removeObject(LOCK_KEY);
522 lockService.releaseAllLocksForUser(testDoc.getPessimisticLocks(), allPersons[0]);
523 testDoc.refreshPessimisticLocks();
524 assertTrue("There should not be any pessimistic locks present on the document", testDoc.getPessimisticLocks().isEmpty());
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539 private void assertCorrectLocksAreInPlace(boolean latestUserHasFullEntry, Map<?,?> finalModes, int expectedLockQuantity,
540 List<PessimisticLock> pessimisticLocks, Person[] expectedOwners, String[] expectedDescriptors) throws Exception {
541
542 if (finalModes != null) {
543 assertEquals("The last user that tried to establish locks does not have the expected status on their full entry privileges",
544 latestUserHasFullEntry, StringUtils.equalsIgnoreCase(KRADConstants.KUALI_DEFAULT_TRUE_VALUE, (String)(finalModes.get(
545 AuthorizationConstants.EditMode.FULL_ENTRY))));
546 }
547
548 assertEquals("The wrong number of pessimistic locks are in place", expectedLockQuantity, pessimisticLocks.size());
549
550 for (int i = pessimisticLocks.size() - 1; i > -1; i--) {
551 assertTrue("The lock at index " + i + " did not have the expected owner of " + expectedOwners[i].getPrincipalName(),
552 pessimisticLocks.get(i).isOwnedByUser(expectedOwners[i]));
553 if (expectedDescriptors != null) {
554 assertTrue("The lock at index " + i + " did not have the expected lock descriptor of " + expectedDescriptors[i],
555 pessimisticLocks.get(i).getLockDescriptor().equals(expectedDescriptors[i]));
556 }
557 }
558 }
559 }