Search in sources :

Example 6 with EzySessionManager

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

the class EzySimpleDataHandlerTest method handleRequestExceptionCase2.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void handleRequestExceptionCase2() throws Exception {
    int zoneId = 1;
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    EzyZone zone = mock(EzyZone.class);
    when(zoneContext.getZone()).thenReturn(zone);
    EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
    when(zone.getUserManager()).thenReturn(zoneUserManager);
    when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    EzyServer server = mock(EzyServer.class);
    when(serverContext.getServer()).thenReturn(server);
    EzyServerControllers controllers = mock(EzyServerControllers.class);
    EzyInterceptor streamingInterceptor = mock(EzyInterceptor.class);
    when(controllers.getStreamingInterceptor()).thenReturn(streamingInterceptor);
    EzyStreamingController streamingController = mock(EzyStreamingController.class);
    when(controllers.getStreamingController()).thenReturn(streamingController);
    EzyInterceptor loginInterceptor = mock(EzyInterceptor.class);
    when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInterceptor);
    EzyController loginController = mock(EzyController.class);
    when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
    when(server.getControllers()).thenReturn(controllers);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    when(server.getSessionManager()).thenReturn(sessionManager);
    EzyCloseSession closeSession = mock(EzyCloseSession.class);
    when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
    EzySettings settings = mock(EzySettings.class);
    when(settings.isDebug()).thenReturn(true);
    when(server.getSettings()).thenReturn(settings);
    EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
    when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
    EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
    when(settings.getLogger()).thenReturn(loggerSetting);
    EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
    when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
    when(ignoredCommandsSetting.getCommands()).thenReturn(new HashSet<>());
    EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
    when(maxRequestPerSecond.getValue()).thenReturn(3);
    when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
    when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
    MyTestDataHandler handler = new MyTestDataHandler(serverContext, session);
    when(session.getDelegate()).thenReturn(handler);
    when(session.isActivated()).thenReturn(true);
    EzyArray loginData = EzyEntityFactory.newArrayBuilder().append("zone").append("username").append("password").append(EzyEntityFactory.newObject()).build();
    doThrow(new IllegalStateException("server maintain")).when(loginInterceptor).intercept(any(), any());
    FieldUtil.setFieldValue(handler, "context", null);
    FieldUtil.setFieldValue(handler, "active", false);
    MethodInvoker.create().object(handler).method("handleRequest").param(EzyConstant.class, EzyCommand.LOGIN).param(EzyArray.class, loginData).invoke();
}
Also used : EzyZone(com.tvd12.ezyfoxserver.EzyZone) EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyStreamingController(com.tvd12.ezyfoxserver.controller.EzyStreamingController) EzyLoggerSetting(com.tvd12.ezyfoxserver.setting.EzyLoggerSetting) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzyCloseSession(com.tvd12.ezyfoxserver.command.EzyCloseSession) EzySessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 7 with EzySessionManager

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

the class EzySimpleDataHandlerTest method handleRequestExceptionCase1.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void handleRequestExceptionCase1() throws Exception {
    int zoneId = 1;
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    EzyZone zone = mock(EzyZone.class);
    when(zoneContext.getZone()).thenReturn(zone);
    EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
    when(zone.getUserManager()).thenReturn(zoneUserManager);
    when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    EzyServer server = mock(EzyServer.class);
    when(serverContext.getServer()).thenReturn(server);
    EzyServerControllers controllers = mock(EzyServerControllers.class);
    EzyInterceptor streamingInterceptor = mock(EzyInterceptor.class);
    when(controllers.getStreamingInterceptor()).thenReturn(streamingInterceptor);
    EzyStreamingController streamingController = mock(EzyStreamingController.class);
    when(controllers.getStreamingController()).thenReturn(streamingController);
    EzyInterceptor loginInterceptor = mock(EzyInterceptor.class);
    when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInterceptor);
    EzyController loginController = mock(EzyController.class);
    when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
    when(server.getControllers()).thenReturn(controllers);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    when(server.getSessionManager()).thenReturn(sessionManager);
    EzyCloseSession closeSession = mock(EzyCloseSession.class);
    when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
    EzySettings settings = mock(EzySettings.class);
    when(settings.isDebug()).thenReturn(true);
    when(server.getSettings()).thenReturn(settings);
    EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
    when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
    EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
    when(settings.getLogger()).thenReturn(loggerSetting);
    EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
    when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
    when(ignoredCommandsSetting.getCommands()).thenReturn(new HashSet<>());
    EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
    when(maxRequestPerSecond.getValue()).thenReturn(3);
    when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
    when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
    MyTestDataHandler handler = new MyTestDataHandler(serverContext, session);
    when(session.getDelegate()).thenReturn(handler);
    when(session.isActivated()).thenReturn(true);
    EzyArray loginData = EzyEntityFactory.newArrayBuilder().append("zone").append("username").append("password").append(EzyEntityFactory.newObject()).build();
    EzyArray requestData = EzyEntityFactory.newArrayBuilder().append(EzyCommand.LOGIN.getId()).append(loginData).build();
    doThrow(new IllegalStateException("server maintain")).when(loginInterceptor).intercept(any(), any());
    FieldUtil.setFieldValue(handler, "context", null);
    handler.dataReceived(EzyCommand.LOGIN, requestData);
}
Also used : EzyZone(com.tvd12.ezyfoxserver.EzyZone) EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyStreamingController(com.tvd12.ezyfoxserver.controller.EzyStreamingController) EzyLoggerSetting(com.tvd12.ezyfoxserver.setting.EzyLoggerSetting) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzyCloseSession(com.tvd12.ezyfoxserver.command.EzyCloseSession) EzySessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 8 with EzySessionManager

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

the class EzySimpleDataHandlerTest method processMaxRequestPerSecondDisconnect.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void processMaxRequestPerSecondDisconnect() {
    // given
    EzySession session = spy(EzyAbstractSession.class);
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyServer server = mock(EzyServer.class);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    when(server.getSessionManager()).thenReturn(sessionManager);
    when(serverContext.getServer()).thenReturn(server);
    EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
    EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
    when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
    when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
    EzySettings settings = mock(EzySettings.class);
    when(server.getSettings()).thenReturn(settings);
    when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
    EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
    EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
    when(ignoredCommandsSetting.getCommands()).thenReturn(Collections.emptySet());
    when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
    when(settings.getLogger()).thenReturn(loggerSetting);
    MyTestDataHandler sut = new MyTestDataHandler(serverContext, session);
    // when
    sut.processMaxRequestPerSecond();
    // then
    verify(sessionManager, times(1)).removeSession(session, EzyDisconnectReason.MAX_REQUEST_PER_SECOND);
}
Also used : EzySessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyLoggerSetting(com.tvd12.ezyfoxserver.setting.EzyLoggerSetting) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 9 with EzySessionManager

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

the class EzyLoginControllerTest method testEzyMaxUserExceptionCase.

@SuppressWarnings("rawtypes")
@Test(expectedExceptions = EzyMaxUserException.class)
public void testEzyMaxUserExceptionCase() {
    EzyServerContext ctx = mock(EzyServerContext.class);
    EzyStatistics userStats = new EzySimpleStatistics();
    EzySimpleServer server = new EzySimpleServer();
    server.setStatistics(userStats);
    when(ctx.getServer()).thenReturn(server);
    server.setResponseApi(mock(EzyResponseApi.class));
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    server.setSessionManager(sessionManager);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(ctx.getZoneContext("example")).thenReturn(zoneContext);
    EzySimpleZone zone = new EzySimpleZone();
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zone.setSetting(zoneSetting);
    when(zoneContext.getZone()).thenReturn(zone);
    EzyZoneUserManager zoneUserManager = EzyZoneUserManagerImpl.builder().maxUsers(1).build();
    zone.setUserManager(zoneUserManager);
    EzySession session = newSession();
    session.setToken("abcdef");
    EzyArray data = newLoginData();
    EzyLoginController controller = new EzyLoginController();
    EzySimpleLoginRequest request = new EzySimpleLoginRequest();
    request.deserializeParams(data);
    request.setSession(session);
    controller.handle(ctx, request);
    data.set(1, "test1");
    request.deserializeParams(data);
    controller.handle(ctx, request);
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyLoginController(com.tvd12.ezyfoxserver.controller.EzyLoginController) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySimpleLoginRequest(com.tvd12.ezyfoxserver.request.EzySimpleLoginRequest) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleZoneSetting(com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting) Test(org.testng.annotations.Test)

Example 10 with EzySessionManager

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

the class EzyLoginProcessorTest method applyWithStreamingEnable.

@SuppressWarnings("rawtypes")
@Test
public void applyWithStreamingEnable() {
    // given
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zoneSetting.getStreaming().setEnable(true);
    EzySimpleZone zone = new EzySimpleZone();
    zone.setSetting(zoneSetting);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(zoneContext.getZone()).thenReturn(zone);
    EzySimpleServer server = new EzySimpleServer();
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    server.setSessionManager(sessionManager);
    EzyZoneUserManager userManager = EzyZoneUserManagerImpl.builder().build();
    zone.setUserManager(userManager);
    EzyStatistics statistics = mock(EzyStatistics.class);
    EzyUserStatistics userStatistics = mock(EzyUserStatistics.class);
    when(statistics.getUserStats()).thenReturn(userStatistics);
    server.setStatistics(statistics);
    EzyServerContext serverContext = mock(EzyServerContext.class);
    when(serverContext.getServer()).thenReturn(server);
    EzyLoginProcessor sut = new EzyLoginProcessor(serverContext);
    EzySessionDelegate sessionDelegate = mock(EzySessionDelegate.class);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    session.setDelegate(sessionDelegate);
    EzyUserLoginEvent event = mock(EzyUserLoginEvent.class);
    when(event.getUsername()).thenReturn("monkey");
    when(event.getSession()).thenReturn(session);
    when(event.isStreamingEnable()).thenReturn(true);
    // when
    sut.apply(zoneContext, event);
    // then
    Asserts.assertTrue(session.isStreamingEnable());
}
Also used : EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyUserLoginEvent(com.tvd12.ezyfoxserver.event.EzyUserLoginEvent) EzyUserStatistics(com.tvd12.ezyfoxserver.statistics.EzyUserStatistics) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySessionDelegate(com.tvd12.ezyfoxserver.delegate.EzySessionDelegate) EzySimpleZoneSetting(com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting) EzyLoginProcessor(com.tvd12.ezyfoxserver.controller.EzyLoginProcessor) Test(org.testng.annotations.Test)

Aggregations

EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)39 Test (org.testng.annotations.Test)33 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)20 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)19 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)19 EzyZoneUserManager (com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager)19 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)17 EzyServer (com.tvd12.ezyfoxserver.EzyServer)12 EzyServerControllers (com.tvd12.ezyfoxserver.wrapper.EzyServerControllers)12 EzyZone (com.tvd12.ezyfoxserver.EzyZone)11 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyStreamingController (com.tvd12.ezyfoxserver.controller.EzyStreamingController)11 EzyInterceptor (com.tvd12.ezyfoxserver.interceptor.EzyInterceptor)11 EzyLoggerSetting (com.tvd12.ezyfoxserver.setting.EzyLoggerSetting)11 EzySessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting)11 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)11 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)11 BaseTest (com.tvd12.test.base.BaseTest)11 EzyCloseSession (com.tvd12.ezyfoxserver.command.EzyCloseSession)10