Skip to content

Commit a306d2d

Browse files
author
Fabrice V
committed
GTFS types validation. time
-add validation code -specified format HH:MM:SS or H:MM:SS -add unit testing of method
1 parent 1e49ff8 commit a306d2d

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/main/java/org/mobilitydata/gtfsvalidator/rules/ValidationRules.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ public class ValidationRules {
7575
public static final ValidationRule E011 = new ValidationRule("E011", "ERROR", "Invalid field value",
7676
"A field of type URL must use http or https scheme and conform to RFC2396",
7777
"is not a valid URL");
78+
79+
public static final ValidationRule E012 = new ValidationRule("E012", "ERROR", "Invalid field value",
80+
"A field of type time can't be parsed",
81+
"is not in valid HH:MM:SS or H:MM:SS format");
7882
}

src/main/java/org/mobilitydata/gtfsvalidator/util/GTFSTypeValidationUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ String parseAndValidateTimeZone(@NotNull String fieldName,
232232
return rawValue;
233233
}
234234

235+
public static @Nullable
236+
String validateTime(@NotNull String fieldName,
237+
@Nullable String rawValue,
238+
@NotNull List<OccurrenceModel>outList) {
239+
240+
if(Strings.isNullOrEmpty(rawValue)) {
241+
return null;
242+
}
243+
244+
if (!TIME_PATTERN.matcher(rawValue).matches()) {
245+
RuleUtils.addOccurrence(E012, fieldName, outList);
246+
return null;
247+
}
248+
249+
return rawValue;
250+
}
251+
235252

236253
private static @Nullable
237254
String validateString(@NotNull String fieldName,
@@ -263,5 +280,6 @@ private static boolean isPrintableAscii(char ch) {
263280
return ch >= 32 && ch < 127;
264281
}
265282
private static final Pattern COLOR_6_DIGITS_HEXADECIMAL_PATTERN = Pattern.compile("^#[0-9A-Fa-f]{6}$");
283+
private static final Pattern TIME_PATTERN = Pattern.compile("([0-9][0-9]|[0-9]):[0-5][0-9]:[0-5][0-9]");
266284
private static final String[] VALID_URL_SCHEMES = { "http", "https" };
267285
}

src/test/java/org/mobilitydata/gtfsvalidator/util/GTFSTypeValidationUtilsTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,54 @@ void parseAndValidateColor() {
773773
assertEquals("E007", error.getRule().getErrorId());
774774
}
775775

776+
@Test
777+
public void validateTime() {
778+
779+
List<OccurrenceModel> testList = new ArrayList<>();
780+
781+
// typical case
782+
String returned = GTFSTypeValidationUtils.validateTime(fieldName,
783+
"99:59:59",
784+
testList);
785+
786+
assertEquals("99:59:59", returned);
787+
assertEquals(0, testList.size());
788+
789+
// typical case
790+
returned = GTFSTypeValidationUtils.validateTime(fieldName,
791+
"9:59:59",
792+
testList);
793+
794+
assertEquals("9:59:59", returned);
795+
assertEquals(0, testList.size());
796+
797+
// null
798+
returned = GTFSTypeValidationUtils.validateTime(fieldName,
799+
null,
800+
testList);
801+
assertNull(returned);
802+
assertEquals(0, testList.size());
803+
804+
// empty
805+
returned = GTFSTypeValidationUtils.validateTime(fieldName,
806+
"",
807+
testList);
808+
809+
assertNull(returned);
810+
assertEquals(0, testList.size());
811+
812+
// any non parsable
813+
returned = GTFSTypeValidationUtils.validateTime(fieldName,
814+
"abc",
815+
testList);
816+
assertNull(returned);
817+
assertEquals(1, testList.size());
818+
OccurrenceModel error = testList.get(0);
819+
assertEquals(fieldName, error.getPrefix());
820+
assertEquals("E012", error.getRule().getErrorId());
821+
822+
}
823+
776824
@Test
777825
public void parseAndValidateTimeZone() {
778826

0 commit comments

Comments
 (0)