Search in sources :

Example 16 with EzyApplication

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

the class EzyAppResponseImplTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    EzyAppContext context = mock(EzyAppContext.class);
    EzyApplication application = mock(EzyApplication.class);
    EzyAppUserManager userManager = EzyAppUserManagerImpl.builder().build();
    when(context.getApp()).thenReturn(application);
    when(application.getUserManager()).thenReturn(userManager);
    EzyAppResponse response = new EzyAppResponseImpl(context);
    Field recipients = EzyAbstractResponse.class.getDeclaredField("recipients");
    Field exclusiveRecipients = EzyAbstractResponse.class.getDeclaredField("exclusiveRecipients");
    recipients.setAccessible(true);
    exclusiveRecipients.setAccessible(true);
    assert ((Collection) recipients.get(response)).size() == 0;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 0;
    EzyUser user1 = newUser("user1");
    response.user(user1);
    assert ((Collection) recipients.get(response)).size() == 2;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 0;
    EzyUser user2 = newUser("user2");
    response.user(user2);
    response.user(user2, true);
    assert ((Collection) recipients.get(response)).size() == 4;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 2;
    EzyUser user3 = newUser("user3");
    EzyUser user4 = newUser("user4");
    response.users(user3, user4);
    response.users(new EzyUser[] { user3, null }, true);
    assert ((Collection) recipients.get(response)).size() == 8;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 4;
    EzyUser user5 = newUser("user5");
    EzyUser user6 = newUser("user6");
    response.users(Lists.newArrayList(user5, user6));
    response.users(Lists.newArrayList(user5, null), true);
    assert ((Collection) recipients.get(response)).size() == 12;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 6;
    EzyUser user7 = newUser("user7");
    EzyUser user8 = newUser("user8");
    userManager.addUser(user7);
    userManager.addUser(user8);
    response.username("user7");
    response.username("user7", true);
    assert ((Collection) recipients.get(response)).size() == 14;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 8;
    EzyUser user9 = newUser("user9");
    EzyUser user10 = newUser("user10");
    EzyUser user11 = newUser("user11");
    userManager.addUser(user9);
    userManager.addUser(user10);
    userManager.addUser(user11);
    response.usernames("user9", "user10");
    response.usernames(new String[] { "user11" }, true);
    assert ((Collection) recipients.get(response)).size() == 18;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 10;
    EzyUser user12 = newUser("user12");
    EzyUser user13 = newUser("user13");
    EzyUser user14 = newUser("user14");
    userManager.addUser(user12);
    userManager.addUser(user13);
    userManager.addUser(user14);
    response.usernames(Lists.newArrayList("user12", "user13"));
    response.usernames(Lists.newArrayList("user14"), true);
    assert ((Collection) recipients.get(response)).size() == 22;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 12;
    EzySession session1 = new ExSession();
    response.session(session1);
    assert ((Collection) recipients.get(response)).size() == 23;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 12;
    EzySession session2 = new ExSession();
    EzySession session3 = new ExSession();
    response.sessions(session2, session3);
    response.sessions(new EzySession[] { session3 }, true);
    assert ((Collection) recipients.get(response)).size() == 25;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 13;
    EzySession session4 = new ExSession();
    EzySession session5 = new ExSession();
    response.sessions(Lists.newArrayList(session4, session5));
    response.sessions(Lists.newArrayList(session4, session5), true);
    assert ((Collection) recipients.get(response)).size() == 27;
    assert ((Collection) exclusiveRecipients.get(response)).size() == 15;
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) Field(java.lang.reflect.Field) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppResponseImpl) EzyAppResponse(com.tvd12.ezyfoxserver.command.EzyAppResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 17 with EzyApplication

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

the class EzyAppResponseImplTest method newResponse.

private EzyAppResponse newResponse() {
    EzyAppContext context = mock(EzyAppContext.class);
    EzyApplication application = mock(EzyApplication.class);
    EzyAppUserManager userManager = EzyAppUserManagerImpl.builder().build();
    when(context.getApp()).thenReturn(application);
    when(application.getUserManager()).thenReturn(userManager);
    return new EzyAppResponseImpl(context);
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyAppUserManager(com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppResponseImpl)

Example 18 with EzyApplication

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

the class EzyAppSendResponseImplTest method test.

@Test
public void test() {
    EzyAppContext appContext = mock(EzyAppContext.class);
    EzyApplication app = mock(EzyApplication.class);
    when(appContext.getApp()).thenReturn(app);
    EzySimpleAppSetting appSetting = new EzySimpleAppSetting();
    appSetting.setName("test");
    when(app.getSetting()).thenReturn(appSetting);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(appContext.getParent()).thenReturn(zoneContext);
    EzyServerContext serverContext = mock(EzyServerContext.class);
    when(zoneContext.getParent()).thenReturn(serverContext);
    EzyAppSendResponseImpl cmd = new EzyAppSendResponseImpl(appContext);
    EzyData data = EzyEntityFactory.newArrayBuilder().build();
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    cmd.execute(data, session, false);
    cmd.execute(data, Lists.newArrayList(session), false);
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyData(com.tvd12.ezyfox.entity.EzyData) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppSendResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppSendResponseImpl) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 19 with EzyApplication

use of com.tvd12.ezyfoxserver.EzyApplication 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 20 with EzyApplication

use of com.tvd12.ezyfoxserver.EzyApplication 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);
    }
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyRequestAppParams(com.tvd12.ezyfoxserver.request.EzyRequestAppParams) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppRequestController(com.tvd12.ezyfoxserver.app.EzyAppRequestController) EzyUserManager(com.tvd12.ezyfoxserver.wrapper.EzyUserManager) EzyUserRequestAppEvent(com.tvd12.ezyfoxserver.event.EzyUserRequestAppEvent) EzySession(com.tvd12.ezyfoxserver.entity.EzySession)

Aggregations

EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)23 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)21 EzyAppUserManager (com.tvd12.ezyfoxserver.wrapper.EzyAppUserManager)19 Test (org.testng.annotations.Test)15 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)12 BaseTest (com.tvd12.test.base.BaseTest)10 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)9 EzySimpleAppSetting (com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting)9 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)8 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)7 EzySimpleAppUserDelegate (com.tvd12.ezyfoxserver.delegate.EzySimpleAppUserDelegate)6 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)6 EzySimpleUserAccessAppEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent)5 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)5 EzyAppResponseImpl (com.tvd12.ezyfoxserver.command.impl.EzyAppResponseImpl)4 EzyAccessAppController (com.tvd12.ezyfoxserver.controller.EzyAccessAppController)4 EzySimpleUserAccessedAppEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserAccessedAppEvent)4 EzySimpleAccessAppRequest (com.tvd12.ezyfoxserver.request.EzySimpleAccessAppRequest)4 EzyAppResponse (com.tvd12.ezyfoxserver.command.EzyAppResponse)2 EzyAppSetting (com.tvd12.ezyfoxserver.setting.EzyAppSetting)2