Search in sources :

Example 26 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzyRequestPluginControllerTest method test.

@Test
public void test() {
    EzyRequestPluginController controller = new EzyRequestPluginController();
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(serverContext.getZoneContext(1)).thenReturn(zoneContext);
    EzyPluginContext pluginContext = mock(EzyPluginContext.class);
    when(zoneContext.getPluginContext(1)).thenReturn(pluginContext);
    EzyPlugin plugin = mock(EzyPlugin.class);
    when(pluginContext.getPlugin()).thenReturn(plugin);
    EzyPluginRequestController requestController = mock(EzyPluginRequestController.class);
    when(plugin.getRequestController()).thenReturn(requestController);
    EzySimpleRequestPluginRequest request = new EzySimpleRequestPluginRequest();
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzySimpleUser user = new EzySimpleUser();
    user.setZoneId(1);
    user.setId(1);
    user.setName("test");
    request.setSession(session);
    request.setUser(user);
    EzyArray array = EzyEntityFactory.newArrayBuilder().append(1).append(EzyEntityFactory.newArrayBuilder()).build();
    request.deserializeParams(array);
    controller.handle(serverContext, request);
}
Also used : EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzySimpleRequestPluginRequest(com.tvd12.ezyfoxserver.request.EzySimpleRequestPluginRequest) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyRequestPluginController(com.tvd12.ezyfoxserver.controller.EzyRequestPluginController) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyPluginContext(com.tvd12.ezyfoxserver.context.EzyPluginContext) EzyPluginRequestController(com.tvd12.ezyfoxserver.plugin.EzyPluginRequestController) EzyPlugin(com.tvd12.ezyfoxserver.EzyPlugin) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 27 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionCreatorTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() {
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    EzySimpleSessionManagementSetting sessionSetting = new EzySimpleSessionManagementSetting();
    EzySessionCreator creator = EzySimpleSessionCreator.builder().sessionManager(sessionManager).sessionSetting(sessionSetting).build();
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    when(sessionManager.provideSession(any())).thenReturn(session);
    assert creator.create(mock(EzyChannel.class)) == session;
}
Also used : EzySessionCreator(com.tvd12.ezyfoxserver.creator.EzySessionCreator) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 28 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzyLoginProcessorTest method processUserSessionsButNotAllowToChangeSession.

@SuppressWarnings("rawtypes")
@Test
public void processUserSessionsButNotAllowToChangeSession() {
    // given
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zoneSetting.getUserManagement().setMaxSessionPerUser(1);
    zoneSetting.getUserManagement().setAllowChangeSession(false);
    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();
    EzyUser user = mock(EzyUser.class);
    when(user.getName()).thenReturn("monkey");
    when(user.getSessionCount()).thenReturn(1);
    userManager.addUser(user);
    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
    Throwable e = Asserts.assertThrows(() -> sut.apply(zoneContext, event));
    // then
    Asserts.assertEquals(EzyLoginErrorException.class, e.getClass());
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) 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)

Example 29 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzyLoginProcessorTest method applyWithStreamingDisableByEvent.

@SuppressWarnings("rawtypes")
@Test
public void applyWithStreamingDisableByEvent() {
    // 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(false);
    // when
    sut.apply(zoneContext, event);
    // then
    Asserts.assertFalse(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)

Example 30 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzySimpleDataHandlerTest method checkMaxRequestPerSecondCase.

@SuppressWarnings("rawtypes")
@Test
public void checkMaxRequestPerSecondCase() 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();
    handler.dataReceived(EzyCommand.LOGIN, requestData);
    EzySimpleUser user = new EzySimpleUser();
    user.addSession(session);
    user.setZoneId(zoneId);
    handler.onSessionLoggedIn(user);
    handler.streamingReceived(new byte[] { 1, 2, 3 });
    EzyArray accessAppData = EzyEntityFactory.newArrayBuilder().append("app").append(EzyEntityFactory.newObject()).build();
    EzyArray requestData2 = EzyEntityFactory.newArrayBuilder().append(EzyCommand.APP_ACCESS.getId()).append(accessAppData).build();
    handler.dataReceived(EzyCommand.APP_ACCESS, requestData2);
    handler.dataReceived(EzyCommand.LOGIN, requestData);
    Thread.sleep(1200);
    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) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) 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)

Aggregations

EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)46 Test (org.testng.annotations.Test)35 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)25 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)23 BaseTest (com.tvd12.test.base.BaseTest)20 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)19 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)17 EzyZoneUserManager (com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager)16 EzyArray (com.tvd12.ezyfox.entity.EzyArray)14 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)12 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyStreamingController (com.tvd12.ezyfoxserver.controller.EzyStreamingController)11 EzyInterceptor (com.tvd12.ezyfoxserver.interceptor.EzyInterceptor)11 EzyServerControllers (com.tvd12.ezyfoxserver.wrapper.EzyServerControllers)11 EzyServer (com.tvd12.ezyfoxserver.EzyServer)10 EzyZone (com.tvd12.ezyfoxserver.EzyZone)10 EzyCloseSession (com.tvd12.ezyfoxserver.command.EzyCloseSession)10 EzyLoggerSetting (com.tvd12.ezyfoxserver.setting.EzyLoggerSetting)10 EzySessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting)10 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)10