Skip to content

Commit

Permalink
HV-1971 fix CNPJ validation when all digits are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
edurbs committed Jan 14, 2024
1 parent 729cd01 commit 9c9bf7e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @author Hardy Ferentschik
* @author Eduardo Resende Batista Soares
*/
public class CNPJValidator implements ConstraintValidator<CNPJ, CharSequence> {
private static final Pattern DIGITS_ONLY = Pattern.compile( "\\d+" );
Expand Down Expand Up @@ -55,6 +56,14 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
return true;
}

// Check for repeated digits
boolean allDigitsSame = value.toString().chars()
.filter( Character::isDigit )
.distinct().count() == 1;
if ( allDigitsSame ) {
return false;
}

if ( DIGITS_ONLY.matcher( value ).matches() ) {
return withoutSeparatorMod11Validator1.isValid( value, context )
&& withoutSeparatorMod11Validator2.isValid( value, context );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,61 @@

public class CNPJValidatorTest extends AbstractConstrainedTest {

private String[] invalidCNPJs = {"00.000.000/0000-00", "11.111.111/1111-11",
"22.222.222/2222-22", "33.333.333/3333-33",
"44.444.444/4444-44", "55.555.555/5555-55",
"66.666.666/6666-66", "77.777.777/7777-77",
"88.888.888/8888-88", "99.999.999/9999-99"
};

private String[] validCNPJs = {"41.348.630/0001-39", "47.673.240/0001-10",
"65.627.745/0001-20", "81.110.141/0001-69", "68.321.178/0001-78",
"47.235.630/0001-09", "52.583.338/0001-17", "48.560.263/0001-81",
"16.468.665/0001-64", "11.720.867/0001-38", "00.000.000/0001-91"
};

@Test
@TestForIssue(jiraKey = "HV-1971")
public void any_same_digit_cnpj_with_separator_is_invalid() {
for ( String cnpj : invalidCNPJs ) {
Set<ConstraintViolation<Company>> violations = validator.validate( new Company( cnpj ) );
assertThat( violations ).containsOnlyViolations(
violationOf( CNPJ.class ).withProperty( "cnpj" )
);
}
}

@Test
@TestForIssue(jiraKey = "HV-1971")
public void any_same_digit_cnpj_without_separator_is_invalid() {
for ( String cnpj : invalidCNPJs ) {
String cnpjWithoutseparator = cnpj.replaceAll( "\\D+", "" );
Set<ConstraintViolation<Company>> violations = validator.validate( new Company( cnpjWithoutseparator ) );
assertThat( violations ).containsOnlyViolations(
violationOf( CNPJ.class ).withProperty( "cnpj" )
);
}
}

@Test
@TestForIssue(jiraKey = "HV-1971")
public void correct_list_of_cnpj_with_separator_validates() {
for ( String cnpj : validCNPJs ) {
Set<ConstraintViolation<Company>> violations = validator.validate( new Company( cnpj ) );
assertNoViolations( violations );
}
}

@Test
@TestForIssue(jiraKey = "HV-1971")
public void correct_list_of_cnpj_without_separator_validates() {
for ( String cnpj : validCNPJs ) {
String cnpjWithoutseparator = cnpj.replaceAll( "\\D+", "" );
Set<ConstraintViolation<Company>> violations = validator.validate( new Company( cnpjWithoutseparator ) );
assertNoViolations( violations );
}
}

@Test
@TestForIssue(jiraKey = "HV-491")
public void correct_cnpj_with_separator_validates() {
Expand Down

0 comments on commit 9c9bf7e

Please sign in to comment.