package com.mattrixwv.cipherstream.controller.polysubstitution; import static org.junit.jupiter.api.Assertions.*; 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 com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; @Tag("unit-test") @ExtendWith(MockitoExtension.class) public class PlayfairCipherControllerTest{ @InjectMocks private PlayfairCipherController playfairCipherController; private static final String PLAYFAIR_INPUT_STRING = "Hide the gold in - the@tree+stump"; private static final String DECODED_INPUT_STRING = "Hide the gold in - the@trexe+stump"; private static final String PLAYFAIR_OUTPUT_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif"; private static final String PLAYFAIR_KEYWORD = "Play-fair@Exam ple"; private ObjectMapper objectMapper = new ObjectMapper(); @Test public void testEncodePlayfair(){ ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_INPUT_STRING); ObjectNode returnedJson = playfairCipherController.encodePlayfair(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(PLAYFAIR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); //Verify invalid params are caught final ObjectNode blankNode = objectMapper.createObjectNode(); assertThrows(InvalidCipherParameterException.class, () -> { playfairCipherController.encodePlayfair(blankNode); }); } @Test public void testDecodePlayfair(){ ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_OUTPUT_STRING); ObjectNode returnedJson = playfairCipherController.decodePlayfair(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(DECODED_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); //Verify invalid params are caught final ObjectNode blankNode = objectMapper.createObjectNode(); assertThrows(InvalidCipherParameterException.class, () -> { playfairCipherController.decodePlayfair(blankNode); }); } private ObjectNode generateParams(String keyword, String inputString){ ObjectNode cipherParams = objectMapper.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; } }