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,91 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = AffineCipherController.class)
public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/affine";
private String decodedString = "Message to^encode";
private String encodedString = "Pbtthlb yz^burzwb";
private int key1 = 5;
private int key2 = 7;
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1);
decodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1);
encodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeAffine() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(affineLogger, times(1)).info("Encoding Affine");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeAffine() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(affineLogger, times(1)).info("Decoding Affine");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,76 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 AffineCipherControllerTest{
@InjectMocks
private AffineCipherController affineCipherController;
private static final String AFFINE_INPUT_STRING = "Message to^encode";
private static final String AFFINE_OUTPUT_STRING = "Pbtthlb yz^burzwb";
private static final int AFFINE_KEY_1 = 5;
private static final int AFFINE_KEY_2 = 7;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING);
ObjectNode returnedJson = affineCipherController.encodeAffine(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.encodeAffine(blankNode);
});
}
@Test
public void testDecodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_OUTPUT_STRING);
ObjectNode returnedJson = affineCipherController.decodeAffine(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.decodeAffine(blankNode);
});
}
private ObjectNode generateParams(int key1, int key2, 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.AFFINE_KEY_1, key1);
cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,86 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = AtbashCipherController.class)
public class AtbashCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/atbash";
private String decodedString = "Message to^encode";
private String encodedString = "Nvhhztv gl^vmxlwv";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeAtbash() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(atbashLogger, times(1)).info("Encoding Atbash");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeAtbash() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(atbashLogger, times(1)).info("Decoding Atbash");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,68 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 AtbashCipherControllerTest{
@InjectMocks
private AtbashCipherController atbashCipherController;
private static final String ATBASH_INPUT_STRING = "Message to^encode";
private static final String ATBASH_OUTPUT_STRING = "Nvhhztv gl^vmxlwv";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeAtbash(){
ObjectNode cipherParams = generateParams(ATBASH_INPUT_STRING);
ObjectNode returnedJson = atbashCipherController.encodeAtbash(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.encodeAtbash(blankNode);
});
}
@Test
public void testDecodeAtbash(){
ObjectNode cipherParams = generateParams(ATBASH_OUTPUT_STRING);
ObjectNode returnedJson = atbashCipherController.decodeAtbash(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.decodeAtbash(blankNode);
});
}
private ObjectNode generateParams(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.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = AutokeyCipherController.class)
public class AutokeyCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/autokey";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh fs^wfcuhx";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeAutokey() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(autokeyLogger, times(1)).info("Encoding Autokey");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeAutokey() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(autokeyLogger, times(1)).info("Decoding Autokey");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 AutokeyCipherControllerTest{
@InjectMocks
private AutokeyCipherController autokeyCipherController;
private static final String AUTOKEY_INPUT_STRING = "Message to^encode";
private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx";
private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING);
ObjectNode returnedJson = autokeyCipherController.encodeAutokey(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.encodeAutokey(blankNode);
});
}
@Test
public void testDecodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_OUTPUT_STRING);
ObjectNode returnedJson = autokeyCipherController.decodeAutokey(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.decodeAutokey(blankNode);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,85 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = BaconianCipherController.class)
public class BaconianCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/baconian";
private String decodedString = "Message to^encode";
private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.replaceAll("[^a-zA-Z]", ""));
}
@Test
public void testEncodeBaconian() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baconianLogger, times(1)).info("Encoding Baconian");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeBaconian() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.replaceAll("[^a-zA-Z]", "")));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baconianLogger, times(1)).info("Decoding Baconian");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,71 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 BaconianCipherControllerTest{
@InjectMocks
private BaconianCipherController baconianCipherController;
private static final String BACONIAN_INPUT_STRING = "Message to-encode";
private static final String BACONIAN_OUTPUT_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
private static final String BACONIAN_DECODED_STRING = "Messagetoencode";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeBaconian(){
ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING);
ObjectNode returnedJson = baconianCipherController.encodeBaconian(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.encodeBaconian(blankNode);
});
}
@Test
public void testDecodeBaconian(){
ObjectNode cipherParams = generateParams(BACONIAN_OUTPUT_STRING);
ObjectNode returnedJson = baconianCipherController.decodeBaconian(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.decodeBaconian(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = BaseXCipherController.class)
public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/basex";
private String decodedString = "A+B@C d\te\nf";
private String encodedString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
private int base = 2;
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.BASE_X_BASE, base);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.BASE_X_BASE, base);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeBaseXEncode() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baseXLogger, times(1)).info("Encoding BaseX");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeBaseXDecode() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baseXLogger, times(1)).info("Decoding BaseX");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,71 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 BaseXCipherControllerTest{
@InjectMocks
private BaseXCipherController baseXCipherController;
private static final int BASE_X_BASE = 2;
private static final String BASE_X_INPUT_STRING = "A+B@C d\te\nf";
private static final String BASE_X_OUTPUT_STRING = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeBaseX(){
ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_INPUT_STRING);
ObjectNode returnedJson = baseXCipherController.encodeBaseX(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.encodeBaseX(blankNode);
});
}
@Test
public void testDecodeBaseX(){
ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_OUTPUT_STRING);
ObjectNode returnedJson = baseXCipherController.decodeBaseX(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.decodeBaseX(blankNode);
});
}
private ObjectNode generateParams(int base, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
cipherParams.put(CipherParameterUtil.BASE_X_BASE, base);
return cipherParams;
}
}

View File

@@ -0,0 +1,88 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = BeaufortCipherController.class)
public class BeaufortCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/beaufort";
private String decodedString = "Message to^encode";
private String encodedString = "Yageolz rq^ujmdag";
private String keyword = "Ke*y word";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(beaufortLogger, times(1)).info("Encoding Beaufort");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(beaufortLogger, times(1)).info("Decoding Beaufort");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 BeaufortCipherControllerTest{
@InjectMocks
private BeaufortCipherController beaufortCipherController;
private static final String BEAUFORT_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String BEAUFORT_INPUT_STRING = "Message to^encode";
private static final String BEAUFORT_OUTPUT_STRING = "Yageolz rq^ujmdag";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void encodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_INPUT_STRING);
ObjectNode returnedJson = beaufortCipherController.encodeBeaufort(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.encodeBeaufort(blankNode);
});
}
@Test
public void decodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_OUTPUT_STRING);
ObjectNode returnedJson = beaufortCipherController.decodeBeaufort(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.decodeBeaufort(blankNode);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = CaesarCipherController.class)
public class CaesarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/caesar";
private String decodedString = "The quick brown fox jumps over - the lazy dog";
private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
private int shiftAmount = 23;
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeCaesar() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(caesarLogger, times(1)).info("Encoding Caesar");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeCaesar() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(caesarLogger, times(1)).info("Decoding Caesar");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 CaesarCipherControllerTest{
@InjectMocks
private CaesarCipherController caesarCipherController;
private static final String CAESAR_INPUT_STRING = "The quick brown fox jumps over - the lazy dog";
private static final String CAESAR_OUTPUT_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
private static final int CAESAR_SHIFT_AMOUNT = 23;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeCaesar(){
ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_INPUT_STRING);
ObjectNode returnedJson = caesarCipherController.encodeCaesar(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.encodeCaesar(blankParams);
});
}
@Test
public void testDecodeCaesar(){
ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_OUTPUT_STRING);
ObjectNode returnedJson = caesarCipherController.decodeCaesar(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.decodeCaesar(blankParams);
});
}
private ObjectNode generateParams(int shiftAmount, 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.CAESAR_SHIFT_AMOUNT, shiftAmount);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = OneTimePadCipherController.class)
public class OneTimePadCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/oneTimePad";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh mv^egkgws";
private String keyword = "keywordThatIsTotallyRandom";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(oneTimePadLogger, times(1)).info("Encoding One Time Pad");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(oneTimePadLogger, times(1)).info("Decoding One Time Pad");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,73 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 OneTimePadCipherControllerTest{
@InjectMocks
private OneTimePadCipherController oneTimePadCipherController;
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 ObjectMapper objectMapper = new ObjectMapper();
@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());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
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());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
oneTimePadCipherController.decodeOneTimePad(blankNode);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,98 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = PortaCipherController.class)
public class PortaCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/porta";
private String decodedString = "Message to^encode";
private String encodedString = "Rtghuos bm^qcwgrw";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodePorta() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString());
verify(mdc, times(1)).put("requestId", requestId);
verify(mdc, times(1)).put("ip", "192.168.1.1");
verify(mdc, times(1)).put("url", url + "/encode");
verify(mdc, times(1)).clear();
//Controller
verify(portaLogger, times(1)).info("Encoding Porta");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodePorta() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString());
verify(mdc, times(1)).put("requestId", requestId);
verify(mdc, times(1)).put("ip", "192.168.1.1");
verify(mdc, times(1)).put("url", url + "/decode");
verify(mdc, times(1)).clear();
//Controller
verify(portaLogger, times(1)).info("Decoding Porta");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 PortaCipherControllerTest{
@InjectMocks
private PortaCipherController portaCipherController;
private static final String PORTA_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String PORTA_INPUT_STRING = "Message to^encode";
private static final String PORTA_OUTPUT_STRING = "Rtghuos bm^qcwgrw";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodePorta(){
ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_INPUT_STRING);
ObjectNode returnedJson = portaCipherController.encodePorta(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.encodePorta(blankParams);
});
}
@Test
public void testDecodePorta(){
ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_OUTPUT_STRING);
ObjectNode returnedJson = portaCipherController.decodePorta(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.decodePorta(blankParams);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = SubstitutionCipherController.class)
public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/substitution";
private String decodedString = "Message to^encode";
private String encodedString = "Oguucig vq^gpeqfg";
private String keyword = "cdefghijklmnopqrstuvwxyzab";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(substitutionLogger, times(1)).info("Encoding Substitution");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(substitutionLogger, times(1)).info("Decoding Substitution");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 SubstitutionCipherControllerTest{
@InjectMocks
private SubstitutionCipherController substitutionCipherController;
private static final String SUBSTITUTION_KEYWORD = "cdefghijklmnopqrstuvwxyzab9876543210";
private static final String SUBSTITUTION_INPUT_STRING = "Message to&encode 123";
private static final String SUBSTITUTION_OUTPUT_STRING = "Oguucig vq&gpeqfg 876";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeSubstitution(){
ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_INPUT_STRING);
ObjectNode returnedJson = substitutionCipherController.encodeSubstitution(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.encodeSubstitution(blankParams);
});
}
@Test
public void testDecodeSubstitution(){
ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_OUTPUT_STRING);
ObjectNode returnedJson = substitutionCipherController.decodeSubstitution(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.decodeSubstitution(blankParams);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = VigenereCipherController.class)
public class VigenereCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/vigenere";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh ds^cjqfgo";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeVigenere() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(vigenereLogger, times(1)).info("Encoding Vigenere");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeVigenere() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(vigenereLogger, times(1)).info("Decoding Vigenere");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.monosubstitution;
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 VigenereCipherControllerTest{
@InjectMocks
private VigenereCipherController vigenereCipherController;
private static final String VIGENERE_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String VIGENERE_INPUT_STRING = "Message to^encode";
private static final String VIGENERE_OUTPUT_STRING = "Wiqooxh ds^cjqfgo";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeVigenere(){
ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_INPUT_STRING);
ObjectNode returnedJson = vigenereCipherController.encodeVigenere(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.encodeVigenere(blankParams);
});
}
@Test
public void testDecodeVigenere(){
ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_OUTPUT_STRING);
ObjectNode returnedJson = vigenereCipherController.decodeVigenere(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.decodeVigenere(blankParams);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}