Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerIntegrationTest.java
2024-04-28 16:25:39 -04:00

184 lines
7.7 KiB
Java

package com.mattrixwv.cipherstream.controller.polysubstitution;
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 = HillCipherController.class)
public class HillCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
@Autowired
private HillCipherController hillCipherController;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.HillCipherController")
protected Logger hillLogger;
//Fields
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String url = "/hill";
private static final String decodedString = "Message to^encoded";
private static final String encodedString = "Mgkeqge ul^ikhisplrd";
private static final int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
private static final String hillName = "hillName";
private static final String hillDescription = "hillDescription";
private static final List<String> hillExplanation = List.of("hillExplanation1", "hillExplanation2", "hillExplanation3");
private static final List<String> hillFacts = List.of("hillFact1", "hillFact2", "hillFact3");
@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.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray));
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.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray));
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString + "xx");
ReflectionTestUtils.setField(hillCipherController, "hillName", hillName);
ReflectionTestUtils.setField(hillCipherController, "hillDescription", hillDescription);
ReflectionTestUtils.setField(hillCipherController, "hillExplanation", hillExplanation);
ReflectionTestUtils.setField(hillCipherController, "hillFacts", hillFacts);
}
@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_NAME).value(hillName))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(hillDescription))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(hillExplanation.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(hillExplanation.get(0), hillExplanation.get(1), hillExplanation.get(2))))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(hillFacts.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(hillFacts.get(0), hillFacts.get(1), hillFacts.get(2))));
//Filter
super.verifyFilter(url);
//Controller
verify(hillLogger, times(1)).info("Getting info for {}", hillName);
verifyNoMoreInteractions(hillLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
}
@Test
public void testEncodeHill() 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(hillLogger, times(1)).info("Encoding {}", hillName);
verifyNoMoreInteractions(hillLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
//Cipher Aspect
verifyAspectLogging(decodedNode);
}
@Test
public void testEncodehill_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(hillLogger, times(1)).info("Encoding {}", hillName);
verifyNoMoreInteractions(hillLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test
public void testDecodeHill() 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 + "xx"));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(hillLogger, times(1)).info("Decoding {}", hillName);
verifyNoMoreInteractions(hillLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
//Cipher Aspect
verifyAspectLogging(encodedNode);
}
@Test
public void testDecodehill_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(hillLogger, times(1)).info("Decoding {}", hillName);
verifyNoMoreInteractions(hillLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
}