package com.mattrixwv.cipherstream.monosubstitution; 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; @ExtendWith(MockitoExtension.class) public class CaesarTest{ @InjectMocks private Caesar cipher; @Mock private Logger logger; //Variables private static final String DECODED_STRING = "The quick brown fox jumps over - the lAzy dog"; private static final String DECODED_STRING_CLEAN = "thequickbrownfoxjumpsoverthelazydog"; private static final String ENCODED_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald"; private static final String ENCODED_STRING_CLEAN = "qebnrfzhyoltkclugrjmplsboqebixwvald"; private static final int SHIFT = 23; @Test public void testConstructor_default(){ cipher = new Caesar(); assertEquals("", cipher.inputString); assertEquals("", cipher.outputString); assertEquals(0, cipher.shift); assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); } @Test public void testConstructor_preservesCapitals(){ cipher = new Caesar(true, false, false); assertEquals("", cipher.inputString); assertEquals("", cipher.outputString); assertEquals(0, cipher.shift); assertTrue(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); } @Test public void testConstructor_preservesSymbols(){ cipher = new Caesar(false, true, false); assertEquals("", cipher.inputString); assertEquals("", cipher.outputString); assertEquals(0, cipher.shift); assertFalse(cipher.preserveCapitals); assertTrue(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); } @Test public void testConstructor_preservesWhitespace(){ cipher = new Caesar(false, false, true); assertEquals("", cipher.inputString); assertEquals("", cipher.outputString); assertEquals(0, cipher.shift); assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertTrue(cipher.preserveSymbols); } @Test public void testSetShift(){ cipher.setShift(SHIFT); assertEquals(SHIFT, cipher.shift); verify(logger, times(1)).debug("Setting shift {}", SHIFT); verify(logger, times(1)).debug("Cleaned shift {}", SHIFT); } @Test public void testSetShift_large(){ cipher.setShift(SHIFT + 26); assertEquals(SHIFT, cipher.shift); verify(logger, times(1)).debug("Setting shift {}", SHIFT + 26); verify(logger, times(1)).debug("Cleaned shift {}", SHIFT); } @Test public void testSetShift_negative(){ cipher.setShift(SHIFT - 26); assertEquals(SHIFT, cipher.shift); verify(logger, times(1)).debug("Setting shift {}", SHIFT - 26); verify(logger, times(1)).debug("Cleaned shift {}", SHIFT); } @Test public void testSetInputString(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.setInputString(DECODED_STRING); assertEquals(DECODED_STRING, cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", DECODED_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 '{}'", DECODED_STRING); } @Test public void testSetInputString_noCapitals(){ cipher.preserveCapitals = false; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.setInputString(DECODED_STRING); assertEquals(DECODED_STRING.toLowerCase(), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", DECODED_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 '{}'", DECODED_STRING.toLowerCase()); } @Test public void testSetInputString_noWhitespace(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = false; cipher.preserveSymbols = true; cipher.setInputString(DECODED_STRING); assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", DECODED_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 '{}'", DECODED_STRING.replaceAll("\\s", "")); } @Test public void testSetInputString_noSymbols(){ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = false; cipher.setInputString(DECODED_STRING); assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); verify(logger, times(1)).debug("Original input string '{}'", DECODED_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 '{}'", DECODED_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("Original input string '{}'", ""); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Cleaned input string '{}'", ""); } @Test public void testEncode(){ cipher.inputString = DECODED_STRING; cipher.shift = SHIFT; cipher.encode(); assertEquals(ENCODED_STRING, cipher.outputString); verify(logger, times(1)).debug("Encoding"); verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); verify(logger, times(2)).debug("Encoding uppercase"); verify(logger, never()).debug("Wrapping arround to Z"); verify(logger, times(1)).debug("Wrapping around to A"); verify(logger, times(33)).debug("Encoding lowercase"); verify(logger, never()).debug("Wrapping around to z"); verify(logger, times(31)).debug("Wrapping around to a"); verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar()); verify(logger, times(1)).debug("Saving encoded string '{}'", ENCODED_STRING); } @Test public void testEncode_negative(){ cipher.inputString = DECODED_STRING; cipher.shift = SHIFT - 26; cipher.encode(); assertEquals(ENCODED_STRING, cipher.outputString); verify(logger, times(1)).debug("Encoding"); verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); verify(logger, times(2)).debug("Encoding uppercase"); verify(logger, times(1)).debug("Wrapping around to Z"); verify(logger, never()).debug("Wrapping around to A"); verify(logger, times(33)).debug("Encoding lowercase"); verify(logger, times(2)).debug("Wrapping around to z"); verify(logger, never()).debug("Wrapping around to a"); verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar()); verify(logger, times(1)).debug("Saving encoded string '{}'", ENCODED_STRING); } @Test public void testDecode(){ cipher.inputString = ENCODED_STRING; cipher.shift = SHIFT; cipher.decode(); assertEquals(DECODED_STRING, cipher.outputString); verify(logger, times(1)).debug("Decoding"); verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); verify(logger, times(2)).debug("Decoding uppercase"); verify(logger, times(1)).debug("Wrapping around to Z"); verify(logger, never()).debug("Wrapping around to A"); verify(logger, times(33)).debug("Decoding lowercase"); verify(logger, times(31)).debug("Wrapping around to z"); verify(logger, never()).debug("Wrapping around to a"); verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar()); verify(logger, times(1)).debug("Saving decoded string '{}'", DECODED_STRING); } @Test public void testDecode_negative(){ cipher.inputString = ENCODED_STRING; cipher.shift = SHIFT - 26; cipher.decode(); assertEquals(DECODED_STRING, cipher.outputString); verify(logger, times(1)).debug("Decoding"); verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); verify(logger, times(2)).debug("Decoding uppercase"); verify(logger, never()).debug("Wrapping around to Z"); verify(logger, times(1)).debug("Wrapping around to A"); verify(logger, times(33)).debug("Decoding lowercase"); verify(logger, never()).debug("Wrapping around to z"); verify(logger, times(2)).debug("Wrapping around to a"); verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar()); verify(logger, times(1)).debug("Saving decoded string '{}'", DECODED_STRING); } @Test public void testGetters(){ cipher.inputString = DECODED_STRING; cipher.outputString = ENCODED_STRING; cipher.shift = SHIFT; assertEquals(DECODED_STRING, cipher.getInputString()); assertEquals(ENCODED_STRING, cipher.getOutputString()); assertEquals(SHIFT, cipher.getShift()); } @Test public void testReset(){ cipher.inputString = DECODED_STRING; cipher.outputString = ENCODED_STRING; cipher.shift = SHIFT; cipher.reset(); assertEquals("", cipher.inputString); assertEquals("", cipher.outputString); assertEquals(0, cipher.shift); verify(logger, times(1)).debug("Resetting fields"); } @Test public void testPracticalEncoding(){ cipher = new Caesar(true, true, true); String output = cipher.encode(SHIFT, DECODED_STRING); assertEquals(DECODED_STRING, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(ENCODED_STRING, cipher.outputString); assertEquals(ENCODED_STRING, output); } @Test public void testPracticalEncoding_clean(){ cipher = new Caesar(false, false, false); String output = cipher.encode(SHIFT, DECODED_STRING); assertEquals(DECODED_STRING_CLEAN, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(ENCODED_STRING_CLEAN, cipher.outputString); assertEquals(ENCODED_STRING_CLEAN, output); } @Test public void testPracticalEncoding_negative(){ cipher = new Caesar(true, true, true); String output = cipher.encode(-3, DECODED_STRING); assertEquals(DECODED_STRING, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(ENCODED_STRING, cipher.outputString); assertEquals(ENCODED_STRING, output); } @Test public void testPracticalDecoding(){ cipher = new Caesar(true, true, true); String output = cipher.decode(SHIFT, ENCODED_STRING); assertEquals(ENCODED_STRING, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(DECODED_STRING, cipher.outputString); assertEquals(DECODED_STRING, output); } @Test public void testPracticalDecoding_clean(){ cipher = new Caesar(false, false, false); String output = cipher.decode(SHIFT, ENCODED_STRING); assertEquals(ENCODED_STRING_CLEAN, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(DECODED_STRING_CLEAN, cipher.outputString); assertEquals(DECODED_STRING_CLEAN, output); } @Test public void testPracticalDecoding_negative(){ cipher = new Caesar(true, true, true); String output = cipher.decode(SHIFT - 26, ENCODED_STRING); assertEquals(ENCODED_STRING, cipher.inputString); assertEquals(SHIFT, cipher.shift); assertEquals(DECODED_STRING, cipher.outputString); assertEquals(DECODED_STRING, output); } }