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.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.utils.CipherInfoUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; @Tag("unit-test") @ExtendWith(MockitoExtension.class) public class OneTimePadCipherControllerTest{ @InjectMocks private OneTimePadCipherController oneTimePadCipherController; //Fields private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String ONE_TIME_PAD_KEYWORD = "keywordThatIsTotallyRandom"; private static final String ONE_TIME_PAD_INPUT_STRING = "Message to^encode"; private static final String ONE_TIME_PAD_OUTPUT_STRING = "Wiqooxh mv^egkgws"; private static final String oneTimePadName = "oneTimePadName"; private static final String oneTimePadDescription = "oneTimePadDescription"; private static final List oneTimePadExplanation = List.of("oneTimePadExplanation1", "oneTimePadExplanation2", "oneTimePadExplanation3"); private static final List oneTimePadFacts = List.of("oneTimePadFact1", "oneTimePadFact2", "oneTimePadFact3"); @BeforeEach public void setup(){ ReflectionTestUtils.setField(oneTimePadCipherController, "oneTimePadName", oneTimePadName); ReflectionTestUtils.setField(oneTimePadCipherController, "oneTimePadDescription", oneTimePadDescription); ReflectionTestUtils.setField(oneTimePadCipherController, "oneTimePadExplanation", oneTimePadExplanation); ReflectionTestUtils.setField(oneTimePadCipherController, "oneTimePadFacts", oneTimePadFacts); } @Test public void tetGetCipherInfo(){ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(oneTimePadName, oneTimePadDescription, oneTimePadExplanation, oneTimePadFacts); ObjectNode returnedJson = oneTimePadCipherController.getCipherInfo(); assertEquals(infoNode, returnedJson); } @Test public void testEncodeOneTimePad(){ ObjectNode cipherParams = generateParams(ONE_TIME_PAD_KEYWORD, ONE_TIME_PAD_INPUT_STRING); ObjectNode returnedJson = oneTimePadCipherController.encodeOneTimePad(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(ONE_TIME_PAD_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testEncodeOneTimePad_invalidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { oneTimePadCipherController.encodeOneTimePad(blankNode); }); } @Test public void testDecodeOneTimePad(){ ObjectNode cipherParams = generateParams(ONE_TIME_PAD_KEYWORD, ONE_TIME_PAD_OUTPUT_STRING); ObjectNode returnedJson = oneTimePadCipherController.decodeOneTimePad(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(ONE_TIME_PAD_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testDecodeOneTimePad_invalidParameter(){ assertThrows(InvalidCipherParameterException.class, () -> { oneTimePadCipherController.decodeOneTimePad(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; } }