Skip to content

Commit 4b32a04

Browse files
committed
Fix for reading resource.xml files generated in an older IPT.
XStream since 1.4.9 requires this fix.
1 parent 6d832c8 commit 4b32a04

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

src/main/java/org/gbif/ipt/service/manage/impl/ResourceManagerImpl.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import org.gbif.doi.service.DoiException;
77
import org.gbif.doi.service.DoiExistsException;
88
import org.gbif.doi.service.InvalidMetadataException;
9-
import org.gbif.dwc.terms.DcTerm;
109
import org.gbif.dwc.terms.DwcTerm;
11-
import org.gbif.dwc.terms.GbifTerm;
12-
import org.gbif.dwc.terms.IucnTerm;
1310
import org.gbif.dwc.terms.Term;
1411
import org.gbif.dwc.terms.TermFactory;
1512
import org.gbif.dwca.io.Archive;
@@ -679,10 +676,6 @@ private void defineXstreamMapping(UserEmailConverter userConverter, Organisation
679676
xstream.registerConverter(passwordConverter);
680677

681678
xstream.addDefaultImplementation(ExtensionProperty.class, Term.class);
682-
xstream.addDefaultImplementation(DwcTerm.class, Term.class);
683-
xstream.addDefaultImplementation(DcTerm.class, Term.class);
684-
xstream.addDefaultImplementation(GbifTerm.class, Term.class);
685-
xstream.addDefaultImplementation(IucnTerm.class, Term.class);
686679
xstream.registerConverter(orgConverter);
687680
xstream.registerConverter(jdbcInfoConverter);
688681
}
@@ -1121,7 +1114,7 @@ private Resource loadFromDir(File resourceDir, @Nullable User creator, ActionLog
11211114

11221115
LOG.debug("Read resource configuration for " + shortname);
11231116
return resource;
1124-
} catch (FileNotFoundException e) {
1117+
} catch (Exception e) {
11251118
LOG.error("Cannot read resource configuration for " + shortname, e);
11261119
throw new InvalidConfigException(TYPE.RESOURCE_CONFIG,
11271120
"Cannot read resource configuration for " + shortname + ": " + e.getMessage());

src/test/java/org/gbif/ipt/service/manage/impl/ResourceManagerImplTest.java

+92
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,18 @@ public ResourceManagerImpl getResourceManagerImpl() throws IOException, SAXExcep
218218
InputStream eventCoreIs = ResourceManagerImplTest.class.getResourceAsStream("/extensions/dwc_event_2015-04-24.xml");
219219
Extension eventCore = extensionFactory.build(eventCoreIs);
220220

221+
// construct simple images extension
222+
InputStream simpleImageIs = ResourceManagerImplTest.class.getResourceAsStream("/extensions/simple_image.xml");
223+
Extension simpleImage = extensionFactory.build(simpleImageIs);
224+
221225
ExtensionManager extensionManager = mock(ExtensionManager.class);
222226

223227
// mock ExtensionManager returning different Extensions
224228
when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Occurrence")).thenReturn(occurrenceCore);
225229
when(extensionManager.get("http://rs.tdwg.org/dwc/terms/Event")).thenReturn(eventCore);
226230
when(extensionManager.get("http://rs.tdwg.org/dwc/xsd/simpledarwincore/SimpleDarwinRecord"))
227231
.thenReturn(occurrenceCore);
232+
when(extensionManager.get("http://rs.gbif.org/terms/1.0/Image")).thenReturn(simpleImage);
228233

229234
ExtensionRowTypeConverter extensionRowTypeConverter = new ExtensionRowTypeConverter(extensionManager);
230235
ConceptTermConverter conceptTermConverter = new ConceptTermConverter(extensionRowTypeConverter);
@@ -490,6 +495,93 @@ public void testCreateFromSingleGzipFile()
490495
assertTrue(res.getEml().getDescription().isEmpty());
491496
}
492497

498+
/**
499+
* test resource (with extension) creation from zipped resource folder.
500+
*/
501+
@Test
502+
public void testCreateWithExtensionFromZippedFile()
503+
throws AlreadyExistingException, ImportException, SAXException, ParserConfigurationException, IOException,
504+
InvalidFilenameException {
505+
// retrieve sample zipped resource folder
506+
File resourceXML = FileUtils.getClasspathFile("resources/amphibians/resource.xml");
507+
// mock finding resource.xml file
508+
when(mockedDataDir.resourceFile(anyString(), anyString())).thenReturn(resourceXML);
509+
510+
// retrieve sample zipped resource folder
511+
File emlXML = FileUtils.getClasspathFile("resources/amphibians/eml.xml");
512+
// mock finding eml.xml file
513+
when(mockedDataDir.resourceEmlFile(anyString(), any(BigDecimal.class))).thenReturn(emlXML);
514+
515+
// create instance of manager
516+
ResourceManager resourceManager = getResourceManagerImpl();
517+
518+
// retrieve sample zipped resource folder
519+
File zippedResourceFolder = FileUtils.getClasspathFile("resources/amphibians.zip");
520+
521+
// create a new resource.
522+
resourceManager.create("amphibians", null, zippedResourceFolder, creator, baseAction);
523+
524+
// test if new resource was added to the resources list.
525+
assertEquals(1, resourceManager.list().size());
526+
527+
// get added resource.
528+
Resource res = resourceManager.get("amphibians");
529+
530+
// test if resource was added correctly.
531+
assertEquals("amphibians", res.getShortname());
532+
assertEquals(creator, res.getCreator());
533+
assertEquals(creator, res.getModifier());
534+
535+
// test if resource.xml was created.
536+
assertTrue(mockedDataDir.resourceFile("amphibians", ResourceManagerImpl.PERSISTENCE_FILE).exists());
537+
538+
// properties that get preserved
539+
// there are 2 source files
540+
assertEquals(2, res.getSources().size());
541+
assertEquals("danbifdb", res.getSources().get(0).getName());
542+
assertEquals(65, res.getSource("danbifdb").getColumns());
543+
544+
// there is 1 mapping
545+
assertEquals(2, res.getMappings().size());
546+
assertEquals("danbifdb", res.getMappings().get(0).getSource().getName());
547+
assertEquals(Constants.DWC_ROWTYPE_OCCURRENCE, res.getMappings().get(0).getExtension().getRowType());
548+
assertEquals("http://rs.gbif.org/terms/1.0/Image", res.getMappings().get(1).getExtension().getRowType());
549+
550+
// properties that get reset
551+
assertEquals(Constants.INITIAL_RESOURCE_VERSION, res.getEmlVersion());
552+
// the resource shouldn't be registered
553+
assertFalse(res.isRegistered());
554+
// the resource shouldn't have any managers
555+
assertEquals(0, res.getManagers().size());
556+
// the resource shouldn't have a last published date
557+
assertNull(res.getLastPublished());
558+
// the resource shouldn't be registered (no org, no key)
559+
assertNull(res.getKey());
560+
assertNull(res.getOrganisation());
561+
// the status should be private
562+
assertEquals(PublicationStatus.PRIVATE, res.getStatus());
563+
// the resource should have a created date
564+
assertNotNull(res.getCreated());
565+
// the record count is 0
566+
assertEquals(0, res.getRecordsPublished());
567+
// the DOI was reset
568+
assertNull(res.getDoi());
569+
assertEquals(IdentifierStatus.UNRESERVED, res.getIdentifierStatus());
570+
assertNull(res.getDoiOrganisationKey());
571+
// the change summary was reset
572+
assertNull(res.getChangeSummary());
573+
// the VersionHistory was cleared
574+
assertEquals(0, res.getVersionHistory().size());
575+
// the auto-publication was reset
576+
assertEquals(PublicationMode.AUTO_PUBLISH_OFF, res.getPublicationMode());
577+
assertNull(res.getUpdateFrequency());
578+
assertNull(res.getNextPublished());
579+
// the other last modified dates were also reset
580+
assertNull(res.getMetadataModified());
581+
assertNull(res.getMappingsModified());
582+
assertNull(res.getSourcesModified());
583+
}
584+
493585
/**
494586
* test resource creation from zipped file, but resource.xml references non-existent extension.
495587
*/

0 commit comments

Comments
 (0)