package com.mattrixwv.cipherstream.controller.polysubstitution; import static org.hamcrest.Matchers.*; import static org.mockito.Mockito.*; 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.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.util.ReflectionTestUtils; 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 = PlayfairCipherController.class) public class PlayfairCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ @Autowired private MockMvc mockMvc; @Autowired private PlayfairCipherController playfairCipherController; //Loggers @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PlayfairCipherController") protected Logger playfairLogger; //Fields private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String url = "/playfair"; private static final String decodedString = "Hide the gold in - the@tree+stump"; private static final String decodedStringPadded = "Hide the gold in - the@trexe+stump"; private static final String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; private static final String keyword = "Play-fair@Exam ple"; private static final String playfairName = "playfairName"; private static final String playfairDescription = "playfairDescription"; private static final List playfairExplanation = List.of("playfairExplanation1", "playfairExplanation2", "playfairExplanation3"); private static final List playfairFacts = List.of("playfairFact1", "playfairFact2", "playfairFact3"); @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, decodedStringPadded); ReflectionTestUtils.setField(playfairCipherController, "playfairName", playfairName); ReflectionTestUtils.setField(playfairCipherController, "playfairDescription", playfairDescription); ReflectionTestUtils.setField(playfairCipherController, "playfairExplanation", playfairExplanation); ReflectionTestUtils.setField(playfairCipherController, "playfairFacts", playfairFacts); } @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(playfairName)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(playfairDescription)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(playfairExplanation.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(playfairExplanation.get(0), playfairExplanation.get(1), playfairExplanation.get(2)))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(playfairFacts.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(playfairFacts.get(0), playfairFacts.get(1), playfairFacts.get(2)))); //Filter super.verifyFilter(url); //Controller verify(playfairLogger, times(1)).info("Getting info for {}", playfairName); verifyNoMoreInteractions(playfairLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, playfairName); } @Test public void testEncodePlayfair() 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 super.verifyFilter(url + "/encode"); //Controller verify(playfairLogger, times(1)).info("Encoding {}", playfairName); verifyNoMoreInteractions(playfairLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, playfairName); //Cipher Aspect verifyAspectLogging(decodedNode); } @Test public void testEncodeplayfair_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 super.verifyFilter(url + "/encode"); //Controller verify(playfairLogger, times(1)).info("Encoding {}", playfairName); verifyNoMoreInteractions(playfairLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, playfairName); //Cipher Aspect verifyNoInteractions(aspectLogger); } @Test public void testDecodePlayfair() 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(decodedStringPadded)); //Filter super.verifyFilter(url + "/decode"); //Controller verify(playfairLogger, times(1)).info("Decoding {}", playfairName); verifyNoMoreInteractions(playfairLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, playfairName); //Cipher Aspect verifyAspectLogging(encodedNode); } @Test public void testDecodeplayfair_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 super.verifyFilter(url + "/decode"); //Controller verify(playfairLogger, times(1)).info("Decoding {}", playfairName); verifyNoMoreInteractions(playfairLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, playfairName); //Cipher Aspect verifyNoInteractions(aspectLogger); } }