package com.mattrixwv.cipherstream.controller.polysubstitution; 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.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; import org.springframework.test.web.servlet.MockMvc; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.utils.CipherInfoUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; @Tag("integration-test") @WebMvcTest(controllers = TrifidCipherController.class) public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ @Autowired private MockMvc mockMvc; //Loggers @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.TrifidCipherController") protected Logger trifidLogger; //Fields private String url = "/cipherStream/trifid"; private String decodedString = "Message to^encode+"; private String encodedString = "Gqdokpd od^ljvflf+"; private String keyword = "keyword"; private String trifidFill = "="; private int grouplength = Integer.MAX_VALUE; private static final ObjectNode blankNode = mapper.createObjectNode(); @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.TRIFID_FILL, trifidFill); decodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength); 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.TRIFID_FILL, trifidFill); encodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength); encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); } @Test public void testGetCipherInfo() throws Exception{ ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.TRIFID_CIPHER_NAME, CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION); mockMvc.perform(get(url) .header("X-Request-Id", requestId) .header("X-Forwarded-For", ipAddress)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.TRIFID_CIPHER_NAME)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION)); //Filter super.verifyFilter(url); //Controller verify(trifidLogger, times(1)).info("Getting info for {}", CipherInfoUtil.TRIFID_CIPHER_NAME); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME); //Cipher Aspect verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode); } @Test public void testEncodeTrifid() throws Exception{ mockMvc.perform(get(url + "/encode") .header("X-Request-Id", requestId) .header("X-Forwarded-For", ipAddress) .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(trifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME); //Cipher Aspect 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(trifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME); //Cipher Aspect verifyNoInteractions(aspectLogger); } @Test public void testDecodeTrifid() throws Exception{ mockMvc.perform(get(url + "/decode") .header("X-Request-Id", requestId) .header("X-Forwarded-For", ipAddress) .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(trifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME); //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(trifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME); //Cipher Aspect verifyNoInteractions(aspectLogger); } }