Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerIntegrationTest.java
2026-01-06 23:48:37 -05:00

197 lines
8.0 KiB
Java

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;
import tools.jackson.databind.node.ObjectNode;
@Tag("integration-test")
@WebMvcTest(controllers = RailFenceController.class)
public class RailFenceControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
@Autowired
private RailFenceController railFenceController;
//Loggers
//TODO: Fix logger testing
//@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.RailFenceController")
//protected Logger railFenceLogger;
//Fields
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String url = "/railFence";
private static final String decodedString = "Message to^encode";
private static final String encodedString = "Maooesg te^cdsene";
private static final int rails = 3;
private static final String railFenceName = "railFenceName";
private static final String railFenceDescription = "railFenceDescription";
private static final List<String> railFenceExplanation = List.of("railFenceExplanation1", "railFenceExplanation2", "railFenceExplanation3");
private static final List<String> railFenceFacts = List.of("railFenceFact1", "railFenceFact2", "railFenceFact3");
@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.RAIL_FENCE_RAILS, rails);
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.RAIL_FENCE_RAILS, rails);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
ReflectionTestUtils.setField(railFenceController, "railFenceName", railFenceName);
ReflectionTestUtils.setField(railFenceController, "railFenceDescription", railFenceDescription);
ReflectionTestUtils.setField(railFenceController, "railFenceExplanation", railFenceExplanation);
ReflectionTestUtils.setField(railFenceController, "railFenceFacts", railFenceFacts);
}
@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(railFenceName))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(railFenceDescription))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasSize(railFenceExplanation.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_EXPLANATION, hasItems(railFenceExplanation.get(0), railFenceExplanation.get(1), railFenceExplanation.get(2))))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS).isArray())
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasSize(railFenceFacts.size())))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_FACTS, hasItems(railFenceFacts.get(0), railFenceFacts.get(1), railFenceFacts.get(2))));
//Filter
//TODO: Fix logger testing
/*
super.verifyFilter(url);
//Controller
verify(railFenceLogger, times(1)).info("Getting info for {}", railFenceName);
verifyNoMoreInteractions(railFenceLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, railFenceName);
*/
}
@Test
public void testEncodeRailFence() 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
//TODO: Fix logger testing
/*
super.verifyFilter(url + "/encode");
//Controller
verify(railFenceLogger, times(1)).info("Encoding {}", railFenceName);
verifyNoMoreInteractions(railFenceLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, railFenceName);
//Cipher Aspect
verifyAspectLogging(decodedNode);
*/
}
@Test
public void testEncoderailFence_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
//TODO: Fix logger testing
/*
super.verifyFilter(url + "/encode");
//Controller
verify(railFenceLogger, times(1)).info("Encoding {}", railFenceName);
verifyNoMoreInteractions(railFenceLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, railFenceName);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
*/
}
@Test
public void testDecodeRailFence() 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
//TODO: Fix logger testing
/*
super.verifyFilter(url + "/decode");
//Controller
verify(railFenceLogger, times(1)).info("Decoding {}", railFenceName);
verifyNoMoreInteractions(railFenceLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, railFenceName);
//Cipher Aspect
verifyAspectLogging(encodedNode);
*/
}
@Test
public void testDecoderailFence_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
//TODO: Fix logger testing
/*
super.verifyFilter(url + "/decode");
//Controller
verify(railFenceLogger, times(1)).info("Decoding {}", railFenceName);
verifyNoMoreInteractions(railFenceLogger);
verify(mdc, times(1)).put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, railFenceName);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
*/
}
}