1 /** 2 * Copyright 2005-2014 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.rice.kns.document; 17 18 import org.junit.Ignore; 19 import org.junit.Test; 20 import org.kuali.rice.krad.test.KRADTestCase; 21 22 /** 23 * @deprecated KNS test class, convert to KRAD equivalent if applicable. 24 */ 25 @Deprecated 26 @Ignore // everything is commented out 27 public class DocumentAuthorizerBaseTest extends KRADTestCase { 28 // TODO rewrite this test 29 // private static final Logger LOG = Logger.getLogger(DocumentAuthorizerBaseTest.class); 30 // 31 // private static final String SUPERVISOR_USER = "ABNEY"; 32 // private static final String SUPERVISOR_UNIVERSAL = "2237202707"; 33 // private static final String NONSUPER_USER = "BARTH"; 34 // private static final String NONSUPER_UNIVERSAL = "5998202207"; 35 // private static final String NONSUPER_2_USER = "COOTER"; 36 // private static final String NONSUPER_2_UNIVERSAL = "598746310"; 37 // 38 // private DocumentAuthorizer documentAuthorizer; 39 // private PessimisticLockService pessimisticLock; 40 // 41 // @Override 42 // public void setUp() throws Exception { 43 // super.setUp(); 44 // documentAuthorizer = new DocumentAuthorizerBase(); 45 // pessimisticLock = KRADServiceLocatorInternal.getPessimisticLockService(); 46 // } 47 // 48 // @Override 49 // public void tearDown() throws Exception { 50 // try { 51 // new ClearDatabaseLifecycle(Arrays.asList(new String[]{"KNS_PESSIMISTIC_LOCK_T"}), null).start(); 52 // } catch (Exception e) { 53 // LOG.warn("Caught exception trying to clear Pessimistic Lock table"); 54 // } 55 // super.tearDown(); 56 // } 57 // 58 // // following is the Supervisor & Initiator grid 59 // 60 // // ## Supervisor? UserIsInitiator? ApprovalRequested? canSupervise Result 61 // // ----------------------------------------------------------------------------------- 62 // // A true true true true 63 // // 64 // // B true true false false 65 // // 66 // // C true false true true 67 // // 68 // // D true false false true 69 // // 70 // // E false * * false 71 // // 72 73 /* This test can no longer use mock objects because the isSupervisor() method is no longer on the Person object. 74 * To check for supervisor status you know must to through the group service and check membership in the supervisor group. 75 */ 76 @Test 77 public void testCanSuperviseAsInitiatorA() { 78 // 79 // DocumentActionFlags flags; 80 // Document document; 81 // 82 // // scenario A 83 // //document = new MockDocument(getSuperUser(), true); 84 // //flags = documentAuthorizer.getDocumentActionFlags(document, getSuperUser()); 85 // //assertTrue(flags.getCanSupervise()); 86 // 87 // // scenario B 88 // document = new MockDocument(getSuperUser(), false); 89 // flags = documentAuthorizer.getDocumentActionFlags(document, getSuperUser()); 90 // assertFalse(flags.getCanSupervise()); 91 // 92 // // scenario C 93 // document = new MockDocument(getNonSuperUser(), true); 94 // flags = documentAuthorizer.getDocumentActionFlags(document, getSuperUser()); 95 // assertTrue(flags.getCanSupervise()); 96 // 97 // // scenario D 98 // document = new MockDocument(getNonSuperUser(), false); 99 // flags = documentAuthorizer.getDocumentActionFlags(document, getSuperUser()); 100 // assertTrue(flags.getCanSupervise()); 101 // 102 } 103 104 // 105 // private void verifyEditModeExists(Map editMode, String editModeToTest, String expectedValue) { 106 // assertNotNull("Edit mode map should have a valid value for " + editModeToTest, editMode.get(editModeToTest)); 107 // assertEquals("Edit mode map should contain entry with key '" + editModeToTest + "' and value '" + expectedValue + "'", expectedValue, editMode.get(editModeToTest)); 108 // } 109 // 110 // private void verifyEditModeDoesNotExist(Map editMode, String editModeToTest) { 111 // assertNull("Edit mode map should not have an entry for " + editModeToTest, editMode.get(editModeToTest)); 112 // } 113 // 114 // protected Person getWorkflowPessimisticLockOwnerUser() { 115 // Person person = KimImplServiceLocator.getPersonService().getPersonByPrincipalName(KNSConstants.SYSTEM_USER); 116 // if (person == null) { 117 // throw new RuntimeException("Cannot find user with network id '" + KNSConstants.SYSTEM_USER); 118 // } 119 // return person; 120 // } 121 // 122 // /** 123 // * This method tests the Pessimistic locking mechanism when no custom lock descriptors are being used by the document in 124 // * the default manner (locks are unique to user and document) 125 // * 126 // * @throws Exception 127 // */ 128 @Test 129 public void testEstablishLocks_DefaultDocumentAuthorizerBase() throws Exception { 130 // PessimisticLockService lockService = KRADServiceLocatorInternal.getPessimisticLockService(); 131 // 132 // // test no lock creation needed 133 // Document document = new MockDocument(getNonSuperUser(), false); 134 // int originalExpectedLockCount = 0; 135 // // no lock should be created 136 // int expectedLockCountIncrease = 0; 137 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 138 // Map oldEditMode = getEditMode_ReadOnly(); 139 // String testString = "dummyString"; 140 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 141 // Map newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 142 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 143 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 144 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 145 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 146 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 147 // 148 // // test valid lock creation where no user has any locks and edit modes returned are same as passed in 149 // document = new MockDocument(getNonSuperUser(), false); 150 // originalExpectedLockCount = 0; 151 // // one lock should be created 152 // expectedLockCountIncrease = 1; 153 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 154 // oldEditMode = getEditMode_FullEntry(); 155 // testString = "dummyString"; 156 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 157 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 158 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 159 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 160 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 161 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY); 162 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 163 // 164 // // test invalid lock status (2 locks by 2 different users) throws exception 165 // document = new MockDocument(getNonSuperUser(), false); 166 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getNonSuperUser())); 167 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getSuperUser())); 168 // try { 169 // this.pessimisticLock.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 170 // fail("Document authorizer should have thrown exception of type PessimisticLockingException"); 171 // } catch (PessimisticLockingException e) { 172 // // expected result 173 // } catch (Exception e) { 174 // fail("Document authorizer threw an exception but it was not of type PessimisticLockingException... it was a " + e.getClass().getName()); 175 // } 176 // 177 // // test valid lock status (2 locks by 2 different users with one user being workflow lock user) does not throw 178 // // exception 179 // document = new MockDocument(getNonSuperUser(), false); 180 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getNonSuperUser())); 181 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getWorkflowPessimisticLockOwnerUser())); 182 // try { 183 // this.pessimisticLock.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 184 // } catch (Exception e) { 185 // fail("Document authorizer should not have thrown an exception but did with type '" + e.getClass().getName() + "' and message: " + e.getMessage()); 186 // } 187 // 188 // // test other user already has lock should return editMode of ReadOnly 189 // document = new MockDocument(getNonSuperUser(), false); 190 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getSuperUser())); 191 // originalExpectedLockCount = 1; 192 // // one lock should be created 193 // expectedLockCountIncrease = 0; 194 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 195 // oldEditMode = getEditMode_FullEntry(); 196 // testString = "dummyString"; 197 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 198 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 199 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 200 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 201 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 202 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 203 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 204 // 205 // // test current user already has lock and that edit modes returned are same as passed in 206 // document = new MockDocument(getNonSuperUser(), false); 207 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), getNonSuperUser())); 208 // originalExpectedLockCount = 1; 209 // // one lock should be created 210 // expectedLockCountIncrease = 0; 211 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 212 // oldEditMode = getEditMode_FullEntry(); 213 // testString = "dummyString"; 214 // oldEditMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, testString); 215 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 216 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 217 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 218 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE); 219 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 220 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, testString); 221 // 222 } 223 // 224 // /** 225 // * This method tests the Pessimistic locking mechanism when custom lock descriptors are being used by the document in the 226 // * default manner (locks are unique to user, document, and lock descriptor) 227 // * 228 // * @throws Exception 229 // */ 230 @Test 231 public void testEstablishLocks_CustomDocumentAuthorizerBase() throws Exception { 232 // GlobalVariables.setUserSession(new UserSession("quickstart")); 233 // PessimisticLockService lockService = KRADServiceLocatorInternal.getPessimisticLockService(); 234 // DocumentAuthorizer documentAuthorizer = new TestDocumentAuthorizerBase(); 235 // String lockDescriptor1 = "test the first lock"; 236 // String lockDescriptor2 = "this is the second lock"; 237 // String lockDescriptor3 = "booyah locks"; 238 // 239 // // test no lock creation needed 240 // Document document = new MockDocument(getNonSuperUser(), false); 241 // String expectedLockDescriptor = lockDescriptor1; 242 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 243 // int originalExpectedLockCount = 0; 244 // // no lock should be created 245 // int expectedLockCountIncrease = 0; 246 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 247 // Map oldEditMode = getEditMode_ReadOnly(); 248 // String testString = "dummyString"; 249 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 250 // Map newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 251 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 252 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 253 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 254 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 255 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 256 // 257 // // test lock descriptors nullify PessimisticLockingException throwing 258 // document = new MockDocument(getNonSuperUser(), false); 259 // expectedLockDescriptor = lockDescriptor3; 260 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 261 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getNonSuperUser())); 262 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getSuperUser())); 263 // try { 264 // this.pessimisticLock.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 265 // } catch (Exception e) { 266 // fail("Document authorizer should not have thrown an exception but did with type '" + e.getClass().getName() + "' and message: " + e.getMessage()); 267 // } 268 // 269 // // test valid lock status (2 locks by 2 different users with one user being workflow lock user) does not throw 270 // // exception 271 // document = new MockDocument(getNonSuperUser(), false); 272 // expectedLockDescriptor = lockDescriptor3; 273 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 274 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getNonSuperUser())); 275 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getWorkflowPessimisticLockOwnerUser())); 276 // try { 277 // //documentAuthorizer.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 278 // this.pessimisticLock.establishLocks(document,getEditMode_FullEntry(), getNonSuperUser()); 279 // } catch (Exception e) { 280 // fail("Document authorizer should not have thrown an exception but did with type '" + e.getClass().getName() + "' and message: " + e.getMessage()); 281 // } 282 // 283 // // test valid lock creation with descriptor where no user has any locks and edit modes returned are same as passed in 284 // document = new MockDocument(getNonSuperUser(), false); 285 // expectedLockDescriptor = lockDescriptor3; 286 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 287 // originalExpectedLockCount = 0; 288 // // one lock should be created 289 // expectedLockCountIncrease = 1; 290 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 291 // oldEditMode = getEditMode_FullEntry(); 292 // testString = "dummyString"; 293 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 294 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 295 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 296 // assertEquals("Lock descriptor from new lock is incorrect", expectedLockDescriptor, document.getPessimisticLocks().get(0).getLockDescriptor()); 297 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 298 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 299 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY); 300 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 301 // 302 // // test other user already has lock with same descriptor should return editMode of ReadOnly 303 // document = new MockDocument(getNonSuperUser(), false); 304 // expectedLockDescriptor = lockDescriptor1; 305 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 306 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getSuperUser())); 307 // originalExpectedLockCount = 1; 308 // // no lock should be created 309 // expectedLockCountIncrease = 0; 310 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 311 // oldEditMode = getEditMode_FullEntry(); 312 // testString = "dummyString"; 313 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 314 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 315 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 316 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 317 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 318 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 319 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 320 // assertNotNull("Establish locks should return a UNVIEWABLE edit mode", newEditMode.get(AuthorizationConstants.EditMode.UNVIEWABLE)); 321 // assertEquals("Establish locks method returned invalid UNVIEWABLE edit mode", testString, newEditMode.get(AuthorizationConstants.EditMode.UNVIEWABLE)); 322 // 323 // // test other user already has lock with different descriptor should return given edit modes 324 // document = new MockDocument(getNonSuperUser(), false); 325 // expectedLockDescriptor = lockDescriptor1; 326 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 327 // PessimisticLock oldLock = lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getNonSuperUser()); 328 // document.addPessimisticLock(oldLock); 329 // originalExpectedLockCount = 1; 330 // // one lock should be created 331 // expectedLockCountIncrease = 1; 332 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 333 // oldEditMode = getEditMode_FullEntry(); 334 // testString = "is this the string"; 335 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 336 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 337 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 338 // for (PessimisticLock lock : document.getPessimisticLocks()) { 339 // if (!lock.getId().equals(oldLock.getId())) { 340 // assertEquals("Lock descriptor of new lock is incorrect", expectedLockDescriptor, lock.getLockDescriptor()); 341 // } 342 // } 343 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 344 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 345 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY); 346 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 347 // 348 // // test current user has lock with same lock descriptor and other users have no locks should return given edit modes 349 // document = new MockDocument(getNonSuperUser(), false); 350 // expectedLockDescriptor = lockDescriptor1; 351 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 352 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), expectedLockDescriptor, getNonSuperUser())); 353 // originalExpectedLockCount = 1; 354 // // no lock should be created 355 // expectedLockCountIncrease = 0; 356 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 357 // oldEditMode = getEditMode_FullEntry(); 358 // testString = "is this the string"; 359 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 360 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 361 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 362 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 363 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 364 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY); 365 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 366 // 367 // // test current user has lock with different lock descriptor and other users have no locks should return given edit 368 // // modes 369 // document = new MockDocument(getNonSuperUser(), false); 370 // expectedLockDescriptor = lockDescriptor1; 371 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 372 // oldLock = lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getNonSuperUser()); 373 // document.addPessimisticLock(oldLock); 374 // originalExpectedLockCount = 1; 375 // // one lock should be created 376 // expectedLockCountIncrease = 1; 377 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 378 // oldEditMode = getEditMode_FullEntry(); 379 // testString = "is this the string"; 380 // oldEditMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, testString); 381 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 382 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 383 // for (PessimisticLock lock : document.getPessimisticLocks()) { 384 // if (!lock.getId().equals(oldLock.getId())) { 385 // assertEquals("Lock descriptor of new lock is incorrect", expectedLockDescriptor, lock.getLockDescriptor()); 386 // } 387 // } 388 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 389 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 390 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, testString); 391 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE); 392 // 393 // // test invalid lock status (2 locks with same lock descriptor by 2 different users) throws exception 394 // document = new MockDocument(getNonSuperUser(), false); 395 // expectedLockDescriptor = lockDescriptor3; 396 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 397 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getNonSuperUser())); 398 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getSecondNonSuperUser())); 399 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getSuperUser())); 400 // try { 401 // this.pessimisticLock.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 402 // fail("Document authorizer should have thrown exception of type PessimisticLockingException"); 403 // } catch (PessimisticLockingException e) { 404 // // expected result 405 // } catch (Exception e) { 406 // fail("Document authorizer threw an exception but it was not of type PessimisticLockingException... it was a " + e.getClass().getName()); 407 // } 408 // 409 // // test valid lock status (2 locks by 2 different users with one user being workflow lock user) does not throw 410 // // exception 411 // document = new MockDocument(getNonSuperUser(), false); 412 // expectedLockDescriptor = lockDescriptor3; 413 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 414 // Person user = getWorkflowPessimisticLockOwnerUser(); 415 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, user)); 416 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getSuperUser())); 417 // try { 418 // this.pessimisticLock.establishLocks(document, getEditMode_FullEntry(), getNonSuperUser()); 419 // } catch (Exception e) { 420 // fail("Document authorizer should not have thrown an exception but did with type '" + e.getClass().getName() + "' and message: " + e.getMessage()); 421 // } 422 // 423 // // test current user has lock with different descriptor and other users have locks with same descriptors should 424 // // return read only editmodes 425 // document = new MockDocument(getNonSuperUser(), false); 426 // expectedLockDescriptor = lockDescriptor3; 427 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 428 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getNonSuperUser())); 429 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getSuperUser())); 430 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getSecondNonSuperUser())); 431 // originalExpectedLockCount = 3; 432 // // no lock should be created 433 // expectedLockCountIncrease = 0; 434 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 435 // oldEditMode = getEditMode_FullEntry(); 436 // testString = "is this the string"; 437 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 438 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 439 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 440 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 441 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 442 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 443 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 444 // 445 // // test current user has lock with same descriptor and other users have locks with different descriptors should 446 // // return given editmodes 447 // document = new MockDocument(getNonSuperUser(), false); 448 // expectedLockDescriptor = lockDescriptor3; 449 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 450 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), expectedLockDescriptor, getNonSuperUser())); 451 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getSuperUser())); 452 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getSecondNonSuperUser())); 453 // originalExpectedLockCount = 3; 454 // // no lock should be created 455 // expectedLockCountIncrease = 0; 456 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 457 // oldEditMode = getEditMode_FullEntry(); 458 // testString = "is this the string"; 459 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 460 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 461 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 462 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 463 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 464 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY); 465 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 466 // 467 // // test current user has lock with different lock descriptor and other users have locks with different lock 468 // // descriptor should return given edit modes 469 // document = new MockDocument(getNonSuperUser(), false); 470 // expectedLockDescriptor = lockDescriptor1; 471 // List<Long> existingLockIds = new ArrayList<Long>(); 472 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 473 // oldLock = lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getSecondNonSuperUser()); 474 // existingLockIds.add(oldLock.getId()); 475 // document.addPessimisticLock(oldLock); 476 // oldLock = lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getSecondNonSuperUser()); 477 // existingLockIds.add(oldLock.getId()); 478 // document.addPessimisticLock(oldLock); 479 // oldLock = lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getSuperUser()); 480 // existingLockIds.add(oldLock.getId()); 481 // document.addPessimisticLock(oldLock); 482 // originalExpectedLockCount = existingLockIds.size(); 483 // // one lock should be created 484 // expectedLockCountIncrease = 1; 485 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 486 // oldEditMode = getEditMode_FullEntry(); 487 // testString = "is this the string"; 488 // oldEditMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, testString); 489 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 490 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 491 // for (PessimisticLock lock : document.getPessimisticLocks()) { 492 // if (!existingLockIds.contains(lock.getId())) { 493 // // this must be the newly created lock 494 // assertEquals("Lock descriptor of new lock is incorrect", expectedLockDescriptor, lock.getLockDescriptor()); 495 // } 496 // } 497 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 498 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 499 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, testString); 500 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE); 501 // 502 // // test current user has lock with wrong descriptor and no entry edit modes return given editmodes 503 // document = new MockDocument(getNonSuperUser(), false); 504 // expectedLockDescriptor = lockDescriptor3; 505 // GlobalVariables.getUserSession().addObject(TestDocumentAuthorizerBase.USER_SESSION_OBJECT_KEY, (Object) expectedLockDescriptor); 506 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor1, getNonSuperUser())); 507 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor3, getSuperUser())); 508 // document.addPessimisticLock(lockService.generateNewLock(document.getDocumentNumber(), lockDescriptor2, getSecondNonSuperUser())); 509 // originalExpectedLockCount = 3; 510 // // no lock should be created 511 // expectedLockCountIncrease = 0; 512 // assertEquals("Lock count is incorrect", originalExpectedLockCount, document.getPessimisticLocks().size()); 513 // oldEditMode = getEditMode_ReadOnly(); 514 // testString = "is this the string"; 515 // oldEditMode.put(AuthorizationConstants.EditMode.UNVIEWABLE, testString); 516 // newEditMode = this.pessimisticLock.establishLocks(document, oldEditMode, getNonSuperUser()); 517 // assertEquals("Lock count is incorrect", originalExpectedLockCount + expectedLockCountIncrease, document.getPessimisticLocks().size()); 518 // assertEquals("Establish locks method returned invalid number of edit modes", oldEditMode.size(), newEditMode.size()); 519 // verifyEditModeDoesNotExist(newEditMode, AuthorizationConstants.EditMode.FULL_ENTRY); 520 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.VIEW_ONLY, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 521 // verifyEditModeExists(newEditMode, AuthorizationConstants.EditMode.UNVIEWABLE, testString); 522 } 523 // 524 @Test 525 public void testHasPreRouteEditAuthorization() throws Exception { 526 // //TODO: the test for preRouteEditAuthorization should be removed 527 // GlobalVariables.setUserSession(new UserSession("quickstart")); 528 // PessimisticLockTestDocumentAuthorizer lockDocumentAuthorizer = new PessimisticLockTestDocumentAuthorizer(); 529 // lockDocumentAuthorizer.USES_PESSIMISTIC_LOCKING = false; 530 // 531 // Person initiatorUser = getNonSuperUser(); 532 // Person authorizerUser = getNonSuperUser(); 533 // Document document = new MockDocument(initiatorUser, false); 534 // assertTrue(authorizerUser + " should have Initiate Authorization due to initator being " + initiatorUser, lockDocumentAuthorizer.hasInitiateAuthorization(document, authorizerUser)); 535 // //assertTrue(authorizerUser + " should have Pre Route Edit Authorization", lockDocumentAuthorizer.hasPreRouteEditAuthorization(document, authorizerUser)); 536 // 537 // initiatorUser = getNonSuperUser(); 538 // authorizerUser = getSuperUser(); 539 // document = new MockDocument(initiatorUser, false); 540 // assertFalse(authorizerUser + " should not have Initiate Authorization due to initator being " + initiatorUser, lockDocumentAuthorizer.hasInitiateAuthorization(document, authorizerUser)); 541 // //assertFalse(authorizerUser + " should not have Pre Route Edit Authorization", lockDocumentAuthorizer.hasPreRouteEditAuthorization(document, authorizerUser)); 542 // 543 // // switch to using pessimistic locking for the next tests 544 // lockDocumentAuthorizer.USES_PESSIMISTIC_LOCKING = true; 545 // 546 // initiatorUser = getNonSuperUser(); 547 // authorizerUser = getSuperUser(); 548 // document = new MockDocument(initiatorUser, false); 549 // assertFalse(authorizerUser + " should not have Initiate Authorization due to initator being " + initiatorUser, lockDocumentAuthorizer.hasInitiateAuthorization(document, authorizerUser)); 550 // //assertFalse(authorizerUser + " should not have Pre Route Edit Authorization", lockDocumentAuthorizer.hasPreRouteEditAuthorization(document, authorizerUser)); 551 // 552 // initiatorUser = getNonSuperUser(); 553 // authorizerUser = getNonSuperUser(); 554 // document = new MockDocument(initiatorUser, false); 555 // document.addPessimisticLock(KRADServiceLocatorInternal.getPessimisticLockService().generateNewLock(document.getDocumentNumber(), getSecondNonSuperUser())); 556 // assertTrue(authorizerUser + " should have Initiate Authorization due to initator being " + initiatorUser, lockDocumentAuthorizer.hasInitiateAuthorization(document, authorizerUser)); 557 // //assertFalse(authorizerUser + " should not have Pre Route Edit Authorization", lockDocumentAuthorizer.hasPreRouteEditAuthorization(document, authorizerUser)); 558 // 559 // initiatorUser = getNonSuperUser(); 560 // authorizerUser = getNonSuperUser(); 561 // document = new MockDocument(initiatorUser, false); 562 // document.addPessimisticLock(KRADServiceLocatorInternal.getPessimisticLockService().generateNewLock(document.getDocumentNumber(), authorizerUser)); 563 // assertTrue(authorizerUser + " should have Initiate Authorization due to initator being " + initiatorUser, lockDocumentAuthorizer.hasInitiateAuthorization(document, authorizerUser)); 564 // //assertTrue(authorizerUser + " should have Pre Route Edit Authorization", lockDocumentAuthorizer.hasPreRouteEditAuthorization(document, authorizerUser)); 565 } 566 // 567 @Test 568 public void testGetDocumentActionFlagsUsingPessimisticLocking() throws Exception { 569 // GlobalVariables.setUserSession(new UserSession("quickstart")); 570 // String editModeTrueValue = "TRUE"; 571 // String editModeFalseValue = "FALSE"; 572 // PessimisticLockTestDocumentAuthorizer lockDocumentAuthorizer = new PessimisticLockTestDocumentAuthorizer(); 573 // 574 // Person initiatorUser = getNonSuperUser(); 575 // Person authorizerUser = getNonSuperUser(); 576 // Document document = new MockDocument(initiatorUser, false); 577 // lockDocumentAuthorizer.USES_PESSIMISTIC_LOCKING = false; 578 // Map editMode = new HashMap(); 579 // lockDocumentAuthorizer.setEditMode(MapUtils.putAll(editMode, new Map.Entry[]{new DefaultMapEntry(AuthorizationConstants.EditMode.VIEW_ONLY,"TRUE")})); 580 // DocumentFormPlaceholder formDummy = implementDocumentAuthorizerMethods(lockDocumentAuthorizer, document, new MockPerson(GlobalVariables.getUserSession().getPerson())); 581 } 582 // 583 // /** 584 // * This is a stand-in object for a {@link KualiDocumentFormBase} object 585 // */ 586 // private class DocumentFormPlaceholder { 587 // public Map editingMode; 588 // public DocumentActionFlags documentActionFlags; 589 // } 590 // 591 // /** 592 // * This method is a stand-in method to duplicate the behavior of 593 // * {@link KualiDocumentFormBase#populateAuthorizationFields(DocumentAuthorizer)} and 594 // * {@link KualiDocumentFormBase#useDocumentAuthorizer(DocumentAuthorizer)} 595 // * 596 // * @param document - 597 // * document to use for edit modes and action flags 598 // * @param kualiUser - 599 // * user to use in place of what may be in GlobalVariables 600 // * @return a stand-in object for {@link KualiDocumentFormBase} that holds the edit mode map and the 601 // * {@link DocumentActionFlags} object that would be held in the {@link KualiDocumentFormBase} 602 // */ 603 // private DocumentFormPlaceholder implementDocumentAuthorizerMethods(PessimisticLockTestDocumentAuthorizer documentAuthorizer, Document document, Person kualiUser) { 604 // // Person kualiUser = GlobalVariables.getUserSession().getPerson(); 605 // // DocumentAuthorizer documentAuthorizer = KRADServiceLocatorInternal.getDocumentAuthorizationService().getDocumentAuthorizer(document); 606 // Map editMode = documentAuthorizer.getEditMode(document, kualiUser); 607 // if (documentAuthorizer.usesPessimisticLocking(document)) { 608 // editMode = this.pessimisticLock.establishLocks(document, editMode, kualiUser); 609 // } 610 // DocumentFormPlaceholder formPlaceholder = new DocumentFormPlaceholder(); 611 // formPlaceholder.editingMode = editMode; 612 // formPlaceholder.documentActionFlags = documentAuthorizer.getDocumentActionFlags(document, kualiUser); 613 // return formPlaceholder; 614 // } 615 // 616 // private Person getSuperUser() { 617 // return new MockPerson(SUPERVISOR_UNIVERSAL, SUPERVISOR_USER, true); 618 // } 619 // 620 // private Person getNonSuperUser() { 621 // return new MockPerson(NONSUPER_UNIVERSAL, NONSUPER_USER, false); 622 // } 623 // 624 // private Person getSecondNonSuperUser() { 625 // return new MockPerson(NONSUPER_2_UNIVERSAL, NONSUPER_2_USER, false); 626 // } 627 // 628 // private Map getEditMode_ReadOnly() { 629 // return constructEditMode(AuthorizationConstants.EditMode.VIEW_ONLY); 630 // } 631 // 632 // private Map getEditMode_FullEntry() { 633 // return constructEditMode(AuthorizationConstants.EditMode.FULL_ENTRY); 634 // } 635 // 636 // private Map constructEditMode(String editModeValue) { 637 // Map editMode = new HashMap(); 638 // editMode.put(editModeValue, DocumentAuthorizerBase.EDIT_MODE_DEFAULT_TRUE_VALUE); 639 // return editMode; 640 // } 641 // 642 // private class MockDocument extends DocumentBase { 643 // 644 // private MockDocument() { 645 // super(); 646 // } 647 // 648 // public MockDocument(Person initiator, boolean isApprovalRequested) { 649 // this(); 650 // this.documentNumber = "1234567890"; 651 // getDocumentHeader().setWorkflowDocument(new MockWorkflowDocument(initiator, isApprovalRequested)); 652 // } 653 // 654 // @Override 655 // protected LinkedHashMap toStringMapper() { 656 // LinkedHashMap map = new LinkedHashMap(); 657 // map.put("class", "MockDocument"); 658 // map.put(KNSPropertyConstants.DOCUMENT_NUMBER, documentNumber); 659 // map.put("initiator", getDocumentHeader().getWorkflowDocument().getInitiatorNetworkId()); 660 // return map; 661 // } 662 // 663 // public boolean getAllowsCopy() { 664 // return false; 665 // } 666 // 667 // } 668 // 669 // private class MockWorkflowDocument extends MockWorkflowDocument { 670 // 671 // private Person initiator; 672 // private boolean approvalRequested; 673 // 674 // private MockWorkflowDocument() {}; 675 // 676 // public MockWorkflowDocument(Person initiator, boolean isApprovalRequested) { 677 // this.initiator = initiator; 678 // this.approvalRequested = isApprovalRequested; 679 // } 680 // 681 // public String getInitiatorNetworkId() { 682 // return initiator.getPrincipalName(); 683 // } 684 // 685 // public boolean isApprovalRequested() { 686 // return approvalRequested; 687 // } 688 // 689 // public boolean userIsInitiator(Person user) { 690 // return initiator.getPrincipalId().equalsIgnoreCase(user.getPrincipalId()); 691 // } 692 // 693 // /** 694 // * This overridden method ... 695 // * 696 // * @see org.kuali.rice.krad.workflow.service.KualiWorkflowDocument#appSpecificRouteDocumentToGroup(java.lang.String, java.lang.String, int, java.lang.String, org.kuali.rice.kim.bo.group.dto.GroupInfo, java.lang.String, boolean) 697 // */ 698 // public void appSpecificRouteDocumentToGroup(String actionRequested, 699 // String routeTypeName, String annotation, 700 // GroupIdDTO groupId, String responsibilityDesc, 701 // boolean forceAction) throws WorkflowException { 702 // // TODO sp20369 - THIS METHOD NEEDS JAVADOCS 703 // 704 // } 705 // 706 // } 707 // 708 // private class MockPerson extends PersonImpl { 709 // private boolean supervisor; 710 // private String principalId; 711 // private String principalName; 712 // 713 // private MockPerson() {}; 714 // 715 // public MockPerson(Person user) { 716 // this(user.getPrincipalId(), user.getPrincipalName(), KimApiServiceLocator.getIdentityManagementService().isMemberOfGroup(user.getPrincipalId(), org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_KFS_NAMESPACE_CODE, KRADServiceLocatorInternal.getKualiConfigurationService().getPropertyValueAsString(KNSConstants.KNS_NAMESPACE, KNSConstants.DetailTypes.DOCUMENT_DETAIL_TYPE, KNSConstants.CoreApcParms.SUPERVISOR_WORKGROUP))); 717 // } 718 // 719 // public MockPerson(String universalId, String userId, boolean supervisor) { 720 // this.principalId = universalId; 721 // this.principalName = userId; 722 // this.supervisor = supervisor; 723 // } 724 // 725 // public boolean isSupervisorUser() { 726 // return supervisor; 727 // } 728 // 729 // public String toString() { 730 // return ((supervisor) ? "Supervisor " : "") + "User (" + getPrincipalName() + ")"; 731 // } 732 // 733 // public String getPrincipalId() { 734 // return this.principalId == null ? super.getPrincipalId() : this.principalId; 735 // } 736 // 737 // public String getPrincipalName() { 738 // return this.principalName == null ? super.getPrincipalName() : this.principalName; 739 // } 740 // 741 // } 742 }