package com.mattrixwv.cipherstream.controller.polysubstitution; import static org.hamcrest.Matchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.List; 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.webmvc.test.autoconfigure.WebMvcTest; import org.springframework.http.MediaType; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.utils.CipherInfoUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import tools.jackson.databind.node.ObjectNode; @Tag("integration-test") @WebMvcTest(controllers = BifidCipherController.class) public class BifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ @Autowired private MockMvc mockMvc; @Autowired private BifidCipherController bifidCipherController; //Loggers //TODO: Fix logger testing //@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.BifidCipherController") //protected Logger bifidLogger; //Fields private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String url = "/bifid"; private static final String decodedString = "Message to^encode"; private static final String encodedString = "Mqaokne kc^vdodzd"; private static final String keyword = "keyword"; private static final String bifidName = "bifidName"; private static final String bifidDescription = "bifidDescription"; private static final List bifidExplanation = List.of("bifidExplanation1", "bifidExplanation2", "bifidExplanation3"); private static final List bifidFacts = List.of("bifidFact1", "bifidFact2", "bifidFact3"); @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); ReflectionTestUtils.setField(bifidCipherController, "bifidName", bifidName); ReflectionTestUtils.setField(bifidCipherController, "bifidDescription", bifidDescription); ReflectionTestUtils.setField(bifidCipherController, "bifidExplanation", bifidExplanation); ReflectionTestUtils.setField(bifidCipherController, "bifidFacts", bifidFacts); } @Test public void testGetCipherInfo() throws Exception{ 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(bifidName)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(bifidDescription)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(bifidExplanation.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(bifidExplanation.get(0), bifidExplanation.get(1), bifidExplanation.get(2)))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(bifidFacts.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(bifidFacts.get(0), bifidFacts.get(1), bifidFacts.get(2)))); //Filter //TODO: Fix logger testing /* super.verifyFilter(url); //Controller verify(bifidLogger, times(1)).info("Getting info for {}", bifidName); verifyNoMoreInteractions(bifidLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, bifidName); */ } @Test public void testEncodeBifid() throws Exception{ mockMvc.perform(post(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 //TODO: Fix logger testing /* super.verifyFilter(url + "/encode"); //Controller verify(bifidLogger, times(1)).info("Encoding {}", bifidName); verifyNoMoreInteractions(bifidLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, bifidName); //Cipher Aspect verifyAspectLogging(decodedNode); */ } @Test public void testEncodebifid_error() throws Exception{ mockMvc.perform(post(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 //TODO: Fix logger testing /* super.verifyFilter(url + "/encode"); //Controller verify(bifidLogger, times(1)).info("Encoding {}", bifidName); verifyNoMoreInteractions(bifidLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, bifidName); //Cipher Aspect verifyNoInteractions(aspectLogger); */ } @Test public void testDecodeBifid() throws Exception{ mockMvc.perform(post(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 //TODO: Fix logger testing /* super.verifyFilter(url + "/decode"); //Controller verify(bifidLogger, times(1)).info("Decoding {}", bifidName); verifyNoMoreInteractions(bifidLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, bifidName); //Cipher Aspect verifyAspectLogging(encodedNode); */ } @Test public void testDecodebifid_error() throws Exception{ mockMvc.perform(post(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 //TODO: Fix logger testing /* super.verifyFilter(url + "/decode"); //Controller verify(bifidLogger, times(1)).info("Decoding {}", bifidName); verifyNoMoreInteractions(bifidLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, bifidName); //Cipher Aspect verifyNoInteractions(aspectLogger); */ } }