Skip to content

Commit 2fec96a

Browse files
committed
email validator and phonevalidator
1 parent 10bd887 commit 2fec96a

File tree

10 files changed

+138
-24
lines changed

10 files changed

+138
-24
lines changed

raveandroid/src/main/java/com/flutterwave/raveandroid/account/AccountPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
251251
Class phoneViewType = dataHashMap.get(RaveConstants.fieldPhone).getViewType();
252252

253253

254-
if (!amountValidator.isAmountValid(amount)) {
254+
if (!amountValidator.isAmountValid(Double.valueOf(amount))) {
255255
valid = false;
256256
mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
257257
}

raveandroid/src/main/java/com/flutterwave/raveandroid/card/CardPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
225225
Class cardNoStrippedViewType = dataHashMap.get(RaveConstants.fieldcardNoStripped).getViewType();
226226

227227
try{
228-
boolean isAmountValidated = amountValidator.isAmountValid(amount);
228+
boolean isAmountValidated = amountValidator.isAmountValid(Double.valueOf(amount));
229229
if (!isAmountValidated) {
230230
valid = false; mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
231231
}

raveandroid/src/main/java/com/flutterwave/raveandroid/ghmobilemoney/GhMobileMoneyPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
204204

205205
int network = Integer.valueOf(dataHashMap.get(RaveConstants.fieldNetwork).getData());
206206

207-
if (!amountValidator.isAmountValid(amount)) {
207+
if (!amountValidator.isAmountValid(Double.valueOf(amount))) {
208208
valid = false;
209209
mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
210210
}

raveandroid/src/main/java/com/flutterwave/raveandroid/mpesa/MpesaPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
163163
Class phoneViewType = dataHashMap.get(RaveConstants.fieldPhone).getViewType();
164164

165165

166-
if (!amountValidator.isAmountValid(amount)) {
166+
if (!amountValidator.isAmountValid(Double.valueOf(amount))) {
167167
valid = false;
168168
mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
169169
}

raveandroid/src/main/java/com/flutterwave/raveandroid/ugmobilemoney/UgMobileMoneyPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
157157
String phone = dataHashMap.get(RaveConstants.fieldPhone).getData();
158158
Class phoneViewType = dataHashMap.get(RaveConstants.fieldPhone).getViewType();
159159

160-
if (!amountValidator.isAmountValid(amount)) {
160+
if (!amountValidator.isAmountValid(Double.valueOf(amount))) {
161161
valid = false;
162162
mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
163163
}

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,7 @@
22

33
public class AmountValidator {
44

5-
public boolean isAmountValid(Double amount){
6-
if (amount != null && !amount.toString().isEmpty()){
7-
return amount > 0;
8-
}
9-
else
10-
{
11-
return false;
12-
}
13-
}
14-
15-
public boolean isAmountValid(String amount){
16-
if (amount != null && !amount.toString().isEmpty()){
17-
return Double.valueOf(amount) > 0;
18-
}
19-
else
20-
{
21-
return false;
22-
}
5+
public boolean isAmountValid(Double amount) {
6+
return amount > 0;
237
}
248
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.flutterwave.raveandroid.validators;
22

3+
import android.util.Patterns;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
38
public class EmailValidator {
49

510
public boolean isEmailValid(String email) {
6-
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
11+
Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
12+
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(email);
13+
return matcher.find();
714
}
815
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.flutterwave.raveandroid.validators;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.*;
10+
11+
@RunWith(JUnit4.class)
12+
public class AmountValidatorTest {
13+
14+
AmountValidator SUT;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
SUT = new AmountValidator();
19+
}
20+
21+
@Test
22+
public void isAmountvalid_isCorrectAmountPassed_returnsTrue(){
23+
Double amount = 1.0;
24+
boolean isAmountValid = SUT.isAmountValid(amount);
25+
assertThat(true, is(isAmountValid));
26+
}
27+
28+
@Test
29+
public void isAmountvalid_isNegativeAmountPassed_returnsFalse(){
30+
Double amount = -1.0;
31+
boolean isAmountValid = SUT.isAmountValid(amount);
32+
assertThat(false, is(isAmountValid));
33+
}
34+
35+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.flutterwave.raveandroid.validators;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.*;
10+
11+
@RunWith(JUnit4.class)
12+
public class EmailValidatorTest {
13+
14+
private EmailValidator SUT;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
SUT = new EmailValidator();
19+
}
20+
21+
@Test
22+
public void isEmailValid_isCorrectEmailPassed_returnsTrue(){
23+
String email = "test@flutterwave.com";
24+
boolean isEmailValid = SUT.isEmailValid(email);
25+
assertThat(true, is(isEmailValid));
26+
}
27+
28+
@Test
29+
public void isEmailValid_isEmptyUsernamePassed_returnsFalse(){
30+
String email = "@flutterwave.com";
31+
boolean isEmailValid = SUT.isEmailValid(email);
32+
assertThat(false, is(isEmailValid));
33+
}
34+
35+
@Test
36+
public void isEmailValid_isEmptyDomainPassed_returnsFalse(){
37+
String email = "test@.com";
38+
boolean isEmailValid = SUT.isEmailValid(email);
39+
assertThat(false, is(isEmailValid));
40+
}
41+
42+
@Test
43+
public void isEmailValid_isTopLevelLessthan2DomainPassed_returnsFalse(){
44+
String email = "test@flutterwave.c";
45+
boolean isEmailValid = SUT.isEmailValid(email);
46+
assertThat(false, is(isEmailValid));
47+
}
48+
49+
@Test
50+
public void isEmailValid_isTopLevelMorethan6DomainPassed_returnsFalse(){
51+
String email = "test@flutterwave.commmmm";
52+
boolean isEmailValid = SUT.isEmailValid(email);
53+
assertThat(false, is(isEmailValid));
54+
}
55+
56+
@Test
57+
public void isEmailValid_isEmptyTopLevelDomainPassed_returnsFalse(){
58+
String email = "test@flutterwave";
59+
boolean isEmailValid = SUT.isEmailValid(email);
60+
assertThat(false, is(isEmailValid));
61+
}
62+
63+
@Test
64+
public void isEmailValid_isEmptyEmailPassed_returnsFalse(){
65+
String email = "";
66+
boolean isEmailValid = SUT.isEmailValid(email);
67+
assertThat(false, is(isEmailValid));
68+
}
69+
70+
@Test
71+
public void isEmailValid_isNotEmailPassed_returnsFalse(){
72+
String email = "1./&$)";
73+
boolean isEmailValid = SUT.isEmailValid(email);
74+
assertThat(false, is(isEmailValid));
75+
}
76+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.flutterwave.raveandroid.validators;
2+
3+
import org.junit.Before;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class PhoneValidatorTest {
8+
9+
@Before
10+
public void setUp() throws Exception {
11+
}
12+
}

0 commit comments

Comments
 (0)