Added MessageDecoder interface. All decoder implement this.
Added DecoderFactory. This creates an appropriate decoder based on a MessageType. BoatActionDecoder implements MessageDecoder. BoatLocationDecoder implements MessageDecoder. HeartBeatDecoder implements MessageDecoder. JoinAcceptance implements MessageDecoder. RaceStatusDecoder implements MessageDecoder. RequestToJoinDecoder implements MessageDecoder. XMLMessageDecoder implements MessageDecoder. Refactored CourseWind decoder/encoder. CourseWind decoder/encoder is for an individual CourseWind. CourseWinds decoder/encoder is for the combined message from the API. Documented BoatAction, and it now contains a BoatActionEnum instead of a byte. Refactored CourseWind and CourseWinds classes. They now expose correct units, instead of packed units. Added CourseWindDecoderTest, and updated CourseWindsDecoderTest. issue #35 #36 #story[1095]
This commit is contained in:
@@ -29,10 +29,10 @@ public class BoatActionDecoderTest {
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
BoatActionDecoder testDecoder = new BoatActionDecoder(testEncodedMessage);
|
||||
BoatActionEnum decodedBoatAction = testDecoder.getBoatAction();
|
||||
BoatActionDecoder testDecoder = new BoatActionDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
|
||||
BoatAction decodedMessage = new BoatAction(decodedBoatAction);
|
||||
BoatAction decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,8 @@ public class BoatLocationDecoderTest {
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(testMessage);
|
||||
|
||||
//Decode.
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
BoatLocation decodedTest = testDecoder.getMessage();
|
||||
|
||||
//Check if valid.
|
||||
|
||||
@@ -59,12 +59,10 @@ public class BoatStatusDecoderTest {
|
||||
private static BoatStatus encodeDecodeBoatStatus(BoatStatus boatStatus) {
|
||||
|
||||
BoatStatusEncoder boatStatusEncoder = new BoatStatusEncoder();
|
||||
|
||||
byte[] boatStatusEncoded = boatStatusEncoder.encode(boatStatus);
|
||||
|
||||
BoatStatusDecoder boatStatusDecoder = new BoatStatusDecoder(boatStatusEncoded);
|
||||
|
||||
BoatStatus boatStatusDecoded = boatStatusDecoder.getMessage();
|
||||
BoatStatusDecoder boatStatusDecoder = new BoatStatusDecoder();
|
||||
BoatStatus boatStatusDecoded = boatStatusDecoder.decode(boatStatusEncoded);
|
||||
|
||||
return boatStatusDecoded;
|
||||
}
|
||||
|
||||
@@ -1,57 +1,77 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.MessageEncoders.CourseWindEncoder;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.CourseWind;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import shared.model.Bearing;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
* Test for the CourseWind encoder and decoder
|
||||
*/
|
||||
public class CourseWindDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a CourseWind message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
public void courseWindEncodeDecodeTest() throws Exception {
|
||||
|
||||
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);
|
||||
CourseWind courseWind = new CourseWind(
|
||||
1,
|
||||
time,
|
||||
2,
|
||||
Bearing.fromDegrees(45),
|
||||
4,
|
||||
Bearing.fromDegrees(70),
|
||||
Bearing.fromDegrees(160),
|
||||
(byte) 0x13 );
|
||||
|
||||
|
||||
byte[] testEncodedCourseWind = RaceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
|
||||
CourseWind courseWindDecoded = encodeDecodeCourseWind(courseWind);
|
||||
|
||||
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());
|
||||
compareCourseWindMessages(courseWind, courseWindDecoded);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes and decodes a CourseWind, and returns it.
|
||||
* @param courseWind The CourseWind to encode and decode.
|
||||
* @return The decoded CourseWind.
|
||||
*/
|
||||
private static CourseWind encodeDecodeCourseWind(CourseWind courseWind) {
|
||||
|
||||
CourseWindEncoder courseWindEncoder = new CourseWindEncoder();
|
||||
byte[] courseWindEncoded = courseWindEncoder.encode(courseWind);
|
||||
|
||||
CourseWindDecoder courseWindDecoder = new CourseWindDecoder();
|
||||
CourseWind courseWindDecoded = courseWindDecoder.decode(courseWindEncoded);
|
||||
|
||||
return courseWindDecoded;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two CourseWind messages to check that they are equal.
|
||||
* @param original The original CourseWind message.
|
||||
* @param decoded The decoded CourseWind message.
|
||||
*/
|
||||
public static void compareCourseWindMessages(CourseWind original, CourseWind decoded) {
|
||||
|
||||
Assert.assertEquals(original.getID(), decoded.getID());
|
||||
Assert.assertEquals(original.getTime(), decoded.getTime());
|
||||
Assert.assertEquals(original.getRaceID(), decoded.getRaceID());
|
||||
Assert.assertEquals(original.getWindDirection().degrees(), decoded.getWindDirection().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getWindSpeedKnots(), decoded.getWindSpeedKnots(), 0.01);
|
||||
Assert.assertEquals(original.getBestUpwindAngle().degrees(), decoded.getBestUpwindAngle().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getBestDownwindAngle().degrees(), decoded.getBestDownwindAngle().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getFlags(), decoded.getFlags());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.CourseWind;
|
||||
import network.Messages.CourseWinds;
|
||||
import org.junit.Test;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for CourseWinds encoder and decoder.
|
||||
*/
|
||||
public class CourseWindsDecoderTest {
|
||||
|
||||
/**
|
||||
* Tests if a CourseWinds message can be encoded and decoded correctly.
|
||||
* @throws Exception Thrown if an error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void courseWindsEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind1 = new CourseWind(
|
||||
1,
|
||||
time1,
|
||||
2,
|
||||
Bearing.fromDegrees(45),
|
||||
4,
|
||||
Bearing.fromDegrees(70),
|
||||
Bearing.fromDegrees(160),
|
||||
(byte) 0xCE );
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind2 = new CourseWind(
|
||||
2,
|
||||
time2,
|
||||
2,
|
||||
Bearing.fromDegrees(55),
|
||||
4,
|
||||
Bearing.fromDegrees(80),
|
||||
Bearing.fromDegrees(180),
|
||||
(byte) 0x0D );
|
||||
|
||||
List<CourseWind> testCourseWinds = new ArrayList<>();
|
||||
testCourseWinds.add(testCourseWind1);
|
||||
testCourseWinds.add(testCourseWind2);
|
||||
|
||||
CourseWinds courseWinds = new CourseWinds(CourseWinds.currentMessageVersionNumber, (byte) 2, testCourseWinds);
|
||||
|
||||
|
||||
byte[] testEncodedCourseWind = RaceVisionByteEncoder.encode(courseWinds);
|
||||
|
||||
CourseWindsDecoder courseWindsDecoder = new CourseWindsDecoder();
|
||||
courseWindsDecoder.decode(testEncodedCourseWind);
|
||||
CourseWinds courseWindsDecoded = courseWindsDecoder.getMessage();
|
||||
|
||||
compareCourseWindsMessages(courseWinds, courseWindsDecoded);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two course winds messages to ensure they are the same.
|
||||
* @param original The original message.
|
||||
* @param decoded The decoded message.
|
||||
*/
|
||||
public static void compareCourseWindsMessages(CourseWinds original, CourseWinds decoded) {
|
||||
|
||||
//Compare header.
|
||||
assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
assertEquals(original.getSelectedWindID(), decoded.getSelectedWindID());
|
||||
assertEquals(original.getCourseWinds().size(), decoded.getCourseWinds().size());
|
||||
|
||||
//Compare each CourseWind.
|
||||
List<CourseWind> originalWinds = original.getCourseWinds();
|
||||
List<CourseWind> decodedWinds = decoded.getCourseWinds();
|
||||
|
||||
Iterator<CourseWind> originalIterator = originalWinds.iterator();
|
||||
Iterator<CourseWind> decodedIterator = decodedWinds.iterator();
|
||||
|
||||
while (originalIterator.hasNext() && decodedIterator.hasNext()) {
|
||||
|
||||
CourseWindDecoderTest.compareCourseWindMessages(originalIterator.next(), decodedIterator.next());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,8 @@ public class HeartBeatDecoderTest {
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
HeartBeatDecoder testDecoder = new HeartBeatDecoder(testEncodedMessage);
|
||||
HeartBeatDecoder testDecoder = new HeartBeatDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
HeartBeat decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
|
||||
@@ -27,7 +27,8 @@ public class JoinAcceptanceDecoderTest {
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
JoinAcceptanceDecoder testDecoder = new JoinAcceptanceDecoder(testEncodedMessage);
|
||||
JoinAcceptanceDecoder testDecoder = new JoinAcceptanceDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
JoinAcceptance decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
|
||||
@@ -89,7 +89,8 @@ public class RaceStatusDecoderTest {
|
||||
|
||||
byte[] encodedRaceStatus = RaceVisionByteEncoder.encode(raceStatusOriginal);
|
||||
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder();
|
||||
decoderTest.decode(encodedRaceStatus);
|
||||
|
||||
RaceStatus decodedMessage = decoderTest.getMessage();
|
||||
|
||||
|
||||
@@ -28,7 +28,8 @@ public class RequestToJoinDecoderTest {
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
RequestToJoinDecoder testDecoder = new RequestToJoinDecoder(testEncodedMessage);
|
||||
RequestToJoinDecoder testDecoder = new RequestToJoinDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
RequestToJoin decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
|
||||
@@ -45,7 +45,8 @@ public class XMLMessageDecoderTest {
|
||||
|
||||
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(encodedXML);
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder();
|
||||
decoderXML.decode(encodedXML);
|
||||
XMLMessage decodedMessage = decoderXML.getMessage();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user