Browse Source

Test case written for validation rule

master
Preyea Regmi 5 years ago
parent
commit
9115ae2a41
  1. 142
      app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/model/recipientaddV3/ValidationRuleModel.java
  2. 6
      app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/presenter/recipientaddV3/AddRecipientV3ValidationExecutor.java
  3. 2
      app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/presenter/recipientaddV3/RecipientAddV3Presenter.java
  4. 14
      app/src/test/java/com/gmeremit/online/gmeremittance_native/AddRecipientV3ValidationExecutorTester.java
  5. 120
      app/src/test/java/com/gmeremit/online/gmeremittance_native/ValidationRuleTest.java

142
app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/model/recipientaddV3/ValidationRuleModel.java

@ -5,21 +5,45 @@ import com.google.gson.annotations.SerializedName;
public class ValidationRuleModel {
@SerializedName("Field Id")
@Expose
private Integer filedId;
@SerializedName("Field")
@Expose
private String field;
@SerializedName("FieldRequired")
@Expose
private String fieldRequired;
@SerializedName("FieldLength")
private Boolean fieldRequired;
@SerializedName("MaxFieldLength")
@Expose
private Integer maxFieldLength;
@SerializedName("MinFieldLength")
@Expose
private int maxfieldLength;
@SerializedName("AllowSpecialCharacter")
private Integer minFieldLength;
@SerializedName("KeyboardType")
@Expose
private boolean allowSpecialCharacter;
@SerializedName("AllowNumber")
private String keyboardType;
@SerializedName("LocalKeyboardAllow")
@Expose
private boolean allowNumber;
private Boolean localKeyboardAllow;
public ValidationRuleModel(Integer filedId, String field, Boolean fieldRequired, Integer maxFieldLength, Integer minFieldLength, String keyboardType, Boolean localKeyboardAllow) {
this.filedId = filedId;
this.field = field;
this.fieldRequired = fieldRequired;
this.maxFieldLength = maxFieldLength;
this.minFieldLength = minFieldLength;
this.keyboardType = keyboardType;
this.localKeyboardAllow = localKeyboardAllow;
}
public Integer getFiledId() {
return filedId;
}
public void setFiledId(Integer filedId) {
this.filedId = filedId;
}
public String getField() {
return field;
@ -29,72 +53,100 @@ public class ValidationRuleModel {
this.field = field;
}
public String getFieldRequired() {
public Boolean isFieldRequired() {
return fieldRequired;
}
public void setFieldRequired(String fieldRequired) {
public void setFieldRequired(Boolean fieldRequired) {
this.fieldRequired = fieldRequired;
}
public int getMaxfieldLength() {
return maxfieldLength;
public Integer getMaxFieldLength() {
return maxFieldLength;
}
public void setMaxfieldLength(int maxfieldLength) {
this.maxfieldLength = maxfieldLength;
public void setMaxFieldLength(Integer maxFieldLength) {
this.maxFieldLength = maxFieldLength;
}
public boolean getAllowSpecialCharacter() {
return allowSpecialCharacter;
public Integer getMinFieldLength() {
return minFieldLength;
}
public void setAllowSpecialCharacter(boolean allowSpecialCharacter) {
this.allowSpecialCharacter = allowSpecialCharacter;
public void setMinFieldLength(Integer minFieldLength) {
this.minFieldLength = minFieldLength;
}
public boolean getAllowNumber() {
return allowNumber;
public String getKeyboardType() {
return keyboardType;
}
public void setAllowNumber(boolean allowNumber) {
this.allowNumber = allowNumber;
public void setKeyboardType(String keyboardType) {
this.keyboardType = keyboardType;
}
public ValidationRuleModel(String field, String fieldRequired, int fieldLength, boolean allowSpecialCharacter, boolean allowNumber) {
this.field = field;
this.fieldRequired = fieldRequired;
this.maxfieldLength = fieldLength;
this.allowSpecialCharacter = allowSpecialCharacter;
this.allowNumber = allowNumber;
public Boolean getLocalKeyboardAllow() {
return localKeyboardAllow;
}
public boolean shouldShowView() {
return "M".equalsIgnoreCase(fieldRequired);
public void setLocalKeyboardAllow(Boolean localKeyboardAllow) {
this.localKeyboardAllow = localKeyboardAllow;
}
public boolean shouldallowSpecialChracter() {
return allowSpecialCharacter;
public String getFieldIsRequiredErrorMessage() {
return "";
}
public boolean shouldAllowNumber() {
return allowNumber;
//
// public boolean validateValueAndReturnErrorMessageOnFail(String value) {
// if (fieldRequired) {
//
// //maxFieldLength is applied
// if (maxfieldLength > 0) {
// //value length is within the bound
// return value.length() <= maxfieldLength&&value.length()>0;
// } else {
// return value.length() > 0;
// }
// }
// //If field is not required then just relay the value, backend will handle
// return true;
// }
public String validateValueAndReturnErrorMessageOnFail(String value) {
if (isFieldRequired()) {
if (value == null || value.length() == 0)
return getFieldIsRequiredErrorMessage();
else {
String result = validateMinLengthAndReturnErrorMessageOnFail(value);
if (result == null) {
if (value.length() <= maxFieldLength)
return null;
else
return getFieldLengthIsGreaterThanUpperBoundDefinedErrorMessage();
} else
return result;
}
} else {
return null;
}
}
private String validateMinLengthAndReturnErrorMessageOnFail(String value) {
if (value.length() > minFieldLength)
return null;
else
return getFieldLengthIsLessThanLowerBoundDefinedErrorMessage();
}
public boolean isValueValid(String value) {
if (shouldShowView()) {
public String getFieldLengthIsLessThanLowerBoundDefinedErrorMessage() {
return null;
}
//maxFieldLength is applied
if (maxfieldLength > 0) {
//value length is within the bound
return value.length() <= maxfieldLength&&value.length()>0;
} else {
return value.length() > 0;
}
}
//If field is not required then just relay the value, backend will handle
return true;
public String getFieldLengthIsGreaterThanUpperBoundDefinedErrorMessage() {
return null;
}
}

6
app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/presenter/recipientaddV3/AddRecipientV3ValidationExecutor.java

@ -31,7 +31,7 @@ public class AddRecipientV3ValidationExecutor {
case FIELD_FULL_NAME:
view.updateValueToWidgetFullName(recipientInfo.getName());
view.updateValidationRuleToWidgetFullName(rule.shouldShowView(), rule.shouldallowSpecialChracter(), rule.getMaxfieldLength());
view.updateValidationRuleToWidgetFullName(rule.isFieldRequired(), true, 50);
validationRuleModelMap.put(FIELD_FULL_NAME,rule);
break;
}
@ -41,7 +41,7 @@ public class AddRecipientV3ValidationExecutor {
public void validateFullName(String fullName)
{
ValidationRuleModel validationRuleModel=validationRuleModelMap.get(FIELD_FULL_NAME);
if(validationRuleModel!=null&&validationRuleModel.isValueValid(fullName)) {
if(validationRuleModel!=null&&validationRuleModel.validateValueAndReturnErrorMessageOnFail(fullName)==null) {
recipientInfo.setName(fullName);
view.setErrorOnWidgetFullName(null);
validateAll();
@ -56,7 +56,7 @@ public class AddRecipientV3ValidationExecutor {
private void validateAll() {
/**
* Iterate through all validation rule hasmap and isValueValid with correspongind receiver info field
* Iterate through all validation rule hasmap and validateValueAndReturnErrorMessageOnFail with correspongind receiver info field
*
*/
}

2
app/src/main/java/com/gmeremit/online/gmeremittance_native/recipientV3/presenter/recipientaddV3/RecipientAddV3Presenter.java

@ -48,7 +48,7 @@ public class RecipientAddV3Presenter extends BasePresenter implements RecipientA
private void mockValidationRules() {
List<ValidationRuleModel> rules=new ArrayList<>();
rules.add(new ValidationRuleModel("Full Name","M",10,true,true));
// rules.add(new ValidationRuleModel("Full Name","M",10,true,true));
this.availableDynamicValidaitonRule=rules;
new Handler().postDelayed(()-> applyValidationRulesToView(),1500);
}

14
app/src/test/java/com/gmeremit/online/gmeremittance_native/AddRecipientV3ValidationExecutorTester.java

@ -11,7 +11,6 @@ import org.mockito.Mockito;
public class AddRecipientV3ValidationExecutorTester {
public static final ValidationRuleModel FULL_NAME_VALIDATION_RULE_REQUIRED_ONLY_ASCII=new ValidationRuleModel("Full Name","M",10,true,true);
@Test(expected = IllegalArgumentException.class)
public void constructor_Should_Throw_IAE_For_Null_Parameter()
@ -31,21 +30,8 @@ public class AddRecipientV3ValidationExecutorTester {
validationExecutor.applyDynamicValidationRuleToView(null);
}
@Test
public void fullname_validation_view_required_should_apply_to_corresponding_view()
{
RecipientAddV3PresenterInterface.RecipientAddV3ContractInterface view=Mockito.mock(RecipientAddV3PresenterInterface.RecipientAddV3ContractInterface.class);
ReceiverInfoV3Model receiverInfoV3Model= Mockito.mock(ReceiverInfoV3Model.class);
AddRecipientV3ValidationExecutor validationExecutor= new AddRecipientV3ValidationExecutor(view,receiverInfoV3Model);
validationExecutor.applyDynamicValidationRuleToView(FULL_NAME_VALIDATION_RULE_REQUIRED_ONLY_ASCII);
Mockito.verify(view).updateValidationRuleToWidgetFullName(false,true,0);
}
}

120
app/src/test/java/com/gmeremit/online/gmeremittance_native/ValidationRuleTest.java

@ -0,0 +1,120 @@
package com.gmeremit.online.gmeremittance_native;
import com.gmeremit.online.gmeremittance_native.recipientV3.model.recipientaddV3.ValidationRuleModel;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ValidationRuleTest {
public static final String SOME_RANDOM_STRING_VALUE = "Some random value";
public static final String EMPTY_STRING_VALUE = "";
public static final String EMPTY_NULL_VALUE = null;
public static final String SOME_RANDOM_STRING_WITH_KNOWN_LENGTH = "So Fine";
public static final int FIELD_LENGTH_LOWER_BOUND=3;
public static final String SOME_RANDOM_STRING_WITH_LENGTH_LESS_THAN_LOWER_BOUND="So";
public static final String SOME_RANDOM_STRING_WITH_LENGTH_GREATER_THAN_UPPER_BOUND="So Fine, Is it";
public static final int FIELD_LENGTH_UPPER_BOUND=5;
public static final String FIELD_IS_REQUIRED_ERROR_MESSAGE = "Field is required";
public static final String FIELD_LENGTH_IS_LESS_ERROR_MESSAGE = "Field length is less than expected";
public static final String FIELD_LENGTH_IS_GREATER_ERROR_MESSAGE = "Field length is greater than expected";
public static final boolean FIELD_REQUIRED_FALSE = false;
public static final boolean FIELD_REQUIRED_TRUE = true;
@Test
public void validateAll_Should_Return_NULL_When_Field_Required_Flag_Is_False() {
ValidationRuleModel validationRuleWithFieldRequiredFlagFalse = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_FALSE, -1, 2, "", false);
assertNull("Should return NULL when field required flag is " + FIELD_REQUIRED_FALSE, validationRuleWithFieldRequiredFlagFalse.validateValueAndReturnErrorMessageOnFail(SOME_RANDOM_STRING_VALUE));
}
@Test
public void validateAll_Should_Return_Field_Is_Required_Error_Message_When_Field_Required_Flag_Is_True() {
ValidationRuleModel validationRuleWithFieldRequiredFlagTrue = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_TRUE, -1, 2, "", false);
ValidationRuleModel rule = Mockito.spy(validationRuleWithFieldRequiredFlagTrue );
Mockito.when(rule.getFieldIsRequiredErrorMessage()).thenReturn(FIELD_IS_REQUIRED_ERROR_MESSAGE);
assertEquals("Should return field require error message: " + FIELD_IS_REQUIRED_ERROR_MESSAGE,
FIELD_IS_REQUIRED_ERROR_MESSAGE, rule.validateValueAndReturnErrorMessageOnFail(EMPTY_STRING_VALUE)
);
assertEquals("Should return field require error message: " + FIELD_IS_REQUIRED_ERROR_MESSAGE,
FIELD_IS_REQUIRED_ERROR_MESSAGE, rule.validateValueAndReturnErrorMessageOnFail(EMPTY_NULL_VALUE)
);
}
@Test
public void validateAll_Should_Return_NULL_When_Passed_Value_Is_Greater_Or_Equal_To_Min_Val_Required() {
ValidationRuleModel validationRuleWithFieldRequiredFlagTrue = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_TRUE, -1, FIELD_LENGTH_LOWER_BOUND, "", false);
// ValidationRuleModel rule = Mockito.spy(validationRuleWithFieldRequiredFlagTrue );
//
// Mockito.when(rule.getFieldIsRequiredErrorMessage()).thenReturn(FIELD_IS_REQUIRED_ERROR_MESSAGE);
assertNull("Should return NULL when passes param length i.e. "+ SOME_RANDOM_STRING_WITH_KNOWN_LENGTH.length() +" is greater than "+FIELD_LENGTH_LOWER_BOUND,
validationRuleWithFieldRequiredFlagTrue.validateValueAndReturnErrorMessageOnFail(SOME_RANDOM_STRING_WITH_KNOWN_LENGTH)
);
}
@Test
public void validateAll_Should_Return_Lower_Bound_Error_Message_When_Passed_Value_Is_Less_Than_Min_Val_Required() {
ValidationRuleModel validationRuleWithFieldRequiredFlagTrue = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_TRUE, -1, FIELD_LENGTH_LOWER_BOUND, "", false);
ValidationRuleModel rule = Mockito.spy(validationRuleWithFieldRequiredFlagTrue);
Mockito.when(rule.getFieldLengthIsLessThanLowerBoundDefinedErrorMessage()).thenReturn(FIELD_LENGTH_IS_LESS_ERROR_MESSAGE);
assertEquals(
"Should return lower bound error message: " + FIELD_LENGTH_IS_LESS_ERROR_MESSAGE+ " for data: "+ SOME_RANDOM_STRING_WITH_KNOWN_LENGTH +" with lower bound defined: "+FIELD_LENGTH_LOWER_BOUND,
FIELD_LENGTH_IS_LESS_ERROR_MESSAGE, rule.validateValueAndReturnErrorMessageOnFail(SOME_RANDOM_STRING_WITH_LENGTH_LESS_THAN_LOWER_BOUND)
);
}
@Test
public void validateAll_Should_Return_NULL_When_Passed_Value_Is_Less_Or_Equal_To_Max_Val_Required() {
ValidationRuleModel validationRuleWithFieldRequiredFlagTrue = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_TRUE, FIELD_LENGTH_UPPER_BOUND, FIELD_LENGTH_LOWER_BOUND, "", false);
// ValidationRuleModel rule = Mockito.spy(validationRuleWithFieldRequiredFlagTrue );
//
// Mockito.when(rule.getFieldIsRequiredErrorMessage()).thenReturn(FIELD_IS_REQUIRED_ERROR_MESSAGE);
assertNull("Should return NULL when passes param length i.e. "+ SOME_RANDOM_STRING_WITH_KNOWN_LENGTH.length() +" is lesser or equal to "+FIELD_LENGTH_UPPER_BOUND,
validationRuleWithFieldRequiredFlagTrue.validateValueAndReturnErrorMessageOnFail(SOME_RANDOM_STRING_WITH_KNOWN_LENGTH)
);
}
@Test
public void validateAll_Should_Return_Upper_Bound_Error_Message_When_Passed_Value_Is_Greater_Than_MAx_Val_Required() {
ValidationRuleModel validationRuleWithFieldRequiredFlagTrue = new ValidationRuleModel(0, "Full name", FIELD_REQUIRED_TRUE, FIELD_LENGTH_UPPER_BOUND, FIELD_LENGTH_LOWER_BOUND, "", false);
ValidationRuleModel rule = Mockito.spy(validationRuleWithFieldRequiredFlagTrue);
Mockito.when(rule.getFieldLengthIsGreaterThanUpperBoundDefinedErrorMessage()).thenReturn(FIELD_LENGTH_IS_GREATER_ERROR_MESSAGE);
assertEquals(
"Should return upper bound error message. for Data: \'"+ SOME_RANDOM_STRING_WITH_LENGTH_GREATER_THAN_UPPER_BOUND +"\' length: "+SOME_RANDOM_STRING_WITH_LENGTH_GREATER_THAN_UPPER_BOUND.length()+" with upper bound defined: "+FIELD_LENGTH_UPPER_BOUND,
FIELD_LENGTH_IS_GREATER_ERROR_MESSAGE, rule.validateValueAndReturnErrorMessageOnFail(SOME_RANDOM_STRING_WITH_LENGTH_GREATER_THAN_UPPER_BOUND)
);
}
}
Loading…
Cancel
Save