Search in sources :

Example 1 with ResourceManager

use of org.gbif.ipt.service.manage.ResourceManager in project ipt by gbif.

the class CreateResourceActionTest method setup.

@BeforeEach
public void setup() throws IOException, DeletionNotAllowedException {
    // mock DataDir returning temp file (needed during import stage)
    DataDir mockDataDir = mock(DataDir.class);
    when(mockDataDir.tmpFile(anyString(), anyString())).thenReturn(FileUtils.createTempDir());
    // mock DataDir returning resources directory
    when(mockDataDir.dataFile(anyString())).thenReturn(RESOURCES_DIRECTORY);
    // mock resource manager that returns resource with shortname "bugs"
    Resource res = new Resource();
    res.setShortname(SHORTNAME);
    ResourceManager mockResourceManager = mock(ResourceManager.class);
    when(mockResourceManager.get(SHORTNAME)).thenReturn(res);
    // mock resource manager that deletes resource directory "bugs"
    doAnswer(invocation -> org.apache.commons.io.FileUtils.deleteQuietly(new File(RESOURCES_DIRECTORY, SHORTNAME))).when(mockResourceManager).delete(res, true);
    // mock action
    action = new CreateResourceAction(mock(SimpleTextProvider.class), mock(AppConfig.class), mock(RegistrationManager.class), mockResourceManager, mockDataDir, mock(VocabulariesManager.class));
}
Also used : Resource(org.gbif.ipt.model.Resource) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) File(java.io.File) DataDir(org.gbif.ipt.config.DataDir) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ResourceManager

use of org.gbif.ipt.service.manage.ResourceManager in project ipt by gbif.

the class OverviewActionTest method setup.

@BeforeEach
public void setup() throws IOException, ParserConfigurationException, SAXException, AlreadyExistingException, ImportException {
    ResourceManager mockResourceManager = mock(ResourceManager.class);
    ListValuedMap<String, Date> processFailures = new ArrayListValuedHashMap<>();
    processFailures.put("res1", new Date());
    processFailures.put("res1", new Date());
    when(mockResourceManager.getProcessFailures()).thenReturn(processFailures);
    // mock returning eml-1.0.xml
    emlFile = File.createTempFile("eml-1.0", ".xml");
    AppConfig mockCfg = mock(AppConfig.class);
    DataDir mockDataDir = mock(DataDir.class);
    when(mockDataDir.resourceEmlFile(any(), any(BigDecimal.class))).thenReturn(emlFile);
    when(mockCfg.getDataDir()).thenReturn(mockDataDir);
    // mock action
    action = new OverviewAction(mock(SimpleTextProvider.class), mockCfg, mock(RegistrationManager.class), mockResourceManager, mock(UserAccountManager.class), mock(ExtensionManager.class), mock(GenerateDwcaFactory.class), mock(VocabulariesManager.class), mock(RegistryManager.class));
}
Also used : AppConfig(org.gbif.ipt.config.AppConfig) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Date(java.util.Date) DataDir(org.gbif.ipt.config.DataDir) BigDecimal(java.math.BigDecimal) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ResourceManager

use of org.gbif.ipt.service.manage.ResourceManager in project ipt by gbif.

the class ResourceManagerImplTest method testMissingIdElementInCoreMapping.

/**
 * Test resource creation from zipped sampling-event DwC-A where event core is missing the id element.
 * The test ensures that an ImportException gets thrown, as the id element is mandatory with extensions.
 */
@Test
public void testMissingIdElementInCoreMapping() throws Exception {
    // create instance of manager
    ResourceManager resourceManager = getResourceManagerImpl();
    // retrieve zipped DwC-A file
    File dwca = FileUtils.getClasspathFile("resources/dwca-noidelementincoremapping.zip");
    // create copy of DwC-A file in tmp dir, used to mock saving source resource filesource
    File tmpDir = FileUtils.createTempDir();
    List<File> files = CompressionUtil.unzipFile(tmpDir, dwca, false);
    // core event.txt file
    File uncompressedEvent = files.get(0);
    TextFileSource fileSourceEvent = new TextFileSource();
    fileSourceEvent.setFile(uncompressedEvent);
    fileSourceEvent.setName("event.txt");
    // extension occurrence.txt file
    File uncompressedOccurrence = files.get(2);
    TextFileSource fileSourceOccurrence = new TextFileSource();
    fileSourceOccurrence.setFile(uncompressedOccurrence);
    fileSourceOccurrence.setName("occurrence.txt");
    when(mockSourceManager.add(any(Resource.class), any(File.class), anyString())).thenReturn(fileSourceEvent).thenReturn(fileSourceOccurrence);
    // create a new resource.
    assertThrows(ImportException.class, () -> resourceManager.create("res-extension", null, dwca, creator, baseAction));
}
Also used : TextFileSource(org.gbif.ipt.model.TextFileSource) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceManager

use of org.gbif.ipt.service.manage.ResourceManager in project ipt by gbif.

the class ResourceManagerImplTest method testDifferentCoreIdTermIndexInExtension.

/**
 * Test resource creation from zipped sampling-event DwC-A where occurrence extension coreId element index and coreId
 * term mapping index are different. The test ensures that the coreId element index is set to that of the core term mapping index.
 */
@Test
public void testDifferentCoreIdTermIndexInExtension() throws ParserConfigurationException, SAXException, IOException, InvalidFilenameException, ImportException, AlreadyExistingException {
    // create instance of manager
    ResourceManager resourceManager = getResourceManagerImpl();
    // retrieve zipped DwC-A file
    File dwca = FileUtils.getClasspathFile("resources/dwca-differentcoreidtermindex.zip");
    // create copy of DwC-A file in tmp dir, used to mock saving source resource filesource
    File tmpDir = FileUtils.createTempDir();
    List<File> files = CompressionUtil.unzipFile(tmpDir, dwca, false);
    // core event.txt file
    File uncompressedEvent = files.get(0);
    TextFileSource fileSourceEvent = new TextFileSource();
    fileSourceEvent.setFile(uncompressedEvent);
    fileSourceEvent.setName("event.txt");
    // extension occurrence.txt file
    File uncompressedOccurrence = files.get(2);
    TextFileSource fileSourceOccurrence = new TextFileSource();
    fileSourceOccurrence.setFile(uncompressedOccurrence);
    fileSourceOccurrence.setName("occurrence.txt");
    when(mockSourceManager.add(any(Resource.class), any(File.class), anyString())).thenReturn(fileSourceEvent).thenReturn(fileSourceOccurrence);
    // create a new resource.
    Resource resource = resourceManager.create("res-differentcoreidtermindex", null, dwca, creator, baseAction);
    ExtensionMapping extensionMapping = resource.getMapping(Constants.DWC_ROWTYPE_OCCURRENCE, 0);
    assertEquals(28, extensionMapping.getFields().size());
    PropertyMapping coreIdTermPropertyMapping = extensionMapping.getField(Constants.DWC_EVENT_ID);
    assertNotNull(coreIdTermPropertyMapping);
    assertNotNull(coreIdTermPropertyMapping.getIndex());
    assertEquals(Integer.valueOf(16), coreIdTermPropertyMapping.getIndex());
}
Also used : ExtensionMapping(org.gbif.ipt.model.ExtensionMapping) Resource(org.gbif.ipt.model.Resource) PropertyMapping(org.gbif.ipt.model.PropertyMapping) TextFileSource(org.gbif.ipt.model.TextFileSource) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceManager

use of org.gbif.ipt.service.manage.ResourceManager in project ipt by gbif.

the class ResourceManagerImplTest method testCreateFromSingleGzipFile.

/**
 * test resource creation from single DwC-A gzipped file.
 */
@Test
public void testCreateFromSingleGzipFile() throws AlreadyExistingException, ImportException, SAXException, ParserConfigurationException, IOException, InvalidFilenameException {
    // create instance of manager
    ResourceManager resourceManager = getResourceManagerImpl();
    // retrieve sample gzip DwC-A file
    File dwca = FileUtils.getClasspathFile("resources/occurrence2.txt.gz");
    // create copy of DwC-A file in tmp dir, used to mock saving source resource filesource
    File tmpDir = FileUtils.createTempDir();
    List<File> files = CompressionUtil.ungzipFile(tmpDir, dwca, false);
    File uncompressed = files.get(0);
    TextFileSource fileSource = new TextFileSource();
    fileSource.setFile(uncompressed);
    // it has 16 rows, plus 1 header line
    fileSource.setRows(16);
    fileSource.setIgnoreHeaderLines(1);
    fileSource.setEncoding("UTF-8");
    fileSource.setFieldsTerminatedByEscaped("/t");
    fileSource.setName("singleTxt");
    when(mockSourceManager.add(any(Resource.class), any(File.class), anyString())).thenReturn(fileSource);
    // create a new resource.
    resourceManager.create("res-single-gz", null, dwca, creator, baseAction);
    // test if new resource was added to the resources list.
    assertEquals(1, resourceManager.list().size());
    // get added resource.
    Resource res = resourceManager.get("res-single-gz");
    // test if resource was added correctly.
    assertEquals("res-single-gz", res.getShortname());
    assertEquals(creator, res.getCreator());
    assertEquals(creator, res.getModifier());
    // test if resource.xml was created.
    assertTrue(mockedDataDir.resourceFile("res-single-gz", ResourceManagerImpl.PERSISTENCE_FILE).exists());
    // note: source gets added to resource in sourceManager.add, and since we're mocking this call we can't set source
    // there is 1 mapping
    assertEquals(1, res.getMappings().size());
    assertEquals("singletxt", res.getMappings().get(0).getSource().getName());
    assertEquals(Constants.DWC_ROWTYPE_OCCURRENCE, res.getMappings().get(0).getExtension().getRowType());
    assertEquals(23, res.getMappings().get(0).getFields().size());
    assertEquals(0, res.getMappings().get(0).getIdColumn().intValue());
    // there are no eml properties
    assertNull(res.getEml().getTitle());
    assertTrue(res.getEml().getDescription().isEmpty());
}
Also used : Resource(org.gbif.ipt.model.Resource) TextFileSource(org.gbif.ipt.model.TextFileSource) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceManager (org.gbif.ipt.service.manage.ResourceManager)27 File (java.io.File)18 Resource (org.gbif.ipt.model.Resource)18 Test (org.junit.jupiter.api.Test)17 BeforeEach (org.junit.jupiter.api.BeforeEach)9 BigDecimal (java.math.BigDecimal)7 ArrayList (java.util.ArrayList)7 AppConfig (org.gbif.ipt.config.AppConfig)7 DataDir (org.gbif.ipt.config.DataDir)7 TextFileSource (org.gbif.ipt.model.TextFileSource)7 RegistrationManager (org.gbif.ipt.service.admin.RegistrationManager)6 SimpleTextProvider (org.gbif.ipt.struts2.SimpleTextProvider)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 Date (java.util.Date)4 ConfigWarnings (org.gbif.ipt.config.ConfigWarnings)4 ExtensionMapping (org.gbif.ipt.model.ExtensionMapping)4 PropertyMapping (org.gbif.ipt.model.PropertyMapping)4 DefaultLocaleProviderFactory (com.opensymphony.xwork2.DefaultLocaleProviderFactory)3 LocaleProviderFactory (com.opensymphony.xwork2.LocaleProviderFactory)3 Container (com.opensymphony.xwork2.inject.Container)3