use of org.orcid.core.exception.ActivityIdentifierValidationException in project ORCID-Source by ORCID.
the class MemberV2ApiServiceDelegator_FundingTest method testAddFundingWithInvalidExtIdTypeFail.
@Test
public void testAddFundingWithInvalidExtIdTypeFail() {
String orcid = "4444-4444-4444-4499";
SecurityContextTestUtils.setUpSecurityContext(orcid, ScopePathType.ACTIVITIES_READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
Funding funding = Utils.getFunding();
try {
funding.getExternalIdentifiers().getExternalIdentifier().get(0).setType("INVALID");
serviceDelegator.createFunding(orcid, funding);
fail();
} catch (ActivityIdentifierValidationException e) {
} catch (Exception e) {
fail();
}
funding.getExternalIdentifiers().getExternalIdentifier().get(0).setType("grant_number");
Response response = serviceDelegator.createFunding(orcid, funding);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
Map<?, ?> map = response.getMetadata();
assertNotNull(map);
assertTrue(map.containsKey("Location"));
List<?> resultWithPutCode = (List<?>) map.get("Location");
Long putCode = Long.valueOf(String.valueOf(resultWithPutCode.get(0)));
// Delete it to roll back the test data
response = serviceDelegator.deleteFunding(orcid, putCode);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
}
use of org.orcid.core.exception.ActivityIdentifierValidationException 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.ActivityIdentifierValidationException in project ORCID-Source by ORCID.
the class MemberV2ApiServiceDelegator_PeerReviewsTest method testAddPeerReviewWithInvalidExtIdTypeFail.
@Test
public void testAddPeerReviewWithInvalidExtIdTypeFail() {
String orcid = "4444-4444-4444-4499";
SecurityContextTestUtils.setUpSecurityContext(orcid, ScopePathType.ACTIVITIES_READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
PeerReview peerReview = Utils.getPeerReview();
// Set both to a correct value
peerReview.getExternalIdentifiers().getExternalIdentifier().get(0).setType("doi");
peerReview.getSubjectExternalIdentifier().setType("doi");
// Check it fail on external identifier type
try {
peerReview.getExternalIdentifiers().getExternalIdentifier().get(0).setType("INVALID");
serviceDelegator.createPeerReview(orcid, peerReview);
fail();
} catch (ActivityIdentifierValidationException e) {
} catch (Exception e) {
fail();
}
/*
* This case is now ok (external-id-api branch 05/16) - adapters ensure
* correct value is stored in DB. try {
* peerReview.getExternalIdentifiers().getExternalIdentifier().get(0).
* setType("DOI"); serviceDelegator.createPeerReview(orcid, peerReview);
* fail(); } catch(ActivityIdentifierValidationException e) {
*
* } catch(Exception e) { fail(); }
*/
// Set the ext id to a correct value to test the subject ext id
peerReview.getExternalIdentifiers().getExternalIdentifier().get(0).setType("doi");
// Check it fail on subject external identifier type
try {
peerReview.getSubjectExternalIdentifier().setType("INVALID");
serviceDelegator.createPeerReview(orcid, peerReview);
fail();
} catch (ActivityIdentifierValidationException e) {
} catch (Exception e) {
fail();
}
/*
* try { peerReview.getSubjectExternalIdentifier().setType("DOI");
* serviceDelegator.createPeerReview(orcid, peerReview); fail(); }
* catch(ActivityIdentifierValidationException e) {
*
* } catch(Exception e) { fail(); }
*/
// Test it works with correct values
peerReview.getExternalIdentifiers().getExternalIdentifier().get(0).setType("doi");
peerReview.getSubjectExternalIdentifier().setType("doi");
Response response = serviceDelegator.createPeerReview(orcid, peerReview);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
Map<?, ?> map = response.getMetadata();
assertNotNull(map);
assertTrue(map.containsKey("Location"));
List<?> resultWithPutCode = (List<?>) map.get("Location");
Long putCode = Long.valueOf(String.valueOf(resultWithPutCode.get(0)));
// Delete it to roll back the test data
response = serviceDelegator.deletePeerReview(orcid, putCode);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
}
use of org.orcid.core.exception.ActivityIdentifierValidationException in project ORCID-Source by ORCID.
the class MemberV2ApiServiceDelegator_WorksTest method testAddWorkWithInvalidExtIdTypeFail.
@Test
public void testAddWorkWithInvalidExtIdTypeFail() {
String orcid = "4444-4444-4444-4499";
SecurityContextTestUtils.setUpSecurityContext(orcid, ScopePathType.ACTIVITIES_READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
Work work = Utils.getWork("work # 1 " + System.currentTimeMillis());
try {
work.getExternalIdentifiers().getExternalIdentifier().get(0).setType("INVALID");
serviceDelegator.createWork(orcid, work);
fail();
} catch (ActivityIdentifierValidationException e) {
} catch (Exception e) {
fail();
}
// Assert that it could be created with a valid value
work.getExternalIdentifiers().getExternalIdentifier().get(0).setType("doi");
Response response = serviceDelegator.createWork(orcid, work);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
Map<?, ?> map = response.getMetadata();
assertNotNull(map);
assertTrue(map.containsKey("Location"));
List<?> resultWithPutCode = (List<?>) map.get("Location");
Long putCode = Long.valueOf(String.valueOf(resultWithPutCode.get(0)));
// Delete it to roll back the test data
response = serviceDelegator.deleteWork(orcid, putCode);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
}
Aggregations