package com.mattrixwv.cipherstream.polysubstitution; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; @ExtendWith(MockitoExtension.class) public class BifidTest{ @InjectMocks private Bifid cipher; @Mock private Logger logger; //Test private static final String INPUT_STRING = "Message to^encode"; private static final String INPUT_STRING_CLEAN = "MESSAGETOENCODE"; private static final String OUTPUT_STRING = "Mqaokne kc^vdodzd"; private static final String OUTPUT_STRING_CLEAN = "MQAOKNEKCVDODZD"; private static final String KEYWORD = "keyword"; private static final String KEYWORD_CLEAN = "KEYWORDABCFGHILMNPQSTUVXZ"; @Test public void testConstructor_default(){ cipher = new Bifid(); assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); assertNotNull(cipher.polybiusSquare); assertFalse(cipher.polybiusSquare.preserveWhitespace); assertFalse(cipher.polybiusSquare.preserveSymbols); assertEquals("", cipher.inputString); assertEquals("", cipher.keyword); assertEquals("", cipher.outputString); } @Test public void testConstructor_preservesCapitals(){ cipher = new Bifid(true, false, false); assertTrue(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); assertNotNull(cipher.polybiusSquare); assertFalse(cipher.polybiusSquare.preserveWhitespace); assertFalse(cipher.polybiusSquare.preserveSymbols); assertEquals("", cipher.inputString); assertEquals("", cipher.keyword); assertEquals("", cipher.outputString); } @Test public void testConstructor_preservesWhitespace(){ cipher = new Bifid(false, true, false); assertFalse(cipher.preserveCapitals); assertTrue(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); assertNotNull(cipher.polybiusSquare); assertFalse(cipher.polybiusSquare.preserveWhitespace); assertFalse(cipher.polybiusSquare.preserveSymbols); assertEquals("", cipher.inputString); assertEquals("", cipher.keyword); assertEquals("", cipher.outputString); } @Test public void testConstructor_preservesSymbols(){ cipher = new Bifid(false, false, true); assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertTrue(cipher.preserveSymbols); assertNotNull(cipher.polybiusSquare); assertFalse(cipher.polybiusSquare.preserveWhitespace); assertFalse(cipher.polybiusSquare.preserveSymbols); assertEquals("", cipher.inputString); assertEquals("", cipher.keyword); assertEquals("", cipher.outputString); } @Test public void testSetKeyword(){ cipher.setKeyword(KEYWORD); assertEquals(KEYWORD, cipher.keyword); verify(logger, times(1)).debug("Setting keyword '{}'", KEYWORD); } @Test public void testSetKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword(null); }); verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString()); } @Test public void testSetInputString(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.setInputString(INPUT_STRING); assertEquals(INPUT_STRING, cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING); } @Test public void testSetInputString_noCapitals(){ cipher.preserveCapitals = false; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.setInputString(INPUT_STRING); assertEquals(INPUT_STRING.toUpperCase(), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING); verify(logger, times(1)).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.toUpperCase()); } @Test public void testSetInputString_noWhitespace(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = false; cipher.preserveSymbols = true; cipher.setInputString(INPUT_STRING); assertEquals(INPUT_STRING.replaceAll("\\s", ""), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING); verify(logger, never()).debug("Removing case"); verify(logger, times(1)).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("\\s", "")); } @Test public void testSetInputString_noSymbols(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = false; cipher.setInputString(INPUT_STRING); assertEquals(INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("[^a-zA-Z\\s]", "")); } @Test public void testSetInputString_blank(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; assertThrows(InvalidInputException.class, () -> { cipher.setInputString(""); }); assertEquals("", cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", ""); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Cleaned input string '{}'", ""); } @Test public void testSetInputString_null(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; assertThrows(InvalidInputException.class, () -> { cipher.setInputString(null); }); assertEquals("", cipher.inputString); verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); } @Test public void testFormatOutput(){ cipher.inputString = INPUT_STRING; cipher.formatOutput(OUTPUT_STRING_CLEAN); assertEquals(OUTPUT_STRING, cipher.outputString); verify(logger, times(1)).debug("Formatting output"); verify(logger, times(17)).debug(eq("Current character {}"), anyChar()); verify(logger, times(1)).debug("Altering uppercase"); verify(logger, times(14)).debug("Altering lowercase"); verify(logger, times(2)).debug("Adding symbol"); verify(logger, times(1)).debug("Formatted output string '{}'", OUTPUT_STRING); } @Test public void testEncode(){ cipher.inputString = INPUT_STRING; cipher.keyword = KEYWORD; cipher.encode(); assertEquals(INPUT_STRING, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(OUTPUT_STRING, cipher.outputString); verify(logger, times(1)).debug("Encoding"); verify(logger, times(1)).debug("Encoding Polybius"); verify(logger, times(1)).debug("Splitting Polybius Square message"); verify(logger, times(30)).debug(eq("Current character '{}'"), anyChar()); verify(logger, times(1)).debug("Decoding Polybius Square"); } @Test public void testDecode(){ cipher.inputString = OUTPUT_STRING; cipher.keyword = KEYWORD; cipher.decode(); assertEquals(OUTPUT_STRING, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(INPUT_STRING, cipher.outputString); verify(logger, times(1)).debug("Decoding"); verify(logger, times(1)).debug("Encoding Polybius Square"); verify(logger, times(1)).debug("Splitting Polybius Square message"); verify(logger, times(15)).debug(eq("Current characters {} {}"), anyChar(), anyChar()); verify(logger, times(1)).debug("Decoding Polybius Square"); } @Test public void testGetters(){ cipher.inputString = INPUT_STRING; cipher.keyword = KEYWORD; cipher.outputString = OUTPUT_STRING; assertEquals(INPUT_STRING, cipher.getInputString()); assertEquals(KEYWORD, cipher.getKeyword()); assertEquals(OUTPUT_STRING, cipher.getOutputString()); } @Test public void testReset(){ cipher.inputString = INPUT_STRING; cipher.keyword = KEYWORD; cipher.outputString = OUTPUT_STRING; cipher.reset(); assertEquals("", cipher.inputString); assertEquals("", cipher.keyword); assertEquals("", cipher.outputString); verify(logger, times(1)).debug("Resetting fields"); } @Test public void testPracticalEncoding(){ cipher = new Bifid(true, true, true); String output = cipher.encode(KEYWORD, INPUT_STRING); assertEquals(INPUT_STRING, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(OUTPUT_STRING, cipher.outputString); assertEquals(output, cipher.outputString); } @Test public void testPracticalEncoding_clean(){ cipher = new Bifid(false, false, false); String output = cipher.encode(KEYWORD, INPUT_STRING); assertEquals(INPUT_STRING_CLEAN, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(OUTPUT_STRING_CLEAN, cipher.outputString); assertEquals(OUTPUT_STRING_CLEAN, output); } @Test public void testPracticalDecoding(){ cipher = new Bifid(true, true, true); String output = cipher.decode(KEYWORD, OUTPUT_STRING); assertEquals(OUTPUT_STRING, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(INPUT_STRING, cipher.outputString); assertEquals(INPUT_STRING, output); } @Test public void testPracticalDecoding_clean(){ cipher = new Bifid(false, false, false); String output = cipher.decode(KEYWORD, OUTPUT_STRING); assertEquals(OUTPUT_STRING_CLEAN, cipher.inputString); assertEquals(KEYWORD_CLEAN, cipher.keyword); assertEquals(INPUT_STRING_CLEAN, cipher.outputString); assertEquals(INPUT_STRING_CLEAN, output); } }