Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adição de validador agência bancária #301

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
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
}
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));
}
}
}