132 lines
6.1 KiB
Java
132 lines
6.1 KiB
Java
package com.mattrixwv.cipherstream.controller.monosubstitution;
|
|
|
|
|
|
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 = SubstitutionCipherController.class)
|
|
public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
@Autowired
|
|
private SubstitutionCipherController substitutionCipherController;
|
|
//Fields
|
|
private static final String URL = "/substitution";
|
|
private static final String DECODED_STRING = "Message to^encode";
|
|
private static final String ENCODED_STRING = "Oguucig vq^gpeqfg";
|
|
private static final String KEYWORD = "cdefghijklmnopqrstuvwxyzab";
|
|
private static final String SUBSTITUTION_NAME = "substitutionName";
|
|
private static final String SUBSTITUTION_DESCRIPTION = "substitutionDescription";
|
|
private static final List<String> SUBSTITUTION_EXPLANATION = List.of("substitutionExplanation1", "substitutionExplanation2", "substitutionExplanation3");
|
|
private static final List<String> SUBSTITUTION_FACTS = List.of("substitutionFact1", "substitutionFact2", "substitutionFact3");
|
|
|
|
|
|
@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, 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.INPUT_STRING, ENCODED_STRING);
|
|
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, DECODED_STRING);
|
|
|
|
ReflectionTestUtils.setField(substitutionCipherController, "substitutionName", SUBSTITUTION_NAME);
|
|
ReflectionTestUtils.setField(substitutionCipherController, "substitutionDescription", SUBSTITUTION_DESCRIPTION);
|
|
ReflectionTestUtils.setField(substitutionCipherController, "substitutionExplanation", SUBSTITUTION_EXPLANATION);
|
|
ReflectionTestUtils.setField(substitutionCipherController, "substitutionFacts", SUBSTITUTION_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_DESCRIPTION).value(SUBSTITUTION_DESCRIPTION))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(SUBSTITUTION_NAME))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray())
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(SUBSTITUTION_EXPLANATION.size())))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(SUBSTITUTION_EXPLANATION.get(0), SUBSTITUTION_EXPLANATION.get(1), SUBSTITUTION_EXPLANATION.get(2))))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray())
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(SUBSTITUTION_FACTS.size())))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(SUBSTITUTION_FACTS.get(0), SUBSTITUTION_FACTS.get(1), SUBSTITUTION_FACTS.get(2))));
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeSubstitution() 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 testEncodeAdfgvx_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 testDecodeSubstitution() 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 testDecodeAdfgvx_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));
|
|
}
|
|
}
|