Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerIntegrationTest.java

135 lines
6.0 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 = AffineCipherController.class)
public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
@Autowired
private AffineCipherController affineCipherController;
//Fields
private static final String URL = "/affine";
private static final String DECODED_STRING = "Message to^encode";
private static final String ENCODED_STRING = "Pbtthlb yz^burzwb";
private static final int KEY_1 = 5;
private static final int KEY_2 = 7;
private static final String AFFINE_NAME = "affineCipherName";
private static final String AFFINE_DESCRIPTION = "affineCipherDescription";
private static final List<String> AFFINE_EXPLANATION = List.of("affineExplanation1", "affineExplanation2", "affineExplanation3");
private static final List<String> AFFINE_FACTS = 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, KEY_1);
decodedNode.put(CipherParameterUtil.AFFINE_KEY_2, KEY_2);
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.AFFINE_KEY_1, KEY_1);
encodedNode.put(CipherParameterUtil.AFFINE_KEY_2, KEY_2);
encodedNode.put(CipherParameterUtil.INPUT_STRING, ENCODED_STRING);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, DECODED_STRING);
ReflectionTestUtils.setField(affineCipherController, "affineName", AFFINE_NAME);
ReflectionTestUtils.setField(affineCipherController, "affineDescription", AFFINE_DESCRIPTION);
ReflectionTestUtils.setField(affineCipherController, "affineExplanation", AFFINE_EXPLANATION);
ReflectionTestUtils.setField(affineCipherController, "affineFacts", AFFINE_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(AFFINE_DESCRIPTION))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(AFFINE_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(AFFINE_EXPLANATION.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(AFFINE_EXPLANATION.get(0), AFFINE_EXPLANATION.get(1), AFFINE_EXPLANATION.get(2))))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(AFFINE_FACTS.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(AFFINE_FACTS.get(0), AFFINE_FACTS.get(1), AFFINE_FACTS.get(2))));
}
@Test
public void testEncodeAffine() 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 testEncodeAffine_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 testDecodeAffine() 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 testDecodeAffine_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));
}
}