2015-12-04 16:22:21 +01:00
|
|
|
/*
|
|
|
|
|
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;
|
|
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
2015-12-04 16:22:21 +01:00
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.runners.Parameterized;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2015-12-17 20:44:07 +01:00
|
|
|
import java.util.List;
|
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;
|
2016-02-02 03:20:14 +01:00
|
|
|
import static java.util.stream.Collectors.joining;
|
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
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(Parameterized.class)
|
|
|
|
|
public class HeaderValidationTest {
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameters
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-02-02 03:20:14 +01:00
|
|
|
private ValidationConfiguration config;
|
2015-12-04 16:22:21 +01:00
|
|
|
private Boolean expectedResult;
|
2015-12-17 21:15:37 +01:00
|
|
|
private List<ValidationMessage> expectedErrors;
|
2015-12-04 16:22:21 +01:00
|
|
|
private String[] headerNames;
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// subject under test
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
private Validator sut;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// parameterized constructor
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-02-02 03:20:14 +01:00
|
|
|
public HeaderValidationTest(String configHeaderNames,
|
2015-12-04 16:22:21 +01:00
|
|
|
String[] headerNames,
|
|
|
|
|
Boolean expectedResult,
|
2015-12-17 21:15:37 +01:00
|
|
|
List<ValidationMessage> expectedErrors) {
|
2016-02-02 03:20:14 +01:00
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
|
this.config = gson.fromJson(configHeaderNames, ValidationConfiguration.class);
|
2015-12-04 16:22:21 +01:00
|
|
|
this.headerNames = headerNames;
|
|
|
|
|
this.expectedResult = expectedResult;
|
2015-12-17 20:44:07 +01:00
|
|
|
this.expectedErrors = expectedErrors;
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// init
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Before
|
|
|
|
|
public void initialize() {
|
|
|
|
|
sut = new Validator(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// tests
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Test
|
|
|
|
|
public void validation() {
|
|
|
|
|
// 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
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Parameterized.Parameters
|
|
|
|
|
public static Collection validationConfigurations() {
|
|
|
|
|
return asList(new Object[][] {
|
2016-02-02 03:20:14 +01:00
|
|
|
{ json(), new String[] {}, true, null },
|
|
|
|
|
{ json("a"), new String[] {"a"}, true, null },
|
|
|
|
|
{ json("a"), new String[] {"b"}, false, singletonList(new ValidationMessage("validation.message.header.match", "0", "a", "b"))},
|
|
|
|
|
{ json("a"), new String[] {"a","b"}, false, singletonList(new ValidationMessage("validation.message.header.length", "2", "1"))},
|
|
|
|
|
{ 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
|
|
|
|
|
|
|
|
public static String json(String... headerNames) {
|
|
|
|
|
return "{\"headers\":{\"list\":[" + asList(headerNames).stream().collect(joining(", ")) + "]}}";
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|