use of org.orcid.core.exception.OrcidValidationException in project ORCID-Source by ORCID.
the class GroupIdRecordManagerTest method testCreateUpdateGetDeleteGroupIdRecord.
@Test
public void testCreateUpdateGetDeleteGroupIdRecord() {
GroupIdRecord g1 = new GroupIdRecord();
g1.setDescription("Description");
g1.setGroupId("orcid-generated:valid-group-id");
g1.setName("Group # " + System.currentTimeMillis());
g1.setType("publisher");
//Test create
g1 = groupIdRecordManager.createGroupIdRecord(g1);
Long putCode = g1.getPutCode();
assertNotNull(putCode);
assertNotNull(g1.getSource());
assertNotNull(g1.getSource().getSourceClientId());
assertEquals(CLIENT_ID, g1.getSource().getSourceClientId().getPath());
//Test find
assertTrue(groupIdRecordManager.exists(g1.getGroupId()));
Optional<GroupIdRecord> existingByGroupId = groupIdRecordManager.findByGroupId(g1.getGroupId());
assertTrue(existingByGroupId.isPresent());
assertNotNull(existingByGroupId.get().getPutCode());
assertEquals(putCode, existingByGroupId.get().getPutCode());
assertEquals(g1.getGroupId(), existingByGroupId.get().getGroupId());
assertNotNull(existingByGroupId.get().getSource());
assertNotNull(existingByGroupId.get().getSource().getSourceClientId());
assertEquals(CLIENT_ID, existingByGroupId.get().getSource().getSourceClientId().getPath());
GroupIdRecord existingByPutCode = groupIdRecordManager.getGroupIdRecord(g1.getPutCode());
assertNotNull(existingByPutCode);
assertNotNull(existingByPutCode.getPutCode());
assertEquals(putCode, existingByPutCode.getPutCode());
assertEquals(g1.getGroupId(), existingByPutCode.getGroupId());
assertNotNull(existingByPutCode.getSource());
assertNotNull(existingByPutCode.getSource().getSourceClientId());
assertEquals(CLIENT_ID, existingByPutCode.getSource().getSourceClientId().getPath());
//Test update with invalid value
try {
g1.setGroupId("invalid-group-id");
groupIdRecordManager.updateGroupIdRecord(g1.getPutCode(), g1);
fail();
} catch (OrcidValidationException e) {
assertEquals("Invalid group-id: '" + g1.getGroupId() + "'", e.getMessage());
} catch (Exception e) {
fail();
}
//Test update with valid value
try {
g1.setGroupId("orcid-generated:other-valid-group-id");
g1 = groupIdRecordManager.updateGroupIdRecord(g1.getPutCode(), g1);
assertNotNull(g1);
assertEquals("orcid-generated:other-valid-group-id", g1.getGroupId());
} catch (Exception e) {
fail();
}
//Test create with put code
try {
g1.setGroupId("orcid-generated:valid-group-id");
groupIdRecordManager.createGroupIdRecord(g1);
fail();
} catch (InvalidPutCodeException e) {
} catch (Exception e) {
fail();
}
//Test create with invalid group id
try {
g1.setPutCode(null);
g1.setGroupId("other-invalid-group-id");
groupIdRecordManager.createGroupIdRecord(g1);
fail();
} catch (OrcidValidationException e) {
assertEquals("Invalid group-id: '" + g1.getGroupId() + "'", e.getMessage());
} catch (Exception e) {
fail();
}
//Test delete
groupIdRecordManager.deleteGroupIdRecord(putCode);
try {
groupIdRecordManager.deleteGroupIdRecord(Long.valueOf(-1L));
} catch (GroupIdRecordNotFoundException e) {
} catch (Exception e) {
fail();
}
}
use of org.orcid.core.exception.OrcidValidationException in project ORCID-Source by ORCID.
the class ActivityValidator method validateWork.
public void validateWork(Work work, SourceEntity sourceEntity, boolean createFlag, boolean isApiRequest, Visibility originalVisibility) {
WorkTitle title = work.getWorkTitle();
if (title == null || title.getTitle() == null || StringUtils.isEmpty(title.getTitle().getContent())) {
throw new ActivityTitleValidationException();
}
if (work.getCountry() != null) {
if (work.getCountry().getValue() == null) {
Map<String, String> params = new HashMap<String, String>();
String values = Arrays.stream(Iso3166Country.values()).map(element -> element.value()).collect(Collectors.joining(", "));
params.put("type", "country");
params.put("values", values);
throw new ActivityTypeValidationException(params);
}
}
//translated title language code
if (title != null && title.getTranslatedTitle() != null) {
String translatedTitle = title.getTranslatedTitle().getContent();
String languageCode = title.getTranslatedTitle().getLanguageCode();
if (PojoUtil.isEmpty(translatedTitle) && !PojoUtil.isEmpty(languageCode)) {
throw new OrcidValidationException("Please specify a translated title or remove the language code");
}
//If translated title language code is null or invalid
if (!PojoUtil.isEmpty(translatedTitle) && (PojoUtil.isEmpty(title.getTranslatedTitle().getLanguageCode()) || !Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).anyMatch(title.getTranslatedTitle().getLanguageCode()::equals))) {
Map<String, String> params = new HashMap<String, String>();
String values = Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).collect(Collectors.joining(", "));
params.put("type", "translated title -> language code");
params.put("values", values);
throw new ActivityTypeValidationException(params);
}
}
if (work.getWorkType() == null) {
Map<String, String> params = new HashMap<String, String>();
String values = Arrays.stream(WorkType.values()).map(element -> element.value()).collect(Collectors.joining(", "));
params.put("type", "work type");
params.put("values", values);
throw new ActivityTypeValidationException(params);
}
if (!PojoUtil.isEmpty(work.getLanguageCode())) {
if (!Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).anyMatch(work.getLanguageCode()::equals)) {
Map<String, String> params = new HashMap<String, String>();
String values = Arrays.stream(SiteConstants.AVAILABLE_ISO_LANGUAGES).collect(Collectors.joining(", "));
params.put("type", "language code");
params.put("values", values);
throw new ActivityTypeValidationException(params);
}
}
//publication date
if (work.getPublicationDate() != null) {
PublicationDate pd = work.getPublicationDate();
Year year = pd.getYear();
Month month = pd.getMonth();
Day day = pd.getDay();
if (year != null) {
try {
Integer.valueOf(year.getValue());
} catch (NumberFormatException n) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "publication date -> year");
params.put("values", "integers");
throw new ActivityTypeValidationException(params);
}
if (year.getValue().length() != 4) {
throw new OrcidValidationException("Invalid year " + year.getValue() + " please specify a four digits value");
}
}
if (month != null) {
try {
Integer.valueOf(month.getValue());
} catch (NumberFormatException n) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "publication date -> month");
params.put("values", "integers");
throw new ActivityTypeValidationException(params);
}
if (month.getValue().length() != 2) {
throw new OrcidValidationException("Invalid month " + month.getValue() + " please specify a two digits value");
}
}
if (day != null) {
try {
Integer.valueOf(day.getValue());
} catch (NumberFormatException n) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "publication date -> day");
params.put("values", "integers");
throw new ActivityTypeValidationException(params);
}
if (day.getValue().length() != 2) {
throw new OrcidValidationException("Invalid day " + day.getValue() + " please specify a two digits value");
}
}
//Check the date is valid
boolean isYearEmpty = (year == null || year.getValue() == null) ? true : false;
boolean isMonthEmpty = (month == null || month.getValue() == null) ? true : false;
boolean isDayEmpty = (day == null || day.getValue() == null) ? true : false;
if (isYearEmpty && (!isMonthEmpty || !isDayEmpty)) {
throw new OrcidValidationException("Invalid date, please specify a year element");
} else if (!isYearEmpty && isMonthEmpty && !isDayEmpty) {
throw new OrcidValidationException("Invalid date, please specify a month element");
} else if (isYearEmpty && isMonthEmpty && !isDayEmpty) {
throw new OrcidValidationException("Invalid date, please specify a year and month elements");
}
}
//citation
if (work.getWorkCitation() != null) {
String citation = work.getWorkCitation().getCitation();
CitationType type = work.getWorkCitation().getWorkCitationType();
if (type == null) {
Map<String, String> params = new HashMap<String, String>();
String values = Arrays.stream(CitationType.values()).map(element -> element.value()).collect(Collectors.joining(", "));
params.put("type", "citation type");
params.put("values", values);
throw new ActivityTypeValidationException(params);
}
if (PojoUtil.isEmpty(citation)) {
throw new OrcidValidationException("Please specify a citation or remove the parent tag");
}
}
if (work.getWorkExternalIdentifiers() == null || work.getWorkExternalIdentifiers().getExternalIdentifier() == null || work.getExternalIdentifiers().getExternalIdentifier().isEmpty()) {
throw new ActivityIdentifierValidationException();
}
if (work.getWorkContributors() != null) {
WorkContributors contributors = work.getWorkContributors();
if (!contributors.getContributor().isEmpty()) {
for (Contributor contributor : contributors.getContributor()) {
if (contributor.getContributorOrcid() != null) {
ContributorOrcid contributorOrcid = contributor.getContributorOrcid();
if (!PojoUtil.isEmpty(contributorOrcid.getUri())) {
if (!OrcidStringUtils.isValidOrcidUri(contributorOrcid.getUri())) {
throw new OrcidValidationException("Invalid contributor URI");
}
}
if (!PojoUtil.isEmpty(contributorOrcid.getPath())) {
if (!OrcidStringUtils.isValidOrcid(contributorOrcid.getPath())) {
throw new OrcidValidationException("Invalid contributor ORCID");
}
}
}
if (contributor.getCreditName() != null) {
if (PojoUtil.isEmpty(contributor.getCreditName().getContent())) {
throw new OrcidValidationException("Please specify a contributor credit name or remove the empty tag");
}
}
if (contributor.getContributorEmail() != null) {
if (PojoUtil.isEmpty(contributor.getContributorEmail().getValue())) {
throw new OrcidValidationException("Please specify a contributor email or remove the empty tag");
}
}
}
}
}
if (work.getPutCode() != null && createFlag) {
Map<String, String> params = new HashMap<String, String>();
if (sourceEntity != null) {
params.put("clientName", sourceEntity.getSourceName());
}
throw new InvalidPutCodeException(params);
}
// Check that we are not changing the visibility
if (isApiRequest && !createFlag) {
Visibility updatedVisibility = work.getVisibility();
validateVisibilityDoesntChange(updatedVisibility, originalVisibility);
}
externalIDValidator.validateWorkOrPeerReview(work.getExternalIdentifiers());
}
use of org.orcid.core.exception.OrcidValidationException in project ORCID-Source by ORCID.
the class ActivityValidator method validateGroupIdRecord.
public void validateGroupIdRecord(GroupIdRecord groupIdRecord, boolean createFlag, SourceEntity sourceEntity) {
if (createFlag) {
if (groupIdRecord.getPutCode() != null) {
Map<String, String> params = new HashMap<String, String>();
if (sourceEntity != null) {
params.put("clientName", sourceEntity.getSourceName());
}
throw new InvalidPutCodeException(params);
}
}
Pattern validGroupIdRegexPattern = Pattern.compile("(ringgold:|issn:|orcid-generated:|fundref:|publons:)([0-9a-zA-Z\\^._~:/?#\\[\\]@!$&'()*+,;=-]){2,}");
Matcher matcher = validGroupIdRegexPattern.matcher(groupIdRecord.getGroupId());
if (!matcher.matches()) {
throw new OrcidValidationException("Invalid group-id: '" + groupIdRecord.getGroupId() + "'");
}
}
use of org.orcid.core.exception.OrcidValidationException in project ORCID-Source by ORCID.
the class TestXmlValidity method testAllOrcidMessages.
@Test
public void testAllOrcidMessages() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:**/*message-latest.xml");
for (Resource resource : resources) {
LOG.info("Found resource: {}", resource);
InputStream is = null;
try {
is = resource.getInputStream();
OrcidMessage message = (OrcidMessage) unmarshaller.unmarshal(is);
validationManager = getValidationManager(message.getMessageVersion());
validationManager.validateMessage(message);
} catch (IOException e) {
Assert.fail("Unable to read resource: " + resource + "\n" + e);
} catch (JAXBException e) {
Assert.fail("ORCID message is not well formed: " + resource + "\n" + e);
} catch (OrcidValidationException e) {
Assert.fail("Validation failed: " + resource + "\n" + e.getCause());
} finally {
IOUtils.closeQuietly(is);
}
}
}
use of org.orcid.core.exception.OrcidValidationException in project ORCID-Source by ORCID.
the class ActivityValidatorTest method validateWork_invalidPublicationDateTest.
@Test
public void validateWork_invalidPublicationDateTest() {
try {
Work work = getWork();
work.getPublicationDate().getYear().setValue("invalid");
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (ActivityTypeValidationException e) {
}
try {
Work work = getWork();
work.getPublicationDate().getMonth().setValue("invalid");
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (ActivityTypeValidationException e) {
}
try {
Work work = getWork();
work.getPublicationDate().getDay().setValue("invalid");
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (ActivityTypeValidationException e) {
}
try {
Work work = getWork();
work.setPublicationDate(new PublicationDate(null, new Month(1), new Day(1)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
try {
Work work = getWork();
work.setPublicationDate(new PublicationDate(new Year(2017), null, new Day(1)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
try {
Work work = getWork();
work.setPublicationDate(new PublicationDate(null, null, new Day(1)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
try {
Work work = getWork();
//Invalid 2 digits year
work.setPublicationDate(new PublicationDate(new Year(25), new Month(1), new Day(1)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
try {
Work work = getWork();
//Invalid 3 digits month
work.setPublicationDate(new PublicationDate(new Year(2017), new Month(100), new Day(1)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
try {
Work work = getWork();
//Invalid 3 digits day
work.setPublicationDate(new PublicationDate(new Year(2017), new Month(1), new Day(100)));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
fail();
} catch (OrcidValidationException e) {
}
Work work = getWork();
work.setPublicationDate(new PublicationDate(new Year(2017), new Month(1), null));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
work = getWork();
work.setPublicationDate(new PublicationDate(new Year(2017), null, null));
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
work = getWork();
activityValidator.validateWork(work, null, true, true, Visibility.PUBLIC);
}
Aggregations