Last active
September 27, 2016 11:47
-
-
Save tranngoclam/2e9ac1441cbb60d269967a7e2d2d04be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.lamtran.rxjava2.validation; | |
/** | |
* Created by lam on 9/25/16. | |
*/ | |
public enum FieldType { | |
USERNAME, PASSWORD, EMAIL, PHONE_NUMBER, CARD_NUMBER | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.lamtran.rxjava2.validation; | |
import android.support.v4.util.Pair; | |
import android.text.TextUtils; | |
import android.util.Patterns; | |
import java.util.ArrayList; | |
import java.util.List; | |
import rx.Observable; | |
/** | |
* Created by lam on 9/25/16. | |
*/ | |
public class RxValidator { | |
private List<Observable<Void>> mObservableList; | |
public RxValidator() { | |
} | |
public void register(Pair<FieldType, String>... pairs) { | |
try { | |
mObservableList = new ArrayList<>(pairs.length); | |
for (Pair<FieldType, String> pair : pairs) { | |
switch (pair.first) { | |
case USERNAME: | |
mObservableList.add(buildUserNameObservable(pair.second)); | |
break; | |
case PASSWORD: | |
mObservableList.add(buildPasswordObservable(pair.second)); | |
break; | |
case EMAIL: | |
mObservableList.add(buildEmailObservable(pair.second)); | |
break; | |
} | |
} | |
} catch (Exception e) { | |
throw new IllegalArgumentException(); | |
} | |
} | |
public Observable<Void> validate() { | |
return Observable.concat(mObservableList) | |
.first(exception -> exception != null); | |
} | |
private Observable<Void> buildEmailObservable(String email) { | |
return Observable.defer(() -> { | |
try { | |
if (!TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches()) { | |
return Observable.just(null); | |
} else { | |
return Observable.error(new InvalidEmailException()); | |
} | |
} catch (Exception e) { | |
return Observable.error(new InvalidEmailException()); | |
} | |
}); | |
} | |
private Observable<Void> buildPasswordObservable(String password) { | |
return Observable.defer(() -> { | |
try { | |
if (!TextUtils.isEmpty(password) && password.length() >= 8) { | |
return Observable.just(null); | |
} else { | |
return Observable.error(new InvalidPasswordException()); | |
} | |
} catch (Exception e) { | |
return Observable.error(new InvalidPasswordException()); | |
} | |
}); | |
} | |
private Observable<Void> buildUserNameObservable(String username) { | |
return Observable.defer(() -> { | |
try { | |
if (!TextUtils.isEmpty(username) && username.length() >= 6 && username.length() <= 10) { | |
return Observable.just(null); | |
} else { | |
return Observable.error(new InvalidUserNameException()); | |
} | |
} catch (Exception e) { | |
return Observable.error(new InvalidUserNameException()); | |
} | |
}); | |
} | |
public static class InvalidEmailException extends Exception { | |
} | |
public static class InvalidPasswordException extends Exception { | |
} | |
public static class InvalidUserNameException extends Exception { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.lamtran.rxjava2; | |
import net.lamtran.rxjava2.validation.FieldType; | |
import net.lamtran.rxjava2.validation.RxValidator; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.robolectric.RobolectricTestRunner; | |
import org.robolectric.annotation.Config; | |
import android.support.v4.util.Pair; | |
import java.util.NoSuchElementException; | |
import rx.observers.TestSubscriber; | |
import static org.hamcrest.CoreMatchers.equalTo; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
/** | |
* Created by lam on 9/26/16. | |
*/ | |
@RunWith(RobolectricTestRunner.class) | |
@Config(constants = BuildConfig.class) | |
public class ValidationTest { | |
private RxValidator mRxValidator; | |
@After | |
public void reset() throws Exception { | |
mRxValidator = null; | |
} | |
@Before | |
public void setup() throws Exception { | |
mRxValidator = new RxValidator(); | |
} | |
@Test | |
public void testAllValid() throws Exception { | |
mRxValidator.register( | |
Pair.create(FieldType.USERNAME, "username"), | |
Pair.create(FieldType.PASSWORD, "password"), | |
Pair.create(FieldType.EMAIL, "[email protected]")); | |
TestSubscriber<Void> testSubscriber = new TestSubscriber<>(); | |
mRxValidator.validate().subscribe(testSubscriber); | |
testSubscriber.assertError(NoSuchElementException.class); | |
} | |
@Test | |
public void testEmailInvalid() throws Exception { | |
mRxValidator.register( | |
Pair.create(FieldType.USERNAME, "username"), | |
Pair.create(FieldType.PASSWORD, "password"), | |
Pair.create(FieldType.EMAIL, "email")); | |
TestSubscriber<Void> testSubscriber = new TestSubscriber<>(); | |
mRxValidator.validate().subscribe(testSubscriber); | |
testSubscriber.assertError(RxValidator.InvalidEmailException.class); | |
assertThat(testSubscriber.getOnErrorEvents().size(), equalTo(1)); | |
} | |
@Test | |
public void testPasswordInvalid() throws Exception { | |
mRxValidator.register( | |
Pair.create(FieldType.USERNAME, "username"), | |
Pair.create(FieldType.PASSWORD, "pwd"), | |
Pair.create(FieldType.EMAIL, "email")); | |
TestSubscriber<Void> testSubscriber = new TestSubscriber<>(); | |
mRxValidator.validate().subscribe(testSubscriber); | |
testSubscriber.assertError(RxValidator.InvalidPasswordException.class); | |
assertThat(testSubscriber.getOnErrorEvents().size(), equalTo(1)); | |
} | |
@Test | |
public void testUserNameAndPasswordInvalid() throws Exception { | |
mRxValidator.register( | |
Pair.create(FieldType.USERNAME, "..."), | |
Pair.create(FieldType.PASSWORD, "password"), | |
Pair.create(FieldType.EMAIL, "email")); | |
TestSubscriber<Void> testSubscriber = new TestSubscriber<>(); | |
mRxValidator.validate().subscribe(testSubscriber); | |
testSubscriber.assertError(RxValidator.InvalidUserNameException.class); | |
assertThat(testSubscriber.getOnErrorEvents().size(), equalTo(1)); | |
} | |
@Test | |
public void testUserNameInvalid() throws Exception { | |
mRxValidator.register( | |
Pair.create(FieldType.USERNAME, "..."), | |
Pair.create(FieldType.PASSWORD, "..."), | |
Pair.create(FieldType.EMAIL, "email")); | |
TestSubscriber<Void> testSubscriber = new TestSubscriber<>(); | |
mRxValidator.validate().subscribe(testSubscriber); | |
testSubscriber.assertError(RxValidator.InvalidUserNameException.class); | |
assertThat(testSubscriber.getOnErrorEvents().size(), equalTo(1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment