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]main
parent
16686678a7
commit
31ce9fff94
@ -0,0 +1,40 @@
|
|||||||
|
package network.MessageEncoders;
|
||||||
|
|
||||||
|
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.BoatAction;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import static network.Utils.ByteConverter.intToBytes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This encoder can encode a {@link BoatAction} message.
|
||||||
|
*/
|
||||||
|
public class BoatActionEncoder implements MessageEncoder {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*/
|
||||||
|
public BoatActionEncoder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encode(AC35Data message) {
|
||||||
|
|
||||||
|
//Downcast.
|
||||||
|
BoatAction boatAction = (BoatAction) message;
|
||||||
|
|
||||||
|
//Message is 1 byte.
|
||||||
|
ByteBuffer boatActionMessage = ByteBuffer.allocate(1);
|
||||||
|
|
||||||
|
boatActionMessage.put(intToBytes(boatAction.getBoatAction(), 1));
|
||||||
|
|
||||||
|
byte [] result = boatActionMessage.array();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue