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

115 lines
4.1 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 TrifidCipherControllerTest{
@InjectMocks
private TrifidCipherController trifidCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String TRIFID_INPUT_STRING = "Message to^encode";
private static final String TRIFID_OUTPUT_STRING = "Gpjqdvd of^odlklf";
private static final String TRIFID_KEYWORD = CipherParameterUtil.KEYWORD;
private static final char TRIFID_FILL_ID = '+';
private static final int TRIFID_GROUP_LENGTH = 3;
private static final String trifidName = "trifidName";
private static final String trifidDescription = "trifidDescription";
private static final List<String> trifidExplanation = List.of("trifidExplanation1", "trifidExplanation2", "trifidExplanation3");
private static final List<String> trifidFacts = List.of("trifidFact1", "trifidFact2", "trifidFact3");
@BeforeEach
public void setup(){
ReflectionTestUtils.setField(trifidCipherController, "trifidName", trifidName);
ReflectionTestUtils.setField(trifidCipherController, "trifidDescription", trifidDescription);
ReflectionTestUtils.setField(trifidCipherController, "trifidExplanation", trifidExplanation);
ReflectionTestUtils.setField(trifidCipherController, "trifidFacts", trifidFacts);
}
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(trifidName, trifidDescription, trifidExplanation, trifidFacts);
ObjectNode returnedJson = trifidCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeTrifid(){
ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_INPUT_STRING);
ObjectNode returnedJson = trifidCipherController.encodeTrifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testEncodeTrifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.encodeTrifid(blankNode);
});
}
@Test
public void testDecodeTrifid(){
ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_OUTPUT_STRING);
ObjectNode returnedJson = trifidCipherController.decodeTrifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testDecodeTrifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.decodeTrifid(blankNode);
});
}
private ObjectNode generateParams(String keyword, char fill, int groupLength, 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.TRIFID_FILL, String.valueOf(fill));
cipherParams.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, groupLength);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}