package cz.vutbr.feec.packets.ttp;



import org.junit.Assert;
import org.junit.Test;

import cz.vutbr.feec.packets.PacketGenerateException;
import cz.vutbr.feec.packets.PacketParseException;

import cz.vutbr.feec.packets.ttp.GeneralTTPHeader;

public class TTPHeaderTest {
	private byte buffer[] = new byte[500];

	@Test
	public void testTTPHeader() throws PacketParseException,
			PacketGenerateException {
		GeneralTTPHeader hdr1 = new GeneralTTPHeader(7);
		// filling the reader
		hdr1.setVersion(5);
	//	hdr1.setPadding(0);
		// packet type already set in definition
		hdr1.setFeedbackTreeId(65535);
		hdr1.setLength(5);

		int length = -1;
		length = hdr1.generate(buffer, 0);

		GeneralTTPHeader hdr2 = new GeneralTTPHeader(7);
		int length2 = hdr2.parse(buffer, 0, length);
		Assert.assertTrue(hdr1.getVersion() == hdr2.getVersion());
		Assert.assertTrue(length == length2);
		Assert.assertTrue(hdr1.equals(hdr2));
	}

	@Test
	public void FTC_Packet() throws PacketParseException,
			PacketGenerateException {
		
		FTCPacket packetFTC = new FTCPacket();
		packetFTC.setVersion(7);
		packetFTC.setPacketType(31);
		packetFTC.setFeedbackTreeId(999);
		packetFTC.setLength(255);
		packetFTC.setFeedbackGroupSize(65000);
		packetFTC.setPriority(7);
		packetFTC.setStatus(31);
		packetFTC.setParameters(15);
		packetFTC.setReserved(15);
		packetFTC.setSessionPort(64000);
		packetFTC.setReservedEnd(63000);
		
		int length = packetFTC.generate(buffer, 0);
		
		FTCPacket packetFTC2 = new FTCPacket();
		
		int length2 = packetFTC2.parse(buffer, 0, length);
		
	//	packetFTC2.setVersion(2);
		Assert.assertTrue(length == length2);
		Assert.assertTrue(packetFTC.equals(packetFTC2));
		Assert.assertTrue(packetFTC.getVersion()==packetFTC2.getVersion());
		Assert.assertTrue(packetFTC.getPacketType()==packetFTC2.getPacketType());
		Assert.assertTrue(packetFTC.getFeedbackTreeId()==packetFTC2.getFeedbackTreeId());
		Assert.assertTrue(packetFTC.getLength()==packetFTC2.getLength());
		Assert.assertTrue(packetFTC.getFeedbackGroupSize()==packetFTC2.getFeedbackGroupSize());
		Assert.assertTrue(packetFTC.getPriority()==packetFTC2.getPriority());
		Assert.assertTrue(packetFTC.getStatus()==packetFTC2.getStatus());
		Assert.assertTrue(packetFTC.getParameters()==packetFTC2.getParameters());
		Assert.assertTrue(packetFTC.getReserved()==packetFTC2.getReserved());
		Assert.assertTrue(packetFTC.getSessionPort()==packetFTC2.getSessionPort());
		Assert.assertTrue(packetFTC.getReservedEnd()==packetFTC2.getReservedEnd());
		
		
		
	}

	@Test
	public void FTD_Packet() throws PacketParseException,
			PacketGenerateException {
		
		FTDPacket packetFTD = new FTDPacket();
		
		packetFTD.setVersion(1);
		packetFTD.setPacketType(1);
		packetFTD.setFeedbackTreeId(1);
		packetFTD.setLength(1);
		packetFTD.setFeedbackTargetPort(1);
		packetFTD.setDefinitionStatus(1);
		packetFTD.setParameters(1);
		
		int length = packetFTD.generate(buffer, 0);
		
		FTDPacket packetFTD2 = new FTDPacket();
		
		int length2 = packetFTD2.parse(buffer, 0, length);
		
		Assert.assertTrue(length == length2);
		Assert.assertTrue(packetFTD.equals(packetFTD2));
		Assert.assertTrue(packetFTD.getVersion()==packetFTD2.getVersion());
		Assert.assertTrue(packetFTD.getPacketType()==packetFTD2.getPacketType());
		Assert.assertTrue(packetFTD.getFeedbackTreeId()==packetFTD2.getFeedbackTreeId());
		Assert.assertTrue(packetFTD.getLength()==packetFTD2.getLength());
		Assert.assertTrue(packetFTD.getFeedbackTargetPort()==packetFTD2.getFeedbackTargetPort());
		Assert.assertTrue(packetFTD.getDefinitionStatus()==packetFTD2.getDefinitionStatus());
		Assert.assertTrue(packetFTD.getParameters()==packetFTD2.getParameters());


	}

	@Test
	public void FTS_FT_Packet() throws PacketParseException,
			PacketGenerateException {
		FTSPacket packetFTS = new FTSPacket();
		
		packetFTS.setVersion(1);
		packetFTS.setPacketType(1);
		packetFTS.setFeedbackTreeId(1);
		packetFTS.setLength(120);
		packetFTS.setLMSseqNumber(1);
		packetFTS.setSessionSize(1);
		
		FTSGroupBlockFT blokFT1 = new FTSGroupBlockFT();
		FTSGroupBlockFT blokFT2 = new FTSGroupBlockFT();
		FTSGroupBlockLM blokLM1 = new FTSGroupBlockLM();
		FTSGroupBlockLM blokLM2 = new FTSGroupBlockLM();
		FTSGroupBlockLM blokLM3 = new FTSGroupBlockLM();
		
		blokFT1.setSFTST(1);
		blokFT1.setLength(24);
		blokFT1.setFeedbackTargetPort(1);
		blokFT1.setFeedbackTargerIP(1);
		blokFT1.setGroupSize(1);
		blokFT1.setPadding(1);
		blokFT1.setPriority(1);
		blokFT1.setHeight(1);
		blokFT1.setVectorFormat(1);
		
		FTS_GB_LMidVectorGroup vector1 = new FTS_GB_LMidVectorGroup();
		FTS_GB_LMidVectorGroup vector2 = new FTS_GB_LMidVectorGroup();
		FTS_GB_LMidVectorGroup vector3 = new FTS_GB_LMidVectorGroup();
		
		vector1.setLandmarkIDvector(1);
		vector2.setLandmarkIDvector(1);
		vector3.setLandmarkIDvector(1);
		
		blokFT1.addGroupBlock(vector1);
		blokFT1.addGroupBlock(vector2);
		blokFT1.addGroupBlock(vector3);
		
		packetFTS.addGroupBlockFT(blokFT1);
		
		blokFT2.setSFTST(1);
		blokFT2.setLength(24);
		blokFT2.setFeedbackTargetPort(2);
		blokFT2.setFeedbackTargerIP(2);
		blokFT2.setGroupSize(2);
		blokFT2.setPadding(1);
		blokFT2.setPriority(2);
		blokFT2.setHeight(2);
		blokFT2.setVectorFormat(2);
		
		vector1.setLandmarkIDvector(2);
		vector2.setLandmarkIDvector(2);
		vector3.setLandmarkIDvector(2);
		
		blokFT2.addGroupBlock(vector1);
		blokFT2.addGroupBlock(vector2);
		blokFT2.addGroupBlock(vector3);
		
		packetFTS.addGroupBlockFT(blokFT2);
		
		blokLM1.setSFTST(3);
		blokLM1.setLenght(20);
		blokLM1.setReserved(1);
		blokLM1.setVectorFormat(1);
		blokLM1.setFeedbackTargerIP(1);
		
		vector1.setLandmarkIDvector(1);
		vector2.setLandmarkIDvector(1);
		vector3.setLandmarkIDvector(1);
		
		blokLM1.addGroupBlock(vector1);
		blokLM1.addGroupBlock(vector2);
		blokLM1.addGroupBlock(vector3);
		
		packetFTS.addGroupBlockLM(blokLM1);
		
		blokLM2.setSFTST(3);
		blokLM2.setLenght(20);
		blokLM2.setReserved(2);
		blokLM2.setVectorFormat(2);
		blokLM2.setFeedbackTargerIP(2);
		
		vector1.setLandmarkIDvector(2);
		vector2.setLandmarkIDvector(2);
		vector3.setLandmarkIDvector(2);
		
		blokLM2.addGroupBlock(vector1);
		blokLM2.addGroupBlock(vector2);
		blokLM2.addGroupBlock(vector3);
		
		packetFTS.addGroupBlockLM(blokLM2);
		
		blokLM3.setSFTST(3);
		blokLM3.setLenght(20);
		blokLM3.setReserved(3);
		blokLM3.setVectorFormat(3);
		blokLM3.setFeedbackTargerIP(3);
		
		vector1.setLandmarkIDvector(3);
		vector2.setLandmarkIDvector(3);
		vector3.setLandmarkIDvector(3);
		
		blokLM3.addGroupBlock(vector1);
		blokLM3.addGroupBlock(vector2);
		blokLM3.addGroupBlock(vector3);
		
		packetFTS.addGroupBlockLM(blokLM3);
		
		int length = packetFTS.generate(buffer, 0);
		
		FTSPacket packetFTS2 = new FTSPacket();
		
		int length2 = packetFTS2.parse(buffer, 0, length);
		
		Assert.assertTrue(length == length2);
		Assert.assertTrue(packetFTS.equals(packetFTS2));
		Assert.assertTrue(packetFTS.getVersion()==packetFTS2.getVersion());
		Assert.assertTrue(packetFTS.getPacketType()==packetFTS2.getPacketType());
		Assert.assertTrue(packetFTS.getFeedbackTreeId()==packetFTS2.getFeedbackTreeId());
		Assert.assertTrue(packetFTS.getLMSseqNumber()==packetFTS2.getLMSseqNumber());
		Assert.assertTrue(packetFTS.getSessionSize()==packetFTS2.getSessionSize());
		Assert.assertTrue(packetFTS.getLength()==packetFTS2.getLength());
		
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getSFTST()==packetFTS2.getGroupBlockLM(2).getSFTST());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getLenght()==packetFTS2.getGroupBlockLM(2).getLenght());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getReserved()==packetFTS2.getGroupBlockLM(2).getReserved()
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getVectorFormat()==packetFTS2.getGroupBlockLM(2).getVectorFormat());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getFeedbackTargerIP()==packetFTS2.getGroupBlockLM(2).getFeedbackTargerIP());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getGroupBlock(1).getLandmarkIDvector()==packetFTS2.getGroupBlockLM(2).getGroupBlock(1).getLandmarkIDvector());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getGroupBlock(2).getLandmarkIDvector()==packetFTS2.getGroupBlockLM(2).getGroupBlock(2).getLandmarkIDvector());
		Assert.assertTrue(packetFTS.getGroupBlockLM(2).getGroupBlock(3).getLandmarkIDvector()==packetFTS2.getGroupBlockLM(2).getGroupBlock(1).getLandmarkIDvector());
	}
	
	
}
