Search in sources :

Example 26 with EzyResponse

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

the class EzySendResponseImplTest method responseMultiSuccessCaseButNotDebug.

@Test
public void responseMultiSuccessCaseButNotDebug() throws Exception {
    // when
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(false);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzySimpleServer server = new EzySimpleServer();
    server.setResponseApi(responseApi);
    server.setSettings(settings);
    EzySendResponseImpl cmd = new EzySendResponseImpl(server);
    EzyResponse response = new EzySimpleResponse(EzyCommand.APP_REQUEST);
    EzySession recipient = spy(EzyAbstractSession.class);
    List<EzySession> recipients = Collections.singletonList(recipient);
    // when
    cmd.execute(response, recipients, false, false, EzyTransportType.TCP);
    // then
    verify(responseApi, times(1)).response(any(EzyPackage.class), anyBoolean());
}
Also used : EzySimpleResponse(com.tvd12.ezyfoxserver.response.EzySimpleResponse) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyPackage(com.tvd12.ezyfoxserver.response.EzyPackage) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySendResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl) Test(org.testng.annotations.Test)

Example 27 with EzyResponse

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

the class EzySendResponseImplTest method responseMultiErrorTest.

@Test
public void responseMultiErrorTest() throws Exception {
    // given
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(true);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    doThrow(new IllegalArgumentException()).when(responseApi).response(any(EzyPackage.class), anyBoolean());
    EzySimpleServer server = new EzySimpleServer();
    server.setResponseApi(responseApi);
    server.setSettings(settings);
    EzySendResponseImpl cmd = new EzySendResponseImpl(server);
    EzyResponse response = new EzySimpleResponse(EzyCommand.APP_REQUEST);
    EzySession recipient = spy(EzyAbstractSession.class);
    List<EzySession> recipients = Collections.singletonList(recipient);
    // when
    cmd.execute(response, recipients, false, false, EzyTransportType.TCP);
    // then
    verify(responseApi, times(1)).response(any(EzyPackage.class), anyBoolean());
}
Also used : EzySimpleResponse(com.tvd12.ezyfoxserver.response.EzySimpleResponse) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyPackage(com.tvd12.ezyfoxserver.response.EzyPackage) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySendResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl) Test(org.testng.annotations.Test)

Example 28 with EzyResponse

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

the class EzySimpleServerContextTest method sendNowTest.

@Test
public void sendNowTest() {
    // given
    EzyResponse response = mock(EzyResponse.class);
    EzySession recipient = mock(EzySession.class);
    EzySendResponse sendResponse = mock(EzySendResponse.class);
    doNothing().when(sendResponse).execute(response, recipient, false, true, EzyTransportType.TCP);
    EzySimpleServerContext sut = new EzySimpleServerContext();
    FieldUtil.setFieldValue(sut, "sendResponse", sendResponse);
    // when
    sut.sendNow(response, recipient);
    // then
    verify(sendResponse, times(1)).execute(response, recipient, false, true, EzyTransportType.TCP);
}
Also used : EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySendResponse(com.tvd12.ezyfoxserver.command.EzySendResponse) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 29 with EzyResponse

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

the class EzySimpleZoneContextTest method normalCaseTest.

@SuppressWarnings("unchecked")
@Test
public void normalCaseTest() {
    EzyServerContext parent = mock(EzyServerContext.class);
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zoneSetting.setName("test");
    EzySimpleZone zone = new EzySimpleZone();
    zone.setUserManager(mock(EzyZoneUserManager.class));
    zone.setSetting(zoneSetting);
    EzySimpleZoneContext context = new EzySimpleZoneContext();
    context.setZone(zone);
    context.setParent(parent);
    context.init();
    assert context.get(EzyBroadcastEvent.class) != null;
    Asserts.assertNull(context.get(Void.class));
    context.addCommand(ExCommand.class, ExCommand::new);
    assert context.cmd(ExCommand.class) != null;
    Asserts.assertNull(context.cmd(Void.class));
    context.broadcast(EzyEventType.SERVER_INITIALIZING, new EzySimpleServerInitializingEvent(), true);
    context.broadcastApps(EzyEventType.SERVER_READY, new EzySimpleServerReadyEvent(), true);
    EzySimpleUser user = new EzySimpleUser();
    user.setName("dungtv");
    EzyUserAccessAppEvent accessAppEvent = new EzySimpleUserAccessAppEvent(user);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, "dungtv", true);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, user, true);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, EzyPredicates.ALWAYS_TRUE, true);
    // noinspection EqualsWithItself
    assert context.equals(context);
    EzyZoneContext zoneContext2 = mock(EzyZoneContext.class);
    EzySimpleZone zone2 = new EzySimpleZone();
    when(zoneContext2.getZone()).thenReturn(zone2);
    assert !zoneContext2.equals(context);
    System.out.println(context.hashCode() + ", " + context.hashCode());
    assert context.hashCode() == context.hashCode();
    EzyResponse response = mock(EzyResponse.class);
    EzySession recipient = spy(EzyAbstractSession.class);
    context.send(response, recipient, false);
    context.send(response, Lists.newArrayList(recipient), false);
    context.stream(new byte[0], recipient);
    context.stream(new byte[0], Lists.newArrayList(recipient));
    context.destroy();
}
Also used : EzySimpleServerInitializingEvent(com.tvd12.ezyfoxserver.event.EzySimpleServerInitializingEvent) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleZoneContext(com.tvd12.ezyfoxserver.context.EzySimpleZoneContext) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzyBroadcastEvent(com.tvd12.ezyfoxserver.command.EzyBroadcastEvent) EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzySimpleServerReadyEvent(com.tvd12.ezyfoxserver.event.EzySimpleServerReadyEvent) EzySimpleZoneSetting(com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting) EzyUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzyUserAccessAppEvent) EzySimpleUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 30 with EzyResponse

use of com.tvd12.ezyfoxserver.response.EzyResponse 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)

Aggregations

EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)25 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)17 Test (org.testng.annotations.Test)12 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)11 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)9 EzySendResponseImpl (com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl)8 EzySimpleResponse (com.tvd12.ezyfoxserver.response.EzySimpleResponse)8 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)8 EzyPackage (com.tvd12.ezyfoxserver.response.EzyPackage)7 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)5 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)4 EzyArray (com.tvd12.ezyfox.entity.EzyArray)3 EzyErrorParams (com.tvd12.ezyfoxserver.response.EzyErrorParams)3 EzyAppSetting (com.tvd12.ezyfoxserver.setting.EzyAppSetting)3 EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)2 EzyBroadcastEvent (com.tvd12.ezyfoxserver.command.EzyBroadcastEvent)2 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)2 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)2 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)2 EzySimpleServerReadyEvent (com.tvd12.ezyfoxserver.event.EzySimpleServerReadyEvent)2