Search in sources :

Example 21 with EzyAppUserManager

use of com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager in project ezyfox-server by youngmonkeys.

the class EzyResponseTest method test.

@Test
public void test() {
    // given
    String username1 = RandomUtil.randomShortAlphabetString();
    String username2 = RandomUtil.randomShortAlphabetString();
    EzySession session1 = mock(EzySession.class);
    EzySession session2 = mock(EzySession.class);
    EzyUser user1 = mock(EzyUser.class);
    when(user1.getSessions()).thenReturn(Collections.singletonList(session1));
    EzyUser user2 = mock(EzyUser.class);
    when(user2.getSessions()).thenReturn(Collections.singletonList(session2));
    EzyAppUserManager userManager = mock(EzyAppUserManager.class);
    when(userManager.getUser(username1)).thenReturn(user1);
    when(userManager.getUser(username2)).thenReturn(user2);
    EzyApplication app = mock(EzyApplication.class);
    when(app.getUserManager()).thenReturn(userManager);
    EzyAppContext appContext = mock(EzyAppContext.class);
    when(appContext.getApp()).thenReturn(app);
    // when
    EzyResponse sut = new EzyAppResponseImpl(appContext).usernames(username1, username2).usernames(Arrays.asList(username1, username2));
    // then
    Set<EzySession> recipients = FieldUtil.getFieldValue(sut, "recipients");
    Asserts.assertEquals(recipients, Sets.newHashSet(session1, session2));
    verify(user1, times(2)).getSessions();
    verify(user2, times(2)).getSessions();
    verify(appContext, times(1)).getApp();
    verify(app, times(1)).getUserManager();
    verify(userManager, times(2)).getUser(username1);
    verify(userManager, times(2)).getUser(username2);
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppResponseImpl) EzyResponse(com.tvd12.ezyfoxserver.command.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 22 with EzyAppUserManager

use of com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager in project ezyfox-server by youngmonkeys.

the class EzyAccessAppController method doHandle.

protected void doHandle(EzyServerContext ctx, EzyAccessAppRequest request) {
    EzyUser user = request.getUser();
    int zoneId = user.getZoneId();
    EzyAccessAppParams params = request.getParams();
    EzyZoneContext zoneContext = ctx.getZoneContext(zoneId);
    EzyAppContext appContext = zoneContext.getAppContext(params.getAppName());
    EzyApplication app = appContext.getApp();
    EzyAppSetting appSetting = app.getSetting();
    EzyAppUserManager appUserManger = app.getUserManager();
    EzySession session = request.getSession();
    String username = user.getName();
    Lock lock = appUserManger.getLock(username);
    lock.lock();
    try {
        boolean hasNotAccessed = !appUserManger.containsUser(user);
        if (hasNotAccessed) {
            checkAppUserMangerAvailable(appUserManger);
        }
        EzyUserAccessAppEvent accessAppEvent = new EzySimpleUserAccessAppEvent(user);
        appContext.handleEvent(EzyEventType.USER_ACCESS_APP, accessAppEvent);
        if (hasNotAccessed) {
            addUser(appUserManger, user, appSetting);
            EzyUserAccessedAppEvent accessedAppEvent = new EzySimpleUserAccessedAppEvent(user);
            appContext.handleEvent(EzyEventType.USER_ACCESSED_APP, accessedAppEvent);
        }
        EzyArray output = accessAppEvent.getOutput();
        EzyResponse accessAppResponse = newAccessAppResponse(appSetting, output);
        ctx.send(accessAppResponse, session, false);
    } finally {
        lock.unlock();
        appUserManger.removeLock(username);
    }
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyAccessAppParams(com.tvd12.ezyfoxserver.request.EzyAccessAppParams) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzySimpleUserAccessedAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Lock(java.util.concurrent.locks.Lock) EzyAppSetting(com.tvd12.ezyfoxserver.setting.EzyAppSetting) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyUserAccessedAppEvent(com.tvd12.ezyfoxserver.event.EzyUserAccessedAppEvent) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzyUserAccessAppEvent) EzySimpleUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent)

Example 23 with EzyAppUserManager

use of com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager in project ezyfox-server by youngmonkeys.

the class EzyExitAppController method handle.

@Override
public void handle(EzyServerContext ctx, EzyExitAppRequest request) {
    int appId = request.getParams().getAppId();
    EzyAppContext appContext = ctx.getAppContext(appId);
    EzyApplication app = appContext.getApp();
    EzyAppUserManager userManager = app.getUserManager();
    userManager.removeUser(request.getUser(), EzyUserRemoveReason.EXIT_APP);
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext)

Example 24 with EzyAppUserManager

use of com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager in project ezyfox-server by youngmonkeys.

the class EzySocketUserRemovalHandler method removeUserFromApp.

protected void removeUserFromApp(EzyAppContext appContext, EzyUserRemovedEvent event) {
    EzyUser user = event.getUser();
    EzyApplication app = appContext.getApp();
    EzyAppUserManager userManager = app.getUserManager();
    try {
        boolean contains = userManager.containsUser(user);
        if (contains) {
            userManager.removeUser(user, event.getReason());
        }
    } catch (Exception e) {
        String appName = app.getSetting().getName();
        logger.error("remove user: {} from app: {} error", event.getUser(), appName, e);
    }
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)

Example 25 with EzyAppUserManager

use of com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager in project ezyfox-server by youngmonkeys.

the class EzySocketUserRemovalHandlerTest method notifyUserRemovedToPluginsExceptionCaseTest.

@Test
public void notifyUserRemovedToPluginsExceptionCaseTest() {
    TestBlockingSocketUserRemovalQueue queue = new TestBlockingSocketUserRemovalQueue();
    EzyAppContext appContext1 = mock(EzyAppContext.class);
    EzyAppUserManager userManager1 = mock(EzyAppUserManager.class);
    when(userManager1.containsUser(any(EzyUser.class))).thenReturn(true);
    EzyApplication app1 = mock(EzyApplication.class);
    when(app1.getUserManager()).thenReturn(userManager1);
    when(appContext1.getApp()).thenReturn(app1);
    doThrow(new RuntimeException()).when(appContext1).handleEvent(any(EzyConstant.class), any(EzyUserEvent.class));
    EzyAppContext appContext2 = mock(EzyAppContext.class);
    EzyAppUserManager userManager2 = mock(EzyAppUserManager.class);
    when(userManager2.containsUser(any(EzyUser.class))).thenReturn(false);
    EzyApplication app2 = mock(EzyApplication.class);
    when(app2.getUserManager()).thenReturn(userManager2);
    when(appContext2.getApp()).thenReturn(app2);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(zoneContext.getAppContexts()).thenReturn(Lists.newArrayList(appContext1, appContext2));
    EzyZone zone = mock(EzyZone.class);
    when(zoneContext.getZone()).thenReturn(zone);
    EzyZoneSetting zoneSetting = mock(EzyZoneSetting.class);
    when(zoneSetting.getName()).thenReturn("test");
    when(zone.getSetting()).thenReturn(zoneSetting);
    doThrow(new RuntimeException()).when(zoneContext).broadcastPlugins(any(EzyConstant.class), any(EzyUserEvent.class), anyBoolean());
    EzySimpleUser user = new EzySimpleUser();
    user.setName("test");
    EzySocketUserRemoval item = new EzySimpleSocketUserRemoval(zoneContext, user, EzyUserRemoveReason.EXIT_APP);
    queue.add(item);
    EzySocketUserRemovalHandler handler = new EzySocketUserRemovalHandler(queue);
    handler.handleEvent();
    handler.destroy();
}
Also used : EzyZone(com.tvd12.ezyfoxserver.EzyZone) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyZoneSetting(com.tvd12.ezyfoxserver.setting.EzyZoneSetting) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyConstant(com.tvd12.ezyfox.constant.EzyConstant) EzyUserEvent(com.tvd12.ezyfoxserver.event.EzyUserEvent) Test(org.testng.annotations.Test)

Aggregations

EzyAppUserManager (com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager)26 EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)19 Test (org.testng.annotations.Test)18 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)16 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)9 BaseTest (com.tvd12.test.base.BaseTest)9 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)8 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)6 EzySimpleAppSetting (com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting)6 EzyEventControllers (com.tvd12.ezyfoxserver.wrapper.EzyEventControllers)6 EzyErrorScheduledExecutorService (com.tvd12.ezyfox.concurrent.EzyErrorScheduledExecutorService)5 EzySimpleApplication (com.tvd12.ezyfoxserver.EzySimpleApplication)5 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)5 EzySimpleZone (com.tvd12.ezyfoxserver.EzySimpleZone)5 EzySimpleAppContext (com.tvd12.ezyfoxserver.context.EzySimpleAppContext)5 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)5 EzySimpleZoneContext (com.tvd12.ezyfoxserver.context.EzySimpleZoneContext)5 EzySimpleAppUserDelegate (com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate)5