package com.mattrixwv.cipherstream.controller.monosubstitution; 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 AutokeyCipherControllerTest{ @InjectMocks private AutokeyCipherController autokeyCipherController; //Fields private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String AUTOKEY_INPUT_STRING = "Message to^encode"; private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx"; private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD; private static final String autokeyName = "autokeyName"; private static final String autokeyDescription = "autokeyDescription"; private static final List autokeyExplanation = List.of("autokeyExplanation1", "autokeyExplanation2", "autokeyExplanation3"); private static final List autokeyFacts = List.of("autokeyFact1", "autokeyFact2", "autokeyFact3"); @BeforeEach public void setup(){ ReflectionTestUtils.setField(autokeyCipherController, "autokeyName", autokeyName); ReflectionTestUtils.setField(autokeyCipherController, "autokeyDescription", autokeyDescription); ReflectionTestUtils.setField(autokeyCipherController, "autokeyExplanation", autokeyExplanation); ReflectionTestUtils.setField(autokeyCipherController, "autokeyFacts", autokeyFacts); } @Test public void testGetCipherInfo(){ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(autokeyName, autokeyDescription, autokeyExplanation, autokeyFacts); ObjectNode returnedJson = autokeyCipherController.getCipherInfo(); assertEquals(infoNode, returnedJson); } @Test public void testEncodeAutokey(){ ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING); ObjectNode returnedJson = autokeyCipherController.encodeAutokey(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString()); } @Test public void testEncodeAutokey_invalidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { autokeyCipherController.encodeAutokey(blankNode); }); } @Test public void testDecodeAutokey(){ ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_OUTPUT_STRING); ObjectNode returnedJson = autokeyCipherController.decodeAutokey(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString()); } @Test public void testDecodeAutokey_invapidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { autokeyCipherController.decodeAutokey(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; } }