Skip to content

Commit 5e10c56

Browse files
committed
validator refactor and unit test
AccountNoValidator BankCodeValidator BanksMinimum100AccountPaymentValidator BvnValidator DateOfBirthValidator
1 parent 77264ba commit 5e10c56

15 files changed

+325
-37
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.flutterwave.raveandroid.validators;
22

3-
public class AccountNoValidator {
3+
import java.util.regex.Pattern;
44

5-
public boolean isAccountNumberValid(String accNo) {
6-
boolean isValid = true;
5+
public class AccountNoValidator {
76

8-
if (!(accNo != null && accNo.length() == 10)) {
9-
isValid = false;
7+
public boolean isAccountNumberValid(String accountNo) {
8+
if (accountNo != null) {
9+
return Pattern.matches("\\d{10}", accountNo);
10+
} else {
11+
return false;
1012
}
11-
12-
return isValid;
1313
}
1414
}

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/AmountValidator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.flutterwave.raveandroid.validators;
22

3+
import java.util.regex.Pattern;
4+
35
public class AmountValidator {
46

57
public boolean isAmountValid(Double amount) {
@@ -8,11 +10,9 @@ public boolean isAmountValid(Double amount) {
810

911
public boolean isAmountValid(String amount) {
1012

11-
try {
12-
Double.parseDouble(amount);
13-
return true;
14-
} catch (NumberFormatException e) {
15-
e.printStackTrace();
13+
if (amount != null && !amount.isEmpty()) {
14+
return Pattern.matches("^[0-9]*$", amount);
15+
} else {
1616
return false;
1717
}
1818

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.flutterwave.raveandroid.validators;
22

3+
import java.util.regex.Pattern;
4+
35
public class BankCodeValidator {
46

57
public boolean isBankCodeValid(String bankCode) {
68

7-
boolean isValid = true;
8-
99
if (bankCode == null) {
10-
isValid = false;
10+
return false;
11+
} else {
12+
return Pattern.matches("\\d{3}", bankCode);
1113
}
12-
return isValid;
1314
}
1415
}

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/BanksMinimum100AccountPaymentValidator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
public class BanksMinimum100AccountPaymentValidator {
44

5+
BankCodeValidator bankCodeValidator = new BankCodeValidator();
56
public boolean isPaymentValid(String bankCode, Double amount) {
67

7-
return (!bankCode.equalsIgnoreCase("058") &&
8-
!bankCode.equalsIgnoreCase("011"))
9-
|| amount > 100;
8+
boolean isBankCodeValid = bankCodeValidator.isBankCodeValid(bankCode);
109

10+
if (isBankCodeValid) {
11+
return (bankCode.equalsIgnoreCase("058") &&
12+
bankCode.equalsIgnoreCase("011"))
13+
|| amount > 100;
14+
} else {
15+
return false;
16+
}
1117
}
1218
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.flutterwave.raveandroid.validators;
22

3+
import java.util.regex.Pattern;
4+
35
public class BvnValidator {
46

57
public boolean isBvnValid(String bvn) {
6-
boolean isValid = true;
7-
8-
if (!(bvn != null && bvn.length() == 11)) {
9-
isValid = false;
8+
if (bvn != null) {
9+
return Pattern.matches("\\d{11}", bvn);
10+
} else {
11+
return false;
1012
}
11-
12-
return isValid;
1313
}
1414
}

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/CardExpiryValidator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
public class CardExpiryValidator {
66

77
public boolean isCardExpiryValid(String cardExpiry) {
8-
return Pattern.matches("\\d\\d/\\d\\d", cardExpiry);
8+
if (cardExpiry != null) {
9+
return Pattern.matches("\\d\\d/\\d\\d", cardExpiry);
10+
} else {
11+
return false;
12+
}
13+
914
}
1015
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.flutterwave.raveandroid.validators;
22

3+
import java.util.regex.Pattern;
4+
35
public class DateOfBirthValidator {
46

57
public boolean isDateValid(String dateOfBirth) {
6-
7-
boolean isValid = true;
8-
9-
if (!(dateOfBirth != null && dateOfBirth.length() == 10)) {
8+
if (dateOfBirth != null) {
9+
return Pattern.matches("\\d\\d/\\d\\d/\\d\\d\\d\\d", dateOfBirth);
10+
} else {
1011
return false;
1112
}
12-
return isValid;
1313
}
1414
}

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/EmailValidator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
public class EmailValidator {
77

88
public boolean isEmailValid(String email) {
9-
Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
10-
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
11-
return matcher.find();
9+
if (email != null) {
10+
Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
11+
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
12+
return matcher.find();
13+
} else {
14+
return false;
15+
}
1216
}
1317
}

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/PhoneValidator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
public class PhoneValidator {
66

77
public boolean isPhoneValid(String phone) {
8-
if (phone.length() > 1) {
9-
return Pattern.matches("^[0-9]*$", phone);
8+
if (phone != null) {
9+
if (phone.length() > 1) {
10+
return Pattern.matches("^[0-9]*$", phone);
11+
} else {
12+
return false;
13+
}
1014
} else {
1115
return false;
1216
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.flutterwave.raveandroid.validators;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
public class AccountNoValidatorTest {
10+
11+
AccountNoValidator SUT;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
SUT = new AccountNoValidator();
16+
}
17+
18+
@Test
19+
public void isAccountNumberValid_isCorrectAccounNoPassed_returnsTrue() {
20+
String accountNo = "1234567890";
21+
boolean isAccountNoValid = SUT.isAccountNumberValid(accountNo);
22+
assertThat(true, is(isAccountNoValid));
23+
}
24+
25+
@Test
26+
public void isAccountNumberValid_isLettersPassed_returnsFalse() {
27+
String accountNo = "account";
28+
boolean isAccountNoValid = SUT.isAccountNumberValid(accountNo);
29+
assertThat(false, is(isAccountNoValid));
30+
}
31+
32+
@Test
33+
public void isAccountNumberValid_isLessThan10Passed_returnsFalse() {
34+
String accountNo = "12345";
35+
boolean isAccountNoValid = SUT.isAccountNumberValid(accountNo);
36+
assertThat(false, is(isAccountNoValid));
37+
}
38+
39+
@Test
40+
public void isAccountNumberValid_isMoreThan10Passed_returnsFalse() {
41+
String accountNo = "1234567890123";
42+
boolean isAccountNoValid = SUT.isAccountNumberValid(accountNo);
43+
assertThat(false, is(isAccountNoValid));
44+
}
45+
46+
@Test
47+
public void isAccountNumberValid_isEmptyPassed_returnsFalse() {
48+
String accountNo = "";
49+
boolean isAccountNoValid = SUT.isAccountNumberValid(accountNo);
50+
assertThat(false, is(isAccountNoValid));
51+
}
52+
}

0 commit comments

Comments
 (0)