Renamed Heartbeat to HeartBeat.

Added HeartBeatDecoder.
Added HeartBeatEncoder.
BinaryMessageDecoder now uses HeartBeatDecoder.
MockOutput now logs a warning if a heartBeat cannot be encoded.
Added HeartBeatDecoderTest.
issue #35 #36
#story[1095]
This commit is contained in:
fjc40
2017-08-05 23:27:28 +12:00
parent c3ed30019c
commit 8ef906472b
9 changed files with 192 additions and 44 deletions
@@ -0,0 +1,74 @@
package network.MessageDecoders;
import network.Exceptions.InvalidMessageException;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
import network.Messages.HeartBeat;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Test for the HeartBeat encoder and decoder
*/
public class HeartBeatDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private HeartBeat encodeDecodeMessage(HeartBeat message) throws InvalidMessageException {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
//Decode.
HeartBeatDecoder testDecoder = new HeartBeatDecoder(testEncodedMessage);
HeartBeat decodedMessage = testDecoder.getMessage();
return decodedMessage;
}
/**
* Tests if a heartbeat message with a given sequence number can be encoded and decoded correctly.
* @param sequenceNumber The sequenceNumber to use.
*/
private void heartBeatTest(long sequenceNumber) throws Exception {
//Prepare message.
HeartBeat beforeMessage = new HeartBeat(sequenceNumber);
//Encode/decode it.
HeartBeat afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getSequenceNumber(), afterMessage.getSequenceNumber());
}
/**
* Tests if a heartbeat message with a sequence number of zero can be encoded and decoded correctly.
*/
@Test
public void heartBeatZeroTest() throws Exception {
heartBeatTest(0);
}
/**
* Tests if a heartbeat message with a sequence number of 1234512 can be encoded and decoded correctly.
*/
@Test
public void heartBeatNonZeroTest() throws Exception {
heartBeatTest(1234512);
}
}