Search in sources :

Example 1 with EzyMaxUserException

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));
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) ReentrantLock(java.util.concurrent.locks.ReentrantLock) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleAppUserDelegate(com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAccessAppController(com.tvd12.ezyfoxserver.controller.EzyAccessAppController) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzySimpleAccessAppRequest(com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest) EzySimpleUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with EzyMaxUserException

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();
}
Also used : EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzySimpleUserManager(com.tvd12.ezyfoxserver.wrapper.EzySimpleUserManager) EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) Test(org.testng.annotations.Test)

Example 3 with EzyMaxUserException

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();
}
Also used : EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzySimpleUserManager(com.tvd12.ezyfoxserver.wrapper.EzySimpleUserManager) EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) Test(org.testng.annotations.Test)

Example 4 with EzyMaxUserException

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;
    }
}
Also used : EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyZoneNotFoundException(com.tvd12.ezyfoxserver.exception.EzyZoneNotFoundException) EzyLoginParams(com.tvd12.ezyfoxserver.request.EzyLoginParams) EzyLoginErrorException(com.tvd12.ezyfoxserver.exception.EzyLoginErrorException) EzyUserLoginEvent(com.tvd12.ezyfoxserver.event.EzyUserLoginEvent) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyLoginErrorException(com.tvd12.ezyfoxserver.exception.EzyLoginErrorException) EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzyZoneNotFoundException(com.tvd12.ezyfoxserver.exception.EzyZoneNotFoundException)

Aggregations

EzyMaxUserException (com.tvd12.ezyfoxserver.exception.EzyMaxUserException)4 Test (org.testng.annotations.Test)3 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)2 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)2 EzySimpleUserManager (com.tvd12.ezyfoxserver.wrapper.EzySimpleUserManager)2 EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)1 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)1 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)1 EzyAccessAppController (com.tvd12.ezyfoxserver.controller.EzyAccessAppController)1 EzySimpleAppUserDelegate (com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate)1 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)1 EzySimpleUserAccessAppEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent)1 EzyUserLoginEvent (com.tvd12.ezyfoxserver.event.EzyUserLoginEvent)1 EzyLoginErrorException (com.tvd12.ezyfoxserver.exception.EzyLoginErrorException)1 EzyZoneNotFoundException (com.tvd12.ezyfoxserver.exception.EzyZoneNotFoundException)1 EzyLoginParams (com.tvd12.ezyfoxserver.request.EzyLoginParams)1 EzySimpleAccessAppRequest (com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest)1 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)1 EzySimpleAppSetting (com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting)1