111 lines
3.8 KiB
Java
111 lines
3.8 KiB
Java
package com.mattrixwv.cipherstream.controller.polysubstitution;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import java.util.List;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Tag;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
|
|
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
|
|
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
|
|
|
|
import tools.jackson.databind.ObjectMapper;
|
|
import tools.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
@Tag("unit-test")
|
|
@ExtendWith(MockitoExtension.class)
|
|
public class RailFenceControllerTest{
|
|
@InjectMocks
|
|
private RailFenceController railFenceController;
|
|
//Fields
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
private static final ObjectNode blankNode = mapper.createObjectNode();
|
|
private static final String RAIL_FENCE_INPUT_STRING = "Message to^encode";
|
|
private static final String RAIL_FENCE_OUTPUT_STRING = "Moetese ne^sgcdao";
|
|
private static final int RAIL_FENCE_RAILS = 5;
|
|
private static final String RAIL_FENCE_NAME = "railFenceName";
|
|
private static final String RAIL_FENCE_DESCRIPTION = "railFenceDescription";
|
|
private static final List<String> RAIL_FENCE_EXPLANATION = List.of("railFenceExplanation1", "railFenceExplanation2", "railFenceExplanation3");
|
|
private static final List<String> RAIL_FENCE_FACTS = List.of("railFenceFact1", "railFenceFact2", "railFenceFact3");
|
|
|
|
|
|
@BeforeEach
|
|
public void setup(){
|
|
ReflectionTestUtils.setField(railFenceController, "railFenceName", RAIL_FENCE_NAME);
|
|
ReflectionTestUtils.setField(railFenceController, "railFenceDescription", RAIL_FENCE_DESCRIPTION);
|
|
ReflectionTestUtils.setField(railFenceController, "railFenceExplanation", RAIL_FENCE_EXPLANATION);
|
|
ReflectionTestUtils.setField(railFenceController, "railFenceFacts", RAIL_FENCE_FACTS);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void tetGetCipherInfo(){
|
|
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(RAIL_FENCE_NAME, RAIL_FENCE_DESCRIPTION, RAIL_FENCE_EXPLANATION, RAIL_FENCE_FACTS);
|
|
|
|
|
|
ObjectNode returnedJson = railFenceController.getCipherInfo();
|
|
|
|
|
|
assertEquals(infoNode, returnedJson);
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeRailFence(){
|
|
ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING);
|
|
|
|
|
|
ObjectNode returnedJson = railFenceController.encodeRailFence(cipherParams);
|
|
|
|
|
|
assertEquals(cipherParams, returnedJson);
|
|
assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeRailFence_invalidParameters(){
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
railFenceController.encodeRailFence(blankNode);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeRailFence(){
|
|
ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_OUTPUT_STRING);
|
|
|
|
|
|
ObjectNode returnedJson = railFenceController.decodeRailFence(cipherParams);
|
|
|
|
|
|
assertEquals(cipherParams, returnedJson);
|
|
assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeRailFence_invalidParameters(){
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
railFenceController.encodeRailFence(blankNode);
|
|
});
|
|
}
|
|
|
|
private ObjectNode generateParams(int rails, String inputString){
|
|
ObjectNode cipherParams = mapper.createObjectNode();
|
|
|
|
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
|
|
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
|
|
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
|
|
cipherParams.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails);
|
|
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
|
|
|
|
return cipherParams;
|
|
}
|
|
}
|