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

102 lines
3.6 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.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 AtbashCipherControllerTest{
@InjectMocks
private AtbashCipherController atbashCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectNode blankNode = mapper.createObjectNode();
private static final String ATBASH_INPUT_STRING = "Message to^encode";
private static final String ATBASH_OUTPUT_STRING = "Nvhhztv gl^vmxlwv";
private static final String atbashName = "atbashName";
private static final String atbashDescription = "atbashDescription";
private static final List<String> atbashExplanation = List.of("atbashExplanation1", "atbashExplanation2", "atbashExplanation3");
private static final List<String> atbashFacts = List.of("atbashFact1", "atbashFact2", "atbashFact3");
@BeforeEach
public void setup(){
ReflectionTestUtils.setField(atbashCipherController, "atbashName", atbashName);
ReflectionTestUtils.setField(atbashCipherController, "atbashDescription", atbashDescription);
ReflectionTestUtils.setField(atbashCipherController, "atbashExplanation", atbashExplanation);
ReflectionTestUtils.setField(atbashCipherController, "atbashFacts", atbashFacts);
}
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(atbashName, atbashDescription, atbashExplanation, atbashFacts);
ObjectNode returnedJson = atbashCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeAtbash(){
ObjectNode cipherParams = generateParams(ATBASH_INPUT_STRING);
ObjectNode returnedJson = atbashCipherController.encodeAtbash(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testEncodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.encodeAtbash(blankNode);
});
}
@Test
public void testDecodeAtbash(){
ObjectNode cipherParams = generateParams(ATBASH_OUTPUT_STRING);
ObjectNode returnedJson = atbashCipherController.decodeAtbash(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testDecodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.decodeAtbash(blankNode);
});
}
private ObjectNode generateParams(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.INPUT_STRING, inputString);
return cipherParams;
}
}