Updated more tests

This commit is contained in:
Matthew Ellison
2024-04-08 16:44:38 -04:00
parent 1a93a30b42
commit ff11cd72c3
52 changed files with 1813 additions and 294 deletions

View File

@@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,12 +23,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class HillCipherControllerTest{
@InjectMocks
private HillCipherController hillCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String HILL_INPUT_STRING = "Message to^encode";
private static final String HILL_OUTPUT_STRING = "Mgkeqge ul^ikhisp";
private static final int[][] KEY = {{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
private ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.HILL_CIPHER_NAME, CipherInfoUtil.HILL_CIPHER_DESCRIPTION);
ObjectNode returnedJson = hillCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeHill() throws JsonProcessingException{
ObjectNode cipherParams = generateParams(HILL_INPUT_STRING);
@@ -38,9 +52,10 @@ public class HillCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testEncodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.encodeHill(blankNode);
});
@@ -56,22 +71,24 @@ public class HillCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testDecodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.decodeHill(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
JsonNode keyNode = objectMapper.valueToTree(KEY);
JsonNode keyNode = mapper.valueToTree(KEY);
cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode);
return cipherParams;