use of com.tvd12.ezyfoxserver.context.EzyAppContext in project ezyfox-server by youngmonkeys.
the class EzyZonesStarterTest method normalCaseTest.
@Test
public void normalCaseTest() {
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleZonesSetting zonesSetting = settings.getZones();
EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
zoneSetting.setName("test");
zonesSetting.setItem(zoneSetting);
EzySimpleAppsSetting appsSetting = new EzySimpleAppsSetting();
EzySimpleAppSetting appSetting = new EzySimpleAppSetting();
appSetting.setName("apps");
appSetting.setFolder("apps");
appSetting.setEntryLoader(ExEntryLoader.class.getName());
appsSetting.setItem(appSetting);
zoneSetting.setApplications(appsSetting);
EzySimplePluginsSetting pluginsSetting = new EzySimplePluginsSetting();
EzySimplePluginSetting pluginSetting = new EzySimplePluginSetting();
pluginSetting.setName("plugins");
pluginSetting.setFolder("plugins");
pluginSetting.setEntryLoader(ExPluginEntryLoader.class.getName());
pluginsSetting.setItem(pluginSetting);
zoneSetting.setPlugins(pluginsSetting);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
server.setConfig(new EzySimpleConfig());
EzyServerContext serverContext = mock(EzyServerContext.class);
when(serverContext.getServer()).thenReturn(server);
EzySimpleZone zone = new EzySimpleZone();
zone.setSetting(zoneSetting);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
when(zoneContext.getZone()).thenReturn(zone);
when(serverContext.getZoneContext("test")).thenReturn(zoneContext);
EzySimpleApplication app = new EzySimpleApplication();
app.setSetting(appSetting);
EzyAppContext appContext = mock(EzyAppContext.class);
when(appContext.getApp()).thenReturn(app);
when(zoneContext.getAppContext("apps")).thenReturn(appContext);
EzySimplePlugin plugin = new EzySimplePlugin();
plugin.setSetting(pluginSetting);
EzyPluginContext pluginContext = mock(EzyPluginContext.class);
when(pluginContext.getPlugin()).thenReturn(plugin);
when(zoneContext.getPluginContext("plugins")).thenReturn(pluginContext);
Map<String, ClassLoader> appClassLoaders = new HashMap<>();
appClassLoaders.put("apps", new EzyAppClassLoader(new File("test-data"), getClass().getClassLoader()));
server.setAppClassLoaders(appClassLoaders);
EzyZonesStarter starter = EzyZonesStarter.builder().serverContext(serverContext).build();
starter.start();
}
use of com.tvd12.ezyfoxserver.context.EzyAppContext in project ezyfox-server by youngmonkeys.
the class EzyServerContextsTest method test.
@Test
public void test() {
EzySimpleApplication app = new EzySimpleApplication();
app.setUserManager(EzyAppUserManagerImpl.builder().maxUsers(1).appName("test").build());
EzyAppContext appContext = mock(EzyAppContext.class);
when(appContext.getApp()).thenReturn(app);
EzySimpleUser user = new EzySimpleUser();
assert !EzyServerContexts.containsUser(appContext, user);
assert !EzyServerContexts.containsUser(appContext, "test");
assert EzyServerContexts.getUserManager(appContext) != null;
}
use of com.tvd12.ezyfoxserver.context.EzyAppContext 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);
}
use of com.tvd12.ezyfoxserver.context.EzyAppContext in project ezyfox-server by youngmonkeys.
the class EzyRequestAppController method handle.
@Override
public void handle(EzyServerContext ctx, EzyRequestAppRequest request) {
EzyRequestAppParams params = request.getParams();
EzyAppContext appCtx = ctx.getAppContext(params.getAppId());
EzyApplication app = appCtx.getApp();
EzyAppRequestController requestController = app.getRequestController();
// user manager for checking, user must be managed
EzyUserManager userManger = appCtx.getApp().getUserManager();
EzyUser user = request.getUser();
// check user joined app or not to prevent spam request
boolean hasAccessed = userManger.containsUser(user);
if (hasAccessed) {
// redirect handling to app
EzyUserRequestAppEvent event = newRequestAppEvent(request);
requestController.handle(appCtx, event);
} else {
EzySession session = request.getSession();
responseRequestAppError(ctx, session);
}
}
use of com.tvd12.ezyfoxserver.context.EzyAppContext 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);
}
}
Aggregations