RequestToJoinEnum contains an int instead of a byte.

Added requestToJoin and joinAcceptance encoding functions to RaceVisionByteEncoder.
Implemented JoinAcceptanceDecoder.
Implemented RequestToJoinDecoder.
Added tests for encoding/decoding RequestToJoin and JoinAcceptance messages.

issue #35
#story[1095]
This commit is contained in:
fjc40
2017-08-05 20:42:25 +12:00
parent ca2b8a8899
commit 7ea5b31fa1
6 changed files with 366 additions and 15 deletions
@@ -0,0 +1,99 @@
package network.MessageDecoders;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.Enums.JoinAcceptanceEnum;
import network.Messages.JoinAcceptance;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test for the {@link network.Messages.JoinAcceptance} encoder and decoder
*/
public class JoinAcceptanceDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private JoinAcceptance encodeDecodeMessage(JoinAcceptance message) {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.joinAcceptance(message);
//Decode.
JoinAcceptanceDecoder testDecoder = new JoinAcceptanceDecoder(testEncodedMessage);
JoinAcceptance decodedMessage = testDecoder.getMessage();
return decodedMessage;
}
/**
* Tests if a specific acceptance type message can be encoded and decoded correctly.
* @param type The type of acceptance response.
* @param sourceID The source ID to assign.
*/
private void responseTypeTest(JoinAcceptanceEnum type, int sourceID) throws Exception {
//Prepare message.
JoinAcceptance beforeMessage = new JoinAcceptance(type, sourceID);
//Encode/decode it.
JoinAcceptance afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getAcceptanceType().getValue(), afterMessage.getAcceptanceType().getValue());
assertEquals(beforeMessage.getSourceID(), afterMessage.getSourceID());
}
/**
* Tests if a join success message, with a source ID, can be encoded and decoded correctly.
*/
@Test
public void joinSuccessSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 12345);
}
/**
* Tests if a join success message, with no source ID, can be encoded and decoded correctly.
*/
@Test
public void joinSuccessNoSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 0);
}
/**
* Tests if a participants full message can be encoded and decoded correctly.
*/
@Test
public void participantFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.RACE_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a ghosts full message can be encoded and decoded correctly.
*/
@Test
public void ghostFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.GHOST_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a server full message can be encoded and decoded correctly.
*/
@Test
public void serverFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.SERVER_FULL, 0);
}
}
@@ -0,0 +1,82 @@
package network.MessageDecoders;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.RequestToJoin;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test for the RequestToJoin encoder and decoder
*/
public class RequestToJoinDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private RequestToJoin encodeDecodeMessage(RequestToJoin message) {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.requestToJoin(message);
//Decode.
RequestToJoinDecoder testDecoder = new RequestToJoinDecoder(testEncodedMessage);
RequestToJoin decodedMessage = testDecoder.getMessage();
return decodedMessage;
}
/**
* Tests if a specific request type message can be encoded and decoded correctly.
* @param type The type of join request.
*/
private void requestTypeTest(RequestToJoinEnum type) throws Exception {
//Prepare message.
RequestToJoin beforeMessage = new RequestToJoin(type);
//Encode/decode it.
RequestToJoin afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getRequestType().getValue(), afterMessage.getRequestType().getValue());
}
/**
* Tests if a spectator request message can be encoded and decoded correctly.
*/
@Test
public void spectatorTest() throws Exception {
requestTypeTest(RequestToJoinEnum.SPECTATOR);
}
/**
* Tests if a participant request message can be encoded and decoded correctly.
*/
@Test
public void participantTest() throws Exception {
requestTypeTest(RequestToJoinEnum.PARTICIPANT);
}
/**
* Tests if a ghost request message can be encoded and decoded correctly.
*/
@Test
public void ghostTest() throws Exception {
requestTypeTest(RequestToJoinEnum.GHOST);
}
}