package com.mattrixwv.cipherstream.controller.combination; import static org.junit.jupiter.api.Assertions.*; 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 AdfgvxCipherControllerTest{ @InjectMocks private AdfgvxCipherController adfgvxCipherController; //Fields private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String KEYWORD = CipherParameterUtil.KEYWORD; private static final String SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD; private static final String INPUT_STRING = "Message to-encode"; private static final String OUTPUT_STRING = "AXgvdavfxgagfa afag-aaxdxfgdagda"; private static final String adfgvxName = "adfgvxName"; private static final String adfgvxDescription = "adfgvxDescription"; @BeforeEach public void setup(){ ReflectionTestUtils.setField(adfgvxCipherController, "adfgvxName", adfgvxName); ReflectionTestUtils.setField(adfgvxCipherController, "adfgvxDescription", adfgvxDescription); } @Test public void tetGetCipherInfo(){ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(adfgvxName, adfgvxDescription); ObjectNode returnedJson = adfgvxCipherController.getCipherInfo(); assertEquals(infoNode, returnedJson); } @Test public void testEncodeAdfgvx(){ ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, INPUT_STRING); ObjectNode returnedJson = adfgvxCipherController.encodeAdfgvx(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testEncodeAdfgvx_invalidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { adfgvxCipherController.encodeAdfgvx(blankNode); }); } @Test public void testDecodeAdfgvx(){ ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, OUTPUT_STRING); ObjectNode returnedJson = adfgvxCipherController.decodeAdfgvx(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testDecodeAdfgvx_invalidParameters(){ //Verify invalid params are caught assertThrows(InvalidCipherParameterException.class, () -> { adfgvxCipherController.decodeAdfgvx(blankNode); }); } private ObjectNode generateParams(String keyword, String squareKeyword, 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.SQUARE_KEYWORD, squareKeyword); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); return cipherParams; } }