package seng302.Networking.MessageDecoders;

import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * Created by hba56 on 20/04/17.
 */
public class XMLMessageDecoderTest {
    @Test
    public void getByteArrayTest(){
        try{
            StringBuilder xmlString;
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    this.getClass().getResourceAsStream(("../../../raceXML/Regatta.xml"))));
            String line;
            xmlString = new StringBuilder();
            while((line=br.readLine())!= null){
                xmlString.append(line.trim());
            }
            long time = System.currentTimeMillis();
            XMLMessageEncoder testEncoder = new  XMLMessageEncoder((short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());

            byte[] encodedXML = testEncoder.encode();

            XMLMessageDecoder decoderXML = new XMLMessageDecoder(encodedXML);
            decoderXML.decode();


            Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
            Assert.assertEquals((short)1, decoderXML.getAckNumber());
            Assert.assertEquals(time, decoderXML.getTimeStamp());
            Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
            Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
            Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
