- Timestamp:
- 07/05/08 22:16:04 (5 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/components/server/test/ome/server/utests/sessions/SessMgrUnitTest.java
r2126 r2592 10 10 import java.util.List; 11 11 12 import ome.conditions.ApiUsageException; 12 import net.sf.ehcache.CacheManager; 13 import ome.api.local.LocalAdmin; 14 import ome.api.local.LocalQuery; 15 import ome.api.local.LocalUpdate; 16 import ome.conditions.AuthenticationException; 13 17 import ome.conditions.SecurityViolation; 14 18 import ome.conditions.SessionException; … … 20 24 import ome.model.meta.ExperimenterGroup; 21 25 import ome.model.meta.Session; 26 import ome.services.sessions.SessionContext; 22 27 import ome.services.sessions.SessionManagerImpl; 28 import ome.services.sessions.state.SessionCache; 29 import ome.services.util.Executor; 30 import ome.system.OmeroContext; 23 31 import ome.system.Principal; 24 32 import ome.system.Roles; 33 import ome.testing.MockServiceFactory; 25 34 26 35 import org.jmock.Mock; 27 36 import org.jmock.MockObjectTestCase; 28 37 import org.jmock.core.Constraint; 38 import org.jmock.core.Invocation; 39 import org.jmock.core.Stub; 29 40 import org.testng.annotations.BeforeTest; 30 41 import org.testng.annotations.Test; … … 36 47 public class SessMgrUnitTest extends MockObjectTestCase { 37 48 38 private static class TestSessionManager extends SessionManagerImpl { 39 40 } 41 42 private Mock adminMock, updateMock, queryMock; 43 private TestSessionManager mgr; 44 private Session session; 45 46 @BeforeTest 47 public void config() { 48 mgr = new TestSessionManager(); 49 mgr.setRoles(new Roles()); 50 51 session = new Session(); 52 session.setUuid("uuid"); 53 session.setId(1L); 54 } 55 49 private final class DoWorkStub implements Stub { 50 public Object invoke(Invocation i) throws Throwable { 51 Executor.Work work = (Executor.Work) i.parameterValues.get(1); 52 return work.doWork(null, null, sf); 53 } 54 55 public StringBuffer describeTo(StringBuffer sb) { 56 sb.append("calls doWork on work"); 57 return sb; 58 } 59 } 60 61 private final class TestManager extends SessionManagerImpl { 62 Session doDefine() { 63 return define("uuid", "message", System.currentTimeMillis(), 64 defaultTimeToIdle, defaultTimeToLive, "Test", "rw----"); 65 } 66 } 67 68 private final OmeroContext oc = new OmeroContext( 69 "classpath:ome/testing/empty.xml"); 70 private final MockServiceFactory sf = new MockServiceFactory(); 71 private Mock exMock; 72 private TestManager mgr; 73 private SessionCache cache; 74 75 // State 76 Session session = new Session(); 56 77 Principal principal = new Principal("u", "g", "Test"); 57 78 String credentials = "password"; … … 62 83 List<String> userRoles = Collections.singletonList("single"); 63 84 85 @BeforeTest 86 public void config() { 87 88 exMock = mock(Executor.class); 89 sf.mockAdmin = mock(LocalAdmin.class); 90 sf.mockUpdate = mock(LocalUpdate.class); 91 sf.mockQuery = mock(LocalQuery.class); 92 93 cache = new SessionCache(); 94 cache.setCacheManager(CacheManager.getInstance()); 95 96 mgr = new TestManager(); 97 mgr.setRoles(new Roles()); 98 mgr.setSessionCache(cache); 99 mgr.setExecutor((Executor) exMock.proxy()); 100 mgr.setApplicationContext(oc); 101 mgr.setDefaultTimeToIdle(100 * 1000L); 102 mgr.setDefaultTimeToLive(300 * 1000L); 103 104 session = mgr.doDefine(); 105 session.setId(1L); 106 107 user.setOmeName(principal.getName()); 108 } 109 64 110 void prepareSessionCreation() { 65 adminMock.expects(once()).method("checkPassword").will( 111 112 exMock.expects(atLeastOnce()).method("execute").will(new DoWorkStub()); 113 114 sf.mockAdmin.expects(once()).method("checkPassword").will( 66 115 returnValue(true)); 67 adminMock.expects(once()).method("userProxy").will(returnValue(user)); 68 adminMock.expects(once()).method("groupProxy").will(returnValue(group)); 69 adminMock.expects(once()).method("getMemberOfGroupIds").will( 116 sf.mockAdmin.expects(atLeastOnce()).method("userProxy").will( 117 returnValue(user)); 118 sf.mockAdmin.expects(atLeastOnce()).method("groupProxy").will( 119 returnValue(group)); 120 sf.mockAdmin.expects(atLeastOnce()).method("getMemberOfGroupIds").will( 70 121 returnValue(m_ids)); 71 adminMock.expects(once()).method("getLeaderOfGroupIds").will(122 sf.mockAdmin.expects(atLeastOnce()).method("getLeaderOfGroupIds").will( 72 123 returnValue(l_ids)); 73 adminMock.expects(once()).method("getUserRoles").will(124 sf.mockAdmin.expects(atLeastOnce()).method("getUserRoles").will( 74 125 returnValue(userRoles)); 75 adminMock.expects(once()).method("checkPassword").will(126 sf.mockAdmin.expects(once()).method("checkPassword").will( 76 127 returnValue(true)); 77 queryMock.expects(once()).method("findAllByQuery")128 sf.mockQuery.expects(once()).method("findAllByQuery") 78 129 .will( 79 130 returnValue(Collections 80 131 .singletonList(new ExperimenterGroup()))); 81 updateMock.expects(once()).method("saveObject"); 132 sf.mockUpdate.expects(atLeastOnce()).method("saveAndReturnObject") 133 .will(returnValue(session)); 134 135 } 136 137 void prepareSessionUpdate() { 138 prepareSessionCreation(); 139 // exMock.expects(atLeastOnce()).method("execute").will(new 140 // DoWorkStub()); 141 // 142 // sf.mockAdmin.expects(once()).method("userProxy") 143 // .will(returnValue(user)); 144 // sf.mockAdmin.expects(atLeastOnce()).method("groupProxy").will( 145 // returnValue(group)); 146 // sf.mockUpdate.expects(once()).method("saveAndReturnObject").will( 147 // new SetIdStub(1L)); 148 // 82 149 } 83 150 … … 91 158 92 159 prepareSessionCreation(); 93 session= mgr.create(principal, credentials);160 assert session == mgr.create(principal, credentials); 94 161 assertNotNull(session); 95 162 assertNotNull(session.getUuid()); … … 141 208 testCreateNewSession(); 142 209 session.setDefaultEventType("somethingnew"); 143 updateMock.expects(once()).method("saveAndReturnObject").will(210 sf.mockUpdate.expects(once()).method("saveAndReturnObject").will( 144 211 returnValue(rv)); 212 213 prepareSessionUpdate(); 145 214 Session test = mgr.update(session); 146 assert True(test == rv); // insists that a copy is performed215 assertFalse(test == rv); // insists that a copy is performed 147 216 } 148 217 … … 204 273 205 274 }; 206 updateMock.expects(once()).method("saveObject").with(closedSession);275 sf.mockUpdate.expects(once()).method("saveObject").with(closedSession); 207 276 mgr.close(session.getUuid()); 208 277 assertNull(mgr.find(session.getUuid())); … … 224 293 225 294 }; 226 updateMock.expects(once()).method("saveObject").with(closedSession);295 sf.mockUpdate.expects(once()).method("saveObject").with(closedSession); 227 296 mgr.close(session.getUuid()); 228 297 assertNull(mgr.find(session.getUuid())); … … 260 329 261 330 void prepareForCreateSession() { 262 adminMock.expects(once()).method("userProxy").will(returnValue(user)); 263 adminMock.expects(once()).method("groupProxy").will(returnValue(group)); 264 adminMock.expects(once()).method("getMemberOfGroupIds").will( 331 sf.mockAdmin.expects(once()).method("userProxy") 332 .will(returnValue(user)); 333 sf.mockAdmin.expects(once()).method("groupProxy").will( 334 returnValue(group)); 335 sf.mockAdmin.expects(atLeastOnce()).method("getMemberOfGroupIds").will( 265 336 returnValue(m_ids)); 266 adminMock.expects(once()).method("getLeaderOfGroupIds").will(337 sf.mockAdmin.expects(atLeastOnce()).method("getLeaderOfGroupIds").will( 267 338 returnValue(l_ids)); 268 adminMock.expects(once()).method("getUserRoles").will(339 sf.mockAdmin.expects(once()).method("getUserRoles").will( 269 340 returnValue(userRoles)); 270 adminMock.expects(once()).method("checkPassword").will(341 sf.mockAdmin.expects(once()).method("checkPassword").will( 271 342 returnValue(true)); 272 343 } 273 344 274 @Test(expectedExceptions = A piUsageException.class)345 @Test(expectedExceptions = AuthenticationException.class) 275 346 public void testCreateSessionFailsAUEOnNullPrincipal() throws Exception { 276 adminMock.expects(once()).method("checkPassword").will(347 sf.mockAdmin.expects(once()).method("checkPassword").will( 277 348 returnValue(false)); 278 349 mgr.create(null, "password"); 279 350 } 280 351 281 @Test(expectedExceptions = A piUsageException.class)352 @Test(expectedExceptions = AuthenticationException.class) 282 353 public void testCreateSessionFailsAUEOnNullOmeName() throws Exception { 283 adminMock.expects(once()).method("checkPassword").will(354 sf.mockAdmin.expects(once()).method("checkPassword").will( 284 355 returnValue(false)); 285 356 mgr.create(new Principal(null, null, null), "password"); 286 357 } 287 358 288 @Test(expectedExceptions = SecurityViolation.class)359 @Test(expectedExceptions = AuthenticationException.class) 289 360 public void testCreateSessionFailsSV() throws Exception { 290 adminMock.expects(once()).method("checkPassword").will(361 sf.mockAdmin.expects(once()).method("checkPassword").will( 291 362 returnValue(false)); 292 363 mgr.create(principal, "password"); … … 296 367 public void testChecksForDefaultGroupsOnCreation() throws Exception { 297 368 prepareForCreateSession(); 298 queryMock.expects(once()).method("findAllByQuery").will( 369 sf.mockAdmin.expects(once()).method("getDefaultGroup").will( 370 returnValue(group)); 371 sf.mockQuery.expects(once()).method("findAllByQuery").will( 299 372 returnValue(Collections.EMPTY_LIST)); 300 373 mgr.create(new Principal("user", "user", "User"), "user"); … … 321 394 fail("NYI"); 322 395 } 396 397 @Test 398 public void testReferenceCounting() throws Exception { 399 testCreateNewSession(); 400 String uuid = session.getUuid(); 401 SessionContext ctx = cache.getSessionContext(uuid); 402 403 assertEquals(1, ctx.refCount()); 404 assertNull(ctx.getSession().getClosed()); 405 406 mgr.create(new Principal(uuid)); 407 assertEquals(2, ctx.refCount()); 408 assertNull(ctx.getSession().getClosed()); 409 410 mgr.close(uuid); 411 assertEquals(1, ctx.refCount()); 412 assertNull(ctx.getSession().getClosed()); 413 414 prepareSessionUpdate(); 415 mgr.close(uuid); 416 assertEquals(0, ctx.refCount()); 417 assertNotNull(ctx.getSession().getClosed()); 418 } 323 419 }
