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

110 lines
3.8 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.ObjectMapper;
import tools.jackson.databind.node.ObjectNode;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class PolybiusSquareControllerTest{
@InjectMocks
private PolybiusSquareController polybiusSquareController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String POLYBIUS_INPUT_STRING = "B A-T";
private static final String POLYBIUS_OUTPUT_STRING = "15 14-52";
private static final String POLYBIUS_KEYWORD = "Z Y+ X-";
private static final String polybiusName = "polybiusName";
private static final String polybiusDescription = "polybiusDescription";
private static final List<String> polybiusExplanation = List.of("PolybiusExplanation1", "PolybiusExplanation2", "PolybiusExplanation3");
private static final List<String> polybiusFacts = List.of("PolybiusFact1", "PolybiusFact2", "PolybiusFact3");
@BeforeEach
public void setup(){
ReflectionTestUtils.setField(polybiusSquareController, "polybiusName", polybiusName);
ReflectionTestUtils.setField(polybiusSquareController, "polybiusDescription", polybiusDescription);
ReflectionTestUtils.setField(polybiusSquareController, "polybiusExplanation", polybiusExplanation);
ReflectionTestUtils.setField(polybiusSquareController, "polybiusFacts", polybiusFacts);
}
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(polybiusName, polybiusDescription, polybiusExplanation, polybiusFacts);
ObjectNode returnedJson = polybiusSquareController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodePolybius(){
ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_INPUT_STRING);
ObjectNode returnedJson = polybiusSquareController.encodePolybius(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testEncodePolybius_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.encodePolybius(blankNode);
});
}
@Test
public void testDecodePolybius(){
ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_OUTPUT_STRING);
ObjectNode returnedJson = polybiusSquareController.decodePolybius(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testDecodePolybius_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.decodePolybius(blankNode);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}