1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.kns.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.junit.Test;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.kns.KNSTestCase;
22  import org.kuali.rice.kns.authorization.AuthorizationConstants;
23  import org.kuali.rice.krad.UserSession;
24  import org.kuali.rice.krad.document.Document;
25  import org.kuali.rice.krad.document.authorization.PessimisticLock;
26  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.service.PessimisticLockService;
29  import org.kuali.rice.krad.service.impl.PessimisticLockServiceImpl;
30  import org.kuali.rice.krad.test.document.AccountType2MaintainableImpl;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.kuali.rice.test.BaselineTestCase;
34  
35  import java.io.Serializable;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import static org.junit.Assert.*;
41  
42  
43  
44  
45  
46  
47  @Deprecated
48  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
49  public class PessimisticLockServiceTest extends KNSTestCase {
50  
51      String sessionId = "ad4d6c83-4d0f-4309-a528-c2f81ec00395";
52  
53      @Override
54      public void setUp() throws Exception {
55          super.setUp();
56          GlobalVariables.setUserSession(new UserSession("quickstart"));
57          GlobalVariables.getUserSession().setKualiSessionId(sessionId);
58      }
59  
60      
61  
62  
63  
64  
65  
66  
67      @Test
68      public void testPessimisticLockingWithCustomMaintainableLockDescriptors() throws Exception {
69          MaintenanceDocument maintDoc = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(
70                  "AccountType2MaintenanceDocument");
71          assertTrue("The AccountType2MaintenanceDocument should be using pessimistic locking",
72                  KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDocumentEntry(
73                          maintDoc.getNewMaintainableObject().getDataObjectClass().getSimpleName()
74                                  + "MaintenanceDocument").getUsePessimisticLocking());
75          assertTrue("The AccountType2MaintenanceDocument should be using custom lock descriptors",
76                  maintDoc.useCustomLockDescriptors());
77          assertTrue("The AccountType2MaintenanceDocument's new maintainable uses the wrong class",
78                  maintDoc.getNewMaintainableObject() instanceof AccountType2MaintainableImpl);
79          AccountType2MaintainableImpl newMaint = (AccountType2MaintainableImpl) maintDoc.getNewMaintainableObject();
80          assertTrue("The AccountType2MaintainableImpl should be using custom lock descriptors",
81                  newMaint.useCustomLockDescriptors());
82  
83          
84          assertCustomLockDescriptorsAreWorking(maintDoc, AccountType2MaintainableImpl.ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT,
85                  AccountType2MaintainableImpl.EDIT_CODE_ONLY, AccountType2MaintainableImpl.EDIT_NAME_ONLY);
86      }
87  
88      
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100     private void assertCustomLockDescriptorsAreWorking(Document testDoc, final String LOCK_KEY,
101             final Serializable LOCK_VALUE1, final Serializable LOCK_VALUE2) throws Exception {
102         PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
103 
104         
105         UserSession quickstartSession = new UserSession("quickstart");
106         Person[] allPersons = {quickstartSession.getPerson(), null};
107         Map<String, String> editMode = new HashMap<String, String>();
108         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
109         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
110         String[] allDescriptors = {testDoc.getCustomLockDescriptor(quickstartSession.getPerson()), null};
111         assertNotNull("The document should have generated a custom lock descriptor", allDescriptors[0]);
112         Map<?, ?> finalModes = lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
113 
114         
115         assertCorrectLocksAreInPlace(true, finalModes, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
116 
117         
118         editMode = new HashMap<String, String>();
119         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
120         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
121         lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
122         assertCorrectLocksAreInPlace(false, null, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
123 
124         
125         UserSession adminSession = new UserSession("admin");
126         editMode = new HashMap<String, String>();
127         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
128         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE1);
129         assertEquals("The document should have generated the same lock descriptors for both 'quickstart' and 'admin'",
130                 allDescriptors[0], testDoc.getCustomLockDescriptor(adminSession.getPerson()));
131         finalModes = lockService.establishLocks(testDoc, editMode, adminSession.getPerson());
132         assertCorrectLocksAreInPlace(false, finalModes, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
133 
134         
135         allPersons[1] = adminSession.getPerson();
136         editMode = new HashMap<String, String>();
137         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
138         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
139         allDescriptors[1] = testDoc.getCustomLockDescriptor(adminSession.getPerson());
140         assertNotNull("The document should have generated a custom lock descriptor", allDescriptors[1]);
141         assertNotSame("'quickstart' and 'admin' should have different custom lock descriptors now", allDescriptors[0],
142                 allDescriptors[1]);
143         finalModes = lockService.establishLocks(testDoc, editMode, adminSession.getPerson());
144         assertCorrectLocksAreInPlace(true, finalModes, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
145 
146         
147         editMode = new HashMap<String, String>();
148         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
149         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
150         lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
151         assertCorrectLocksAreInPlace(false, null, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
152 
153         
154         lockService.releaseAllLocksForUser(testDoc.getPessimisticLocks(), allPersons[1], allDescriptors[1]);
155         testDoc.refreshPessimisticLocks();
156         assertCorrectLocksAreInPlace(false, null, 1, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
157         allPersons[1] = allPersons[0];
158         editMode = new HashMap<String, String>();
159         editMode.put(AuthorizationConstants.EditMode.FULL_ENTRY, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
160         GlobalVariables.getUserSession().addObject(LOCK_KEY, LOCK_VALUE2);
161         finalModes = lockService.establishLocks(testDoc, editMode, quickstartSession.getPerson());
162         assertCorrectLocksAreInPlace(true, finalModes, 2, testDoc.getPessimisticLocks(), allPersons, allDescriptors);
163 
164         
165         GlobalVariables.getUserSession().removeObject(LOCK_KEY);
166         lockService.releaseAllLocksForUser(testDoc.getPessimisticLocks(), allPersons[0]);
167         testDoc.refreshPessimisticLocks();
168         assertTrue("There should not be any pessimistic locks present on the document",
169                 testDoc.getPessimisticLocks().isEmpty());
170     }
171 
172     
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188     private void assertCorrectLocksAreInPlace(boolean latestUserHasFullEntry, Map<?, ?> finalModes,
189             int expectedLockQuantity, List<PessimisticLock> pessimisticLocks, Person[] expectedOwners,
190             String[] expectedDescriptors) throws Exception {
191         
192         if (finalModes != null) {
193             assertEquals(
194                     "The last user that tried to establish locks does not have the expected status on their full entry privileges",
195                     latestUserHasFullEntry, StringUtils.equalsIgnoreCase(KRADConstants.KUALI_DEFAULT_TRUE_VALUE,
196                     (String) (finalModes.get(AuthorizationConstants.EditMode.FULL_ENTRY))));
197         }
198         
199         assertEquals("The wrong number of pessimistic locks are in place", expectedLockQuantity,
200                 pessimisticLocks.size());
201         
202         for (int i = pessimisticLocks.size() - 1; i > -1; i--) {
203             assertTrue("The lock at index " + i + " did not have the expected owner of " + expectedOwners[i]
204                     .getPrincipalName(), pessimisticLocks.get(i).isOwnedByUser(expectedOwners[i]));
205             if (expectedDescriptors != null) {
206                 assertTrue("The lock at index "
207                         + i
208                         + " did not have the expected lock descriptor of "
209                         + expectedDescriptors[i], pessimisticLocks.get(i).getLockDescriptor().equals(
210                         expectedDescriptors[i]));
211             }
212         }
213     }
214 }