Search in sources :

Example 1 with EzyArray

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

the class EzyWsResponseApiTest method responseTest.

@Test
public void responseTest() throws Exception {
    // given
    EzyObjectToStringEncoder encoder = mock(EzyObjectToStringEncoder.class);
    EzyWsResponseApi sut = new EzyWsResponseApi(encoder);
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(true);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.WEBSOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack);
    // then
    verify(encoder, times(1)).encode(data, String.class);
    verify(encoder, times(0)).toMessageContent(data);
    verify(encoder, times(0)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToStringEncoder(com.tvd12.ezyfox.codec.EzyObjectToStringEncoder) EzyWsResponseApi(com.tvd12.ezyfoxserver.api.EzyWsResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 2 with EzyArray

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

the class EzySocketResponseApiTest method secureResponseImmediateTest.

@Test
public void secureResponseImmediateTest() throws Exception {
    // given
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    byte[] bytes = RandomUtil.randomShortByteArray();
    EzyObjectToByteEncoder encoder = mock(EzyObjectToByteEncoder.class);
    when(encoder.toMessageContent(data)).thenReturn(bytes);
    EzySocketResponseApi sut = new EzySocketResponseApi(encoder);
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(true);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack, true);
    // then
    verify(encoder, times(1)).toMessageContent(data);
    verify(encoder, times(sessionCount)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToByteEncoder(com.tvd12.ezyfox.codec.EzyObjectToByteEncoder) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySocketResponseApi(com.tvd12.ezyfoxserver.api.EzySocketResponseApi) Test(org.testng.annotations.Test)

Example 3 with EzyArray

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

the class EzySocketResponseApiTest method normalResponseTest.

@Test
public void normalResponseTest() throws Exception {
    // given
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    EzyObjectToByteEncoder encoder = mock(EzyObjectToByteEncoder.class);
    byte[] bytes = RandomUtil.randomShortByteArray();
    when(encoder.encode(data)).thenReturn(bytes);
    EzySocketResponseApi sut = new EzySocketResponseApi(encoder);
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(false);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack);
    // then
    verify(encoder, times(1)).encode(data);
    verify(encoder, times(0)).toMessageContent(data);
    verify(encoder, times(0)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToByteEncoder(com.tvd12.ezyfox.codec.EzyObjectToByteEncoder) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySocketResponseApi(com.tvd12.ezyfoxserver.api.EzySocketResponseApi) Test(org.testng.annotations.Test)

Example 4 with EzyArray

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

the class EzySimpleDataHandler method handleRequest.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected void handleRequest(EzyConstant cmd, EzyArray data) {
    try {
        EzyRequest request = newRequest(cmd, data);
        try {
            EzyInterceptor interceptor = controllers.getInterceptor(cmd);
            interceptor.intercept(context, request);
            EzyController controller = controllers.getController(cmd);
            controller.handle(context, request);
        } finally {
            request.release();
        }
    } catch (Exception e) {
        if (context != null) {
            Throwable throwable = requestHandleException(session, cmd, data, e);
            context.handleException(Thread.currentThread(), throwable);
        } else {
            if (active) {
                logger.warn("fatal error, please add an issue to ezyfox-server github " + "with log: {}\nand stacktrace: ", this, e);
            } else {
                logger.warn("can't handle command: {} and data: {}, this session " + "maybe destroyed (session: {}), error message: {}", cmd, data, session, e.getMessage());
            }
        }
    }
}
Also used : EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzyRequest(com.tvd12.ezyfoxserver.request.EzyRequest) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyRequestHandleException.requestHandleException(com.tvd12.ezyfoxserver.exception.EzyRequestHandleException.requestHandleException)

Example 5 with EzyArray

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

the class EzySendResponseImpl method newPackage.

protected EzySimplePackage newPackage(EzyArray data, boolean encrypted, EzyTransportType transportType) {
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(encrypted);
    pack.setTransportType(transportType);
    return pack;
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage)

Aggregations

EzyArray (com.tvd12.ezyfox.entity.EzyArray)80 Test (org.testng.annotations.Test)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)30 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)17 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)15 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)14 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)13 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)13 EzySimpleLoginRequest (com.tvd12.ezyfoxserver.request.EzySimpleLoginRequest)13 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)12 EzyObject (com.tvd12.ezyfox.entity.EzyObject)12 EzyLoginController (com.tvd12.ezyfoxserver.controller.EzyLoginController)12 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)11 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)9 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)8 BaseCoreTest (com.tvd12.ezyfoxserver.testing.BaseCoreTest)8 Collection (com.tvd12.dahlia.core.entity.Collection)7 EzyArray (com.tvd12.ezyfoxserver.client.entity.EzyArray)7 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)7 Record (com.tvd12.dahlia.core.entity.Record)5