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

HV-1971 fix CNPJ validation when all digits are the same #1339

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,13 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
return true;
}

// Check for repeated digits
if ( value.toString().chars()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's replace this with a simple for loop? Something along the lines:

// the number is too short, hence it's invalid anyway:
if ( value.length() < 2 ) {
	return false;
}

char firstDigit = value.charAt( 0 );
char otherDigit = value.charAt( 1 );
for ( int i = 2; i < value.length(); i++ ) {
	char c = value.charAt( i );
	if ( Character.isDigit( c ) && firstDigit != c ) {
		otherDigit = c;
	}
}
if ( firstDigit == otherDigit ) {
	return false;
}

From what I see distinct may not be optimized:

public final IntStream distinct() {
	// While functional and quick to implement, this approach is not very efficient.
	// An efficient version requires an int-specific map/set implementation.
	return boxed().distinct().mapToInt(i -> i);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect, I'll change it. Thanks!

.filter( Character::isDigit )
.distinct().count() == 1 ) {
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