Search in sources :

Example 1 with EzyObjectToByteEncoder

use of com.tvd12.ezyfox.codec.EzyObjectToByteEncoder 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 2 with EzyObjectToByteEncoder

use of com.tvd12.ezyfox.codec.EzyObjectToByteEncoder 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 3 with EzyObjectToByteEncoder

use of com.tvd12.ezyfox.codec.EzyObjectToByteEncoder in project ezyfox-server by youngmonkeys.

the class EzyProxyResponseApiTest method test.

@Test
public void test() throws Exception {
    EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
    EzyProxyResponseApi api = new EzyProxyResponseApi(codecFactory);
    EzyPackage pack = mock(EzyPackage.class);
    api.response(pack);
    EzyObjectToByteEncoder byteEncoder = mock(EzyObjectToByteEncoder.class);
    EzyObjectToStringEncoder stringEncoder = mock(EzyObjectToStringEncoder.class);
    when(codecFactory.newEncoder(EzyConnectionType.SOCKET)).thenReturn(byteEncoder);
    when(codecFactory.newEncoder(EzyConnectionType.WEBSOCKET)).thenReturn(stringEncoder);
    api = new EzyProxyResponseApi(codecFactory);
    api.response(pack);
    api.response(pack, true);
    EzyImmediateDeliver immediateDeliver = mock(EzyImmediateDeliver.class);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    session.setImmediateDeliver(immediateDeliver);
    when(pack.getRecipients(any(EzyConstant.class))).thenReturn(Lists.newArrayList(session));
    api.response(pack);
    api.response(pack, true);
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyProxyResponseApi(com.tvd12.ezyfoxserver.api.EzyProxyResponseApi) EzyPackage(com.tvd12.ezyfoxserver.response.EzyPackage) EzyConstant(com.tvd12.ezyfox.constant.EzyConstant) EzyObjectToStringEncoder(com.tvd12.ezyfox.codec.EzyObjectToStringEncoder) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzyImmediateDeliver(com.tvd12.ezyfoxserver.entity.EzyImmediateDeliver) EzyObjectToByteEncoder(com.tvd12.ezyfox.codec.EzyObjectToByteEncoder) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with EzyObjectToByteEncoder

use of com.tvd12.ezyfox.codec.EzyObjectToByteEncoder in project ezyfox-server by youngmonkeys.

the class EzySocketResponseApiTest method secureResponseTest.

@Test
public void secureResponseTest() 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);
    // 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)

Aggregations

EzyObjectToByteEncoder (com.tvd12.ezyfox.codec.EzyObjectToByteEncoder)4 Test (org.testng.annotations.Test)4 EzyArray (com.tvd12.ezyfox.entity.EzyArray)3 EzySocketResponseApi (com.tvd12.ezyfoxserver.api.EzySocketResponseApi)3 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)3 EzySimplePackage (com.tvd12.ezyfoxserver.socket.EzySimplePackage)3 EzyObjectToStringEncoder (com.tvd12.ezyfox.codec.EzyObjectToStringEncoder)1 EzyConstant (com.tvd12.ezyfox.constant.EzyConstant)1 EzyProxyResponseApi (com.tvd12.ezyfoxserver.api.EzyProxyResponseApi)1 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)1 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)1 EzyImmediateDeliver (com.tvd12.ezyfoxserver.entity.EzyImmediateDeliver)1 EzyPackage (com.tvd12.ezyfoxserver.response.EzyPackage)1 BaseTest (com.tvd12.test.base.BaseTest)1