summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas <>2021-06-25 15:46:48 +0200
committerThomas <>2021-06-25 15:46:48 +0200
commite9d3b7dac0b81a38dfb65dad75b6e8c3f6f2a76f (patch)
tree5af97eb02bed8188895184c2dcde81ad284dd0f2 /src
parentea2295a8671c88a8510f764e70c8f1c135bf5675 (diff)
downloadegovutils-e9d3b7dac0b81a38dfb65dad75b6e8c3f6f2a76f.tar.gz
egovutils-e9d3b7dac0b81a38dfb65dad75b6e8c3f6f2a76f.tar.bz2
egovutils-e9d3b7dac0b81a38dfb65dad75b6e8c3f6f2a76f.zip
add validator to check if age is 14 or older
Diffstat (limited to 'src')
-rw-r--r--src/main/java/at/gv/util/ValidationUtils.java19
-rw-r--r--src/test/java/at/gv/util/test/ValitationUtilsTest.java36
2 files changed, 55 insertions, 0 deletions
diff --git a/src/main/java/at/gv/util/ValidationUtils.java b/src/main/java/at/gv/util/ValidationUtils.java
index cbc7207..108fe27 100644
--- a/src/main/java/at/gv/util/ValidationUtils.java
+++ b/src/main/java/at/gv/util/ValidationUtils.java
@@ -2,11 +2,16 @@ package at.gv.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.Period;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
import java.util.regex.Pattern;
public class ValidationUtils {
private static final String DATEFORMAT = "dd.MM.yyyy";
+ private static final int REPRESENTATION_MIN_AGE = 14;
public static boolean isDateFormat(String dateString) {
@@ -57,5 +62,19 @@ public class ValidationUtils {
return Pattern.compile("[0-9]{4}").matcher(postcode).matches();
}
+
+ public static boolean isOverThirteen(String representativeDateOfBirth) {
+ try {
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATEFORMAT);
+ LocalDate birthdate= LocalDate.parse(representativeDateOfBirth, formatter);
+ Period p = Period.between(birthdate, LocalDate.now());
+ return p.getYears() >= REPRESENTATION_MIN_AGE;
+
+ } catch (DateTimeParseException e) {
+ return false;
+
+ }
+
+ }
}
diff --git a/src/test/java/at/gv/util/test/ValitationUtilsTest.java b/src/test/java/at/gv/util/test/ValitationUtilsTest.java
new file mode 100644
index 0000000..e4e5b3f
--- /dev/null
+++ b/src/test/java/at/gv/util/test/ValitationUtilsTest.java
@@ -0,0 +1,36 @@
+package at.gv.util.test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.lang.RandomStringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+import at.gv.util.ValidationUtils;
+
+@RunWith(BlockJUnit4ClassRunner.class)
+public class ValitationUtilsTest {
+
+ @Test
+ public void dateOfBirthFormat() {
+ assertFalse("wrong format not detected", ValidationUtils.isDateFormat("19-06-2002"));
+ assertFalse("wrong format not detected", ValidationUtils.isOverThirteen(RandomStringUtils.randomAlphanumeric(10)));
+
+ assertTrue("detected", ValidationUtils.isDateFormat("19.06.2002"));
+
+ }
+
+ @Test
+ public void ageVerification() {
+ assertTrue("not over 14", ValidationUtils.isOverThirteen("01.01.1985"));
+ assertTrue("not over 14", ValidationUtils.isOverThirteen("17.06.2007"));
+ assertTrue("not over 14", ValidationUtils.isOverThirteen("18.06.2007"));
+ assertFalse("not over 14", ValidationUtils.isOverThirteen("19.06.2015"));
+
+ assertFalse("not over 14", ValidationUtils.isOverThirteen("19-06-2002"));
+ assertFalse("not over 14", ValidationUtils.isOverThirteen(RandomStringUtils.randomAlphanumeric(10)));
+
+ }
+}