### Question:
DefaultCodeGenerator implements CodeGenerator { @Override public String generate(String key, long counter) throws CodeGenerationException { try { byte[] hash = generateHash(key, counter); return getDigitsFromHash(hash); } catch (Exception e) { throw new CodeGenerationException("Failed to generate code. See nested exception.", e); } } DefaultCodeGenerator();  DefaultCodeGenerator(HashingAlgorithm algorithm);  DefaultCodeGenerator(HashingAlgorithm algorithm, int digits); @Override String generate(String key, long counter);  }### Answer:
@Test public void testDigitLength() throws CodeGenerationException { DefaultCodeGenerator g = new DefaultCodeGenerator(HashingAlgorithm.SHA1); String code = g.generate("W3C5B3WKR4AUKFVWYU2WNMYB756OAKWY", 1567631536); assertEquals(6, code.length()); g = new DefaultCodeGenerator(HashingAlgorithm.SHA1, 8); code = g.generate("W3C5B3WKR4AUKFVWYU2WNMYB756OAKWY", 1567631536); assertEquals(8, code.length()); g = new DefaultCodeGenerator(HashingAlgorithm.SHA1, 4); code = g.generate("W3C5B3WKR4AUKFVWYU2WNMYB756OAKWY", 1567631536); assertEquals(4, code.length()); }
@Test public void testInvalidKeyThrowsCodeGenerationException() throws CodeGenerationException { CodeGenerationException e = assertThrows(CodeGenerationException.class, () -> { DefaultCodeGenerator g = new DefaultCodeGenerator(HashingAlgorithm.SHA1, 4); g.generate("1234", 1567631536); }); assertNotNull(e.getCause()); }