101 lines
3.2 KiB
Java
101 lines
3.2 KiB
Java
package com.mattrixwv.cipherstream.controller.monosubstitution;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
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.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 BaconianCipherControllerTest{
|
|
@InjectMocks
|
|
private BaconianCipherController baconianCipherController;
|
|
//Fields
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
private static final String BACONIAN_INPUT_STRING = "Message to-encode";
|
|
private static final String BACONIAN_OUTPUT_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
private static final String BACONIAN_DECODED_STRING = "Messagetoencode";
|
|
private static final ObjectNode blankNode = mapper.createObjectNode();
|
|
private static final String baconianName = "baconianName";
|
|
private static final String baconianDescription = "baconianDescription";
|
|
|
|
|
|
@BeforeEach
|
|
public void setup(){
|
|
ReflectionTestUtils.setField(baconianCipherController, "baconianName", baconianName);
|
|
ReflectionTestUtils.setField(baconianCipherController, "baconianDescription", baconianDescription);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void tetGetCipherInfo(){
|
|
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(baconianName, baconianDescription);
|
|
|
|
|
|
ObjectNode returnedJson = baconianCipherController.getCipherInfo();
|
|
|
|
|
|
assertEquals(infoNode, returnedJson);
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeBaconian(){
|
|
ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING);
|
|
|
|
|
|
ObjectNode returnedJson = baconianCipherController.encodeBaconian(cipherParams);
|
|
|
|
|
|
assertEquals(cipherParams, returnedJson);
|
|
assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeBaconian_invalidParameters(){
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
baconianCipherController.encodeBaconian(blankNode);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeBaconian(){
|
|
ObjectNode cipherParams = generateParams(BACONIAN_OUTPUT_STRING);
|
|
|
|
|
|
ObjectNode returnedJson = baconianCipherController.decodeBaconian(cipherParams);
|
|
|
|
|
|
assertEquals(cipherParams, returnedJson);
|
|
assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
|
|
}
|
|
|
|
@Test
|
|
public void testDecodeBaconian_invalidParameters(){
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
baconianCipherController.decodeBaconian(blankNode);
|
|
});
|
|
}
|
|
|
|
private ObjectNode generateParams(String inputString){
|
|
ObjectNode cipherParams = mapper.createObjectNode();
|
|
|
|
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
|
|
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
|
|
|
|
return cipherParams;
|
|
}
|
|
}
|