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 POLYBIUS_NAME = "polybiusName"; private static final String POLYBIUS_DESCRIPTION = "polybiusDescription"; private static final List POLYBIUS_EXPLANATION = List.of("PolybiusExplanation1", "PolybiusExplanation2", "PolybiusExplanation3"); private static final List POLYBIUS_FACTS = List.of("PolybiusFact1", "PolybiusFact2", "PolybiusFact3"); @BeforeEach public void setup(){ ReflectionTestUtils.setField(polybiusSquareController, "polybiusName", POLYBIUS_NAME); ReflectionTestUtils.setField(polybiusSquareController, "polybiusDescription", POLYBIUS_DESCRIPTION); ReflectionTestUtils.setField(polybiusSquareController, "polybiusExplanation", POLYBIUS_EXPLANATION); ReflectionTestUtils.setField(polybiusSquareController, "polybiusFacts", POLYBIUS_FACTS); } @Test public void tetGetCipherInfo(){ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(POLYBIUS_NAME, POLYBIUS_DESCRIPTION, POLYBIUS_EXPLANATION, POLYBIUS_FACTS); 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; } }