2015-11-28 23:06:14 +01:00
|
|
|
package ninja.javafx.smartcsv.validation;
|
|
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
2016-09-12 04:48:22 +02:00
|
|
|
import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration;
|
2015-11-28 23:06:14 +01:00
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.runners.Parameterized;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2016-02-02 03:20:14 +01:00
|
|
|
import java.util.List;
|
2015-11-28 23:06:14 +01:00
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
2016-02-02 03:20:14 +01:00
|
|
|
import static java.util.stream.Collectors.joining;
|
2015-12-17 20:44:07 +01:00
|
|
|
import static org.hamcrest.Matchers.contains;
|
2015-11-28 23:06:14 +01:00
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* unit test for validator
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(Parameterized.class)
|
|
|
|
|
public class ValidatorTest {
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameters
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-02-02 03:20:14 +01:00
|
|
|
private ValidationConfiguration config;
|
2015-11-28 23:06:14 +01:00
|
|
|
private String column;
|
|
|
|
|
private String value;
|
|
|
|
|
private Boolean expectedResult;
|
2015-12-17 21:15:37 +01:00
|
|
|
private ValidationMessage expectedError;
|
2015-11-28 23:06:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// subject under test
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
private Validator sut;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameterized constructor
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-02-02 03:20:14 +01:00
|
|
|
public ValidatorTest(String config,
|
2015-11-28 23:06:14 +01:00
|
|
|
String column,
|
|
|
|
|
String value,
|
|
|
|
|
Boolean expectedResult,
|
2015-12-17 21:15:37 +01:00
|
|
|
ValidationMessage expectedError) {
|
2016-02-02 03:20:14 +01:00
|
|
|
System.out.println(config);
|
|
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
|
this.config = gson.fromJson(config, ValidationConfiguration.class);
|
2015-11-28 23:06:14 +01:00
|
|
|
this.column = column;
|
|
|
|
|
this.value = value;
|
|
|
|
|
this.expectedResult = expectedResult;
|
|
|
|
|
this.expectedError = expectedError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// init
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Before
|
|
|
|
|
public void initialize() {
|
|
|
|
|
sut = new Validator(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// tests
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Test
|
|
|
|
|
public void validation() {
|
2016-08-07 16:57:55 +02:00
|
|
|
System.out.println(column + " " + value + " " + expectedResult + " " + expectedError);
|
|
|
|
|
|
2015-11-28 23:06:14 +01:00
|
|
|
// execution
|
2016-08-07 16:57:55 +02:00
|
|
|
ValidationError result = sut.isValid(0, column, value);
|
2015-11-28 23:06:14 +01:00
|
|
|
|
|
|
|
|
// assertion
|
2015-12-07 22:41:59 +01:00
|
|
|
assertThat(result == null, is(expectedResult));
|
2015-11-28 23:06:14 +01:00
|
|
|
if (!expectedResult) {
|
2015-12-17 20:44:07 +01:00
|
|
|
assertThat(result.getMessages(), contains(expectedError));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameters for tests
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Parameterized.Parameters
|
|
|
|
|
public static Collection validationConfigurations() {
|
|
|
|
|
return asList(new Object[][] {
|
2016-02-02 03:20:14 +01:00
|
|
|
{ json("column", "not empty", true), "column", "value", true, null },
|
|
|
|
|
{ json("column", "not empty", true), "column", "", false, new ValidationMessage("validation.message.not.empty") },
|
|
|
|
|
{ json("column", "not empty", true), "column", null, false, new ValidationMessage("validation.message.not.empty") },
|
|
|
|
|
{ json("column", "integer", true), "column", "999", true, null },
|
|
|
|
|
{ json("column", "integer", true), "column", "a", false, new ValidationMessage("validation.message.integer") },
|
|
|
|
|
{ json("column", "double", true), "column", "999", true, null },
|
|
|
|
|
{ json("column", "double", true), "column", "999.000", true, null },
|
|
|
|
|
{ json("column", "double", true), "column", "a", false, new ValidationMessage("validation.message.double") },
|
|
|
|
|
{ json("column", "minlength", 2), "column", "12", true, null },
|
|
|
|
|
{ json("column", "minlength", 2), "column", "1", false, new ValidationMessage("validation.message.min.length", "2") },
|
|
|
|
|
{ json("column", "maxlength", 2), "column", "12", true, null },
|
|
|
|
|
{ json("column", "maxlength", 2), "column", "123", false, new ValidationMessage("validation.message.max.length", "2") },
|
|
|
|
|
{ json("column", "date", "yyyyMMdd"), "column", "20151127", true, null },
|
|
|
|
|
{ json("column", "date", "yyyyMMdd"), "column", "27.11.2015", false, new ValidationMessage("validation.message.date.format", "yyyyMMdd") },
|
|
|
|
|
{ json("column", "alphanumeric", true), "column", "abcABC123", true, null },
|
|
|
|
|
{ json("column", "alphanumeric", true), "column", "-abcABC123", false, new ValidationMessage("validation.message.alphanumeric") },
|
|
|
|
|
{ json("column", "regexp", "[a-z]*"), "column", "abc", true, null },
|
|
|
|
|
{ json("column", "regexp", "[a-z]*"), "column", "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") },
|
|
|
|
|
{ json("column", "groovy", "value.contains('a')? 'true' : 'no a inside'"), "column", "abcdef", true, null },
|
|
|
|
|
{ json("column", "groovy", "value.contains('a')? 'true' : 'no a inside'"), "column", "bcdefg", false, new ValidationMessage("no a inside") },
|
|
|
|
|
{ json("column", "value of", asList("a","b","c","d","e")), "column", "c", true, null },
|
|
|
|
|
{ json("column", "value of", asList("a","b","c","d","e")), "column", "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") },
|
2015-11-28 23:06:14 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
public static String json(String column, String rule, Object value) {
|
2016-08-07 16:57:55 +02:00
|
|
|
String json = "{\"headers\": { \"list\": [\""+column+"\"]},\"columns\":{\"" + column + "\":{\"" + rule + "\":";
|
2016-02-02 03:20:14 +01:00
|
|
|
if (value instanceof String) {
|
|
|
|
|
json += "\""+ value + "\"";
|
|
|
|
|
} else if (value instanceof List) {
|
|
|
|
|
List<String> list = (List<String>)value;
|
|
|
|
|
json += "[" + list.stream().collect(joining(", ")) + "]";
|
|
|
|
|
} else {
|
|
|
|
|
json += value;
|
|
|
|
|
}
|
|
|
|
|
json += "}}}";
|
|
|
|
|
return json;
|
|
|
|
|
}
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|