Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerTest.java
Matthew Ellison ff11cd72c3 Updated more tests
2024-04-08 16:44:38 -04:00

97 lines
3.0 KiB
Java

package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.core.JsonProcessingException;
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;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
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 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);
ObjectNode returnedJson = hillCipherController.encodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testEncodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.encodeHill(blankNode);
});
}
@Test
public void testDecodeHill() throws JsonProcessingException{
ObjectNode cipherParams = generateParams(HILL_OUTPUT_STRING);
ObjectNode returnedJson = hillCipherController.decodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testDecodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.decodeHill(blankNode);
});
}
private ObjectNode generateParams(String inputString){
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 = mapper.valueToTree(KEY);
cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode);
return cipherParams;
}
}