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

110 lines
3.9 KiB
Java

package com.mattrixwv.cipherstream.controller.monosubstitution;
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 AffineCipherControllerTest{
@InjectMocks
private AffineCipherController affineCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String AFFINE_INPUT_STRING = "Message to^encode";
private static final String AFFINE_OUTPUT_STRING = "Pbtthlb yz^burzwb";
private static final int AFFINE_KEY_1 = 5;
private static final int AFFINE_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(){
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(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(AFFINE_NAME, AFFINE_DESCRIPTION, AFFINE_EXPLANATION, AFFINE_FACTS);
ObjectNode returnedJson = affineCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING);
ObjectNode returnedJson = affineCipherController.encodeAffine(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testEncodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.encodeAffine(blankNode);
});
}
@Test
public void testDecodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_OUTPUT_STRING);
ObjectNode returnedJson = affineCipherController.decodeAffine(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asString());
}
@Test
public void testDecodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.decodeAffine(blankNode);
});
}
private ObjectNode generateParams(int key1, int key2, 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.AFFINE_KEY_1, key1);
cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}