package seng302.Networking;

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

import java.io.*;

/**
 * Created by hba56 on 19/04/17.
 */
public class XMLMessageEncoderTest {
    @Test
    public void getByteArrayTest(){
        StringBuilder xmlString;
        try{
            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());
            }
            XMLMessageEncoder testEncoder = new  XMLMessageEncoder((short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());

            byte[] encodedXML = testEncoder.encode();

            //1 + 2 + 6 + 1 + 2 + 2 + 374
            Assert.assertEquals(388, encodedXML.length);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Test
    public void getByteArrayNullTest(){
        StringBuilder xmlString;
        try{
            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());
            }
            XMLMessageEncoder testEncoder = new  XMLMessageEncoder((short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)1, xmlString.toString());

            byte[] encodedXML = testEncoder.encode();

            Assert.assertEquals(null, encodedXML);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
