package com.mattrixwv.cipherstream.controller.monosubstitution; 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 = AffineCipherController.class) public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ @Autowired private MockMvc mockMvc; @Autowired private AffineCipherController affineCipherController; //Loggers @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AffineCipherController") private Logger affineLogger; //Fields private static final ObjectNode blankNode = mapper.createObjectNode(); private static final String url = "/affine"; private static final String decodedString = "Message to^encode"; private static final String encodedString = "Pbtthlb yz^burzwb"; private static final int key1 = 5; private static final int key2 = 7; private static final String affineName = "affineCipherName"; private static final String affineDescription = "affineCipherDescription"; private static final List affineExplanation = List.of("affineExplanation1", "affineExplanation2", "affineExplanation3"); private static final List affineFacts = List.of("affineFact1", "affineFact2", "affineFact3"); @BeforeEach public void setup(){ decodedNode = mapper.createObjectNode(); decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); decodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1); decodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2); decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); encodedNode = mapper.createObjectNode(); encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); encodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1); encodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2); encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); ReflectionTestUtils.setField(affineCipherController, "affineName", affineName); ReflectionTestUtils.setField(affineCipherController, "affineDescription", affineDescription); ReflectionTestUtils.setField(affineCipherController, "affineExplanation", affineExplanation); ReflectionTestUtils.setField(affineCipherController, "affineFacts", affineFacts); } @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_DESCRIPTION).value(affineDescription)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(affineName)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(affineExplanation.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(affineExplanation.get(0), affineExplanation.get(1), affineExplanation.get(2)))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(affineFacts.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(affineFacts.get(0), affineFacts.get(1), affineFacts.get(2)))); //Filter super.verifyFilter(url); //Controller verify(affineLogger, times(1)).info("Getting info for {}", affineName); verifyNoMoreInteractions(affineLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, affineName); } @Test public void testEncodeAffine() 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(affineLogger, times(1)).info("Encoding {}", affineName); verifyNoMoreInteractions(affineLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, affineName); //Cipher Aspect verifyAspectLogging(decodedNode); } @Test public void testEncodeAffine_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(affineLogger, times(1)).info("Encoding {}", affineName); verifyNoMoreInteractions(affineLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, affineName); //Cipher Aspect verifyNoInteractions(aspectLogger); } @Test public void testDecodeAffine() 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 super.verifyFilter(url + "/decode"); //Controller verify(affineLogger, times(1)).info("Decoding {}", affineName); verifyNoMoreInteractions(affineLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, affineName); //Cipher Aspect verifyAspectLogging(encodedNode); } @Test public void testDecodeAffine_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(affineLogger, times(1)).info("Decoding {}", affineName); verifyNoMoreInteractions(affineLogger); verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, affineName); //Cipher Aspect verifyNoInteractions(aspectLogger); } }