From 34a913d761622daf854c1f7e4e2d8c64a7263c38 Mon Sep 17 00:00:00 2001 From: "Hugues.Cazeaux" <hugues.cazeaux@unige.ch> Date: Mon, 21 Mar 2022 10:56:51 +0000 Subject: [PATCH] feat: add ROR ID validation --- .../solidify/controller/OrcidController.java | 2 +- .../service/PersonWithOrcidService.java | 2 +- .../unige/solidify/util/ValidationTool.java | 31 ++- .../test/util/ValidationToolTest.java | 197 ++++++++++++++++++ 4 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 solidify-util/src/test/java/ch/unige/solidify/test/util/ValidationToolTest.java diff --git a/solidify-controller/src/main/java/ch/unige/solidify/controller/OrcidController.java b/solidify-controller/src/main/java/ch/unige/solidify/controller/OrcidController.java index b089a47c6..85065eeec 100644 --- a/solidify-controller/src/main/java/ch/unige/solidify/controller/OrcidController.java +++ b/solidify-controller/src/main/java/ch/unige/solidify/controller/OrcidController.java @@ -133,6 +133,6 @@ public class OrcidController { private PersonWithOrcid getCurrentPerson() { final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - return this.personService.gePerson(authentication); + return this.personService.getPerson(authentication); } } diff --git a/solidify-controller/src/main/java/ch/unige/solidify/service/PersonWithOrcidService.java b/solidify-controller/src/main/java/ch/unige/solidify/service/PersonWithOrcidService.java index 4a76a04ff..59933035c 100644 --- a/solidify-controller/src/main/java/ch/unige/solidify/service/PersonWithOrcidService.java +++ b/solidify-controller/src/main/java/ch/unige/solidify/service/PersonWithOrcidService.java @@ -28,7 +28,7 @@ import org.springframework.security.core.Authentication; import ch.unige.solidify.model.PersonWithOrcid; public interface PersonWithOrcidService { - PersonWithOrcid gePerson(Authentication authentication); + PersonWithOrcid getPerson(Authentication authentication); PersonWithOrcid save(PersonWithOrcid person); diff --git a/solidify-util/src/main/java/ch/unige/solidify/util/ValidationTool.java b/solidify-util/src/main/java/ch/unige/solidify/util/ValidationTool.java index a8765a8ea..2b33ab6d2 100644 --- a/solidify-util/src/main/java/ch/unige/solidify/util/ValidationTool.java +++ b/solidify-util/src/main/java/ch/unige/solidify/util/ValidationTool.java @@ -32,6 +32,8 @@ import java.time.temporal.ChronoField; import java.util.regex.Matcher; import java.util.regex.Pattern; +import ch.unige.solidify.SolidifyConstants; + public class ValidationTool { private static final String DEWEY_CODE_REGEX = "^\\d{1,5}((\\.|-|\\ -\\ |/|\\ /\\ |\\ /)\\d{1,5}){0,8}$"; @@ -41,7 +43,16 @@ public class ValidationTool { private static final String ISBN_REGEX = "^[0-9\\-]+[xX]*$"; private static final String ISSN_REGEX = "^\\d{4}\\-\\d{3}[\\dxX]$"; private static final String DOI_REGEX = "^10.\\d{4,9}/[-._;()<>/:A-Za-z0-9]+$"; - private static final int MAX_ARXIVID_LENGTH = 25; + private static final String RORID_REGEX = "^0[\\d|a-z&&[^ilou]]{6}\\d{2}$"; + + private static final int ARXIVID_MAX_LENGTH = 25; + + /** + * This constructor should not be called + */ + private ValidationTool() { + throw new IllegalStateException(SolidifyConstants.TOOL_CLASS); + } /** * Valid ISSN is made of: 4 number, 1 dash, 3 numbers, 1 number or X (1234-5678 or 0004-567X) @@ -54,8 +65,8 @@ public class ValidationTool { } /** - * Valid DOI is made of: prefix/suffix where prefix is build as 10.NNNN where NNNN are at least four digits greater or - * equal to 1000. + * Valid DOI is made of: prefix/suffix where prefix is build as 10.NNNN where NNNN are at least four + * digits greater or equal to 1000. * * @param doi The DOI code to validate * @return @@ -78,7 +89,8 @@ public class ValidationTool { } /** - * Different formats for dates are valid: yyyy or given date format (at the moment: yyyy-MM-dd and yyyy-MM) + * Different formats for dates are valid: yyyy or given date format (at the moment: yyyy-MM-dd and + * yyyy-MM) * * @param dateStr * @param validDateFormats @@ -159,7 +171,7 @@ public class ValidationTool { public static boolean isValidArxivId(String arxivId) { // Very relax validation, to implement a strict one see: // https://arxiv.org/help/arxiv_identifier - if (arxivId == null || arxivId.length() > MAX_ARXIVID_LENGTH) { + if (arxivId == null || arxivId.length() > ARXIVID_MAX_LENGTH) { return false; } Pattern p = Pattern.compile(ARXIVID_REGEX); @@ -175,4 +187,13 @@ public class ValidationTool { Matcher m = p.matcher(urn); return m.matches(); } + + public static boolean isValidRorId(String rorId) { + if (rorId == null) { + return false; + } + Pattern p = Pattern.compile(RORID_REGEX, Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(rorId); + return m.matches(); + } } diff --git a/solidify-util/src/test/java/ch/unige/solidify/test/util/ValidationToolTest.java b/solidify-util/src/test/java/ch/unige/solidify/test/util/ValidationToolTest.java new file mode 100644 index 000000000..98b79469d --- /dev/null +++ b/solidify-util/src/test/java/ch/unige/solidify/test/util/ValidationToolTest.java @@ -0,0 +1,197 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify Util - ValidationToolTest.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2022 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ + +package ch.unige.solidify.test.util; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.LocalDate; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import ch.unige.solidify.util.ValidationTool; + +class ValidationToolTest { + @Test + void testNegativePMID() { + assertFalse(ValidationTool.isValidPMID("-1")); + } + + @Test + void testAlphabeticalPMID() { + assertFalse(ValidationTool.isValidPMID("abc")); + } + + @Test + void testZeroPMID() { + assertFalse(ValidationTool.isValidPMID("0")); + } + + @Test + void testRegularPMID() { + assertTrue(ValidationTool.isValidPMID("33740420")); + } + + @Test + void testBigPMID() { + assertTrue(ValidationTool.isValidPMID("094215760941650195690145612049561240956109564129056129056910256914025691056901659012564105" + + "275621049576041957601560124965109650942165094215691056910659102569205612654012650251025702475204756024967209467029679026661651")); + } + + @Test + void testMaxYear() { + int currentYear = LocalDate.now().getYear(); + int maxYear = currentYear + 10; + assertTrue(ValidationTool.isValidDate(String.valueOf(maxYear), new String[] { "d.m.yyyy" })); + assertFalse(ValidationTool.isValidDate(String.valueOf(maxYear + 1), new String[] { "d.m.yyyy" })); + } + + @Test + void testDateAsYear() { + assertTrue(ValidationTool.isValidDate("2020", null)); + assertTrue(ValidationTool.isValidDate("2020", new String[] { "yyyy-MM-dd", "yyyy" })); + + assertFalse(ValidationTool.isValidDate("202", new String[] { "d.m.yyyy" })); + assertFalse(ValidationTool.isValidDate("-202", new String[] { "d.m.yyyy" })); + assertFalse(ValidationTool.isValidDate("2500", new String[] { "d.m.yyyy" })); + } + + @Test + void testDateWithFormatSpecified() { + int currentYear = LocalDate.now().getYear(); + int maxYear = currentYear + 10; + + assertTrue(ValidationTool.isValidDate("2020-08", new String[] { "yyyy-MM-dd", "yyyy-MM" })); + assertTrue(ValidationTool.isValidDate("2020-11", new String[] { "yyyy-MM" })); + assertTrue(ValidationTool.isValidDate("2020-9", new String[] { "yyyy-M" })); + assertTrue(ValidationTool.isValidDate("2020-09-01", new String[] { "yyyy-MM-dd" })); + assertTrue(ValidationTool.isValidDate("2020-09-01", new String[] { "yyyy-M-d" })); + assertTrue(ValidationTool.isValidDate("2020-9-1", new String[] { "yyyy-M-d" })); + assertTrue(ValidationTool.isValidDate("01.09.2020", new String[] { "dd.MM.yyyy" })); + assertTrue(ValidationTool.isValidDate("01.09.2020", new String[] { "yyyy-MM", "d.M.yyyy" })); + assertTrue(ValidationTool.isValidDate("1.9.2020", new String[] { "d.M.yyyy" })); + assertTrue(ValidationTool.isValidDate("1.9." + maxYear, new String[] { "d.M.yyyy" })); + + assertFalse(ValidationTool.isValidDate("2020-9-", new String[] { "yyyy-MM-yy", "yyyy-MM" })); + assertFalse(ValidationTool.isValidDate("2020-9", new String[] { "yyyy-MM-yy" })); + assertFalse(ValidationTool.isValidDate("2020-10-12", new String[] { "yyyy-MM" })); + assertFalse(ValidationTool.isValidDate("2020-09-01", null)); + assertFalse(ValidationTool.isValidDate("2020-9-", new String[] { "yyyy-MM" })); + assertFalse(ValidationTool.isValidDate("2020-9-ww", new String[] { "yyyy-MM-yy" })); + assertFalse(ValidationTool.isValidDate("10-2020", new String[] { "yyyy-MM" })); + assertFalse(ValidationTool.isValidDate("2020-09-01", new String[] { "" })); + assertFalse(ValidationTool.isValidDate("2020-09-01", new String[] { "dd.MM.yyyy" })); + assertFalse(ValidationTool.isValidDate("2020-9-1", new String[] { "d.M.yyyy" })); + assertFalse(ValidationTool.isValidDate("01.09.2020", null)); + assertFalse(ValidationTool.isValidDate("01.09.2020", new String[] { "" })); + assertFalse(ValidationTool.isValidDate("01.09.2020", new String[] { "yyyy-MM-dd" })); + assertFalse(ValidationTool.isValidDate("1.9.2020", new String[] { "dd.MM.yyyy" })); + assertFalse(ValidationTool.isValidDate("1.9." + (maxYear + 1), new String[] { "d.M.yyyy" })); + } + + @ParameterizedTest + @ValueSource(strings = { "112", "112.456", "112.456.678", "112/456", "112.456/789", "112.456 / 789", "112.456/789.986", + "112.456 / 789.986/45678", "112-456", "112 - 456", "112 - 456 /314.9" }) + void testValidDewey(String dewey) { + assertTrue(ValidationTool.isValidDeweyCode(dewey)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { "abcd", "1234\\789", "1234//789", "1234--789", "12345.568 / 12546 / test" }) + void testInvalidDewey(String dewey) { + assertFalse(ValidationTool.isValidDeweyCode(dewey)); + } + + @ParameterizedTest + @ValueSource(strings = { "0000-0001-2345-6789", "0000-0001-5109-3700", "0000-0002-1694-233X" }) + void testValidOrcid(String orcid) { + assertTrue(ValidationTool.isValidOrcid(orcid)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { "", "0000-0001-2345-678", "0000-0001-234-6789", "0000-001-2345-6789", "000-0001-2345-6789", "00000001-2345-6789", + "0000-0001-2345-678Y" }) + void testInvalidOrcid(String orcid) { + assertFalse(ValidationTool.isValidOrcid(orcid)); + } + + @ParameterizedTest + @ValueSource(strings = { "1910.10964", "patt-sol/9311007", "cond-mat/9507074", "cond-mat/9507074v1", "1501.00001v1", "0706.0001v2" }) + void testValidArxivId(String arxivId) { + assertTrue(ValidationTool.isValidArxivId(arxivId)); + } + + @ParameterizedTest + @ValueSource(strings = { "", "aaaa+", "%v2v2v2v", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }) + void testInvalidArxivId(String arxivId) { + assertFalse(ValidationTool.isValidArxivId(arxivId)); + } + + @ParameterizedTest + @ValueSource(strings = { "urn:nbn:ch:unige-1495076", "urn:nbn:ch:unige-6002", "urn:lex:eu:council:directive:2010-03-09;2010-19-UE", + "urn:my-nid:my-custo@m-nss/test:yiha" }) + void testValidURN(String urn) { + assertTrue(ValidationTool.isValidURN(urn)); + } + + @ParameterizedTest + @ValueSource(strings = { "", "urn:::", "urn;nbn:ch:unige-1495076", "urn:nbn-ch;unige-1495076", "urn:my-nid:my-custo@m-nss/tes\nt:yiha" }) + void testInvalidURN(String urn) { + assertFalse(ValidationTool.isValidURN(urn)); + } + + @ParameterizedTest + @ValueSource(strings = { "10.1007/978-3-642-28108-2_19", "10.1016/S0735-1097(98)00347-7", "10.1109/5.771073", "10.1186/s40101-021-00255-z", + "10.1002/1522-2675(20010131)84:1<141::AID-HLCA141>3.0.CO;2-R", "10.1002/1522-2675(20010228)84:2<416::AID-HLCA416>3.0.CO;2-K" }) + void testValidDOI(String doi) { + assertTrue(ValidationTool.isValidDOI(doi)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { "", "0000-0001-2345-678", "11.1109/5.771073", "10.109/5.771073", "10.nature/34.77802", "10.10056.21/34.77802" }) + void testInvalidDOI(String doi) { + assertFalse(ValidationTool.isValidDOI(doi)); + } + + @ParameterizedTest + @ValueSource(strings = { "01swzsf04", "02s376052", "05a28rw58", "02crff812", "01xkakk17", "05pmsvm27", "01m1pv723", "007ygn379", "02fsagg23", + "00yjd3n13", "012y9zp98" }) + void testValidRorid(String rorId) { + assertTrue(ValidationTool.isValidRorId(rorId)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { "", "0000-0001-2345-678", "11.1109/5.771073", "01swif04", "01slzsf04", "01uwzsf04", "01sozsf04" }) + void testInvalidRorid(String rorId) { + assertFalse(ValidationTool.isValidRorId(rorId)); + } + +} -- GitLab