2015-12-04 16:22:21 +01:00
|
|
|
/*
|
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
2021-12-10 00:30:59 +01:00
|
|
|
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
|
2015-12-04 16:22:21 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
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;
|
2019-10-23 05:57:06 +02:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
|
import org.junit.jupiter.params.provider.Arguments;
|
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
2015-12-04 16:22:21 +01:00
|
|
|
|
2015-12-17 20:44:07 +01:00
|
|
|
import java.util.List;
|
2019-10-23 05:57:06 +02:00
|
|
|
import java.util.stream.Stream;
|
2015-12-04 16:22:21 +01:00
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
2015-12-17 21:15:37 +01:00
|
|
|
import static java.util.Collections.singletonList;
|
2015-12-04 16:22:21 +01:00
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
2015-12-17 20:44:07 +01:00
|
|
|
import static org.junit.Assert.assertTrue;
|
2015-12-04 16:22:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* unit test for header validator
|
|
|
|
|
*/
|
|
|
|
|
public class HeaderValidationTest {
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-10-23 05:57:06 +02:00
|
|
|
// tests
|
2015-12-04 16:22:21 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-10-23 05:57:06 +02:00
|
|
|
@ParameterizedTest
|
|
|
|
|
@MethodSource("validationConfigurations")
|
|
|
|
|
public void validation(String configHeaderNames,
|
|
|
|
|
String[] headerNames,
|
|
|
|
|
Boolean expectedResult,
|
|
|
|
|
List<ValidationMessage> expectedErrors) {
|
|
|
|
|
// setup
|
2016-02-02 03:20:14 +01:00
|
|
|
Gson gson = new GsonBuilder().create();
|
2019-10-23 05:57:06 +02:00
|
|
|
ValidationConfiguration config = gson.fromJson(configHeaderNames, ValidationConfiguration.class);
|
|
|
|
|
Validator sut = new Validator(config, new TestColumnValueProvider());
|
2015-12-04 16:22:21 +01:00
|
|
|
|
|
|
|
|
// execution
|
2015-12-07 22:41:59 +01:00
|
|
|
ValidationError result = sut.isHeaderValid(headerNames);
|
2015-12-04 16:22:21 +01:00
|
|
|
|
|
|
|
|
// assertion
|
2015-12-07 22:41:59 +01:00
|
|
|
assertThat(result == null, is(expectedResult));
|
2015-12-04 16:22:21 +01:00
|
|
|
if (!expectedResult) {
|
2015-12-17 20:44:07 +01:00
|
|
|
assertTrue(result.getMessages().containsAll(expectedErrors));
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameters for tests
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-10-23 05:57:06 +02:00
|
|
|
public static Stream<Arguments> validationConfigurations() {
|
|
|
|
|
return Stream.of(
|
|
|
|
|
Arguments.of( json(), new String[] {}, true, null ),
|
|
|
|
|
Arguments.of( json("a"), new String[] {"a"}, true, null ),
|
|
|
|
|
Arguments.of( json("a"), new String[] {"b"}, false, singletonList(new ValidationMessage("validation.message.header.match", "0", "a", "b"))),
|
|
|
|
|
Arguments.of( json("a"), new String[] {"a","b"}, false, singletonList(new ValidationMessage("validation.message.header.length", "2", "1"))),
|
|
|
|
|
Arguments.of( json("a", "b"), new String[] {"b", "a"}, false, asList(new ValidationMessage("validation.message.header.match", "0", "a", "b"), new ValidationMessage("validation.message.header.match", "1", "b", "a")) )
|
|
|
|
|
);
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|
2016-02-02 03:20:14 +01:00
|
|
|
|
2019-10-23 02:04:57 +02:00
|
|
|
@SuppressWarnings("StringConcatenationInLoop")
|
2016-02-02 03:20:14 +01:00
|
|
|
public static String json(String... headerNames) {
|
2016-09-23 11:14:45 +02:00
|
|
|
|
|
|
|
|
String json = "{ \"fields\": [";
|
|
|
|
|
|
|
|
|
|
for (String headerName: headerNames) {
|
|
|
|
|
json += "{\"name\" : \""+headerName+"\" },";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (headerNames != null && headerNames.length > 0) {
|
|
|
|
|
json = json.substring(0, json.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json += "]}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return json;
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|