Search in sources :

Example 1 with ExtensionFactory

use of org.gbif.ipt.model.factory.ExtensionFactory 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;
}
Also used : User(org.gbif.ipt.model.User) IPTModule(org.gbif.ipt.config.IPTModule) JdbcSupport(org.gbif.ipt.config.JdbcSupport) ServletModule(com.google.inject.servlet.ServletModule) UserAccountManager(org.gbif.ipt.service.admin.UserAccountManager) OrganisationKeyConverter(org.gbif.ipt.model.converter.OrganisationKeyConverter) Injector(com.google.inject.Injector) MockRegistryManager(org.gbif.ipt.mock.MockRegistryManager) RegistryManager(org.gbif.ipt.service.registry.RegistryManager) ConceptTermConverter(org.gbif.ipt.model.converter.ConceptTermConverter) ExtensionRowTypeConverter(org.gbif.ipt.model.converter.ExtensionRowTypeConverter) RegistrationManager(org.gbif.ipt.service.admin.RegistrationManager) InputStream(java.io.InputStream) ResourceManagerImpl(org.gbif.ipt.service.manage.impl.ResourceManagerImpl) FileSource(org.gbif.ipt.model.FileSource) Resource(org.gbif.ipt.model.Resource) BaseAction(org.gbif.ipt.action.BaseAction) SourceManagerImpl(org.gbif.ipt.service.manage.impl.SourceManagerImpl) UserEmailConverter(org.gbif.ipt.model.converter.UserEmailConverter) VocabulariesManager(org.gbif.ipt.service.admin.VocabulariesManager) JdbcInfoConverter(org.gbif.ipt.model.converter.JdbcInfoConverter) BigDecimal(java.math.BigDecimal) Extension(org.gbif.ipt.model.Extension) PasswordEncrypter(org.gbif.ipt.model.converter.PasswordEncrypter) VocabulariesManagerImpl(org.gbif.ipt.service.admin.impl.VocabulariesManagerImpl) ExtensionManager(org.gbif.ipt.service.admin.ExtensionManager) Struts2GuicePluginModule(com.google.inject.struts2.Struts2GuicePluginModule) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) SimpleTextProvider(org.gbif.ipt.struts2.SimpleTextProvider) HttpClient(org.gbif.utils.HttpClient) ThesaurusHandlingRule(org.gbif.ipt.model.factory.ThesaurusHandlingRule) ArchiveFile(org.gbif.dwc.ArchiveFile) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with ExtensionFactory

use of org.gbif.ipt.model.factory.ExtensionFactory in project ipt by gbif.

the class ResourceManagerImplTest method getResourceManagerImpl.

public ResourceManagerImpl getResourceManagerImpl() throws IOException, SAXException, ParserConfigurationException {
    // mock creation of datasetSubtypes Map, with 2 occurrence subtypes, and 6 checklist subtypes
    Map<String, String> datasetSubtypes = new LinkedHashMap<>();
    datasetSubtypes.put("", "Select a subtype");
    datasetSubtypes.put("taxonomicAuthority", "Taxonomic Authority");
    datasetSubtypes.put("nomenclatorAuthority", "Nomenclator Authority");
    datasetSubtypes.put("inventoryThematic", "Inventory Thematic");
    datasetSubtypes.put("inventoryRegional", "Inventory Regional");
    datasetSubtypes.put("globalSpeciesDataset", "Global Species Dataset");
    datasetSubtypes.put("derivedFromOccurrence", "Derived from Occurrence");
    datasetSubtypes.put(DATASET_SUBTYPE_SPECIMEN_IDENTIFIER, "Specimen");
    datasetSubtypes.put("observation", "Observation");
    // mock getting the vocabulary
    when(mockVocabulariesManager.getI18nVocab(anyString(), anyString(), anyBoolean())).thenReturn(datasetSubtypes);
    // mock the cfg
    when(mockAppConfig.getBaseUrl()).thenReturn("http://localhost:7001/ipt");
    // mock resource link used as EML GUID
    when(mockAppConfig.getResourceGuid("bees")).thenReturn("http://localhost:7001/ipt/resource?id=bees");
    when(mockAppConfig.getResourceGuid("res2")).thenReturn("http://localhost:7001/ipt/resource?id=res2");
    // 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);
    support = injector.getInstance(JdbcSupport.class);
    PasswordEncrypter passwordEncrypter = injector.getInstance(PasswordEncrypter.class);
    JdbcInfoConverter jdbcConverter = new JdbcInfoConverter(support);
    // construct occurrence core Extension
    InputStream occurrenceCoreIs = ResourceManagerImplTest.class.getResourceAsStream("/extensions/dwc_occurrence.xml");
    Extension occurrenceCore = extensionFactory.build(occurrenceCoreIs);
    // construct occurrence core Extension
    InputStream eventCoreIs = ResourceManagerImplTest.class.getResourceAsStream("/extensions/dwc_event_2015-04-24.xml");
    Extension eventCore = extensionFactory.build(eventCoreIs);
    // construct simple images extension
    InputStream simpleImageIs = ResourceManagerImplTest.class.getResourceAsStream("/extensions/simple_image.xml");
    Extension simpleImage = extensionFactory.build(simpleImageIs);
    ExtensionManager extensionManager = mock(ExtensionManager.class);
    // mock ExtensionManager returning different Extensions
    when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Occurrence")).thenReturn(occurrenceCore);
    when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Event")).thenReturn(eventCore);
    when(extensionManager.get("http://rs.tdwg.org/dwc/xsd/simpledarwincore/SimpleDarwinRecord")).thenReturn(occurrenceCore);
    when(extensionManager.get("http://rs.gbif.org/terms/1.0/Image")).thenReturn(simpleImage);
    ExtensionRowTypeConverter extensionRowTypeConverter = new ExtensionRowTypeConverter(extensionManager);
    ConceptTermConverter conceptTermConverter = new ConceptTermConverter(extensionRowTypeConverter);
    // mock finding dwca.zip file that does not exist
    when(mockedDataDir.resourceDwcaFile(anyString())).thenReturn(new File("dwca.zip"));
    return new ResourceManagerImpl(mockAppConfig, mockedDataDir, mockEmailConverter, mockOrganisationKeyConverter, extensionRowTypeConverter, jdbcConverter, mockSourceManager, extensionManager, mockRegistryManager, conceptTermConverter, mockDwcaFactory, passwordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManager);
}
Also used : IPTModule(org.gbif.ipt.config.IPTModule) InputStream(java.io.InputStream) JdbcSupport(org.gbif.ipt.config.JdbcSupport) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JdbcInfoConverter(org.gbif.ipt.model.converter.JdbcInfoConverter) ServletModule(com.google.inject.servlet.ServletModule) LinkedHashMap(java.util.LinkedHashMap) Extension(org.gbif.ipt.model.Extension) PasswordEncrypter(org.gbif.ipt.model.converter.PasswordEncrypter) VocabulariesManagerImpl(org.gbif.ipt.service.admin.impl.VocabulariesManagerImpl) ExtensionManager(org.gbif.ipt.service.admin.ExtensionManager) Injector(com.google.inject.Injector) Struts2GuicePluginModule(com.google.inject.struts2.Struts2GuicePluginModule) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) HttpClient(org.gbif.utils.HttpClient) ThesaurusHandlingRule(org.gbif.ipt.model.factory.ThesaurusHandlingRule) ConceptTermConverter(org.gbif.ipt.model.converter.ConceptTermConverter) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExtensionRowTypeConverter(org.gbif.ipt.model.converter.ExtensionRowTypeConverter)

Example 3 with ExtensionFactory

use of org.gbif.ipt.model.factory.ExtensionFactory in project ipt by gbif.

the class GenerateDCATTest method getResource.

/**
 * Generates a test Resource from zipped resource folder, and populates resource with license, contacts, etc.
 *
 * @param resourceXML resource (XML) configuration file defining column mapping of sourceFile
 *
 * @return test Resource
 */
private Resource getResource(@NotNull File resourceXML) throws IOException, SAXException, ParserConfigurationException, AlreadyExistingException, ImportException, InvalidFilenameException {
    UserAccountManager mockUserAccountManager = mock(UserAccountManager.class);
    UserEmailConverter mockEmailConverter = new UserEmailConverter(mockUserAccountManager);
    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);
    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);
    // construct occurrence core Extension
    InputStream occurrenceCoreIs = GenerateDwcaTest.class.getResourceAsStream("/extensions/dwc_occurrence_2015-04-24.xml");
    Extension occurrenceCore = extensionFactory.build(occurrenceCoreIs);
    ExtensionManager extensionManager = mock(ExtensionManager.class);
    // mock ExtensionManager returning occurrence core Extension
    when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Occurrence")).thenReturn(occurrenceCore);
    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/res1.zip");
    // retrieve sample eml.xml file
    File emlXML = FileUtils.getClasspathFile("resources/res1/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
    SourceManager mockSourceManager = new SourceManagerImpl(mock(AppConfig.class), mockDataDir);
    // create temp directory
    File tmpDataDir = FileUtils.createTempDir();
    when(mockDataDir.tmpDir()).thenReturn(tmpDataDir);
    // create ResourceManagerImpl
    ResourceManagerImpl resourceManager = new ResourceManagerImpl(mockAppConfig, mockDataDir, mockEmailConverter, mockOrganisationKeyConverter, extensionRowTypeConverter, jdbcConverter, mockSourceManager, extensionManager, mockRegistryManager, conceptTermConverter, mockDwcaFactory, passwordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManager);
    // creator
    User creator = new User();
    creator.setEmail("jcuadra@gbif.org");
    creator.setLastname("Cuadra");
    // create a new resource.
    Resource resource = resourceManager.create(RESOURCE_SHORTNAME, null, zippedResourceFolder, creator, baseAction);
    // update resource title and description, to have double quotation marks which need to be escaped
    resource.setTitle("TEST \"RESOURCE\"");
    resource.getEml().getDescription().set(0, "Test \"description\"");
    // update keyword sets: should be three, with "Occurrence" and "Observation" repeating more than once which breaks the feed
    resource.getEml().getKeywords().clear();
    KeywordSet keywordSet1 = new KeywordSet();
    keywordSet1.add("Phytosociology");
    keywordSet1.add("Occurrence");
    keywordSet1.add("Observation");
    keywordSet1.setKeywordThesaurus("n/a");
    KeywordSet keywordSet2 = new KeywordSet();
    keywordSet2.add("Occurrence");
    keywordSet2.setKeywordThesaurus("http://rs.gbif.org/vocabulary/gbif/dataset_type.xml");
    KeywordSet keywordSet3 = new KeywordSet();
    keywordSet3.add("Observation");
    keywordSet3.setKeywordThesaurus("http://rs.gbif.org/vocabulary/gbif/dataset_subtype.xml");
    resource.getEml().addKeywordSet(keywordSet1);
    resource.getEml().addKeywordSet(keywordSet2);
    resource.getEml().addKeywordSet(keywordSet3);
    // CCO
    resource.getEml().setIntellectualRights("This work is licensed under <a href=\"http://creativecommons.org/publicdomain/zero/1.0/legalcode\">Creative Commons CCZero (CC0) 1.0 License</a>.");
    assertEquals("http://creativecommons.org/publicdomain/zero/1.0/legalcode", resource.getEml().parseLicenseUrl());
    // set creator, contact, and metadata provider
    Agent agent = new Agent();
    agent.setFirstName("Eric");
    agent.setLastName("Stienen");
    agent.setEmail("eric.stienen@inbo.be");
    resource.getEml().addCreator(agent);
    resource.getEml().addContact(agent);
    resource.getEml().addMetadataProvider(agent);
    return resource;
}
Also used : User(org.gbif.ipt.model.User) IPTModule(org.gbif.ipt.config.IPTModule) JdbcSupport(org.gbif.ipt.config.JdbcSupport) ServletModule(com.google.inject.servlet.ServletModule) KeywordSet(org.gbif.metadata.eml.KeywordSet) UserAccountManager(org.gbif.ipt.service.admin.UserAccountManager) OrganisationKeyConverter(org.gbif.ipt.model.converter.OrganisationKeyConverter) Injector(com.google.inject.Injector) MockRegistryManager(org.gbif.ipt.mock.MockRegistryManager) RegistryManager(org.gbif.ipt.service.registry.RegistryManager) ConceptTermConverter(org.gbif.ipt.model.converter.ConceptTermConverter) ExtensionRowTypeConverter(org.gbif.ipt.model.converter.ExtensionRowTypeConverter) MockAppConfig(org.gbif.ipt.mock.MockAppConfig) AppConfig(org.gbif.ipt.config.AppConfig) Agent(org.gbif.metadata.eml.Agent) InputStream(java.io.InputStream) ResourceManagerImpl(org.gbif.ipt.service.manage.impl.ResourceManagerImpl) Resource(org.gbif.ipt.model.Resource) BaseAction(org.gbif.ipt.action.BaseAction) SourceManagerImpl(org.gbif.ipt.service.manage.impl.SourceManagerImpl) UserEmailConverter(org.gbif.ipt.model.converter.UserEmailConverter) VocabulariesManager(org.gbif.ipt.service.admin.VocabulariesManager) JdbcInfoConverter(org.gbif.ipt.model.converter.JdbcInfoConverter) Extension(org.gbif.ipt.model.Extension) PasswordEncrypter(org.gbif.ipt.model.converter.PasswordEncrypter) VocabulariesManagerImpl(org.gbif.ipt.service.admin.impl.VocabulariesManagerImpl) ExtensionManager(org.gbif.ipt.service.admin.ExtensionManager) Struts2GuicePluginModule(com.google.inject.struts2.Struts2GuicePluginModule) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) SimpleTextProvider(org.gbif.ipt.struts2.SimpleTextProvider) HttpClient(org.gbif.utils.HttpClient) ThesaurusHandlingRule(org.gbif.ipt.model.factory.ThesaurusHandlingRule) SourceManager(org.gbif.ipt.service.manage.SourceManager) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 4 with ExtensionFactory

use of org.gbif.ipt.model.factory.ExtensionFactory in project ipt by gbif.

the class GenerateDwcaIT method getResource.

/**
 * Generates a test Resource.
 * </br>
 * The test resource is built from the test occurrence resource /res1 (res1/resource.xml, res1/eml.xml) mocking all
 * necessary methods executed by GenerateDwca.call().
 * </br>
 * For flexibility, the source file used to generate the core data file can be changed. The columns of this
 * source file must match the resourceXML configuration file passed in. By changing the source file and resource
 * configuration file, multiple scenarios can be created for testing.
 *
 * @param resourceXML resource (XML) configuration file defining column mapping of sourceFile
 * @param sourceFile  source file
 *
 * @return test Resource
 */
private Resource getResource(@NotNull File resourceXML, @NotNull File sourceFile) 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);
    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);
    // construct occurrence core Extension
    InputStream occurrenceCoreIs = GenerateDwcaTest.class.getResourceAsStream("/extensions/dwc_occurrence_2015-04-24.xml");
    Extension occurrenceCore = extensionFactory.build(occurrenceCoreIs);
    ExtensionManager extensionManager = mock(ExtensionManager.class);
    // 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/xsd/simpledarwincore/SimpleDarwinRecord")).thenReturn(occurrenceCore);
    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/res1.zip");
    // retrieve sample eml.xml file
    File emlXML = FileUtils.getClasspathFile("resources/res1/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);
    // archival mode on
    when(mockAppConfig.isArchivalMode()).thenReturn(true);
    // create ResourceManagerImpl
    ResourceManagerImpl resourceManager = new ResourceManagerImpl(mockAppConfig, mockDataDir, mockEmailConverter, mockOrganisationKeyConverter, extensionRowTypeConverter, jdbcConverter, mockSourceManager, extensionManager, mockRegistryManager, conceptTermConverter, mockDwcaFactory, passwordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManager);
    // create a new resource.
    // 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");
    Resource resource = resourceManager.create(RESOURCE_SHORTNAME, null, zippedResourceFolder, creator, baseAction);
    // copy source file to tmp folder
    File copied = new File(resourceDir, "occurrence.txt");
    // mock file to which source file gets copied to
    when(mockDataDir.sourceFile(any(Resource.class), any(FileSource.class))).thenReturn(copied);
    // publication log file
    File publicationLogFile = new File(resourceDir, DataDir.PUBLICATION_LOG_FILENAME);
    when(mockDataDir.resourcePublicationLogFile(RESOURCE_SHORTNAME)).thenReturn(publicationLogFile);
    // mock log file
    when(mockDataDir.sourceLogFile(anyString(), anyString())).thenReturn(new File(resourceDir, "log.txt"));
    // add SourceBase.TextFileSource fileSource to test Resource
    FileSource fileSource = mockSourceManager.add(resource, sourceFile, "occurrence.txt");
    resource.getMappings().get(0).setSource(fileSource);
    // 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;
}
Also used : User(org.gbif.ipt.model.User) IPTModule(org.gbif.ipt.config.IPTModule) JdbcSupport(org.gbif.ipt.config.JdbcSupport) ServletModule(com.google.inject.servlet.ServletModule) UserAccountManager(org.gbif.ipt.service.admin.UserAccountManager) OrganisationKeyConverter(org.gbif.ipt.model.converter.OrganisationKeyConverter) Injector(com.google.inject.Injector) MockRegistryManager(org.gbif.ipt.mock.MockRegistryManager) RegistryManager(org.gbif.ipt.service.registry.RegistryManager) ConceptTermConverter(org.gbif.ipt.model.converter.ConceptTermConverter) ExtensionRowTypeConverter(org.gbif.ipt.model.converter.ExtensionRowTypeConverter) RegistrationManager(org.gbif.ipt.service.admin.RegistrationManager) InputStream(java.io.InputStream) ResourceManagerImpl(org.gbif.ipt.service.manage.impl.ResourceManagerImpl) FileSource(org.gbif.ipt.model.FileSource) Resource(org.gbif.ipt.model.Resource) BaseAction(org.gbif.ipt.action.BaseAction) SourceManagerImpl(org.gbif.ipt.service.manage.impl.SourceManagerImpl) UserEmailConverter(org.gbif.ipt.model.converter.UserEmailConverter) VocabulariesManager(org.gbif.ipt.service.admin.VocabulariesManager) JdbcInfoConverter(org.gbif.ipt.model.converter.JdbcInfoConverter) BigDecimal(java.math.BigDecimal) Extension(org.gbif.ipt.model.Extension) PasswordEncrypter(org.gbif.ipt.model.converter.PasswordEncrypter) VocabulariesManagerImpl(org.gbif.ipt.service.admin.impl.VocabulariesManagerImpl) ExtensionManager(org.gbif.ipt.service.admin.ExtensionManager) Struts2GuicePluginModule(com.google.inject.struts2.Struts2GuicePluginModule) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) SimpleTextProvider(org.gbif.ipt.struts2.SimpleTextProvider) HttpClient(org.gbif.utils.HttpClient) ThesaurusHandlingRule(org.gbif.ipt.model.factory.ThesaurusHandlingRule) ArchiveFile(org.gbif.dwc.ArchiveFile) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 5 with ExtensionFactory

use of org.gbif.ipt.model.factory.ExtensionFactory in project ipt by gbif.

the class GenerateDwcaTest method getResource.

/**
 * Generates a test Resource.
 * </br>
 * The test resource is built from the test occurrence resource /res1 (res1/resource.xml, res1/eml.xml) mocking all
 * necessary methods executed by GenerateDwca.call().
 * </br>
 * For flexibility, the source file used to generate the core data file can be changed. The columns of this
 * source file must match the resourceXML configuration file passed in. By changing the source file and resource
 * configuration file, multiple scenarios can be created for testing.
 *
 * @param resourceXML resource (XML) configuration file defining column mapping of sourceFile
 * @param sourceFile source file
 *
 * @return test Resource
 */
private Resource getResource(@NotNull File resourceXML, @NotNull File sourceFile) 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);
    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);
    // construct occurrence core Extension
    InputStream occurrenceCoreIs = GenerateDwcaTest.class.getResourceAsStream("/extensions/dwc_occurrence_2015-04-24.xml");
    Extension occurrenceCore = extensionFactory.build(occurrenceCoreIs);
    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);
    // 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);
    when(extensionManager.get("http://rs.tdwg.org/dwc/xsd/simpledarwincore/SimpleDarwinRecord")).thenReturn(occurrenceCore);
    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/res1.zip");
    // retrieve sample eml.xml file
    File emlXML = FileUtils.getClasspathFile("resources/res1/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 ResourceManagerImpl
    ResourceManagerImpl resourceManager = new ResourceManagerImpl(mockAppConfig, mockDataDir, mockEmailConverter, mockOrganisationKeyConverter, extensionRowTypeConverter, jdbcConverter, mockSourceManager, extensionManager, mockRegistryManager, conceptTermConverter, mockDwcaFactory, passwordEncrypter, mockEml2Rtf, mockVocabulariesManager, mockSimpleTextProvider, mockRegistrationManager);
    // create a new resource.
    resource = resourceManager.create(RESOURCE_SHORTNAME, null, zippedResourceFolder, creator, baseAction);
    // copy source file to tmp folder
    File copied = new File(resourceDir, "occurrence.txt");
    // mock file to which source file gets copied to
    when(mockDataDir.sourceFile(any(Resource.class), any(FileSource.class))).thenReturn(copied);
    // mock log file
    when(mockDataDir.sourceLogFile(anyString(), anyString())).thenReturn(new File(resourceDir, "log.txt"));
    // add SourceBase.TextFileSource fileSource to test Resource
    FileSource fileSource = mockSourceManager.add(resource, sourceFile, "occurrence.txt");
    resource.getMappings().get(0).setSource(fileSource);
    // 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;
}
Also used : IPTModule(org.gbif.ipt.config.IPTModule) JdbcSupport(org.gbif.ipt.config.JdbcSupport) ServletModule(com.google.inject.servlet.ServletModule) UserAccountManager(org.gbif.ipt.service.admin.UserAccountManager) OrganisationKeyConverter(org.gbif.ipt.model.converter.OrganisationKeyConverter) Injector(com.google.inject.Injector) MockRegistryManager(org.gbif.ipt.mock.MockRegistryManager) RegistryManager(org.gbif.ipt.service.registry.RegistryManager) ConceptTermConverter(org.gbif.ipt.model.converter.ConceptTermConverter) ExtensionRowTypeConverter(org.gbif.ipt.model.converter.ExtensionRowTypeConverter) RegistrationManager(org.gbif.ipt.service.admin.RegistrationManager) InputStream(java.io.InputStream) ResourceManagerImpl(org.gbif.ipt.service.manage.impl.ResourceManagerImpl) FileSource(org.gbif.ipt.model.FileSource) Resource(org.gbif.ipt.model.Resource) BaseAction(org.gbif.ipt.action.BaseAction) SourceManagerImpl(org.gbif.ipt.service.manage.impl.SourceManagerImpl) UserEmailConverter(org.gbif.ipt.model.converter.UserEmailConverter) VocabulariesManager(org.gbif.ipt.service.admin.VocabulariesManager) JdbcInfoConverter(org.gbif.ipt.model.converter.JdbcInfoConverter) BigDecimal(java.math.BigDecimal) Extension(org.gbif.ipt.model.Extension) PasswordEncrypter(org.gbif.ipt.model.converter.PasswordEncrypter) VocabulariesManagerImpl(org.gbif.ipt.service.admin.impl.VocabulariesManagerImpl) ExtensionManager(org.gbif.ipt.service.admin.ExtensionManager) Struts2GuicePluginModule(com.google.inject.struts2.Struts2GuicePluginModule) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) SimpleTextProvider(org.gbif.ipt.struts2.SimpleTextProvider) HttpClient(org.gbif.utils.HttpClient) ThesaurusHandlingRule(org.gbif.ipt.model.factory.ThesaurusHandlingRule) ArchiveFile(org.gbif.dwc.ArchiveFile) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

ExtensionFactory (org.gbif.ipt.model.factory.ExtensionFactory)7 Injector (com.google.inject.Injector)6 ServletModule (com.google.inject.servlet.ServletModule)6 Struts2GuicePluginModule (com.google.inject.struts2.Struts2GuicePluginModule)6 File (java.io.File)6 SAXParserFactory (javax.xml.parsers.SAXParserFactory)6 IPTModule (org.gbif.ipt.config.IPTModule)6 Extension (org.gbif.ipt.model.Extension)6 ThesaurusHandlingRule (org.gbif.ipt.model.factory.ThesaurusHandlingRule)6 SimpleTextProvider (org.gbif.ipt.struts2.SimpleTextProvider)6 HttpClient (org.gbif.utils.HttpClient)6 InputStream (java.io.InputStream)5 JdbcSupport (org.gbif.ipt.config.JdbcSupport)5 Resource (org.gbif.ipt.model.Resource)5 ConceptTermConverter (org.gbif.ipt.model.converter.ConceptTermConverter)5 ExtensionRowTypeConverter (org.gbif.ipt.model.converter.ExtensionRowTypeConverter)5 JdbcInfoConverter (org.gbif.ipt.model.converter.JdbcInfoConverter)5 PasswordEncrypter (org.gbif.ipt.model.converter.PasswordEncrypter)5 ExtensionManager (org.gbif.ipt.service.admin.ExtensionManager)5 RegistrationManager (org.gbif.ipt.service.admin.RegistrationManager)5