-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adição de validador agência bancária (#301)
* Adicao de validador de agencia bancaria * Remocao do uso de Objects disponivel a partir do Java 7
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
stella-core/src/main/java/br/com/caelum/stella/validation/AgenciaBancariaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package br.com.caelum.stella.validation; | ||
|
||
import br.com.caelum.stella.DigitoGenerator; | ||
import br.com.caelum.stella.MessageProducer; | ||
import br.com.caelum.stella.SimpleMessageProducer; | ||
import br.com.caelum.stella.ValidationMessage; | ||
import br.com.caelum.stella.validation.error.AgenciaBancariaError; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Representa um validador de agencia bancária. | ||
* | ||
* @author Thiago Nascimento | ||
*/ | ||
public class AgenciaBancariaValidator implements Validator<String> { | ||
|
||
public static final Pattern COM_DV = Pattern.compile("(\\d+)\\-([\\dX])"); | ||
public static final Pattern SEM_DV = Pattern.compile("\\d+"); | ||
|
||
private boolean isComDigito = true; | ||
private MessageProducer messageProducer; | ||
|
||
public AgenciaBancariaValidator() { | ||
this.messageProducer = new SimpleMessageProducer(); | ||
} | ||
|
||
public AgenciaBancariaValidator(boolean isComDigito) { | ||
this(); | ||
this.isComDigito = isComDigito; | ||
} | ||
|
||
@Override | ||
public void assertValid(String agencia) { | ||
|
||
List<ValidationMessage> errors = this.invalidMessagesFor(agencia); | ||
|
||
if (!errors.isEmpty()) { | ||
throw new InvalidStateException(errors); | ||
} | ||
} | ||
|
||
@Override | ||
public List<ValidationMessage> invalidMessagesFor(String agencia) { | ||
|
||
List<ValidationMessage> errors = new ArrayList<ValidationMessage>(); | ||
|
||
if (this.isEligible(agencia)) { | ||
|
||
if (this.isComDigito) { | ||
|
||
Matcher matcher = COM_DV.matcher(agencia); | ||
|
||
if (!matcher.find()) { | ||
throw new InvalidStateException(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_FORMAT)); | ||
} | ||
|
||
String dvInformado = matcher.group(2); | ||
String dvComputado = this.computarDigitoVerificador(matcher.group(1)); | ||
|
||
if (!dvInformado.equals(dvComputado)) { | ||
errors.add(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_CHECK_DIGIT)); | ||
} | ||
|
||
} else { | ||
errors.add(this.messageProducer.getMessage(AgenciaBancariaError.CHECK_DIGIT_NOT_FOUND)); | ||
} | ||
|
||
} else { | ||
errors.add(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_FORMAT)); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
@Override | ||
public boolean isEligible(String value) { | ||
|
||
if (value == null || value.trim().isEmpty()) { | ||
return false; | ||
} | ||
|
||
return this.isComDigito ? | ||
COM_DV.matcher(value).matches() : SEM_DV.matcher(value).matches(); | ||
} | ||
|
||
@Override | ||
public String generateRandomValid() { | ||
final String agenciaSemDigitos = new DigitoGenerator().generate(4); | ||
return String.format("%s-%s", agenciaSemDigitos, this.computarDigitoVerificador(agenciaSemDigitos)); | ||
} | ||
|
||
public String computarDigitoVerificador(String agenciaSemDV) { | ||
|
||
String[] algarisms = agenciaSemDV.split(""); | ||
int multiplier = 9; | ||
int sum = 0; | ||
|
||
for (int index = algarisms.length - 1; index >= 0; --index) { | ||
sum += Integer.valueOf(algarisms[index]) * multiplier--; | ||
} | ||
|
||
int rest = sum % 11; | ||
return rest == 10 ? "X" : String.valueOf(rest); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
stella-core/src/main/java/br/com/caelum/stella/validation/error/AgenciaBancariaError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package br.com.caelum.stella.validation.error; | ||
|
||
import br.com.caelum.stella.validation.InvalidValue; | ||
|
||
public enum AgenciaBancariaError implements InvalidValue { | ||
INVALID_CHECK_DIGIT, CHECK_DIGIT_NOT_FOUND, INVALID_FORMAT | ||
} |
70 changes: 70 additions & 0 deletions
70
stella-core/src/test/java/br/com/caelum/stella/validation/AgenciaBancariaValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package br.com.caelum.stella.validation; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.Test; | ||
|
||
import br.com.caelum.stella.MessageProducer; | ||
import br.com.caelum.stella.SimpleMessageProducer; | ||
import br.com.caelum.stella.validation.error.AgenciaBancariaError; | ||
|
||
/** | ||
* @author Thiago Nascimento | ||
*/ | ||
public class AgenciaBancariaValidatorTest { | ||
|
||
private MessageProducer messageProducer = new SimpleMessageProducer(); | ||
|
||
@Test | ||
public void shouldAcceptEligibleAgenciasComDV() { | ||
AgenciaBancariaValidator validator = new AgenciaBancariaValidator(); | ||
assertTrue(validator.isEligible("3610-2")); | ||
assertFalse(validator.isEligible("3610")); | ||
} | ||
|
||
@Test | ||
public void shouldAcceptEligibleAgenciasSemDV() { | ||
AgenciaBancariaValidator validator = new AgenciaBancariaValidator(false); | ||
assertFalse(validator.isEligible("3610-2")); | ||
assertTrue(validator.isEligible("3610")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNoValidationMessagesForCorrectAgenciasComDV() { | ||
AgenciaBancariaValidator validator = new AgenciaBancariaValidator(); | ||
assertTrue(validator.invalidMessagesFor("3610-2").isEmpty()); | ||
assertTrue(validator.invalidMessagesFor("3793-1").isEmpty()); | ||
assertTrue(validator.invalidMessagesFor("197-X").isEmpty()); | ||
assertTrue(validator.invalidMessagesFor("4158-0").isEmpty()); | ||
assertTrue(validator.invalidMessagesFor("2121-0").isEmpty()); | ||
assertTrue(validator.invalidMessagesFor("1284-X").isEmpty()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnInvalidCheckDigitForIncorrectDV() { | ||
|
||
AgenciaBancariaValidator validator = new AgenciaBancariaValidator(); | ||
|
||
try { | ||
validator.assertValid("2121-9"); | ||
fail(); | ||
} catch (InvalidStateException e) { | ||
e.getInvalidMessages().contains(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_CHECK_DIGIT)); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnCheckDigitNotFoundForAgenciaSemDV() { | ||
|
||
AgenciaBancariaValidator validator = new AgenciaBancariaValidator(); | ||
|
||
try { | ||
validator.assertValid("1103"); | ||
fail(); | ||
} catch (InvalidStateException e) { | ||
e.getInvalidMessages().contains(this.messageProducer.getMessage(AgenciaBancariaError.CHECK_DIGIT_NOT_FOUND)); | ||
} | ||
} | ||
} |