refactored networking code from networkingTest

#story[778, 782]
This commit is contained in:
hba56
2017-04-25 12:38:13 +12:00
parent 67b73ec1db
commit e9578fc63b
48 changed files with 2442 additions and 14 deletions
@@ -0,0 +1,65 @@
package seng302.Networking;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.BinaryMesageEncoder;
import seng302.Networking.BinaryMessageDecoder;
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
import seng302.Networking.Utils.MessageType;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by hba56 on 21/04/17.
*/
public class BinaryMessageDecoderTest {
@Test
public void decodeTest(){
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((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
byte[] encodedMessage = testEncoder.encode();
BinaryMesageEncoder testMessage = new BinaryMesageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
testDecoder.decode();
//message length
Assert.assertEquals((short) encodedMessage.length,testDecoder.getMessageLength());
//time stamp
Assert.assertEquals(time,testDecoder.getTimeStamp());
//source ID
Assert.assertEquals((short) 1, testDecoder.getSourceID());
//message type
Assert.assertEquals(26, testDecoder.getMessageType());
XMLMessageDecoder decoderXML = new XMLMessageDecoder(testDecoder.getMessage());
decoderXML.decode();
//tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
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());
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
}catch (IOException e){
System.out.println(e);
}
}
}
@@ -0,0 +1,50 @@
package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageDecoders.BoatLocationDecoder;
import seng302.Networking.Utils.BoatLocationMessage;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
/**
* Created by hba56 on 23/04/17.
*/
public class BoatLocationDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
BoatLocationMessage testMessage = new BoatLocationMessage((byte)1, time, (byte)2,
3, (byte) 1, 180, -180, 4, (short)5,
(short)6, (short)7, 8, 9, 10, 11,
(short) 12, 13,(short) 14 ,(short) 15,
16, 17, (short) 18);
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
byte [] testEncodedMessage = raceVisionByteEncoder.boatLocation(testMessage);
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
BoatLocationMessage decodedTest = testDecoder.getMessage();
Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber());
Assert.assertEquals(testMessage.getTime(), decodedTest.getTime());
Assert.assertEquals(testMessage.getSequenceNumber(), decodedTest.getSequenceNumber());
Assert.assertEquals(testMessage.getDeviceType(), decodedTest.getDeviceType());
Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude());
Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude());
Assert.assertEquals(testMessage.getAltitude(), decodedTest.getAltitude());
Assert.assertEquals(testMessage.getHeading(), decodedTest.getHeading());
Assert.assertEquals(testMessage.getPitch(), decodedTest.getPitch());
Assert.assertEquals(testMessage.getRoll(), decodedTest.getRoll());
Assert.assertEquals(testMessage.getBoatSpeed(), decodedTest.getBoatSpeed());
Assert.assertEquals(testMessage.getBoatCOG(), decodedTest.getBoatCOG());
Assert.assertEquals(testMessage.getBoatSOG(), decodedTest.getBoatSOG());
Assert.assertEquals(testMessage.getApparentWindSpeed(), decodedTest.getApparentWindSpeed());
Assert.assertEquals(testMessage.getTrueWindSpeed(), decodedTest.getTrueWindSpeed());
Assert.assertEquals(testMessage.getTrueWindDirection(), decodedTest.getTrueWindDirection());
Assert.assertEquals(testMessage.getTrueWindAngle(), decodedTest.getTrueWindAngle());
Assert.assertEquals(testMessage.getCurrentDrift(), decodedTest.getCurrentDrift());
Assert.assertEquals(testMessage.getCurrentSet(), decodedTest.getCurrentSet());
Assert.assertEquals(testMessage.getRudderAngle(), decodedTest.getRudderAngle());
}
}
@@ -0,0 +1,60 @@
package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageDecoders.CourseWindDecoder;
import seng302.Networking.Utils.CourseWind;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import java.util.ArrayList;
/**
* Created by hba56 on 23/04/17.
*/
public class CourseWindDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
CourseWind testCourseWind1 = new CourseWind(1, time, 2,
3, 4, 5,
7, 6);
long time2 = System.currentTimeMillis();
CourseWind testCourseWind2 = new CourseWind(2, time2, 2,
3, 4, 5,
7, 6);
ArrayList<CourseWind> testCourseWinds = new ArrayList<CourseWind>();
testCourseWinds.add(testCourseWind1);
testCourseWinds.add(testCourseWind2);
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
byte[] testEncodedCourseWind = raceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind);
ArrayList<CourseWind> testDecodedCourseWinds = testDecoder.getLoopMessages();
Assert.assertEquals(testCourseWinds.get(0).getID(), testDecodedCourseWinds.get(0).getID());
Assert.assertEquals(testCourseWinds.get(0).getTime(), testDecodedCourseWinds.get(0).getTime());
Assert.assertEquals(testCourseWinds.get(0).getRaceID(), testDecodedCourseWinds.get(0).getRaceID());
Assert.assertEquals(testCourseWinds.get(0).getWindDirection(), testDecodedCourseWinds.get(0).getWindDirection());
Assert.assertEquals(testCourseWinds.get(0).getWindSpeed(), testDecodedCourseWinds.get(0).getWindSpeed());
Assert.assertEquals(testCourseWinds.get(0).getBestUpwindAngle(), testDecodedCourseWinds.get(0).getBestUpwindAngle());
Assert.assertEquals(testCourseWinds.get(0).getBestDownwindAngle(), testDecodedCourseWinds.get(0).getBestDownwindAngle());
Assert.assertEquals(testCourseWinds.get(0).getFlags(), testDecodedCourseWinds.get(0).getFlags());
Assert.assertEquals(testCourseWinds.get(1).getID(), testDecodedCourseWinds.get(1).getID());
Assert.assertEquals(testCourseWinds.get(1).getTime(), testDecodedCourseWinds.get(1).getTime());
Assert.assertEquals(testCourseWinds.get(1).getRaceID(), testDecodedCourseWinds.get(1).getRaceID());
Assert.assertEquals(testCourseWinds.get(1).getWindDirection(), testDecodedCourseWinds.get(1).getWindDirection());
Assert.assertEquals(testCourseWinds.get(1).getWindSpeed(), testDecodedCourseWinds.get(1).getWindSpeed());
Assert.assertEquals(testCourseWinds.get(1).getBestUpwindAngle(), testDecodedCourseWinds.get(1).getBestUpwindAngle());
Assert.assertEquals(testCourseWinds.get(1).getBestDownwindAngle(), testDecodedCourseWinds.get(1).getBestDownwindAngle());
Assert.assertEquals(testCourseWinds.get(1).getFlags(), testDecodedCourseWinds.get(1).getFlags());
}
}
@@ -0,0 +1,31 @@
package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageDecoders.RaceStartStatusDecoder;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
/**
* Created by hba56 on 23/04/17.
*/
public class RaceStartStatusDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
long time2 = System.currentTimeMillis();
byte[] encodedRaceStartStatus = raceVisionByteEncoder.raceStartStatus(time, (short)1,
time2, 2, (char)3);
RaceStartStatusDecoder testDecoder = new RaceStartStatusDecoder(encodedRaceStartStatus);
Assert.assertEquals(0b1, testDecoder.getMessageVersion());
Assert.assertEquals(time, testDecoder.getTime());
Assert.assertEquals(1, testDecoder.getAck());
Assert.assertEquals(time2, testDecoder.getStartTime());
Assert.assertEquals(2, testDecoder.getRaceID());
Assert.assertEquals((char)3, testDecoder.getNotification());
}
}
@@ -0,0 +1,50 @@
package seng302.Networking.MessageDecoders;
import javafx.scene.paint.Color;
import org.junit.Assert;
import org.junit.Test;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Networking.MessageDecoders.RaceStatusDecoder;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import java.util.ArrayList;
/**
* Created by hba56 on 23/04/17.
*/
public class RaceStatusDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
BoatInRace boat1 = new BoatInRace("Test", 1, Color.ALICEBLUE, "tt", 1);
Leg testLeg = new Leg("test leg", 1);
boat1.setCurrentLeg(testLeg);
BoatInRace boat2 = new BoatInRace("Test2", 1, Color.ALICEBLUE, "tt", 1);
boat2.setCurrentLeg(testLeg);
ArrayList boats = new ArrayList();
boats.add(boat1);
boats.add(boat2);
long time2 = System.currentTimeMillis();
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
byte[] encodedRaceStatus = raceVisionByteEncoder.raceStatus(time, 1, 2, time2,
(short)2, (short)3,4, boats);
RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);
Assert.assertEquals(0b10, decoderTest.getVersionNum());
Assert.assertEquals(time, decoderTest.getTime());
Assert.assertEquals(1, decoderTest.getRace());
Assert.assertEquals(2, decoderTest.getRaceState());
Assert.assertEquals(time2, decoderTest.getStartTime());
Assert.assertEquals((short)2, decoderTest.getRaceWindDir());
Assert.assertEquals((short)3, decoderTest.getRaceWindSpeed());
Assert.assertEquals(0, decoderTest.getBoats().get(0).getBoatStatus());
Assert.assertEquals(0, decoderTest.getBoats().get(0).getLegNumber());
Assert.assertEquals(0, decoderTest.getBoats().get(0).getNumPenaltiesAwarded());
}
}
@@ -0,0 +1,48 @@
package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 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((byte)1, (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());
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
}catch (IOException e){
System.out.println(e);
}
}
}
@@ -0,0 +1,55 @@
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((byte)1, (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){
System.out.println(e);
}
}
@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((byte)1, (short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)1, xmlString.toString());
byte[] encodedXML = testEncoder.encode();
Assert.assertEquals(null, encodedXML);
}catch (IOException e){
System.out.println(e);
}
}
}