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; @Tag("integration-test") @WebMvcTest(controllers = TrifidCipherController.class) public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ @Autowired private MockMvc mockMvc; @Autowired private TrifidCipherController trifidCipherController; //Fields private static final String URL = "/trifid"; private static final String DECODED_STRING = "Message to^encode+"; private static final String ENCODED_STRING = "Gqdokpd od^ljvflf+"; private static final String KEYWORD = "keyword"; private static final String TRIFID_FILL = "="; private static final int GROUP_LENGTH = Integer.MAX_VALUE; private static final String TRIFID_NAME = "trifidName"; private static final String TRIFID_DESCRIPTION = "trifidDescription"; private static final List TRIFID_EXPLANATION = List.of("trifidExplanation1", "trifidExplanation2", "trifidExplanation3"); private static final List TRIFID_FACTS = List.of("trifidFact1", "trifidFact2", "trifidFact3"); @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, TRIFID_FILL); decodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, GROUP_LENGTH); decodedNode.put(CipherParameterUtil.INPUT_STRING, DECODED_STRING); decodedNode.put(CipherParameterUtil.OUTPUT_STRING, ENCODED_STRING); 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, TRIFID_FILL); encodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, GROUP_LENGTH); encodedNode.put(CipherParameterUtil.INPUT_STRING, ENCODED_STRING); encodedNode.put(CipherParameterUtil.OUTPUT_STRING, DECODED_STRING); ReflectionTestUtils.setField(trifidCipherController, "trifidName", TRIFID_NAME); ReflectionTestUtils.setField(trifidCipherController, "trifidDescription", TRIFID_DESCRIPTION); ReflectionTestUtils.setField(trifidCipherController, "trifidExplanation", TRIFID_EXPLANATION); ReflectionTestUtils.setField(trifidCipherController, "trifidFacts", TRIFID_FACTS); } @Test public void testGetCipherInfo() throws Exception{ mockMvc.perform(get(URL) .header("X-Request-Id", REQUEST_ID) .header("X-Forwarded-For", IP_ADDRESS)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(TRIFID_NAME)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(TRIFID_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(TRIFID_EXPLANATION.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(TRIFID_EXPLANATION.get(0), TRIFID_EXPLANATION.get(1), TRIFID_EXPLANATION.get(2)))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray()) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(TRIFID_FACTS.size()))) .andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(TRIFID_FACTS.get(0), TRIFID_FACTS.get(1), TRIFID_FACTS.get(2)))); } @Test public void testEncodeTrifid() throws Exception{ mockMvc.perform(post(URL + "/encode") .header("X-Request-Id", REQUEST_ID) .header("X-Forwarded-For", IP_ADDRESS) .contentType(MediaType.APPLICATION_JSON) .content(decodedNode.toString())) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(ENCODED_STRING)); } @Test public void testEncodetrifid_error() throws Exception{ mockMvc.perform(post(URL + "/encode") .header("X-Request-Id", REQUEST_ID) .header("X-Forwarded-For", IP_ADDRESS) .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)); } @Test public void testDecodeTrifid() throws Exception{ mockMvc.perform(post(URL + "/decode") .header("X-Request-Id", REQUEST_ID) .header("X-Forwarded-For", IP_ADDRESS) .contentType(MediaType.APPLICATION_JSON) .content(encodedNode.toString())) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(DECODED_STRING)); } @Test public void testDecodetrifid_error() throws Exception{ mockMvc.perform(post(URL + "/decode") .header("X-Request-Id", REQUEST_ID) .header("X-Forwarded-For", IP_ADDRESS) .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)); } }