org.apache.mina.core.session.DummySession Java Examples

The following examples show how to use org.apache.mina.core.session.DummySession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FrameDecoderTest.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
protected void testFrames ( final String resourceName, final Frame... expectedFrames ) throws Exception
{
    final FrameDecoder decoder = new FrameDecoder ();
    final MockProtocolDecoderOutput out = new MockProtocolDecoderOutput ();
    final DummySession session = new DummySession ();

    session.setTransportMetadata ( new DefaultTransportMetadata ( "eclipse.scada", "test", false, true, SocketAddress.class, IoSessionConfig.class, Object.class ) );

    for ( final IoBuffer data : BufferLoader.loadBuffersFromResource ( FrameDecoderTest.class, resourceName ) )
    {
        System.out.println ( "Pushing data packet - " + data.getHexDump () );
        decoder.decode ( session, data, out );
    }

    out.assertMessages ( expectedFrames );
}
 
Example #2
Source File: TestInternalJsonCodec.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    this.codec = new InternalJsonCodec();
    codec.init(serviceContext, null, DefaultMessageFactory.getFactory(), null);

    this.session = new DummySession();
}
 
Example #3
Source File: TestNTGCodecPositive.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
private void decodeBytes(int[] ints) throws Exception{
	byte[] bytes = new byte[ints.length];
	for (int i =0; i<ints.length; i++)
	{
		bytes[i] = (byte) ints[i];
	}

       NTGCodec decodeCodec2 = new NTGCodec();
       decodeCodec2.init(serviceContext, null, DefaultMessageFactory.getFactory(), TestNTGHelper.getDictionary());
	ProtocolDecoderOutput decoderOutput2 = new MockProtocolDecoderOutput();

	IoBuffer toDecode = IoBuffer.wrap( new byte[0] );
	toDecode.setAutoExpand(true);
	toDecode.put(bytes);
	//IoBuffer.wrap( Arrays.copyOf(((IoBuffer)lastMessage).array(), ((IoBuffer)lastMessage).limit() ));
	toDecode.compact();
	toDecode.order(ByteOrder.LITTLE_ENDIAN);

	IoSession decodeSession2 = new DummySession();
	boolean decodableResult = decodeCodec2.decodable( decodeSession2, toDecode );
	Assert.assertTrue( "Test for decoding error.", decodableResult);
	decoderOutput2 = new MockProtocolDecoderOutput();

	boolean decodeResult = decodeCodec2.doDecode( decodeSession2, toDecode, decoderOutput2 );
	Assert.assertTrue( "Decoding error.", decodeResult );

       Assert.assertTrue("Message queue size must not less then 1.", ((AbstractProtocolDecoderOutput)decoderOutput2).getMessageQueue().size() >= 1);
	Object decodedMessage2 = ((AbstractProtocolDecoderOutput)decoderOutput2).getMessageQueue().element();

	Assert.assertTrue( "Object must be instance of IMessage.", decodedMessage2 instanceof IMessage);
	Assert.assertTrue( "Object must be instance of MapMessage.", decodedMessage2 instanceof MapMessage);
}
 
Example #4
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeSeveralMessages() throws Exception
{
	FASTCodec codec = getCodec(DICTIONARY_URI, TEMPLATE_TITLE);
	int[] array = {
			0xA8, 0xC0, 0x81, 0xB0, 0x32, 0x30, 0x31, 0x32,
			0x30, 0x31, 0X30, 0X31, 0x2D, 0x30, 0x31, 0x3A,
			0x30, 0x31, 0x3A, 0x30, 0x31, 0x2E, 0x33, 0x33,
			0xB3, 0xB0, 0x80, 0x4D, 0x41, 0x44, 0x54, 0x59,
			0xB0, 0x74, 0x6E, 0x70, 0x31, 0x32, 0xB3, 0x80,
			0x80,

			0xA8, 0xC0, 0x81, 0xB0, 0x32, 0x30, 0x31, 0x32,
			0x30, 0x31, 0X30, 0X31, 0x2D, 0x30, 0x31, 0x3A,
			0x30, 0x31, 0x3A, 0x30, 0x31, 0x2E, 0x33, 0x33,
			0xB3, 0xB0, 0x80, 0x4D, 0x41, 0x44, 0x54, 0x59,
			0xB0, 0x74, 0x6E, 0x70, 0x31, 0x32, 0xB3, 0x80,
			0x80
	};

	byte[] b = new byte[array.length];
	for (int i=0; i<array.length; i++)
	{
		b[i] = (byte) array[i];
	}

	IoBuffer toDecode = IoBuffer.wrap( b );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();

	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
	System.out.println(decoderOutput.getMessageQueue().element());
	System.out.println(decoderOutput.getMessageQueue().element());

	Assert.assertTrue( "Decoding error.", decodableResult);
	System.out.println("position = "+toDecode.position());
}
 
Example #5
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeMessage() throws Exception
{
	FASTCodec codec = getCodec(DICTIONARY_URI, TEMPLATE_TITLE);
	int[] array = {
			0xA8, 0xC0, 0x81, 0xB0, 0x32, 0x30, 0x31, 0x32,
			0x30, 0x31, 0X30, 0X31, 0x2D, 0x30, 0x31, 0x3A,
			0x30, 0x31, 0x3A, 0x30, 0x31, 0x2E, 0x33, 0x33,
			0xB3, 0xB0, 0x80, 0x4D, 0x41, 0x44, 0x54, 0x59,
			0xB0, 0x74, 0x6E, 0x70, 0x31, 0x32, 0xB3, 0x80,
			0x80};
	byte[] b = new byte[array.length];
	for (int i=0; i<array.length; i++)
	{
		b[i] = (byte) array[i];
	}

	IoBuffer toDecode = IoBuffer.wrap( b );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();

	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
	System.out.println(decoderOutput.getMessageQueue().element());

	Assert.assertTrue( "Decoding error.", decodableResult);
}
 
Example #6
Source File: TestITCHHelper.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
protected IMessage decode(Object lastMessage, ITCHCodec codec) throws Exception {
       if(codec == null) {
           codec = (ITCHCodec)messageHelper.getCodec(serviceContext);
       }
	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	IoSession decodeSession = new DummySession();
	IoBuffer toDecode = IoBuffer.wrap( ((IoBuffer)lastMessage).array() );
	boolean decodeResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
	    
	Assert.assertTrue("Decoding error.", decodeResult);
       Assert.assertTrue("Message queue size must not less then 1.", decoderOutput.getMessageQueue().size() >= 1);

	return (IMessage) decoderOutput.getMessageQueue().element();
}
 
Example #7
Source File: TestITCHVisitorPositive.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test decode message testString from bytes and check Alpha_notrim
 */
@Test
public void testStringAlphaNotrimDecode(){
       byte[] array = { 58, 0, 1, 48, 0, 0, 0, 0,
               46, 13, 102, 102, 115, 116, 49, 32,
               32, 32, 49, 48, 58, 52, 57, 58,
               48, 48, 77, 111, 110, 32, 74, 117,
               108, 32, 48, 52, 32, 49, 52, 58,
               48, 50, 58, 51, 48, 32, 77, 83,
               75, 32, 50, 48, 49, 54, 0, 0,
               0, 0, 0, 0, 0, 0, 0, 0, };
  		IoBuffer toDecode = IoBuffer.wrap( array );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();
	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	try{
		boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
		Assert.assertTrue( "Decoding error.", decodableResult);
		IMessage message=(IMessage) decoderOutput.getMessageQueue().element();
		@SuppressWarnings("unchecked")
           List<IMessage> result = message.getField(ITCHMessageHelper.SUBMESSAGES_FIELD_NAME);
		Assert.assertEquals(2, result.size());
		Assert.assertEquals(1, (int)Integer.valueOf(result.get(1).getField("Alpha_notrim").toString().trim()));
	}catch(Exception e){
		e.printStackTrace(System.err);
		logger.error(e.getMessage(),e);
		Assert.fail(e.getMessage());
	}
}
 
Example #8
Source File: TestITCHCodecPositive.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
/**
 * Try to decode SymbolDirectory from byte[] and print result message into console.
 */
@Ignore@Test
public void testDecodeMessageSymbolDirectory(){
       ITCHCodec codec = (ITCHCodec) getMessageHelper().getCodec(serviceContext);
	int[] array = {
			0x64, 0x00, // Size
			0x01,       // Count
			0x31,       // MD Group
			0x00, 0x00, 0x00, 0x00, // SeqNumber
			0x5C, 0x52, 0xF0, 0x9D, 0xE6, 0x2B, 0xA4, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x01, 0x31, 0x00, 0x00, 0x00, 0x00, 0x5C, 0x52, 0xF8, 0xE9, 0xE7, 0x2B, 0xA9, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x36, 0x32, 0x30, 0x37, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x47, 0x45, 0x4E, 0x45, 0x52, 0x41, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
	byte[] b = new byte[array.length];
	for (int i=0; i<array.length; i++)
	{
		b[i] = (byte) array[i];
	}

	IoBuffer toDecode = IoBuffer.wrap( b );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();
	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	try{
		boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
		System.out.println((IMessage) decoderOutput.getMessageQueue().element());
		Assert.assertTrue( "Decoding error.", decodableResult);
		System.out.println("position = "+toDecode.position());
	}catch(Exception e){
		logger.error(e.getMessage(),e);
		Assert.fail(e.getMessage());
	}

}
 
Example #9
Source File: TestITCHCodecPositive.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
/**
 * Try to decode AddOrderOneByteLength from byte[] and print result message into console.
 */
@Ignore@Test
public void testDecodeMessageAddOrderOneByteLength(){
       ITCHCodec codec = (ITCHCodec) getMessageHelper().getCodec(serviceContext);
       int[] array = {0x2E, 0x00, 0x01, 0x31, 0x10, 0x05, 0x00, 0x00, 0x2E, 0x41, 0xE0, 0x98, 0xC3, 0x22, 0x0E, 0x00,
        0x00, 0x40, 0xC4, 0xD4, 0x57, 0x01, 0x42, 0xE8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3D,
        0xE6, 0x46, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x72, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
       byte[] b = new byte[array.length];
	for (int i=0; i<array.length; i++)
	{
		b[i] = (byte) array[i];
	}

	IoBuffer toDecode = IoBuffer.wrap( b );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();
	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	try{
		boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
		System.out.println((IMessage) decoderOutput.getMessageQueue().element());
		Assert.assertTrue( "Decoding error.", decodableResult);
	}catch(Exception e){
		logger.error(e.getMessage(),e);
		Assert.fail(e.getMessage());
	}
}
 
Example #10
Source File: ITCHMulticastUDPSession.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public ITCHMulticastUDPSession(IServiceContext serviceContext, String name, SailfishURI dictionaryURI, byte marketDataGroup, ITCHMulticastServer service, MessageHelper itchHandler, IMessageFactory msgFactory) {
    this.name = name;
    this.serviceContext = serviceContext;
    this.dictionaryURI = dictionaryURI;
    this.msgFactory = msgFactory;
    this.itchHandler = itchHandler;

    this.codec = itchHandler.getCodec(serviceContext);
    this.marketDataGroup = marketDataGroup;
    this.service = service;
    this.heartbeatInterval = ((ITCHMulticastSettings) service.getSettings()).getHeartbeatInterval();
    this.remoteName = "To Dublin";
    this.dummySession = new DummySession();
}
 
Example #11
Source File: MockProtocolEncoderOutput.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public WriteFuture flush()
{
	DummySession dummySession = new DummySession();
	DefaultWriteFuture writeFuture = new DefaultWriteFuture(dummySession);
	return writeFuture;
}
 
Example #12
Source File: TestITCHCodecPositive.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
/**
 * Check that the isRejected is true for case the number of bytes read is not equal to the specified message length.
 * @throws Exception
 */
@Test
public void testIncorrectLength() throws Exception {
    ITCHCodec codec = (ITCHCodec)getMessageHelper().getCodec(serviceContext);
    int[] array = {
            //header
            0x78, 0x01, 0x04, 0x31, 0x00, 0x00, 0x00, 0x00,
            
            //incorrect length (msg must be rejected)
            0x5E, 0x52, 0xF0, 0x9D, 0xE6, 0x2B, 0xA4, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x31, 0x30, 0x30,
            0x31, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20,
            0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
            
            //correct
            0x5C, 0x52, 0xF0, 0x9D, 0xE6, 0x2B, 0xA4, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x31, 0x30, 0x30,
            0x31, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20,
            0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00,
            0x00, 0x00,
            
            //incorrect length (msg must be rejected)
            0x5A, 0x52, 0xF0, 0x9D, 0xE6, 0x2B, 0xA4, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x31, 0x30, 0x30,
            0x31, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20,
            0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00,
            
            //correct
            0x5C, 0x52, 0xF0, 0x9D, 0xE6, 0x2B, 0xA4, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x20, 0x49, 0x54, 0x31, 0x30, 0x30,
            0x31, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x54, 0x41, 0x48, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x45, 0x55, 0x52, 0x00, 0x20,
            0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00,
            0x00, 0x00 };
    byte[] bytes = new byte[array.length];
    for (int i = 0; i < array.length; i++) {
        bytes[i] = (byte)array[i];
    }
    
    IoBuffer toDecode = IoBuffer.wrap(bytes);
    toDecode.order(ByteOrder.LITTLE_ENDIAN);
    toDecode.position(0);
    
    AbstractProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
    IoSession decodeSession = new DummySession();
    codec.doDecode(decodeSession, toDecode, decoderOutput);
    IMessage msg = (IMessage)decoderOutput.getMessageQueue().element();
    
    List<IMessage> listMsg = msg.getField(ITCHMessageHelper.SUBMESSAGES_FIELD_NAME);
    boolean[] rejectedMessage = { false, true, false, true, false };
    for (int index = 0; index < listMsg.size(); index++) {
        Assert.assertEquals(rejectedMessage[index], listMsg.get(index).getMetaData().isRejected());
    }
}
 
Example #13
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
    this.session = new DummySession();
}
 
Example #14
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeFastMessage_V_1_2() throws Exception {

	FASTCodec codec = getCodec(DICTIONARY_URI_V_1_2, TEMPLATE_TITLE_V_1_2);

	byte[] sourceArray = {
			(byte) 0xb3, (byte) 0xc0, (byte) 0x81, (byte) 0xb0, (byte) 0x32, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x30, (byte) 0x31, (byte) 0x30, (byte) 0x31, (byte) 0x2d, (byte) 0x30, (byte) 0x31, (byte) 0x3a,
			(byte) 0x30, (byte) 0x31, (byte) 0x3a, (byte) 0x30, (byte) 0x31, (byte) 0x2e, (byte) 0x33, (byte) 0x33, (byte) 0xb3, (byte) 0xb0, (byte) 0x80, (byte) 0x4d, (byte) 0x41, (byte) 0x44, (byte) 0x54, (byte) 0x59,
			(byte) 0xb0, (byte) 0x74, (byte) 0x6e, (byte) 0x70, (byte) 0x31, (byte) 0x32, (byte) 0xb3, (byte) 0x80, (byte) 0x80, (byte) 0x81, (byte) 0x15, (byte) 0x6f, (byte) 0x6a, (byte) 0x2c, (byte) 0x14, (byte) 0x75,
			(byte) 0x3d, (byte) 0x3c, (byte) 0xab, (byte) 0x81
	};

	IoBuffer toDecode = IoBuffer.wrap(sourceArray);
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();

	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	boolean decodableResult = codec.doDecode(decodeSession, toDecode, decoderOutput);
	Assert.assertTrue("Decoding error.", decodableResult);

	IMessage message = msgFactory.createMessage("Logon", "Test");

	message.addField("Password", "tnp123");
	message.addField("Username", "MADTY0" );
	message.addField("SendingTime", "20120101-01:01:01.333");
	message.addField("ApplID", "0");
	message.addField("MessageType", "0");
	message.addField("EndOfTransaction", true);
	message.addField("Timestamp", LocalDateTime.of(2019, 12, 12, 15, 30, 30, 555));
	message.addField("DeleteReason", 0);

	AbstractProtocolEncoderOutput output = new MockProtocolEncoderOutput();

	session.write(message);
	codec.encode(session, message, output);
	Queue<Object> msgQueue = output.getMessageQueue();
	Object lastMessage = msgQueue.element();
	byte[] asd = ((IoBuffer) lastMessage).array();
	int limit = ((IoBuffer) lastMessage).limit();
	byte[] bytes = Arrays.copyOf(asd, limit);

	session.write(lastMessage);
	Assert.assertArrayEquals("Compare source and encoded\n" + HexDumper.getHexdump(bytes), sourceArray, bytes);
}
 
Example #15
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeFastMessage() throws Exception {

    String dictName = "FAST";
    SailfishURI dictUri = SailfishURI.unsafeParse(CORE_ALIAS + ":" + dictName);
    FASTCodec codec = getCodec(dictUri, dictName);

    byte[] sourceArray = { (byte) 0xab, (byte) 0xc0, (byte) 0x83, 0x42, (byte) 0xd7, 0x32, 0x30, 0x31, 0x36, 0x30, 0x32, 0x31, 0x30,
            0x2d, 0x30, 0x37, 0x3a, 0x31, 0x30, 0x3a, 0x30, 0x36, 0x2e, 0x31, 0x39, (byte) 0xb3, 0x36, 0x30, 0x36, 0x36, 0x36, (byte) 0xb0,
            (byte) 0x80, (byte) 0x82, (byte) 0x82, (byte) 0xc0, (byte) 0x87, 0x54, 0x4d, (byte) 0xd0, 0x21, (byte) 0xe9, 0x21, (byte) 0xee};

    IoBuffer toDecode = IoBuffer.wrap(sourceArray);
    toDecode.order(ByteOrder.LITTLE_ENDIAN);
    toDecode.position(0);

    IoSession decodeSession = new DummySession();

    MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
    boolean decodableResult = codec.doDecode(decodeSession, toDecode, decoderOutput);
    Assert.assertTrue("Decoding error.", decodableResult);

    IMessage message = DefaultMessageFactory.getFactory().createMessage("ApplicationMessageRequest", "fast");
    message.addField("MsgType", "BW");
    message.addField("SendingTime", "20160210-07:10:06.193");
    message.addField("ApplReqID", "606660");
    message.addField("ApplReqType", 0L);
    message.addField("NoApplIDs", 1L);
    List<IMessage> list = new ArrayList<>();
    IMessage subMessage = DefaultMessageFactory.getFactory().createMessage("ApplicationMessageRequest_IndicesRequestEntries", "fast");
    subMessage.addField("RefApplID", "TMP");
    subMessage.addField("Reserved1", 6L);
    subMessage.addField("ApplBegSeqNum", 4328L);
    subMessage.addField("ApplEndSeqNum", 4333L);

    list.add(subMessage);
    message.addField("IndicesRequestEntries", list);

    AbstractProtocolEncoderOutput output = new MockProtocolEncoderOutput();

    session.write(message);
    codec.encode(session, message, output);
    Queue<Object> msgQueue = output.getMessageQueue();
    Object lastMessage = msgQueue.element();
    byte[] asd = ((IoBuffer) lastMessage).array();
    int limit = ((IoBuffer) lastMessage).limit();
    byte[] bytes = Arrays.copyOf(asd, limit);

    session.write(lastMessage);
    Assert.assertArrayEquals("Compare source and encoded\n" + HexDumper.getHexdump(bytes), sourceArray, bytes);
}
 
Example #16
Source File: TestFastCodec.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoveryFromBadMessage() throws Exception
{
	FASTCodec codec = getCodec(DICTIONARY_URI, TEMPLATE_TITLE);
	int[] array1 = {
			0xA8, 0xC0, 0x81, 0xB0, 0x32, 0x30, 0x31, 0x32,
			0x30, 0x31, 0X30, 0X31, 0x2D, 0x30, 0x31, 0x3A,
			0x30, 0x31, 0x3A, 0x30, 0x31, 0x2E, 0x33, 0x33,
	};

	int[] array2 = {
			0xA8, 0xC0, 0x81, 0xB0, 0x32, 0x30, 0x31, 0x32,
			0x30, 0x31, 0X30, 0X31, 0x2D, 0x30, 0x31, 0x3A,
			0x30, 0x31, 0x3A, 0x30, 0x31, 0x2E, 0x33, 0x33,
			0xB3, 0xB0, 0x80, 0x4D, 0x41, 0x44, 0x54, 0x59,
			0xB0, 0x74, 0x6E, 0x70, 0x31, 0x32, 0xB3, 0x80,
			0x80
	};

	byte[] b = new byte[array1.length];
	for (int i=0; i<array1.length; i++)
	{
		b[i] = (byte) array1[i];
	}

	IoBuffer toDecode = IoBuffer.wrap( b );
	toDecode.order(ByteOrder.LITTLE_ENDIAN);
	toDecode.position(0);

	IoSession decodeSession = new DummySession();

	MockProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();
	boolean decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );
	Assert.assertFalse( "Decoding error.", decodableResult);

	b = new byte[array2.length];
	for (int i=0; i<array2.length; i++)
	{
		b[i] = (byte) array2[i];
	}

	toDecode = IoBuffer.wrap( b );
	decodableResult = codec.doDecode( decodeSession, toDecode, decoderOutput );

	System.out.println(decoderOutput.getMessageQueue().element());

	Assert.assertTrue( "Decoding error.", decodableResult);
	System.out.println("position = "+toDecode.position());
}
 
Example #17
Source File: TestNTGHelper.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Override
public WriteFuture flush() {
	DummySession dummySession = new DummySession();
	DefaultWriteFuture writeFuture = new DefaultWriteFuture(dummySession);
	return writeFuture;
}
 
Example #18
Source File: TestNTGCodecNegative.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
/**
 * Negative test encode Heartbeat message with StartOfMessage=3 and decode it after
 */
@Test
public void testWrongMessage(){
       IMessage message = DefaultMessageFactory.getFactory().createMessage("Heartbeat", "NTG");
       IMessage messageHeader = DefaultMessageFactory.getFactory().createMessage("MessageHeader", "NTG");
	messageHeader.addField("StartOfMessage", 3);
	messageHeader.addField("MessageLength", 9);
	messageHeader.addField("MessageType", "2");
	message.addField("MessageHeader", messageHeader);
	try{
           IDictionaryStructure dictionary = TestNTGHelper.getDictionary();
           IMessageStructure msgStruct = dictionary.getMessages().get(message.getName());
		Assert.assertNotNull("Message structure is null.", msgStruct);
           NTGCodec encodeCodec = new NTGCodec();
		encodeCodec.init(serviceContext, null, DefaultMessageFactory.getFactory(), dictionary);
           ProtocolEncoderOutput output = new TestNTGHelper().new MockProtocolEncoderOutput();
 		encodeCodec.encode(new DummySession(), message, output);

		Queue<Object> msgQueue = ((AbstractProtocolEncoderOutput) output).getMessageQueue();

		Assert.assertNotNull("Message queue from AbstractProtocolEncoderOutput.", msgQueue);
           Assert.assertTrue("Message queue size must be equal 1.", msgQueue.size() == 1);

		Object lastMessage = msgQueue.element();

		if (lastMessage instanceof IoBuffer) {
               NTGCodec decodeCodec = new NTGCodec();
			decodeCodec.init(serviceContext, null, DefaultMessageFactory.getFactory(), dictionary);
			IoBuffer toDecode = IoBuffer.wrap( new byte[0] );
			toDecode.setAutoExpand(true);
			toDecode.put(((IoBuffer)lastMessage).array());
			toDecode.compact();
			toDecode.order(ByteOrder.LITTLE_ENDIAN);

			IoSession decodeSession = new DummySession();
			decodeCodec.decodable(decodeSession, toDecode);
			Assert.fail("Exception hasn't been thrown");
		}
	}catch(Exception e){
		Assert.assertEquals("Unexpected start of message: 3", e.getMessage());
	}

}
 
Example #19
Source File: TestNTGCodecPositive.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
private void testRoundTrip(IMessage message, IDictionaryStructure dictionary) {
	try
	{
           IMessageStructure msgStruct = dictionary.getMessages().get(message.getName());

		Assert.assertNotNull("Message structure is null.", msgStruct);

           NTGCodec encodeCodec = new NTGCodec();

		encodeCodec.init(serviceContext, null, DefaultMessageFactory.getFactory(), dictionary);

           ProtocolEncoderOutput output = new TestNTGHelper().new MockProtocolEncoderOutput();
			encodeCodec.encode(new DummySession(), message, output);

		Queue<Object> msgQueue = ((AbstractProtocolEncoderOutput) output).getMessageQueue();

		Assert.assertNotNull("Message queue from AbstractProtocolEncoderOutput.", msgQueue);
           Assert.assertTrue("Message queue size must be equal 1.", msgQueue.size() == 1);

		Object lastMessage = msgQueue.element();

		if (lastMessage instanceof IoBuffer) {
               NTGCodec decodeCodec = new NTGCodec();
			decodeCodec.init(serviceContext, null, DefaultMessageFactory.getFactory(), dictionary);
			AbstractProtocolDecoderOutput decoderOutput = new MockProtocolDecoderOutput();

			IoBuffer toDecode = IoBuffer.wrap( new byte[0] );
			toDecode.setAutoExpand(true);
			toDecode.put(((IoBuffer)lastMessage).array());
			//IoBuffer.wrap( Arrays.copyOf(((IoBuffer)lastMessage).array(), ((IoBuffer)lastMessage).limit() ));
			toDecode.compact();
			toDecode.order(ByteOrder.LITTLE_ENDIAN);

			IoSession decodeSession = new DummySession();
			boolean decodableResult = decodeCodec.decodable(decodeSession, toDecode);
			decoderOutput = new MockProtocolDecoderOutput();

			Assert.assertTrue("Test for decoding error.", decodableResult);

			boolean decodeResult = decodeCodec.doDecode(decodeSession, toDecode, decoderOutput);
			Assert.assertTrue("Decoding error.", decodeResult);

               Assert.assertTrue("Message queue size must not less then 1.", decoderOutput.getMessageQueue().size() >= 1);
			Object decodedMessage = decoderOutput.getMessageQueue().element();

			Assert.assertTrue( "Object must be instance of IMessage.", decodedMessage instanceof IMessage);
			Assert.assertTrue( "Object must be instance of MapMessage.", decodedMessage instanceof MapMessage);
               Assert.assertTrue("Messages must be equal. Original message:" + JsonMessageConverter.toJson(message, dictionary) + "; "
                       + "result message:" + JsonMessageConverter.toJson((IMessage)decodedMessage, dictionary),
                       message.compare((MapMessage) decodedMessage));
		}
	}
	catch (Exception e)
	{
	    logger.error(e.getMessage(), e);
		Assert.fail(ErrorUtil.formatException(e));
	}
}