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.CipherInfoUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; @Tag("unit-test") @ExtendWith(MockitoExtension.class) public class RailFenceControllerTest{ @InjectMocks private RailFenceController railFenceController; //Fields private static final ObjectMapper mapper = new ObjectMapper(); private static final String RAIL_FENCE_INPUT_STRING = "Message to^encode"; private static final String RAIL_FENCE_OUTPUT_STRING = "Moetese ne^sgcdao"; private static final int RAIL_FENCE_RAILS = 5; private static final ObjectNode blankNode = mapper.createObjectNode(); @Test public void tetGetCipherInfo(){ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.RAIL_FENCE_CIPHER_NAME, CipherInfoUtil.RAIL_FENCE_CIPHER_DESCRIPTION); ObjectNode returnedJson = railFenceController.getCipherInfo(); assertEquals(infoNode, returnedJson); } @Test public void testEncodeRailFence(){ ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING); ObjectNode returnedJson = railFenceController.encodeRailFence(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testEncodeRailFence_invalidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { railFenceController.encodeRailFence(blankNode); }); } @Test public void testDecodeRailFence(){ ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_OUTPUT_STRING); ObjectNode returnedJson = railFenceController.decodeRailFence(cipherParams); assertEquals(cipherParams, returnedJson); assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); } @Test public void testDecodeRailFence_invalidParameters(){ assertThrows(InvalidCipherParameterException.class, () -> { railFenceController.encodeRailFence(blankNode); }); } private ObjectNode generateParams(int rails, 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.RAIL_FENCE_RAILS, rails); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); return cipherParams; } }