Skip to content

Commit 2eedc6a

Browse files
committed
Network Validator Refactor
1 parent 9353d02 commit 2eedc6a

File tree

8 files changed

+518
-49
lines changed

8 files changed

+518
-49
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.flutterwave.raveandroid.validators;
2+
3+
import com.flutterwave.raveandroid.RaveConstants;
4+
5+
import javax.inject.Inject;
6+
7+
public class NetworkValidator {
8+
9+
@Inject
10+
public NetworkValidator() {
11+
}
12+
13+
public boolean isNetworkValid(String network) {
14+
return network != null && network.equalsIgnoreCase(RaveConstants.mtn) && !network.isEmpty();
15+
}
16+
}

raveandroid/src/main/java/com/flutterwave/raveandroid/zmmobilemoney/ZmMobileMoneyFragment.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ private void setListeners() {
105105
private void initializeViews() {
106106
networkSpinner = v.findViewById(R.id.rave_networkSpinner);
107107
instructionsTv = v.findViewById(R.id.instructionsTv);
108-
voucherTil = v.findViewById(R.id.rave_voucherTil);
109-
voucherEt = v.findViewById(R.id.rave_voucherEt);
110108
payButton = v.findViewById(R.id.rave_payButton);
111109
amountTil = v.findViewById(R.id.rave_amountTil);
112110
phoneTil = v.findViewById(R.id.rave_phoneTil);
@@ -175,10 +173,6 @@ private void collectData() {
175173
dataHashMap.put(RaveConstants.fieldPhone, new ViewObject(phoneTil.getId(), phoneEt.getText().toString(), TextInputLayout.class));
176174
dataHashMap.put(RaveConstants.fieldNetwork, new ViewObject(networkSpinner.getId(), String.valueOf(networkSpinner.getSelectedItem()), Spinner.class));
177175

178-
if (voucherTil.getVisibility() == View.VISIBLE) {
179-
dataHashMap.put(RaveConstants.fieldVoucher, new ViewObject(voucherTil.getId(), voucherEt.getText().toString(), TextInputLayout.class));
180-
}
181-
182176
presenter.onDataCollected(dataHashMap);
183177
}
184178

raveandroid/src/main/java/com/flutterwave/raveandroid/zmmobilemoney/ZmMobileMoneyPresenter.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.flutterwave.raveandroid.responses.MobileMoneyChargeResponse;
2020
import com.flutterwave.raveandroid.responses.RequeryResponse;
2121
import com.flutterwave.raveandroid.validators.AmountValidator;
22+
import com.flutterwave.raveandroid.validators.NetworkValidator;
2223
import com.flutterwave.raveandroid.validators.PhoneValidator;
2324

2425
import java.util.HashMap;
@@ -35,7 +36,6 @@
3536
import static com.flutterwave.raveandroid.RaveConstants.validAmountPrompt;
3637
import static com.flutterwave.raveandroid.RaveConstants.validNetworkPrompt;
3738
import static com.flutterwave.raveandroid.RaveConstants.validPhonePrompt;
38-
import static com.flutterwave.raveandroid.RaveConstants.validVoucherPrompt;
3939

4040
/**
4141
* Created by hfetuga on 28/06/2018.
@@ -49,6 +49,8 @@ public class ZmMobileMoneyPresenter implements ZmMobileMoneyContract.UserActions
4949
@Inject
5050
PhoneValidator phoneValidator;
5151
@Inject
52+
NetworkValidator networkValidator;
53+
@Inject
5254
DeviceIdGetter deviceIdGetter;
5355
private Context context;
5456
private ZmMobileMoneyContract.View mView;
@@ -217,24 +219,11 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
217219
String phone = dataHashMap.get(fieldPhone).getData();
218220
Class phoneViewType = dataHashMap.get(fieldPhone).getViewType();
219221

220-
ViewObject voucherViewObject = dataHashMap.get(fieldVoucher);
221-
222-
if (voucherViewObject != null) {
223-
int voucherID = dataHashMap.get(fieldVoucher).getViewId();
224-
String voucher = dataHashMap.get(fieldVoucher).getData();
225-
Class voucherViewType = dataHashMap.get(fieldVoucher).getViewType();
226-
227-
if (voucher.isEmpty()) {
228-
valid = false;
229-
mView.showFieldError(voucherID, validVoucherPrompt, voucherViewType);
230-
}
231-
232-
}
233-
234222
String network = dataHashMap.get(fieldNetwork).getData();
235223

236224
boolean isAmountValidated = amountValidator.isAmountValid(amount);
237225
boolean isPhoneValid = phoneValidator.isPhoneValid(phone);
226+
boolean isNetworkValid = networkValidator.isNetworkValid(network);
238227

239228
if (!isAmountValidated) {
240229
valid = false;
@@ -246,7 +235,7 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
246235
mView.showFieldError(phoneID, validPhonePrompt, phoneViewType);
247236
}
248237

249-
if (network.equals(RaveConstants.selectNetwork)) {
238+
if (!isNetworkValid) {
250239
valid = false;
251240
mView.showToast(validNetworkPrompt);
252241
}

raveandroid/src/main/res/layout/fragment_zm_mobile_money.xml

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,13 @@
3232
android:layout_marginBottom="15dp"
3333
android:text="@string/pay" />
3434

35+
3536
<Spinner
3637
android:id="@+id/rave_networkSpinner"
3738
android:layout_width="match_parent"
3839
android:layout_height="50dp"
3940
android:layout_marginBottom="10dp" />
4041

41-
42-
<TextView
43-
android:id="@+id/instructionsTv"
44-
android:layout_width="wrap_content"
45-
android:layout_height="wrap_content"
46-
android:layout_marginBottom="20dp"
47-
android:gravity="left"
48-
tools:text="@string/vodafone_msg" />
49-
5042
<android.support.design.widget.TextInputLayout
5143
android:id="@+id/rave_phoneTil"
5244
android:layout_width="match_parent"
@@ -63,23 +55,6 @@
6355

6456
</android.support.design.widget.TextInputLayout>
6557

66-
<android.support.design.widget.TextInputLayout
67-
android:id="@+id/rave_voucherTil"
68-
android:layout_width="match_parent"
69-
android:layout_height="wrap_content"
70-
android:layout_marginBottom="20dp">
71-
72-
<android.support.design.widget.TextInputEditText
73-
android:id="@+id/rave_voucherEt"
74-
android:layout_width="match_parent"
75-
android:layout_height="wrap_content"
76-
android:layout_below="@+id/rave_card1"
77-
android:hint="Voucher"
78-
android:inputType="text" />
79-
80-
</android.support.design.widget.TextInputLayout>
81-
82-
8358
<android.support.design.widget.TextInputLayout
8459
android:id="@+id/rave_amountTil"
8560
android:layout_width="match_parent"

raveandroid/src/test/java/com/flutterwave/raveandroid/di/TestAndroidModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.flutterwave.raveandroid.validators.CvvValidator;
1717
import com.flutterwave.raveandroid.validators.DateOfBirthValidator;
1818
import com.flutterwave.raveandroid.validators.EmailValidator;
19+
import com.flutterwave.raveandroid.validators.NetworkValidator;
1920
import com.flutterwave.raveandroid.validators.PhoneValidator;
2021
import com.flutterwave.raveandroid.validators.UrlValidator;
2122

@@ -53,13 +54,18 @@ public EmailValidator providesEmailValidator() {
5354
return Mockito.mock(EmailValidator.class);
5455
}
5556

56-
5757
@Provides
5858
@Singleton
5959
public PhoneValidator providesPhoneValidator() {
6060
return Mockito.mock(PhoneValidator.class);
6161
}
6262

63+
@Provides
64+
@Singleton
65+
public NetworkValidator providesNetworkValidator() {
66+
return Mockito.mock(NetworkValidator.class);
67+
}
68+
6369

6470
@Provides
6571
@Singleton

raveandroid/src/test/java/com/flutterwave/raveandroid/di/TestAppComponent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.flutterwave.raveandroid.mpesa.MpesaPresenterTest;
1313
import com.flutterwave.raveandroid.ugmobilemoney.UgMobileMoneyPresenter;
1414
import com.flutterwave.raveandroid.ugmobilemoney.UgMobileMoneyPresenterTest;
15+
import com.flutterwave.raveandroid.zmmobilemoney.ZmMobileMoneyPresenter;
16+
import com.flutterwave.raveandroid.zmmobilemoney.ZmMobileMoneyPresenterTest;
1517

1618
import javax.inject.Singleton;
1719

@@ -31,6 +33,8 @@ public interface TestAppComponent extends AppComponent {
3133

3234
void inject(GhMobileMoneyPresenterTest ghMobileMoneyPresenterTest);
3335

36+
void inject(ZmMobileMoneyPresenterTest zmMobileMoneyPresenterTest);
37+
3438
void inject(TransactionStatusCheckerTest transactionStatusCheckerTest);
3539

3640
void inject(CardPresenter cardPresenter);
@@ -43,4 +47,6 @@ public interface TestAppComponent extends AppComponent {
4347

4448
void inject(GhMobileMoneyPresenter ghMobileMoneyPresenter);
4549

50+
void inject(ZmMobileMoneyPresenter zmMobileMoneyPresenter);
51+
4652
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 NetworkValidatorTest {
10+
11+
NetworkValidator SUT;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
SUT = new NetworkValidator();
16+
}
17+
18+
@Test
19+
public void isNetworkValid_isCorrectNetworkPassed_returnTrue() {
20+
String network = "mtn";
21+
boolean isNetworkValid = SUT.isNetworkValid(network);
22+
assertThat(true, is(isNetworkValid));
23+
}
24+
25+
@Test
26+
public void isNetworkInValid_isEmptyPassed_returnFalse() {
27+
String network = "";
28+
boolean isNetworkValid = SUT.isNetworkValid(network);
29+
assertThat(false, is(isNetworkValid));
30+
}
31+
32+
@Test
33+
public void isNetworkInValid_isNoNetworkPassed_returnFalse() {
34+
String network = "Select network";
35+
boolean isNetworkValid = SUT.isNetworkValid(network);
36+
assertThat(false, is(isNetworkValid));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)