Skip to content

Commit

Permalink
Fix for reading resource.xml files generated in an older IPT.
Browse files Browse the repository at this point in the history
XStream since 1.4.9 requires this fix.
  • Loading branch information
MattBlissett committed Sep 7, 2020
1 parent 6d832c8 commit 4b32a04
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.gbif.doi.service.DoiException;
import org.gbif.doi.service.DoiExistsException;
import org.gbif.doi.service.InvalidMetadataException;
import org.gbif.dwc.terms.DcTerm;
import org.gbif.dwc.terms.DwcTerm;
import org.gbif.dwc.terms.GbifTerm;
import org.gbif.dwc.terms.IucnTerm;
import org.gbif.dwc.terms.Term;
import org.gbif.dwc.terms.TermFactory;
import org.gbif.dwca.io.Archive;
Expand Down Expand Up @@ -679,10 +676,6 @@ private void defineXstreamMapping(UserEmailConverter userConverter, Organisation
xstream.registerConverter(passwordConverter);

xstream.addDefaultImplementation(ExtensionProperty.class, Term.class);
xstream.addDefaultImplementation(DwcTerm.class, Term.class);
xstream.addDefaultImplementation(DcTerm.class, Term.class);
xstream.addDefaultImplementation(GbifTerm.class, Term.class);
xstream.addDefaultImplementation(IucnTerm.class, Term.class);
xstream.registerConverter(orgConverter);
xstream.registerConverter(jdbcInfoConverter);
}
Expand Down Expand Up @@ -1121,7 +1114,7 @@ private Resource loadFromDir(File resourceDir, @Nullable User creator, ActionLog

LOG.debug("Read resource configuration for " + shortname);
return resource;
} catch (FileNotFoundException e) {
} catch (Exception e) {
LOG.error("Cannot read resource configuration for " + shortname, e);
throw new InvalidConfigException(TYPE.RESOURCE_CONFIG,
"Cannot read resource configuration for " + shortname + ": " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,18 @@ public ResourceManagerImpl getResourceManagerImpl() throws IOException, SAXExcep
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);
Expand Down Expand Up @@ -490,6 +495,93 @@ public void testCreateFromSingleGzipFile()
assertTrue(res.getEml().getDescription().isEmpty());
}

/**
* test resource (with extension) creation from zipped resource folder.
*/
@Test
public void testCreateWithExtensionFromZippedFile()
throws AlreadyExistingException, ImportException, SAXException, ParserConfigurationException, IOException,
InvalidFilenameException {
// retrieve sample zipped resource folder
File resourceXML = FileUtils.getClasspathFile("resources/amphibians/resource.xml");
// mock finding resource.xml file
when(mockedDataDir.resourceFile(anyString(), anyString())).thenReturn(resourceXML);

// retrieve sample zipped resource folder
File emlXML = FileUtils.getClasspathFile("resources/amphibians/eml.xml");
// mock finding eml.xml file
when(mockedDataDir.resourceEmlFile(anyString(), any(BigDecimal.class))).thenReturn(emlXML);

// create instance of manager
ResourceManager resourceManager = getResourceManagerImpl();

// retrieve sample zipped resource folder
File zippedResourceFolder = FileUtils.getClasspathFile("resources/amphibians.zip");

// create a new resource.
resourceManager.create("amphibians", null, zippedResourceFolder, creator, baseAction);

// test if new resource was added to the resources list.
assertEquals(1, resourceManager.list().size());

// get added resource.
Resource res = resourceManager.get("amphibians");

// test if resource was added correctly.
assertEquals("amphibians", res.getShortname());
assertEquals(creator, res.getCreator());
assertEquals(creator, res.getModifier());

// test if resource.xml was created.
assertTrue(mockedDataDir.resourceFile("amphibians", ResourceManagerImpl.PERSISTENCE_FILE).exists());

// properties that get preserved
// there are 2 source files
assertEquals(2, res.getSources().size());
assertEquals("danbifdb", res.getSources().get(0).getName());
assertEquals(65, res.getSource("danbifdb").getColumns());

// there is 1 mapping
assertEquals(2, res.getMappings().size());
assertEquals("danbifdb", res.getMappings().get(0).getSource().getName());
assertEquals(Constants.DWC_ROWTYPE_OCCURRENCE, res.getMappings().get(0).getExtension().getRowType());
assertEquals("http://rs.gbif.org/terms/1.0/Image", res.getMappings().get(1).getExtension().getRowType());

// properties that get reset
assertEquals(Constants.INITIAL_RESOURCE_VERSION, res.getEmlVersion());
// the resource shouldn't be registered
assertFalse(res.isRegistered());
// the resource shouldn't have any managers
assertEquals(0, res.getManagers().size());
// the resource shouldn't have a last published date
assertNull(res.getLastPublished());
// the resource shouldn't be registered (no org, no key)
assertNull(res.getKey());
assertNull(res.getOrganisation());
// the status should be private
assertEquals(PublicationStatus.PRIVATE, res.getStatus());
// the resource should have a created date
assertNotNull(res.getCreated());
// the record count is 0
assertEquals(0, res.getRecordsPublished());
// the DOI was reset
assertNull(res.getDoi());
assertEquals(IdentifierStatus.UNRESERVED, res.getIdentifierStatus());
assertNull(res.getDoiOrganisationKey());
// the change summary was reset
assertNull(res.getChangeSummary());
// the VersionHistory was cleared
assertEquals(0, res.getVersionHistory().size());
// the auto-publication was reset
assertEquals(PublicationMode.AUTO_PUBLISH_OFF, res.getPublicationMode());
assertNull(res.getUpdateFrequency());
assertNull(res.getNextPublished());
// the other last modified dates were also reset
assertNull(res.getMetadataModified());
assertNull(res.getMappingsModified());
assertNull(res.getSourcesModified());
}

/**
* test resource creation from zipped file, but resource.xml references non-existent extension.
*/
Expand Down

0 comments on commit 4b32a04

Please sign in to comment.