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));
}
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));
}
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));
}
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());
}
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());
}
Aggregations