184 lines
7.7 KiB
Java
184 lines
7.7 KiB
Java
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 = BaseXCipherController.class)
|
|
public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
@Autowired
|
|
private BaseXCipherController baseXCipherController;
|
|
//Loggers
|
|
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaseXCipherController")
|
|
protected Logger baseXLogger;
|
|
//Fields
|
|
private static final ObjectNode blankNode = mapper.createObjectNode();
|
|
private static final String url = "/basex";
|
|
private static final String decodedString = "A+B@C d\te\nf";
|
|
private static final String encodedString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
|
private static final int base = 2;
|
|
private static final String baseXName = "baseXName";
|
|
private static final String baseXDescription = "baseXDescription";
|
|
private static final List<String> baseXExplanation = List.of("baseXExplanation1", "baseXExplanation2", "baseXExplanation3");
|
|
private static final List<String> baseXFacts = List.of("baseXFact1", "baseXFact2", "baseXFact3");
|
|
|
|
|
|
@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.BASE_X_BASE, base);
|
|
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.BASE_X_BASE, base);
|
|
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
|
|
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
|
|
|
|
ReflectionTestUtils.setField(baseXCipherController, "baseXName", baseXName);
|
|
ReflectionTestUtils.setField(baseXCipherController, "baseXDescription", baseXDescription);
|
|
ReflectionTestUtils.setField(baseXCipherController, "baseXExplanation", baseXExplanation);
|
|
ReflectionTestUtils.setField(baseXCipherController, "baseXFacts", baseXFacts);
|
|
}
|
|
|
|
|
|
@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(baseXDescription))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(baseXName))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray())
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(baseXExplanation.size())))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(baseXExplanation.get(0), baseXExplanation.get(1), baseXExplanation.get(2))))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray())
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(baseXFacts.size())))
|
|
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(baseXFacts.get(0), baseXFacts.get(1), baseXFacts.get(2))));
|
|
|
|
//Filter
|
|
super.verifyFilter(url);
|
|
//Controller
|
|
verify(baseXLogger, times(1)).info("Getting info for {}", baseXName);
|
|
verifyNoMoreInteractions(baseXLogger);
|
|
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baseXName);
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeBaseXEncode() 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(baseXLogger, times(1)).info("Encoding {}", baseXName);
|
|
verifyNoMoreInteractions(baseXLogger);
|
|
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baseXName);
|
|
//Cipher Aspect
|
|
verifyAspectLogging(decodedNode);
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeBaseX_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.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
|
|
|
|
//Filter
|
|
super.verifyFilter(url + "/encode");
|
|
//Controller
|
|
verify(baseXLogger, times(1)).info("Encoding {}", baseXName);
|
|
verifyNoMoreInteractions(baseXLogger);
|
|
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baseXName);
|
|
//Cipher Aspect
|
|
verifyNoInteractions(aspectLogger);
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeBaseXDecode() 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(baseXLogger, times(1)).info("Decoding {}", baseXName);
|
|
verifyNoMoreInteractions(baseXLogger);
|
|
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baseXName);
|
|
//Cipher Aspect
|
|
verifyAspectLogging(encodedNode);
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeBaseX_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.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
|
|
|
|
//Filter
|
|
super.verifyFilter(url + "/decode");
|
|
//Controller
|
|
verify(baseXLogger, times(1)).info("Decoding {}", baseXName);
|
|
verifyNoMoreInteractions(baseXLogger);
|
|
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baseXName);
|
|
//Cipher Aspect
|
|
verifyNoInteractions(aspectLogger);
|
|
}
|
|
}
|