Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerTest.java
2026-01-06 23:48:37 -05:00

114 lines
3.7 KiB
Java

package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
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 org.springframework.test.util.ReflectionTestUtils;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.node.ObjectNode;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class HillCipherControllerTest{
@InjectMocks
private HillCipherController hillCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
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 String hillName = "hillName";
private static final String hillDescription = "hillDescription";
private static final List<String> hillExplanation = List.of("hillExplanation1", "hillExplanation2", "hillExplanation3");
private static final List<String> hillFacts = List.of("hillFact1", "hillFact2", "hillFact3");
@BeforeEach
public void setup(){
ReflectionTestUtils.setField(hillCipherController, "hillName", hillName);
ReflectionTestUtils.setField(hillCipherController, "hillDescription", hillDescription);
ReflectionTestUtils.setField(hillCipherController, "hillExplanation", hillExplanation);
ReflectionTestUtils.setField(hillCipherController, "hillFacts", hillFacts);
}
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(hillName, hillDescription, hillExplanation, hillFacts);
ObjectNode returnedJson = hillCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeHill(){
ObjectNode cipherParams = generateParams(HILL_INPUT_STRING);
ObjectNode returnedJson = hillCipherController.encodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testEncodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.encodeHill(blankNode);
});
}
@Test
public void testDecodeHill(){
ObjectNode cipherParams = generateParams(HILL_OUTPUT_STRING);
ObjectNode returnedJson = hillCipherController.decodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@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;
}
}