Initial commit

This commit is contained in:
2024-04-07 19:41:57 -04:00
parent 2b3212cb44
commit 8f0d1c64bd
95 changed files with 6787 additions and 45 deletions

View File

@@ -0,0 +1,74 @@
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.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class RailFenceControllerTest{
@InjectMocks
private RailFenceController railFenceController;
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 ObjectMapper objectMapper = new ObjectMapper();
@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());
//Verify inavlid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
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());
//Verify inavlid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
railFenceController.encodeRailFence(blankNode);
});
}
private ObjectNode generateParams(int rails, String inputString){
ObjectNode cipherParams = objectMapper.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;
}
}