mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
first steps for header validation
This commit is contained in:
@@ -89,6 +89,7 @@ public class CSVModel {
|
|||||||
*/
|
*/
|
||||||
public void setHeader(String[] header) {
|
public void setHeader(String[] header) {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
|
revalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,6 +105,10 @@ public class CSVModel {
|
|||||||
* walks through the data and validates each value
|
* walks through the data and validates each value
|
||||||
*/
|
*/
|
||||||
private void revalidate() {
|
private void revalidate() {
|
||||||
|
if (header != null) {
|
||||||
|
validator.isHeaderValid(header);
|
||||||
|
}
|
||||||
|
|
||||||
for (CSVRow row: rows) {
|
for (CSVRow row: rows) {
|
||||||
row.setValidator(validator);
|
row.setValidator(validator);
|
||||||
for (String column: row.getColumns().keySet()) {
|
for (String column: row.getColumns().keySet()) {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import groovy.lang.Script;
|
|||||||
import org.codehaus.groovy.control.CompilationFailedException;
|
import org.codehaus.groovy.control.CompilationFailedException;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.apache.commons.validator.GenericValidator.*;
|
import static org.apache.commons.validator.GenericValidator.*;
|
||||||
@@ -78,7 +79,9 @@ public class Validator {
|
|||||||
public ValidationState isValid(String column, String value) {
|
public ValidationState isValid(String column, String value) {
|
||||||
ValidationState result = new ValidationState();
|
ValidationState result = new ValidationState();
|
||||||
if (validationConfig != null) {
|
if (validationConfig != null) {
|
||||||
Config columnConfig = getColumnConfig(column);
|
Config columnSectionConfig = getColumnSectionConfig();
|
||||||
|
if (columnSectionConfig != null) {
|
||||||
|
Config columnConfig = getColumnConfig(columnSectionConfig, column);
|
||||||
if (columnConfig != null) {
|
if (columnConfig != null) {
|
||||||
checkBlankOrNull(columnConfig, value, result);
|
checkBlankOrNull(columnConfig, value, result);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
@@ -92,6 +95,7 @@ public class Validator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,10 +200,15 @@ public class Validator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Config getColumnConfig(String column) {
|
private Config getColumnSectionConfig() {
|
||||||
return validationConfig.hasPath(column) ? validationConfig.getConfig(column) : null;
|
return validationConfig.hasPath("columns") ? validationConfig.getConfig("columns") : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Config getColumnConfig(Config columnSectionConfig, String column) {
|
||||||
|
return columnSectionConfig.hasPath(column) ? columnSectionConfig.getConfig(column) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private String getString(Config columnConfig, String path) {
|
private String getString(Config columnConfig, String path) {
|
||||||
return columnConfig.hasPath(path) ? columnConfig.getString(path) : null;
|
return columnConfig.hasPath(path) ? columnConfig.getString(path) : null;
|
||||||
}
|
}
|
||||||
@@ -213,4 +222,38 @@ public class Validator {
|
|||||||
return columnConfig.hasPath(path) && columnConfig.getBoolean(path);
|
return columnConfig.hasPath(path) && columnConfig.getBoolean(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ValidationState isHeaderValid(String[] headerNames) {
|
||||||
|
ValidationState result = new ValidationState();
|
||||||
|
if (validationConfig != null) {
|
||||||
|
if (validationConfig.hasPath("headers")) {
|
||||||
|
Config headerSectionConfig = validationConfig.getConfig("headers");
|
||||||
|
if (headerSectionConfig.hasPath("list")) {
|
||||||
|
List<String> headerConfig = headerSectionConfig.getStringList("list");
|
||||||
|
if (headerConfig != null) {
|
||||||
|
if (headerNames.length != headerConfig.size()) {
|
||||||
|
result.invalidate("number of headers is not correct! there are " +
|
||||||
|
headerNames.length +
|
||||||
|
" but there should be " +
|
||||||
|
headerConfig.size());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=0; i<headerConfig.size(); i++) {
|
||||||
|
String header = headerConfig.get(i);
|
||||||
|
if (!header.equals(headerNames[i])) {
|
||||||
|
result.invalidate("header number " +
|
||||||
|
i +
|
||||||
|
" does not match \"" +
|
||||||
|
header +
|
||||||
|
"\" should be \"" +
|
||||||
|
headerNames[i] +
|
||||||
|
"\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package ninja.javafx.smartcsv.validation;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class ConfigMock {
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constants
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private static final String HEADER_SECTION_KEY = "headers";
|
||||||
|
private static final String COLUMN_SECTION_KEY = "columns";
|
||||||
|
private static final String LIST_KEY = "list";
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// mocks
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
public static Config headerSectionConfig(String[] headerNames) {
|
||||||
|
|
||||||
|
Config headerSectionConfig = mock(Config.class);
|
||||||
|
Config listConfig = mock(Config.class);
|
||||||
|
|
||||||
|
when(headerSectionConfig.hasPath(HEADER_SECTION_KEY)).thenReturn(true);
|
||||||
|
when(headerSectionConfig.getConfig(HEADER_SECTION_KEY)).thenReturn(listConfig);
|
||||||
|
|
||||||
|
when(listConfig.hasPath(LIST_KEY)).thenReturn(true);
|
||||||
|
when(listConfig.getStringList(LIST_KEY)).thenReturn(asList(headerNames));
|
||||||
|
|
||||||
|
return headerSectionConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Config columnSectionConfig(String column, String validation, Object value) {
|
||||||
|
|
||||||
|
Config columnSectionConfig = mock(Config.class);
|
||||||
|
Config columnConfig = mock(Config.class);
|
||||||
|
Config validatorConfig = mock(Config.class);
|
||||||
|
|
||||||
|
when(columnSectionConfig.hasPath(COLUMN_SECTION_KEY)).thenReturn(true);
|
||||||
|
when(columnSectionConfig.getConfig(COLUMN_SECTION_KEY)).thenReturn(columnConfig);
|
||||||
|
|
||||||
|
when(columnConfig.hasPath(column)).thenReturn(true);
|
||||||
|
when(columnConfig.getConfig(column)).thenReturn(validatorConfig);
|
||||||
|
|
||||||
|
when(validatorConfig.hasPath(validation)).thenReturn(true);
|
||||||
|
if (value instanceof Boolean) {
|
||||||
|
when(validatorConfig.getBoolean(validation)).thenReturn((Boolean) value);
|
||||||
|
} else if (value instanceof String) {
|
||||||
|
when(validatorConfig.getString(validation)).thenReturn((String) value);
|
||||||
|
} else if (value instanceof Integer) {
|
||||||
|
when(validatorConfig.getInt(validation)).thenReturn((Integer) value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnSectionConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Copyright (c) 2015 javafx.ninja <info@javafx.ninja>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
package ninja.javafx.smartcsv.validation;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static ninja.javafx.smartcsv.validation.ConfigMock.headerSectionConfig;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* unit test for header validator
|
||||||
|
*/
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
public class HeaderValidationTest {
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// parameters
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Config config;
|
||||||
|
private Boolean expectedResult;
|
||||||
|
private String expectedError;
|
||||||
|
private String[] headerNames;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// subject under test
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Validator sut;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// parameterized constructor
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
public HeaderValidationTest(String[] configHeaderNames,
|
||||||
|
String[] headerNames,
|
||||||
|
Boolean expectedResult,
|
||||||
|
String expectedError) {
|
||||||
|
this.config = headerSectionConfig(configHeaderNames);
|
||||||
|
this.headerNames = headerNames;
|
||||||
|
this.expectedResult = expectedResult;
|
||||||
|
this.expectedError = expectedError;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// init
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@Before
|
||||||
|
public void initialize() {
|
||||||
|
sut = new Validator(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// tests
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@Test
|
||||||
|
public void validation() {
|
||||||
|
// execution
|
||||||
|
ValidationState result = sut.isHeaderValid(headerNames);
|
||||||
|
|
||||||
|
// assertion
|
||||||
|
assertThat(result.isValid(), is(expectedResult));
|
||||||
|
if (!expectedResult) {
|
||||||
|
assertThat(result.error(), is(expectedError));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// parameters for tests
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@Parameterized.Parameters
|
||||||
|
public static Collection validationConfigurations() {
|
||||||
|
return asList(new Object[][] {
|
||||||
|
{ new String[] {}, new String[] {}, true, null },
|
||||||
|
{ new String[] {"a"}, new String[] {"a"}, true, null },
|
||||||
|
{ new String[] {"a"}, new String[] {"b"}, false, "header number 0 does not match \"a\" should be \"b\"\n" },
|
||||||
|
{ new String[] {"a"}, new String[] {"a","b"}, false, "number of headers is not correct! there are 2 but there should be 1\n" },
|
||||||
|
{ new String[] {"a", "b"}, new String[] {"b", "a"}, false, "header number 0 does not match \"a\" should be \"b\"\nheader number 1 does not match \"b\" should be \"a\"\n" }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,10 +9,9 @@ import org.junit.runners.Parameterized;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
import static ninja.javafx.smartcsv.validation.ConfigMock.columnSectionConfig;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* unit test for validator
|
* unit test for validator
|
||||||
@@ -46,7 +45,7 @@ public class ValidatorTest {
|
|||||||
String value,
|
String value,
|
||||||
Boolean expectedResult,
|
Boolean expectedResult,
|
||||||
String expectedError) {
|
String expectedError) {
|
||||||
this.config = config(configcolumn, configValidation, configValue);
|
this.config = columnSectionConfig(configcolumn, configValidation, configValue);
|
||||||
this.column = column;
|
this.column = column;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.expectedResult = expectedResult;
|
this.expectedResult = expectedResult;
|
||||||
@@ -78,29 +77,6 @@ public class ValidatorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// mocks
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
private Config config(String column, String validation, Object value) {
|
|
||||||
|
|
||||||
Config columnConfig = mock(Config.class);
|
|
||||||
Config validatorConfig = mock(Config.class);
|
|
||||||
|
|
||||||
when(columnConfig.hasPath(column)).thenReturn(true);
|
|
||||||
when(columnConfig.getConfig(column)).thenReturn(validatorConfig);
|
|
||||||
|
|
||||||
when(validatorConfig.hasPath(validation)).thenReturn(true);
|
|
||||||
if (value instanceof Boolean) {
|
|
||||||
when(validatorConfig.getBoolean(validation)).thenReturn((Boolean) value);
|
|
||||||
} else if (value instanceof String) {
|
|
||||||
when(validatorConfig.getString(validation)).thenReturn((String) value);
|
|
||||||
} else if (value instanceof Integer) {
|
|
||||||
when(validatorConfig.getInt(validation)).thenReturn((Integer)value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return columnConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// parameters for tests
|
// parameters for tests
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user