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:
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user