Added BoatActonEncoder.

Updated ControllerClient to use RaceVisionByteEncoder.encode(message). It also logs a warning if the encoding fails. Also removed pointless and out of place encode/decode of the action enum.
Added a BoatActionDecoderTest which tests encoding/decoding a BoatAction Message.
issue #35 #36
#story[1095]
This commit is contained in:
fjc40
2017-08-05 21:53:54 +12:00
parent 16686678a7
commit 31ce9fff94
6 changed files with 168 additions and 14 deletions
@@ -0,0 +1,109 @@
package network.MessageDecoders;
import network.Exceptions.InvalidMessageException;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.RequestToJoin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Test for the BoatAction encoder and decoder
*/
public class BoatActionDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private BoatAction encodeDecodeMessage(BoatAction message) throws InvalidMessageException {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
//Decode.
BoatActionDecoder testDecoder = new BoatActionDecoder(testEncodedMessage);
BoatActionEnum decodedBoatAction = testDecoder.getBoatAction();
BoatAction decodedMessage = new BoatAction(decodedBoatAction);
return decodedMessage;
}
/**
* Tests if a specific boat action type message can be encoded and decoded correctly.
* @param type The type of boat action.
*/
private void boatActionTypeTest(BoatActionEnum type) throws Exception {
//Prepare message.
BoatAction beforeMessage = new BoatAction(type);
//Encode/decode it.
BoatAction afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getBoatAction(), afterMessage.getBoatAction());
}
/**
* Tests if an autopilot message can be encoded and decoded correctly.
*/
@Test
public void autoPilotTest() throws Exception {
boatActionTypeTest(BoatActionEnum.AUTO_PILOT);
}
/**
* Tests if a sails in message can be encoded and decoded correctly.
*/
@Test
public void sailsInTest() throws Exception {
boatActionTypeTest(BoatActionEnum.SAILS_IN);
}
/**
* Tests if a sails out message can be encoded and decoded correctly.
*/
@Test
public void sailsOutTest() throws Exception {
boatActionTypeTest(BoatActionEnum.SAILS_OUT);
}
/**
* Tests if a tack/gybe message can be encoded and decoded correctly.
*/
@Test
public void tackGybeTest() throws Exception {
boatActionTypeTest(BoatActionEnum.TACK_GYBE);
}
/**
* Tests if an upwind message can be encoded and decoded correctly.
*/
@Test
public void upwindTest() throws Exception {
boatActionTypeTest(BoatActionEnum.UPWIND);
}
/**
* Tests if a downwind message can be encoded and decoded correctly.
*/
@Test
public void downwindTest() throws Exception {
boatActionTypeTest(BoatActionEnum.DOWNWIND);
}
}