use of com.tvd12.ezyfoxserver.exception.EzyMaxUserException in project ezyfox-server by youngmonkeys.
the class EzyAccessAppControllerTest method accessAppFailedDueToMaxUserAfterAccess.
@Test
public void accessAppFailedDueToMaxUserAfterAccess() {
// given
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
when(serverContext.getZoneContext(1)).thenReturn(zoneContext);
EzyAppContext appContext = mock(EzyAppContext.class);
EzyApplication app = mock(EzyApplication.class);
EzySimpleAppSetting appSetting = new EzySimpleAppSetting();
appSetting.setName("test");
when(app.getSetting()).thenReturn(appSetting);
when(appContext.getApp()).thenReturn(app);
EzySimpleAppUserDelegate userDelegate = new EzySimpleAppUserDelegate();
userDelegate.setAppContext(appContext);
EzyAppUserManager appUserManager = mock(EzyAppUserManager.class);
when(appUserManager.getMaxUsers()).thenReturn(1);
when(app.getUserManager()).thenReturn(appUserManager);
when(zoneContext.getAppContext("test")).thenReturn(appContext);
EzySimpleAccessAppRequest request = newRequest(1);
EzyUser user = request.getUser();
when(appUserManager.getLock(user.getName())).thenReturn(new ReentrantLock());
doThrow(new EzyMaxUserException(1, 1)).when(appUserManager).addUser(user);
EzyAccessAppController underTest = new EzyAccessAppController();
// when
Throwable e = Asserts.assertThrows(() -> underTest.handle(serverContext, request));
// then
Asserts.assertEqualsType(e, EzyAccessAppException.class);
Asserts.assertEqualsType(e.getCause(), EzyMaxUserException.class);
verify(appUserManager, times(1)).containsUser(user);
verify(appUserManager, times(1)).getUserCount();
verify(appUserManager, times(1)).getMaxUsers();
verify(appUserManager, times(1)).getAppName();
verify(appUserManager, times(1)).getLock(user.getName());
verify(appUserManager, times(1)).removeLock(user.getName());
verify(appUserManager, times(1)).addUser(user);
verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESS_APP), any(EzySimpleUserAccessAppEvent.class));
verify(serverContext, times(1)).getZoneContext(1);
verify(zoneContext, times(1)).getAppContext("test");
verify(appContext, times(1)).getApp();
verify(app, times(1)).getSetting();
verify(app, times(1)).getUserManager();
// 1 for success, 1 for failure
verify(serverContext, times(1)).send(any(EzyResponse.class), any(EzySession.class), any(boolean.class));
}
use of com.tvd12.ezyfoxserver.exception.EzyMaxUserException in project ezyfox-server by youngmonkeys.
the class EzySimpleUserManagerTest method fromConstructorTest.
@Test
public void fromConstructorTest() {
EzySimpleUserManager manager = new EzySimpleUserManager(1);
assert manager.getMaxUsers() == 1;
assert manager.available();
EzySimpleUser user1 = new EzySimpleUser();
user1.setName("user1");
manager.addUser(user1);
try {
EzySimpleUser user2 = new EzySimpleUser();
user2.setName("user2");
manager.addUser(user2);
} catch (Exception e) {
assert e instanceof EzyMaxUserException;
}
assert !manager.available();
manager.getLock(user1.getName());
manager.clear();
manager.destroy();
}
use of com.tvd12.ezyfoxserver.exception.EzyMaxUserException in project ezyfox-server by youngmonkeys.
the class EzySimpleUserManagerTest method fromBuilderTest.
@Test
public void fromBuilderTest() {
int maxUsers = RandomUtil.randomSmallInt() + 3;
EzySimpleUserManager manager = (EzySimpleUserManager) EzySimpleUserManager.builder().maxUsers(maxUsers).build();
assert manager.getMaxUsers() == maxUsers;
assert manager.available();
EzySimpleUser user1 = new EzySimpleUser();
user1.setName("user1");
manager.addUser(user1);
try {
EzySimpleUser user2 = new EzySimpleUser();
user2.setName("user2");
manager.addUser(user2);
} catch (Exception e) {
assert e instanceof EzyMaxUserException;
}
assert manager.available();
manager.getLock(user1.getName());
manager.clear();
manager.destroy();
}
use of com.tvd12.ezyfoxserver.exception.EzyMaxUserException in project ezyfox-server by youngmonkeys.
the class EzyLoginController method handle.
@Override
public void handle(EzyServerContext ctx, EzyLoginRequest request) {
try {
EzySession session = request.getSession();
EzyLoginParams params = request.getParams();
EzyZoneContext zoneContext = ctx.getZoneContext(params.getZoneName());
EzyUserLoginEvent loginEvent = newLoginEvent(session, params);
try {
control(ctx, zoneContext, loginEvent);
} finally {
loginEvent.release();
}
} catch (EzyLoginErrorException e) {
processException(ctx, request.getSession(), e);
throw e;
} catch (EzyMaxUserException e) {
processException(ctx, request.getSession(), maximumUsers(e));
throw e;
} catch (EzyZoneNotFoundException e) {
processException(ctx, request.getSession(), zoneNotFound(e));
throw e;
} catch (Exception e) {
processException(ctx, request.getSession(), serverError(e));
throw e;
}
}
Aggregations