Updated more tests

This commit is contained in:
Matthew Ellison
2024-04-08 16:44:38 -04:00
parent 1a93a30b42
commit ff11cd72c3
52 changed files with 1813 additions and 294 deletions

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,12 +26,16 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AffineCipherController")
private Logger affineLogger;
//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;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -60,7 +66,7 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AFFINE_CIPHER_DESCRIPTION))
@@ -78,7 +84,7 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeAffine() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -93,11 +99,30 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAffine_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(affineLogger, times(1)).info("Encoding {}", CipherInfoUtil.AFFINE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeAffine() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -111,4 +136,23 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAffine_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(affineLogger, times(1)).info("Decoding {}", CipherInfoUtil.AFFINE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,13 +21,26 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AffineCipherControllerTest{
@InjectMocks
private AffineCipherController affineCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.AFFINE_CIPHER_NAME, CipherInfoUtil.AFFINE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = affineCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING);
@@ -37,9 +51,9 @@ public class AffineCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
}
@Test
public void testEncodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.encodeAffine(blankNode);
});
@@ -55,22 +69,24 @@ public class AffineCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
}
@Test
public void testDecodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.decodeAffine(blankNode);
});
}
private ObjectNode generateParams(int key1, int key2, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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.AFFINE_KEY_1, key1);
cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,10 +26,14 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AtbashCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AtbashCipherController")
protected Logger atbashLogger;
//Fields
private String url = "/cipherStream/atbash";
private String decodedString = "Message to^encode";
private String encodedString = "Nvhhztv gl^vmxlwv";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -54,7 +60,7 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ATBASH_CIPHER_DESCRIPTION))
@@ -72,7 +78,7 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeAtbash() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -87,11 +93,30 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAtbash_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(atbashLogger, times(1)).info("Encoding {}", CipherInfoUtil.ATBASH_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeAtbash() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -105,4 +130,23 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAtbash_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(atbashLogger, times(1)).info("Decoding {}", CipherInfoUtil.ATBASH_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -20,9 +20,11 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AtbashCipherControllerTest{
@InjectMocks
private AtbashCipherController atbashCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
@@ -33,9 +35,9 @@ public class AtbashCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
}
@Test
public void testEncodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.encodeAtbash(blankNode);
});
@@ -49,20 +51,22 @@ public class AtbashCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
}
@Test
public void testDecodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.decodeAtbash(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,16 +26,19 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AutokeyCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AutokeyCipherController")
protected Logger autokeyLogger;
//Fields
private String url = "/cipherStream/autokey";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh fs^wfcuhx";
private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
@@ -58,7 +63,7 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION))
@@ -76,7 +81,7 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
public void testEncodeAutokey() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -91,11 +96,30 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAutokey_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(autokeyLogger, times(1)).info("Encoding {}", CipherInfoUtil.AUTOKEY_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeAutokey() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -109,4 +133,23 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAutokey_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(autokeyLogger, times(1)).info("Decoding {}", CipherInfoUtil.AUTOKEY_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AutokeyCipherControllerTest{
@InjectMocks
private AutokeyCipherController autokeyCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.AUTOKEY_CIPHER_NAME, CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION);
ObjectNode returnedJson = autokeyCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING);
@@ -36,9 +50,10 @@ public class AutokeyCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testEncodeAutokey_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.encodeAutokey(blankNode);
});
@@ -54,21 +69,24 @@ public class AutokeyCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testDecodeAutokey_invapidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.decodeAutokey(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,10 +26,14 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaconianCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaconianCipherController")
protected Logger baconianLogger;
//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";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -54,7 +60,7 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BACONIAN_CIPHER_DESCRIPTION))
@@ -72,7 +78,7 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeBaconian() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -87,11 +93,30 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeBaconian_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baconianLogger, times(1)).info("Encoding {}", CipherInfoUtil.BACONIAN_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeBaconian() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -105,4 +130,23 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeBaconian_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baconianLogger, times(1)).info("Decoding {}", CipherInfoUtil.BACONIAN_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaconianCipherControllerTest{
@InjectMocks
private BaconianCipherController baconianCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BACONIAN_CIPHER_NAME, CipherInfoUtil.BACONIAN_CIPHER_DESCRIPTION);
ObjectNode returnedJson = baconianCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeBaconian(){
ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING);
@@ -36,9 +50,10 @@ public class BaconianCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testEncodeBaconian_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.encodeBaconian(blankNode);
});
@@ -54,18 +69,21 @@ public class BaconianCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testDecodeBaconian_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.decodeBaconian(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaseXCipherController")
protected Logger baseXLogger;
//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;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BASE_X_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
public void testEncodeBaseXEncode() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeBaseX_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baseXLogger, times(1)).info("Encoding {}", CipherInfoUtil.BASE_X_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeBaseXDecode() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeBaseX_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baseXLogger, times(1)).info("Decoding {}", CipherInfoUtil.BASE_X_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaseXCipherControllerTest{
@InjectMocks
private BaseXCipherController baseXCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BASE_X_CIPHER_NAME, CipherInfoUtil.BASE_X_CIPHER_DESCRIPTION);
ObjectNode returnedJson = baseXCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeBaseX(){
ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_INPUT_STRING);
@@ -36,9 +50,10 @@ public class BaseXCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testEncodeBaseX_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.encodeBaseX(blankNode);
});
@@ -54,18 +69,21 @@ public class BaseXCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testDecodeBaseX_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.decodeBaseX(blankNode);
});
}
private ObjectNode generateParams(int base, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
cipherParams.put(CipherParameterUtil.BASE_X_BASE, base);
return cipherParams;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BeaufortCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BeaufortCipherController")
protected Logger beaufortLogger;
//Fields
private String url = "/cipherStream/beaufort";
private String decodedString = "Message to^encode";
private String encodedString = "Yageolz rq^ujmdag";
private String keyword = "Ke*y word";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BEAUFORT_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(beaufortLogger, times(1)).info("Encoding {}", CipherInfoUtil.BEAUFORT_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(beaufortLogger, times(1)).info("Decoding {}", CipherInfoUtil.BEAUFORT_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,14 +21,27 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BeaufortCipherControllerTest{
@InjectMocks
private BeaufortCipherController beaufortCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void encodeBeaufort(){
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BEAUFORT_CIPHER_NAME, CipherInfoUtil.BEAUFORT_CIPHER_DESCRIPTION);
ObjectNode returnedJson = beaufortCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_INPUT_STRING);
@@ -36,16 +50,17 @@ public class BeaufortCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testEncodeBeaufort_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.encodeBeaufort(blankNode);
});
}
@Test
public void decodeBeaufort(){
public void testDecodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_OUTPUT_STRING);
@@ -54,21 +69,24 @@ public class BeaufortCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
@Test
public void testDecodeBeaufort_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.decodeBeaufort(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class CaesarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.CaesarCipherController")
protected Logger caesarLogger;
//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;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeCaesar() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(caesarLogger, times(1)).info("Encoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeCaesar() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(caesarLogger, times(1)).info("Decoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class CaesarCipherControllerTest{
@InjectMocks
private CaesarCipherController caesarCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.CAESAR_CIPHER_NAME, CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION);
ObjectNode returnedJson = caesarCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeCaesar(){
ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_INPUT_STRING);
@@ -36,11 +50,12 @@ public class CaesarCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testEncodeCaesar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.encodeCaesar(blankParams);
caesarCipherController.encodeCaesar(blankNode);
});
}
@@ -54,21 +69,24 @@ public class CaesarCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testDecodeCaesar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.decodeCaesar(blankParams);
caesarCipherController.decodeCaesar(blankNode);
});
}
private ObjectNode generateParams(int shiftAmount, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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.CAESAR_SHIFT_AMOUNT, shiftAmount);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class OneTimePadCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.OneTimePadCipherController")
protected Logger oneTimePadLogger;
//Fields
private String url = "/cipherStream/oneTimePad";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh mv^egkgws";
private String keyword = "keywordThatIsTotallyRandom";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ONE_TIME_PAD_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
public void testEncodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(oneTimePadLogger, times(1)).info("Encoding {}", CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(oneTimePadLogger, times(1)).info("Decoding {}", CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,10 +21,24 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class OneTimePadCipherControllerTest{
@InjectMocks
private OneTimePadCipherController oneTimePadCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME, CipherInfoUtil.ONE_TIME_PAD_CIPHER_DESCRIPTION);
ObjectNode returnedJson = oneTimePadCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeOneTimePad(){
@@ -35,9 +50,10 @@ public class OneTimePadCipherControllerTest{
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();
@Test
public void testEncodeOneTimePad_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
oneTimePadCipherController.encodeOneTimePad(blankNode);
});
@@ -53,21 +69,24 @@ public class OneTimePadCipherControllerTest{
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();
@Test
public void testDecodeOneTimePad_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> {
oneTimePadCipherController.decodeOneTimePad(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}

View File

@@ -9,6 +9,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -25,11 +27,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PortaCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.PortaCipherController")
protected Logger portaLogger;
//Fields
private String url = "/cipherStream/porta";
private String decodedString = "Message to^encode";
private String encodedString = "Rtghuos bm^qcwgrw";
private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -58,7 +64,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.PORTA_CIPHER_DESCRIPTION))
@@ -76,7 +82,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
public void testEncodePorta() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -86,7 +92,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//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("ip", ipAddress);
verify(mdc, times(1)).put("url", url + "/encode");
verify(mdc, times(1)).clear();
//Controller
@@ -95,11 +101,30 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(portaLogger, times(1)).info("Encoding {}", CipherInfoUtil.PORTA_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodePorta() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -109,7 +134,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//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("ip", ipAddress);
verify(mdc, times(1)).put("url", url + "/decode");
verify(mdc, times(1)).clear();
//Controller
@@ -117,4 +142,23 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(portaLogger, times(1)).info("Decoding {}", CipherInfoUtil.PORTA_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PortaCipherControllerTest{
@InjectMocks
private PortaCipherController portaCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.PORTA_CIPHER_NAME, CipherInfoUtil.PORTA_CIPHER_DESCRIPTION);
ObjectNode returnedJson = portaCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodePorta(){
ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_INPUT_STRING);
@@ -36,11 +50,12 @@ public class PortaCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testEncodePorta_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.encodePorta(blankParams);
portaCipherController.encodePorta(blankNode);
});
}
@@ -54,21 +69,24 @@ public class PortaCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testDecodePorta_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.decodePorta(blankParams);
portaCipherController.decodePorta(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.SubstitutionCipherController")
protected Logger substitutionLogger;
//Fields
private String url = "/cipherStream/substitution";
private String decodedString = "Message to^encode";
private String encodedString = "Oguucig vq^gpeqfg";
private String keyword = "cdefghijklmnopqrstuvwxyzab";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.SUBSTITUTION_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
public void testEncodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(substitutionLogger, times(1)).info("Encoding {}", CipherInfoUtil.SUBSTITUTION_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(substitutionLogger, times(1)).info("Decoding {}", CipherInfoUtil.SUBSTITUTION_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class SubstitutionCipherControllerTest{
@InjectMocks
private SubstitutionCipherController substitutionCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.SUBSTITUTION_CIPHER_NAME, CipherInfoUtil.SUBSTITUTION_CIPHER_DESCRIPTION);
ObjectNode returnedJson = substitutionCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeSubstitution(){
ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_INPUT_STRING);
@@ -36,11 +50,12 @@ public class SubstitutionCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testEncodeSubstitution_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.encodeSubstitution(blankParams);
substitutionCipherController.encodeSubstitution(blankNode);
});
}
@@ -54,21 +69,24 @@ public class SubstitutionCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testDecodeSubstitution_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.decodeSubstitution(blankParams);
substitutionCipherController.decodeSubstitution(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}

View File

@@ -8,6 +8,8 @@ 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.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class VigenereCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.VigenereCipherController")
protected Logger vigenereLogger;
//Fields
private String url = "/cipherStream/vigenere";
private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh ds^cjqfgo";
private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach
@@ -57,7 +63,7 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1"))
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.VIGENERE_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeVigenere() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(vigenereLogger, times(1)).info("Encoding {}", CipherInfoUtil.VIGENERE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeVigenere() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(vigenereLogger, times(1)).info("Decoding {}", CipherInfoUtil.VIGENERE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class VigenereCipherControllerTest{
@InjectMocks
private VigenereCipherController vigenereCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
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();
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.VIGENERE_CIPHER_NAME, CipherInfoUtil.VIGENERE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = vigenereCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeVigenere(){
ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_INPUT_STRING);
@@ -36,11 +50,12 @@ public class VigenereCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testEncodeVigenere_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.encodeVigenere(blankParams);
vigenereCipherController.encodeVigenere(blankNode);
});
}
@@ -54,21 +69,24 @@ public class VigenereCipherControllerTest{
assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught
final ObjectNode blankParams = objectMapper.createObjectNode();
@Test
public void testDecodeVigenere_invalidparameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.decodeVigenere(blankParams);
vigenereCipherController.decodeVigenere(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
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;
}
}