use of org.gbif.ipt.service.admin.RegistrationManager in project ipt by gbif.
the class PublishAllResourcesActionTest method setup.
@BeforeEach
public void setup() throws IOException, ParserConfigurationException, SAXException, AlreadyExistingException, ImportException, InvalidFilenameException {
ResourceManagerImplTest test = new ResourceManagerImplTest();
ResourceManagerImpl mockResourceManager = test.getResourceManagerImpl();
// prepare and add resource
Resource resource = test.getNonRegisteredMetadataOnlyResource();
// ensure resource has mandatory metadata filled in, meaning its EML validates and it has a valid publishing org
Eml eml = EmlFactory.build(FileUtils.classpathStream("data/eml.xml"));
eml.setEmlVersion(BigDecimal.valueOf(3.0));
eml.setPreviousEmlVersion(BigDecimal.valueOf(1.0));
resource.setEml(eml);
// assign publishing organisation to resource
Organisation o = new Organisation();
o.setName("TestOrg");
o.setKey(UUID.randomUUID().toString());
resource.setOrganisation(o);
// mock successful lookup for organization by key (done by RegistrationManager in EmlValidator)
RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
when(mockRegistrationManager.get(any(UUID.class))).thenReturn(o);
mockResourceManager.save(resource);
// mock generateDwca() throwing PublicationException, not actually possible, but used to test failed publications
GenerateDwcaFactory mockDwcaFactory = mockResourceManager.getDwcaFactory();
when(mockDwcaFactory.create(any(Resource.class), any(ReportHandler.class))).thenThrow(new PublicationException(PublicationException.TYPE.DWCA, "Mock exception"));
// mock finding versioned EML file - not important which one
File emlXML = File.createTempFile("eml-1.1", ".xml");
when(test.getMockedDataDir().resourceEmlFile(anyString(), any(BigDecimal.class))).thenReturn(emlXML);
// mock finding versioned RTF file - not important which one
File rtf = File.createTempFile("short-1.1", ".rtf");
when(test.getMockedDataDir().resourceRtfFile(anyString(), any(BigDecimal.class))).thenReturn(rtf);
// mock action
action = new PublishAllResourcesAction(mock(SimpleTextProvider.class), mock(AppConfig.class), mockRegistrationManager, mockResourceManager, mock(RegistryManager.class));
}
use of org.gbif.ipt.service.admin.RegistrationManager in project ipt by gbif.
the class OverviewActionOtherIT method testReuseAndReserveExistingRegisteredDoiFailsWrongTarget.
/**
* Test reserving an existing registered DOI (reusing an existing registered DOI) using GBIF's test DataCite account.
* </b>
* But, make it fail by making it use the wrong target URI.
*/
@Test
public void testReuseAndReserveExistingRegisteredDoiFailsWrongTarget() throws Exception {
// common mock AppConfig
AppConfig mockAppConfig = mock(AppConfig.class);
DataDir mockDataDir = mock(DataDir.class);
when(mockAppConfig.getDataDir()).thenReturn(mockDataDir);
// mock returning target URLs
when(mockAppConfig.getResourceUri(anyString())).thenReturn(new URI("http://ipt.gbif-uat.org/resource?r=wrong"));
RegistrationManager mockRegistrationManagerDataCite = mock(RegistrationManager.class);
Organisation oDataCiteGBIF = new Organisation();
oDataCiteGBIF.setKey(ORGANISATION_KEY.toString());
oDataCiteGBIF.setAgencyAccountPrimary(true);
oDataCiteGBIF.setName("GBIF");
oDataCiteGBIF.setDoiPrefix("10.21373");
oDataCiteGBIF.setCanHost(true);
oDataCiteGBIF.setAgencyAccountUsername(cfg.getUser());
oDataCiteGBIF.setAgencyAccountPassword(cfg.getPassword());
oDataCiteGBIF.setDoiRegistrationAgency(DOIRegistrationAgency.DATACITE);
when(mockRegistrationManagerDataCite.findPrimaryDoiAgencyAccount()).thenReturn(oDataCiteGBIF);
when(mockRegistrationManagerDataCite.get(any(UUID.class))).thenReturn(oDataCiteGBIF);
when(mockRegistrationManagerDataCite.getDoiService()).thenReturn(dataCiteService);
// mock action for DataCite
action = new OverviewAction(mock(SimpleTextProvider.class), mockAppConfig, mockRegistrationManagerDataCite, mock(ResourceManager.class), mock(UserAccountManager.class), mock(ExtensionManager.class), mock(GenerateDwcaFactory.class), mock(VocabulariesManager.class), mock(RegistryManager.class));
LOG.info("Testing DataCite with GBIF test Prefix...");
action.setReserveDoi("true");
action.setResource(r);
assertNull(r.getDoi());
assertEquals(IdentifierStatus.UNRESERVED, r.getIdentifierStatus());
assertNotNull(r.getEml().getCitation());
assertNull(r.getEml().getCitation().getIdentifier());
DOI existingDOI = new DOI("doi:10.15469/xhav6t");
// set citation identifier equal to existing registered DOI (doi:10.15469/xhav6t) - this should get reused
r.setCitationAutoGenerated(true);
r.getEml().setCitation(new Citation("Replaced by auto-generated citation", existingDOI.toString()));
// start with private resource - error: resource must be public
r.setStatus(PublicationStatus.PUBLIC);
action.reserveDoi();
// 1 error is expected for the resource having wrong target URI
assertEquals(1, action.getActionErrors().size());
// make sure the existing DOI was NOT reused
assertNull(r.getDoi());
assertNull(r.getDoiOrganisationKey());
assertEquals(IdentifierStatus.UNRESERVED, r.getIdentifierStatus());
// alternate ids updated
assertEquals(0, r.getEml().getAlternateIdentifiers().size());
LOG.info("Existing DOI was NOT reused because resource was private and had wrong target URI");
}
use of org.gbif.ipt.service.admin.RegistrationManager in project ipt by gbif.
the class OverviewActionTest method testUndeleteButResourceOrganisationNoLongerInIPT.
@Test
public void testUndeleteButResourceOrganisationNoLongerInIPT() {
Resource r = new Resource();
DOI doiToUndelete = DOIUtils.mintDOI(DOIRegistrationAgency.DATACITE, Constants.TEST_DOI_PREFIX);
BigDecimal versionToUndelete = new BigDecimal("1.0");
VersionHistory vh = new VersionHistory(versionToUndelete, new Date(), PublicationStatus.PUBLIC);
vh.setDoi(doiToUndelete);
vh.setStatus(IdentifierStatus.PUBLIC);
r.addVersionHistory(vh);
// resource deleted!
r.setStatus(PublicationStatus.DELETED);
// mock organisation that is not associated to IPT
Organisation org = new Organisation();
org.setKey(UUID.randomUUID().toString());
r.setOrganisation(org);
// mock RegistrationManager returning mock DoiService
RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
DoiService mockDataCiteService = mock(RestJsonApiDataCiteService.class);
when(mockRegistrationManager.getDoiService()).thenReturn(mockDataCiteService);
// mock action
action = new OverviewAction(mock(SimpleTextProvider.class), mock(AppConfig.class), mockRegistrationManager, mock(ResourceManager.class), mock(UserAccountManager.class), mock(ExtensionManager.class), mock(GenerateDwcaFactory.class), mock(VocabulariesManager.class), mock(RegistryManager.class));
action.setResource(r);
action.setUndelete("true");
assertEquals("input", action.undelete());
assertEquals(1, action.getActionErrors().size());
}
use of org.gbif.ipt.service.admin.RegistrationManager in project ipt by gbif.
the class ResourceManagerImplIT method data.
public static Stream<Arguments> data() throws IOException {
// Mock classes
AppConfig mockAppConfig = mock(AppConfig.class);
// mock returning resource URI in gbif-uat belong
when(mockAppConfig.getResourceUri(anyString())).thenReturn(UriBuilder.fromPath("http://www.gbif-uat.org:7001/ipt?r=ants").build());
when(mockAppConfig.getResourceVersionUri("ants", new BigDecimal("1.1"))).thenReturn(UriBuilder.fromPath("http://www.gbif-uat.org:7001/ipt?r=ants&v=1.1").build());
when(mockAppConfig.getMaxThreads()).thenReturn(3);
UserAccountManager mockUserAccountManager = mock(UserAccountManager.class);
UserEmailConverter mockEmailConverter = new UserEmailConverter(mockUserAccountManager);
ExtensionRowTypeConverter mockExtensionRowTypeConverter = mock(ExtensionRowTypeConverter.class);
ExtensionManager mockExtensionManager = mock(ExtensionManager.class);
JdbcInfoConverter mockJdbcConverter = mock(JdbcInfoConverter.class);
SourceManager mockSourceManager = mock(SourceManager.class);
RegistryManager mockRegistryManager = MockRegistryManager.buildMock();
GenerateDwcaFactory mockDwcaFactory = mock(GenerateDwcaFactory.class);
PasswordEncrypter mockPasswordEncrypter = mock(PasswordEncrypter.class);
Eml2Rtf mockEml2Rtf = mock(Eml2Rtf.class);
VocabulariesManager mockVocabulariesManager = mock(VocabulariesManager.class);
SimpleTextProvider mockSimpleTextProvider = mock(SimpleTextProvider.class);
ConceptTermConverter mockConceptTermConverter = mock(ConceptTermConverter.class);
// persist eml file for version 1.0 (contents written later)
TMP_EML_FILE = File.createTempFile("eml-1.0", ".xml");
when(MOCK_DATA_DIR.resourceEmlFile("ants", new BigDecimal("1.1"))).thenReturn(TMP_EML_FILE);
// DataCite parameters..
RegistrationManager mockRegistrationManagerDataCite = mock(RegistrationManager.class);
Properties p = PropertiesUtil.loadProperties("datacite.properties");
ClientConfiguration cfg = ClientConfiguration.builder().withBaseApiUrl(p.getProperty("baseApiUrl")).withTimeOut(Long.valueOf(p.getProperty("timeOut"))).withFileCacheMaxSizeMb(Long.valueOf(p.getProperty("fileCacheMaxSizeMb"))).withUser(p.getProperty("user")).withPassword(p.getProperty("password")).build();
// LOG.info("DataCite password (read from Maven property datacite.password)= " + dcCfg.getPassword());
Organisation oDataCite = new Organisation();
oDataCite.setAgencyAccountPrimary(true);
oDataCite.setName("GBIF");
oDataCite.setDoiPrefix(Constants.TEST_DOI_PREFIX);
oDataCite.setCanHost(true);
oDataCite.setAgencyAccountUsername(cfg.getUser());
oDataCite.setAgencyAccountPassword(cfg.getPassword());
oDataCite.setDoiRegistrationAgency(DOIRegistrationAgency.DATACITE);
// mock returning primary DOI agency account
when(mockRegistrationManagerDataCite.findPrimaryDoiAgencyAccount()).thenReturn(oDataCite);
// mock returning DataCite service
DoiService dataCiteService = new RestJsonApiDataCiteService(cfg.getBaseApiUrl(), cfg.getUser(), cfg.getPassword());
when(mockRegistrationManagerDataCite.getDoiService()).thenReturn(dataCiteService);
// mock ResourceManagerImpl for DataCite
ResourceManagerImpl managerDataCite = new ResourceManagerImpl(mockAppConfig, MOCK_DATA_DIR, mockEmailConverter, new OrganisationKeyConverter(mockRegistrationManagerDataCite), mockExtensionRowTypeConverter, mockJdbcConverter, mockSourceManager, mockExtensionManager, mockRegistryManager, mockConceptTermConverter, mockDwcaFactory, mockPasswordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManagerDataCite);
return Stream.of(Arguments.of(managerDataCite, DOIRegistrationAgency.DATACITE, DOIUtils.mintDOI(DOIRegistrationAgency.DATACITE, Constants.TEST_DOI_PREFIX), mockRegistrationManagerDataCite));
}
use of org.gbif.ipt.service.admin.RegistrationManager in project ipt by gbif.
the class GenerateDwcaEventTest method getResource.
/**
* Generates a test sampling event resource having an event core with occurrence extension.
*/
private Resource getResource(@NotNull File resourceXML, @NotNull File eventSourceFile, @NotNull File occurrenceSourceFile) throws IOException, SAXException, ParserConfigurationException, AlreadyExistingException, ImportException, InvalidFilenameException {
UserAccountManager mockUserAccountManager = mock(UserAccountManager.class);
UserEmailConverter mockEmailConverter = new UserEmailConverter(mockUserAccountManager);
RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
OrganisationKeyConverter mockOrganisationKeyConverter = new OrganisationKeyConverter(mockRegistrationManager);
RegistryManager mockRegistryManager = MockRegistryManager.buildMock();
GenerateDwcaFactory mockDwcaFactory = mock(GenerateDwcaFactory.class);
Eml2Rtf mockEml2Rtf = mock(Eml2Rtf.class);
VocabulariesManager mockVocabulariesManager = mock(VocabulariesManager.class);
SimpleTextProvider mockSimpleTextProvider = mock(SimpleTextProvider.class);
mockHandler = mock(ResourceManagerImpl.class);
resourceDir = FileUtils.createTempDir();
BaseAction baseAction = new BaseAction(mockSimpleTextProvider, mockAppConfig, mockRegistrationManager);
// construct ExtensionFactory using injected parameters
Injector injector = Guice.createInjector(new ServletModule(), new Struts2GuicePluginModule(), new IPTModule());
HttpClient httpClient = injector.getInstance(HttpClient.class);
ThesaurusHandlingRule thesaurusRule = new ThesaurusHandlingRule(mock(VocabulariesManagerImpl.class));
SAXParserFactory saxf = injector.getInstance(SAXParserFactory.class);
ExtensionFactory extensionFactory = new ExtensionFactory(thesaurusRule, saxf, httpClient);
JdbcSupport support = injector.getInstance(JdbcSupport.class);
PasswordEncrypter passwordEncrypter = injector.getInstance(PasswordEncrypter.class);
JdbcInfoConverter jdbcConverter = new JdbcInfoConverter(support);
ExtensionManager extensionManager = mock(ExtensionManager.class);
// construct event core Extension
InputStream eventCoreIs = GenerateDwcaTest.class.getResourceAsStream("/extensions/dwc_event_2015-04-24.xml");
Extension eventCore = extensionFactory.build(eventCoreIs);
// construct occurrence core Extension
InputStream occurrenceCoreIs = GenerateDwcaTest.class.getResourceAsStream("/extensions/dwc_occurrence_2015-04-24.xml");
Extension occurrenceCore = extensionFactory.build(occurrenceCoreIs);
// mock ExtensionManager returning occurrence core Extension
when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Occurrence")).thenReturn(occurrenceCore);
when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Event")).thenReturn(eventCore);
ExtensionRowTypeConverter extensionRowTypeConverter = new ExtensionRowTypeConverter(extensionManager);
ConceptTermConverter conceptTermConverter = new ConceptTermConverter(extensionRowTypeConverter);
// mock finding resource.xml file
when(mockDataDir.resourceFile(anyString(), anyString())).thenReturn(resourceXML);
// retrieve sample zipped resource folder
File zippedResourceFolder = FileUtils.getClasspathFile("resources/event/event.zip");
// retrieve sample eml.xml file
File emlXML = FileUtils.getClasspathFile("resources/event/eml.xml");
// mock finding eml.xml file
when(mockDataDir.resourceEmlFile(anyString())).thenReturn(emlXML);
// mock finding dwca.zip file that does not exist
when(mockDataDir.resourceDwcaFile(anyString())).thenReturn(new File("dwca.zip"));
// create SourceManagerImpl
mockSourceManager = new SourceManagerImpl(mock(AppConfig.class), mockDataDir);
// create temp directory
File tmpDataDir = FileUtils.createTempDir();
when(mockDataDir.tmpDir()).thenReturn(tmpDataDir);
// publication log file
File publicationLogFile = new File(resourceDir, DataDir.PUBLICATION_LOG_FILENAME);
when(mockDataDir.resourcePublicationLogFile(RESOURCE_SHORTNAME)).thenReturn(publicationLogFile);
// create ResourceManagerImpl
ResourceManagerImpl resourceManager = new ResourceManagerImpl(mockAppConfig, mockDataDir, mockEmailConverter, mockOrganisationKeyConverter, extensionRowTypeConverter, jdbcConverter, mockSourceManager, extensionManager, mockRegistryManager, conceptTermConverter, mockDwcaFactory, passwordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManager);
// create user
User creator = new User();
creator.setFirstname("Leonardo");
creator.setLastname("Pisano");
creator.setEmail("fi@liberabaci.com");
creator.setLastLoginToNow();
creator.setRole(User.Role.Manager);
creator.setPassword("011235813");
// create a new resource.
resource = resourceManager.create(RESOURCE_SHORTNAME, Resource.CoreRowType.SAMPLINGEVENT.toString(), zippedResourceFolder, creator, baseAction);
// copy event source file to tmp folder
File copiedEvent = new File(resourceDir, "event.txt");
// mock file to which event source file gets copied to
when(mockDataDir.sourceFile(any(Resource.class), any(FileSource.class))).thenReturn(copiedEvent);
// mock log file
when(mockDataDir.sourceLogFile(anyString(), anyString())).thenReturn(new File(resourceDir, "log.txt"));
// add event fileSource to test Resource
FileSource eventFileSource = mockSourceManager.add(resource, eventSourceFile, "event.txt");
resource.getMappings().get(0).setSource(eventFileSource);
// copy occurrence source file to tmp folder
File copiedOccurrence = new File(resourceDir, "occurrence.txt");
// mock file to which occurrence source file gets copied to
when(mockDataDir.sourceFile(any(Resource.class), any(FileSource.class))).thenReturn(copiedOccurrence);
// add occurrence fileSource to test Resource
FileSource occurrenceFileSource = mockSourceManager.add(resource, occurrenceSourceFile, "occurrence.txt");
resource.getMappings().get(1).setSource(occurrenceFileSource);
// mock creation of zipped dwca in temp directory - this later becomes the actual archive generated
when(mockDataDir.tmpFile(anyString(), anyString())).thenReturn(new File(tmpDataDir, "dwca.zip"));
// mock creation of versioned zipped dwca in resource directory
when(mockDataDir.resourceDwcaFile(anyString(), any(BigDecimal.class))).thenReturn(new File(resourceDir, VERSIONED_ARCHIVE_FILENAME));
return resource;
}
Aggregations