Search in sources :

Example 1 with EzySimpleAccessAppRequest

use of com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest in project ezyfox-server by youngmonkeys.

the class EzyAccessAppControllerTest method accessAppFailedDueToMaxUser.

@Test
public void accessAppFailedDueToMaxUser() {
    // 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 = EzyAppUserManagerImpl.builder().maxUsers(1).appName("test").userDelegate(userDelegate).build();
    when(app.getUserManager()).thenReturn(appUserManager);
    when(zoneContext.getAppContext("test")).thenReturn(appContext);
    EzySimpleAccessAppRequest request1 = newRequest(1);
    EzySimpleAccessAppRequest request2 = newRequest(2);
    EzyAccessAppController underTest = new EzyAccessAppController();
    // when
    underTest.handle(serverContext, request1);
    Throwable e = Asserts.assertThrows(() -> underTest.handle(serverContext, request2));
    // then
    Asserts.assertEqualsType(e, EzyAccessAppException.class);
    Asserts.assertEqualsType(e.getCause(), EzyMaxUserException.class);
    verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESS_APP), any(EzySimpleUserAccessAppEvent.class));
    verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESSED_APP), any(EzySimpleUserAccessedAppEvent.class));
    verify(serverContext, times(2)).getZoneContext(1);
    verify(zoneContext, times(2)).getAppContext("test");
    verify(appContext, times(2)).getApp();
    verify(app, times(2)).getSetting();
    verify(app, times(2)).getUserManager();
    // 1 for success, 1 for failure
    verify(serverContext, times(2)).send(any(EzyResponse.class), any(EzySession.class), any(boolean.class));
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleAppUserDelegate(com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzySimpleUserAccessedAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) 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 EzySimpleAccessAppRequest

use of com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest 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 3 with EzySimpleAccessAppRequest

use of com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest in project ezyfox-server by youngmonkeys.

the class EzyAccessAppControllerTest method newRequest.

protected EzySimpleAccessAppRequest newRequest(int index) {
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzySimpleUser user = new EzySimpleUser();
    user.setName("user" + index);
    user.setZoneId(1);
    EzyArray params = EzyEntityFactory.newArrayBuilder().append("test").build();
    EzySimpleAccessAppRequest request = new EzySimpleAccessAppRequest();
    request.setUser(user);
    request.setSession(session);
    request.deserializeParams(params);
    return request;
}
Also used : EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySimpleAccessAppRequest(com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest)

Example 4 with EzySimpleAccessAppRequest

use of com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest in project ezyfox-server by youngmonkeys.

the class EzyAccessAppControllerTest method accessAppSuccessfullyBut2Times.

@Test
public void accessAppSuccessfullyBut2Times() {
    // 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 = EzyAppUserManagerImpl.builder().appName("test").userDelegate(userDelegate).build();
    when(app.getUserManager()).thenReturn(appUserManager);
    when(zoneContext.getAppContext("test")).thenReturn(appContext);
    EzySimpleAccessAppRequest request = newRequest(1);
    EzyAccessAppController underTest = new EzyAccessAppController();
    // when
    underTest.handle(serverContext, request);
    underTest.handle(serverContext, request);
    // then
    verify(appContext, times(2)).handleEvent(eq(EzyEventType.USER_ACCESS_APP), any(EzySimpleUserAccessAppEvent.class));
    verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESSED_APP), any(EzySimpleUserAccessedAppEvent.class));
    verify(serverContext, times(2)).getZoneContext(1);
    verify(zoneContext, times(2)).getAppContext("test");
    verify(appContext, times(2)).getApp();
    verify(app, times(2)).getSetting();
    verify(app, times(2)).getUserManager();
    verify(serverContext, times(2)).send(any(EzyResponse.class), any(EzySession.class), any(boolean.class));
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleAppUserDelegate(com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzySimpleUserAccessedAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) 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 5 with EzySimpleAccessAppRequest

use of com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest in project ezyfox-server by youngmonkeys.

the class EzyAccessAppControllerTest method accessAppSuccessfully.

@Test
public void accessAppSuccessfully() {
    // 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 = EzyAppUserManagerImpl.builder().appName("test").userDelegate(userDelegate).build();
    when(app.getUserManager()).thenReturn(appUserManager);
    when(zoneContext.getAppContext("test")).thenReturn(appContext);
    EzySimpleAccessAppRequest request = newRequest(1);
    EzyAccessAppController underTest = new EzyAccessAppController();
    // when
    underTest.handle(serverContext, request);
    // then
    verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESS_APP), any(EzySimpleUserAccessAppEvent.class));
    verify(appContext, times(1)).handleEvent(eq(EzyEventType.USER_ACCESSED_APP), any(EzySimpleUserAccessedAppEvent.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();
    verify(serverContext, times(1)).send(any(EzyResponse.class), any(EzySession.class), any(boolean.class));
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleAppUserDelegate(com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzySimpleUserAccessedAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) 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)

Aggregations

EzySimpleAccessAppRequest (com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest)6 Test (org.testng.annotations.Test)5 EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)4 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)4 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)4 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)4 EzyAccessAppController (com.tvd12.ezyfoxserver.controller.EzyAccessAppController)4 EzySimpleAppUserDelegate (com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate)4 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzySimpleUserAccessAppEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent)4 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)4 EzySimpleAppSetting (com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting)4 EzyAppUserManager (com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager)4 BaseTest (com.tvd12.test.base.BaseTest)4 EzySimpleUserAccessedAppEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent)3 EzyArray (com.tvd12.ezyfox.entity.EzyArray)1 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)1 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)1 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)1 EzyMaxUserException (com.tvd12.ezyfoxserver.exception.EzyMaxUserException)1