package io.monokkel.core.utils;
import com.google.common.collect.ImmutableMap;
import io.monokkel.exceptions.ValidationError;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import static io.monokkel.core.utils.MapTransformation.transformAgainstMap;
import static org.junit.Assert.*;
public class ValidatorFunctionTest {
private ValidatorFunction validatorFunction;
@Before
public void before(){
validatorFunction = new ValidatorFunction();
}
@Test
public void transform_withLegalMapAndMatchingExpression_expectToReturnMatchTrue(){
Map<String, Object> validationMap = ImmutableMap.of("field","[a-zA-Z0-9]+");
final Object transform = validatorFunction.transform("field", "thisIsALegalField", validationMap);
assertEquals(true,transform);
}
@Test(expected = ValidationError.class)
public void transform_withLegalMapAndNonMatchingExpression_expectToReturnMatchFalse(){
Map<String, Object> validationMap = ImmutableMap.of("field","[a-zA-Z0-9]+");
validatorFunction.transform("field", "this Is A Non LegalField", validationMap);
}
@Test
public void transform_withNoMatchingFieldsInValidationMap_expectToReturnMatchTrue(){
Map<String, Object> validationMap = ImmutableMap.of("nonMatchingField","[a-zA-Z0-9]+");
final Object transform = validatorFunction.transform("field", "this Is A Non LegalField", validationMap);
assertEquals(true,transform);
}
@Test
public void transform_withMapTransformationTransformAgainstMapAndAllLegalField_expectFieldToBeValidatedTrue(){
final ImmutableMap<String, Object> mapToBeValidated = ImmutableMap.of("field", "thisIsALegalField");
final ImmutableMap<String, Object> expressions = ImmutableMap.of("field", "[a-zA-Z0-9]+");
final Map<String, Object> stringObjectMap = transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
assertEquals(true,stringObjectMap.get("field"));
}
@Test
public void transform_withMapTransformationTransformAgainstMapAndAllLongFields_expectFieldToBeValidatedTrue(){
final ImmutableMap<String, Object> mapToBeValidated = ImmutableMap.of("field", 1);
final ImmutableMap<String, Object> expressions = ImmutableMap.of("field", "[0-9]+");
final Map<String, Object> stringObjectMap = transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
assertEquals(true,stringObjectMap.get("field"));
}
@Test
public void transform_withMapTransformationTransformAgainstMapAndAllDoubleFields_expectFieldToBeValidatedTrue(){
final ImmutableMap<String, Object> mapToBeValidated = ImmutableMap.of("field", 1.0);
final ImmutableMap<String, Object> expressions = ImmutableMap.of("field", "[0-9]+\\.[0-9]+");
final Map<String, Object> stringObjectMap = transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
assertEquals(true,stringObjectMap.get("field"));
}
@Test(expected = ValidationError.class)
public void transform_withMapTransformationTransformAgainstMapAndAllLongFieldsWithDoubleExpression_expectFieldToBeValidatedFalse(){
final ImmutableMap<String, Object> mapToBeValidated = ImmutableMap.of("field", 1);
final ImmutableMap<String, Object> expressions = ImmutableMap.of("field", "[0-9]+\\.[0-9]+");
transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
}
@Test
public void transform_withMapTransformationTransformAgainstMapAndAllBooleanFields_expectFieldToBeValidatedTrue(){
final ImmutableMap<String, Object> mapToBeValidated = ImmutableMap.of("field", true);
final ImmutableMap<String, Object> expressions = ImmutableMap.of("field", "(true)|(false)");
final Map<String, Object> stringObjectMap = transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
assertEquals(true,stringObjectMap.get("field"));
}
@Test
@SuppressWarnings("unchecked")
public void transform_withMapTransformationTransformAgainstMapAndWithSubObjects_expectFieldToBeValidatedTrue(){
final Map<String, Object> mapToBeValidated = ImmutableMap.of("ignore","thisIsAvalidField","subobject", ImmutableMap.of("field","thisIsALegalField"));
final Map<String, Object> expressions = ImmutableMap.of("subobject",ImmutableMap.of("field", "[a-zA-Z0-9]+"));
final Map<String, Object> stringObjectMap = transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
final Map<String,Object> subobject = (Map<String,Object>)stringObjectMap.get("subobject");
assertEquals(true, subobject.get("field"));
}
@Test(expected = ValidationError.class)
@SuppressWarnings("unchecked")
public void transform_withMapTransformationTransformAgainstMapAndWithSubObjects_expectFieldToBeValidatedFalse(){
final Map<String, Object> mapToBeValidated = ImmutableMap.of("subobject", ImmutableMap.of("field","this Is A Nonlegal Field"));
final Map<String, Object> expressions = ImmutableMap.of("subobject",ImmutableMap.of("field", "[a-zA-Z0-9]+"));
transformAgainstMap(mapToBeValidated, expressions, new ValidatorFunction());
}
}