Skip to content
Snippets Groups Projects
Commit 61cdb487 authored by felmer's avatar felmer
Browse files

SSDM-8101: fixing bug in SimplePropertyValidator.TimestampValidator....

SSDM-8101: fixing bug in SimplePropertyValidator.TimestampValidator. SimplePropertyValidatorTest introduced.
parent 47c6c667
No related branches found
No related tags found
No related merge requests found
...@@ -209,7 +209,7 @@ public class SimplePropertyValidator ...@@ -209,7 +209,7 @@ public class SimplePropertyValidator
/** /**
* Manually validates the date value on cases which are omitted by DateUtils. * Manually validates the date value on cases which are omitted by DateUtils.
* *
* @param value the date-time value to validate. * @param value the date-time value to validate.
* @throws UserFailureException thrown if the value is not considered as a well formatted date. * @throws UserFailureException thrown if the value is not considered as a well formatted date.
*/ */
private void validateHyphens(final String value) throws UserFailureException private void validateHyphens(final String value) throws UserFailureException
...@@ -218,18 +218,15 @@ public class SimplePropertyValidator ...@@ -218,18 +218,15 @@ public class SimplePropertyValidator
{ {
return; return;
} }
int indexOfHyphen = value.indexOf('-');
final String dateValue = extractDate(value); if (indexOfHyphen >= 0 && indexOfHyphen != 4)
final boolean hyphenFormat = dateValue.matches("\\d*-\\d*-\\d*");
final boolean desiredHyphenFormat = dateValue.matches("\\d{4,}-\\d+-\\d+");
if (hyphenFormat && !desiredHyphenFormat)
{ {
// When the date value uses hyphens as separators but does not have 4 digits for the year value // When the date value uses hyphens as separators but does not have 4 digits for the year value
// throw an exception. // throw an exception.
throwUserFailureException(value); throwUserFailureException(value);
} }
} }
/** /**
* Throws UserFailureException. * Throws UserFailureException.
* *
...@@ -242,18 +239,6 @@ public class SimplePropertyValidator ...@@ -242,18 +239,6 @@ public class SimplePropertyValidator
"Date value '%s' has improper format. It must be one of '%s'.", value, "Date value '%s' has improper format. It must be one of '%s'.", value,
Arrays.toString(DATE_PATTERNS)); Arrays.toString(DATE_PATTERNS));
} }
/**
* Extracts date part from the string representation of a date-time.
*
* @param value the value considered as string representation of date-time.
* @return the date portion of the date-time string.
*/
private final static String extractDate(final String value)
{
final int dateSeparator = Math.min(value.indexOf(' '), value.indexOf('T'));
return dateSeparator >= 0 ? value.substring(0, dateSeparator) : value;
}
} }
public final static class IntegerValidator implements IDataTypeValidator public final static class IntegerValidator implements IDataTypeValidator
......
/*
* Copyright 2019 ETH Zuerich, SIS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.generic.shared.util;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.openbis.generic.shared.util.SimplePropertyValidator.TimestampValidator;
/**
* @author Franz-Josef Elmer
*/
public class SimplePropertyValidatorTest
{
@DataProvider
public Object[][] validTimestamps()
{
return new Object[][] {
{ "2020-05-16", "2020-05-16 00:00:00 +0200" },
{ "2020-5-16", "2020-05-16 00:00:00 +0200" },
{ "2019-01-16", "2019-01-16 00:00:00 +0100" },
{ "2019-01-16 3:4", "2019-01-16 03:04:00 +0100" },
{ "2019-01-16 18:23:56", "2019-01-16 18:23:56 +0100" },
{ "2019-01-16 18:23:56 +0700", "2019-01-16 12:23:56 +0100" },
{ "2019-01-16 18:23:56 GMT", "2019-01-16 19:23:56 +0100" },
{ "1/16/19", "2019-01-16 00:00:00 +0100" },
{ "1/16/19 8:9", "2019-01-16 08:09:00 +0100" },
{ "1/16/19 8:9 p", "2019-01-16 20:09:00 +0100" },
{ "1/16/19 18:19", "2019-01-16 18:19:00 +0100" },
};
}
@DataProvider
public Object[][] invalidTimestamps()
{
return new Object[][] {
{ "2010-05-06T17:13:39" },
{ "10-05-06" },
{ "10-05-06 7:23" },
{ "10-05-06 17:13:39" },
{ "2010-05-36 17:13:39" },
{ "2010-05-06 27:13:39" },
{ "13/12/11 7:39" },
{ "3/12/11 7:39:22" },
};
}
@Test(dataProvider = "validTimestamps")
public void testTimestampValidatorWithValidExamples(String stringToParse, String canonicalTimestamp)
{
TimestampValidator validator = new SimplePropertyValidator.TimestampValidator();
assertEquals(validator.validate(stringToParse), canonicalTimestamp);
}
@Test(dataProvider = "invalidTimestamps")
public void testTimestampValidatorWithInvalidExamples(String stringToParse)
{
TimestampValidator validator = new SimplePropertyValidator.TimestampValidator();
try
{
validator.validate(stringToParse);
} catch (UserFailureException e)
{
assertEquals(e.getMessage(), "Date value '" + stringToParse + "' has improper format. It must be one of "
+ "'[yyyy-MM-dd, yyyy-MM-dd HH:mm, yyyy-MM-dd HH:mm:ss, M/d/yy, M/d/yy h:mm a, M/d/yy HH:mm, "
+ "yyyy-MM-dd HH:mm:ss Z, yyyy-MM-dd HH:mm:ss ZZZZ]'.");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment