package de.vanitasvitae.imi.codes; import static junit.framework.TestCase.assertEquals; import java.io.IOException; import java.util.List; import de.vanitasvitae.imi.codes.input.InvalidOptionException; import de.vanitasvitae.imi.codes.persistence.Repository; import de.vanitasvitae.imi.codes.persistence.SimpleTestRepository; import de.vanitasvitae.imi.codes.types.SampleTubeCode; import de.vanitasvitae.imi.codes.types.SampleType; import de.vanitasvitae.imi.codes.types.StudyNumber; import org.junit.Before; import org.junit.Test; public class CodeGeneratorTest { // Test Repository, which stores sample code records in an ephemeral HashMap, so no cleaning is needed :) private static final Repository repository = new SimpleTestRepository(); @Before public void populateRepository() throws InvalidOptionException, IOException { // Put some dummy values into the repository. repository.setNextSampleCode(new StudyNumber("AAA"), 42); repository.setNextSampleCode(new StudyNumber("BBB"), 56); repository.setNextSampleCode(new StudyNumber("ZZZ"), 9999); } /** * Check, if generated codes match expected output. * * @throws InvalidOptionException NOT expected since the input should be valid. * @throws IOException NOT expected, since we do no IO. */ @Test public void testGenerateCodes() throws InvalidOptionException, IOException { List codes = CodeGenerator.generateCodes(repository, new StudyNumber("AAA"), new SampleType("b"), 34); assertEquals("The size of the list of generated codes must match.", 34, codes.size()); assertEquals("The first generated code must match the expected format.", "AAAb0042", codes.get(0).toString()); for (int i = 1; i < codes.size(); i++) { assertEquals("All codes in the list must match the expected codes.", String.format("AAAb%04d", (42 + i)), codes.get(i).toString()); } } /** * Test whether the method throws an exception, when there would be too many codes generated for a study. * This can happen, if a study has n codes already generated, and the user tries to generate m additional codes, so * that n+m > 9999. This must not happen, since we only have 4 decimal letters to encode the sample number. * * @throws InvalidOptionException expected. * @throws IOException NOT expected, since we do not IO. */ @Test (expected = InvalidOptionException.class) public void testGenerateCodesFailsWhenOverflowing() throws InvalidOptionException, IOException { CodeGenerator.generateCodes(repository, new StudyNumber("ZZZ"), new SampleType("b"), 2); } /** * Test whether the method fails for too big numbers of codes to generate. * This is done to prevent overflows similar as in {@link #testGenerateCodesFailsWhenOverflowing()}. * * @throws InvalidOptionException expected. * @throws IOException NOT expected, since we do no IO. */ @Test (expected = InvalidOptionException.class) public void testGenerateCodesFailsForTooHighNumberOfCodes() throws InvalidOptionException, IOException { CodeGenerator.generateCodes(repository, new StudyNumber("abc"), new SampleType("x"), 10001); } /** * Test whether the method throws an exception for too low number of codes to be generated. * The number of codes to be generated must be at least 1. * * @throws InvalidOptionException expected. * @throws IOException NOT expected (no IO). */ @Test (expected = InvalidOptionException.class) public void testGenerateCodesFailsForTooLowNumberOfCodes() throws InvalidOptionException, IOException { CodeGenerator.generateCodes(repository, new StudyNumber("abc"), new SampleType("x"), -4); } }