Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerTest.java
Matthew Ellison ff11cd72c3 Updated more tests
2024-04-08 16:44:38 -04:00

93 lines
2.9 KiB
Java

package com.mattrixwv.cipherstream.controller.monosubstitution;
import static org.junit.jupiter.api.Assertions.*;
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 com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class AutokeyCipherControllerTest{
@InjectMocks
private AutokeyCipherController autokeyCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String AUTOKEY_INPUT_STRING = "Message to^encode";
private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx";
private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD;
private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.AUTOKEY_CIPHER_NAME, CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION);
ObjectNode returnedJson = autokeyCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING);
ObjectNode returnedJson = autokeyCipherController.encodeAutokey(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testEncodeAutokey_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.encodeAutokey(blankNode);
});
}
@Test
public void testDecodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_OUTPUT_STRING);
ObjectNode returnedJson = autokeyCipherController.decodeAutokey(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testDecodeAutokey_invapidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.decodeAutokey(blankNode);
});
}
private ObjectNode generateParams(String keyword, 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.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}