fixio.netty.codec.FixMessageDecoderTest.java Source code

Java tutorial

Introduction

Here is the source code for fixio.netty.codec.FixMessageDecoderTest.java

Source

/*
 * Copyright 2014 The FIX.io Project
 *
 * The FIX.io Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package fixio.netty.codec;

import fixio.fixprotocol.FieldType;
import fixio.fixprotocol.FixMessageHeader;
import fixio.fixprotocol.FixMessageImpl;
import fixio.fixprotocol.MessageTypes;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.DecoderException;
import org.junit.BeforeClass;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import static fixio.netty.pipeline.FixClock.systemUTC;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.junit.Assert.*;

public class FixMessageDecoderTest {

    private static FixMessageDecoder decoder;

    @BeforeClass
    public static void setUp() throws Exception {
        decoder = new FixMessageDecoder();
    }

    @Test
    public void testDecode() throws Exception {
        List<Object> result = decode(
                "8=FIX.4.1\u00019=90\u000135=0\u000149=INVMGR\u000156=BRKR\u000134=240\u000152=19980604-08:03:31\u000110=129\u0001");

        assertEquals(1, result.size());
        assertTrue(result.get(0) instanceof FixMessageImpl);
        final FixMessageImpl fixMessage = (FixMessageImpl) result.get(0);

        FixMessageHeader header = fixMessage.getHeader();

        assertEquals("FIX.4.1", fixMessage.getHeader().getBeginString());
        assertEquals(MessageTypes.HEARTBEAT, fixMessage.getMessageType());
        assertEquals("INVMGR", header.getSenderCompID());
        assertEquals("BRKR", header.getTargetCompID());
        assertEquals(240, header.getMsgSeqNum());
        assertEquals(129, fixMessage.getChecksum());

        final Long value = fixMessage.getValue(FieldType.SendingTime);
        assertEquals(ZonedDateTime.of(LocalDate.of(1998, 6, 4), LocalTime.of(8, 3, 31), systemUTC().zone())
                .toInstant().toEpochMilli(), value.longValue());
    }

    @Test
    public void testCmeRealData() throws Exception {
        List<Object> result = decode(
                "8=FIX.4.29=39635=BZ34=148949=CME50=G52=20141210-04:12:58.68956=17ACPON57=DUMMY369=36701180=0K41181=42811350=428011=ACP141818477867860=20141210-04:12:58.686533=3797=Y893=Y1028=Y1300=991369=9971:21373=31374=91375=1453=2448=000447=D452=7448=US,IL447=D452=54534=341=ACP141818477617384=60535=99499752041=ACP141818477621484=60535=99499752141=ACP141818477625384=180535=99499752210=228");
        assertEquals(1, result.size());
        assertTrue(result.get(0) instanceof FixMessageImpl);
        System.out.println(result.get(0));
    }

    private List<Object> decode(String message) throws Exception {
        String[] tags = message.split("\u0001");

        List<Object> result = new ArrayList<>();
        for (String tag : tags) {
            decoder.decode(null, Unpooled.wrappedBuffer(tag.getBytes(StandardCharsets.US_ASCII)), result);
        }
        return result;
    }

    @Test
    public void testNoBeginTag() throws Exception {
        String random = randomAlphanumeric(50);

        try {
            decode("100=" + random + "\u00018=FIX.4.2...");
            fail("DecoderException is expected");
        } catch (DecoderException e) {
            assertEquals("BeginString tag expected, but got: 100=" + random.substring(0, 10) + "...",
                    e.getMessage());
        }
    }
}