diff --git a/src/main/java/org/purl/sword/server/fedora/fedoraObjects/VoidDatastream.java b/src/main/java/org/purl/sword/server/fedora/fedoraObjects/VoidDatastream.java
index d56f64b..291413c 100644
--- a/src/main/java/org/purl/sword/server/fedora/fedoraObjects/VoidDatastream.java
+++ b/src/main/java/org/purl/sword/server/fedora/fedoraObjects/VoidDatastream.java
@@ -19,14 +19,16 @@
import org.jdom.Namespace;
/**
- * Represents a datastream that is about to be deleted.
- * Whether the datastream is purged or it's state is set
- * to 'DELETED' depends on the actual strategy carried out
- * by a FileHandler that handles VoidDatastreams.
+ * Represents a datastream without content information.
+ *
+ * It's used to transport generic datastream properties to
+ * the FileHandler. Whether the datastream is purged or it's
+ * state is set to 'DELETED' depends on the actual strategy
+ * carried out by the FileHandler that handles VoidDatastreams.
*/
public class VoidDatastream extends Datastream {
public VoidDatastream(String id) {
- super(id, State.DELETED, null, null);
+ super(id, null, null, null);
}
/**
diff --git a/src/main/java/org/purl/sword/server/fedora/fileHandlers/METSContainer.java b/src/main/java/org/purl/sword/server/fedora/fileHandlers/METSContainer.java
index 661cff2..5e2ffcb 100644
--- a/src/main/java/org/purl/sword/server/fedora/fileHandlers/METSContainer.java
+++ b/src/main/java/org/purl/sword/server/fedora/fileHandlers/METSContainer.java
@@ -36,22 +36,21 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static org.purl.sword.server.fedora.fedoraObjects.State.DELETED;
import static org.purl.sword.server.fedora.fedoraObjects.State.INACTIVE;
public class METSContainer {
public static final Pattern PATTERN = Pattern.compile("^[a-z][a-z0-9\\+\\.\\-]*\\:.*", Pattern.CASE_INSENSITIVE);
-
public static final String DS_ID_SLUBINFO = "SLUB-INFO";
public static final String DS_ID_SLUBINFO_LABEL = "SLUB Administrative Metadata";
+ private static final String DS_ID_MODS = "MODS";
+ private static final String DS_ID_MODS_LABEL = "Object Bibliographic Metadata";
private static final String DS_ID_QUCOSAXML = "QUCOSA-XML";
private static final String DS_ID_QUCOSAXML_LABEL = "Pristine Qucosa XML Metadata";
private static final String DS_MODS_MIME_TYPE = "application/mods+xml";
- private static final String DS_ID_MODS = "MODS";
- private static final String DS_ID_MODS_LABEL = "Object Bibliographic Metadata";
private static final String METS_DMDSEC_PREFIX = "/mets:mets/mets:dmdSec";
private static final String MODS_PREFIX = METS_DMDSEC_PREFIX + "/mets:mdWrap[@MDTYPE='MODS']/mets:xmlData/mods:mods";
-
private final XPathQuery XPATH_FILES = new XPathQuery("/mets:mets/mets:fileSec/mets:fileGrp/mets:file");
private final XPathQuery XPATH_IDENTIFIERS = new XPathQuery(MODS_PREFIX + "/mods:identifier");
private final XPathQuery XPATH_MODS = new XPathQuery(MODS_PREFIX);
@@ -59,6 +58,7 @@ public class METSContainer {
private final XPathQuery XPATH_RELATEDITEMS = new XPathQuery(MODS_PREFIX + "/mods:relatedItem");
private final XPathQuery XPATH_SLUB = new XPathQuery("/mets:mets/mets:amdSec/mets:techMD" + "/mets:mdWrap[@MDTYPE='OTHER' and @OTHERMDTYPE='SLUBINFO']/mets:xmlData/slub:info");
private final XPathQuery XPATH_TITLE = new XPathQuery(MODS_PREFIX + "/mods:titleInfo/mods:title[1]");
+
private final String md5;
private final Document metsDocument;
@@ -125,15 +125,21 @@ public List getAugmentedFileDatastreams() throws SWORDException {
final String id = validateAndReturn("file ID", fileElement.getAttributeValue("ID"));
if (isADeleteRequest(fileElement)) {
- datastreamList.add(new VoidDatastream(id));
+ final VoidDatastream voidDatastream = new VoidDatastream(id);
+ voidDatastream.setState(DELETED);
+ datastreamList.add(voidDatastream);
} else {
- final Element fLocat = validateAndReturn("FLocat element", fileElement.getChild("FLocat", Namespaces.METS));
- final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
- final URI uri = new URI(href);
- final boolean isFile = uri.getScheme().equals("file");
- final String mimetype = validateAndReturn("mime type", fileElement.getAttributeValue("MIMETYPE"));
-
- Datastream ds = buildDatastream(id, fileElement, href, mimetype, isFile);
+ Datastream ds;
+ if (fileElement.getChild("FLocat", Namespaces.METS) != null) {
+ final Element fLocat = validateAndReturn("FLocat element", fileElement.getChild("FLocat", Namespaces.METS));
+ final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
+ final URI uri = new URI(href);
+ final boolean isFile = uri.getScheme().equals("file");
+ final String mimetype = validateAndReturn("mime type", fileElement.getAttributeValue("MIMETYPE"));
+ ds = buildDatastream(id, fileElement, href, mimetype, isFile);
+ } else {
+ ds = new VoidDatastream(id);
+ }
String digestType = emptyIfNull(fileElement.getAttributeValue("CHECKSUMTYPE"));
String digest = emptyIfNull(fileElement.getAttributeValue("CHECKSUM"));
@@ -164,13 +170,15 @@ public List getTemporayFiles() throws SWORDException {
fileElements = XPATH_FILES.selectNodes(metsDocument);
for (Element e : fileElements) {
if (!isADeleteRequest(e)) {
- final Element fLocat = validateAndReturn("FLocat element", e.getChild("FLocat", Namespaces.METS));
- final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
- final URI uri = new URI(href);
- final boolean isFile = uri.getScheme().equals("file");
- final boolean isTemporary = emptyIfNull(fLocat.getAttributeValue("USE")).equals("TEMPORARY");
- if (isFile && isTemporary) {
- filesMarkedForRemoval.add(new File(uri));
+ if (e.getChild("FLocat", Namespaces.METS) != null) {
+ final Element fLocat = validateAndReturn("FLocat element", e.getChild("FLocat", Namespaces.METS));
+ final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
+ final URI uri = new URI(href);
+ final boolean isFile = uri.getScheme().equals("file");
+ final boolean isTemporary = emptyIfNull(fLocat.getAttributeValue("USE")).equals("TEMPORARY");
+ if (isFile && isTemporary) {
+ filesMarkedForRemoval.add(new File(uri));
+ }
}
}
}
diff --git a/src/main/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler.java b/src/main/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler.java
index 332903c..a294d3a 100644
--- a/src/main/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler.java
+++ b/src/main/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler.java
@@ -19,6 +19,7 @@
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
+import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
@@ -111,6 +112,8 @@ private void removeAugmentationWrapperFrom(List datastreams) {
* 5. To replace an existing datastream with a newer version a element has to be
* present, having an ID value equal to the DSID of an existing datastream.
*
+ * 6. Omit the element to update USE attributes of a datastream without providing
+ * content.
* All datastream actions apply to the object PID specified in the deposit.
*
* @param deposit The deposit
@@ -131,15 +134,30 @@ public SWORDEntry updateDeposit(DepositCollection deposit, ServiceDocument servi
if (!deposit.isNoOp()) { // Don't ingest if no-op is set
update(repository, pid, fedoraObject.getDc());
final List datastreams = metsContainer.getDatastreams();
+
+ final XMLInlineDatastream repositorySlubInfo = (XMLInlineDatastream) repository.getDatastream(pid, METSContainer.DS_ID_SLUBINFO);
+ XMLInlineDatastream depositSlubInfo = (XMLInlineDatastream) findDatastream(METSContainer.DS_ID_SLUBINFO, datastreams);
+ if (repositorySlubInfo != null) {
+ if (depositSlubInfo == null) {
+ depositSlubInfo = repositorySlubInfo;
+ datastreams.add(repositorySlubInfo);
+ } else {
+ try {
+ mergeAttachmentElements(depositSlubInfo, repositorySlubInfo);
+ } catch (JDOMException e) {
+ throw new SWORDException("Cannot merge deposit SLUB-INFO with repository SLUB-INFO", e);
+ }
+ }
+ }
+
ensureAugmentedSlubInfoDatastream(datastreams);
removeAugmentationWrapperFrom(datastreams);
final List fileDatastreams = findDatastreams("ATT-", datastreams);
- final Datastream slubInfoDatastream = findDatastream(METSContainer.DS_ID_SLUBINFO, datastreams);
updateIfPresent(repository, pid, metsContainer.getModsDatastream());
updateAttachmentDatastreams(repository, pid, fileDatastreams);
- updateOrAdd(repository, pid, slubInfoDatastream);
+ updateOrAdd(repository, pid, depositSlubInfo);
updateOrAdd(repository, pid, metsContainer.getQucosaXmlDatastream());
delete(metsContainer.getTemporayFiles());
}
@@ -147,6 +165,43 @@ public SWORDEntry updateDeposit(DepositCollection deposit, ServiceDocument servi
return swordEntry;
}
+ private void mergeAttachmentElements(XMLInlineDatastream into, XMLInlineDatastream from) throws JDOMException {
+ Element intoRights = into.toXML().getRootElement().getChild("rights", Namespaces.SLUB);
+
+ if (intoRights == null) {
+ intoRights = new Element("rights", Namespaces.SLUB);
+ into.toXML().getRootElement().addContent(intoRights);
+ }
+
+ final Element fromRights = from.toXML().getRootElement().getChild("rights", Namespaces.SLUB);
+
+ final Map fromAttachmentElementMap = getAttachmentElementMap(
+ (fromRights == null) ? null : XPATH_ATTACHMENTS.selectNodes(fromRights));
+
+ final Map intoAttachmentElementMap = getAttachmentElementMap(
+ (fromRights == null) ? null : XPATH_ATTACHMENTS.selectNodes(intoRights));
+
+ fromAttachmentElementMap.keySet().removeAll(intoAttachmentElementMap.keySet());
+
+ for (Element element : fromAttachmentElementMap.values()) {
+ intoRights.addContent((Content) element.clone());
+ }
+ }
+
+ private Map getAttachmentElementMap(final List fromList) {
+ return new HashMap() {{
+ CollectionUtils.forAllDo(
+ fromList,
+ new Closure() {
+ @Override
+ public void execute(Object input) {
+ Element item = (Element) input;
+ put(item.getAttributeValue("ref"), item);
+ }
+ });
+ }};
+ }
+
private METSContainer loadAndValidate(DepositCollection deposit) throws SWORDException {
validateDeposit(deposit);
METSContainer metsContainer = loadMets(deposit);
@@ -273,7 +328,7 @@ private boolean hasMd5(DepositCollection deposit) {
private void updateAttachmentDatastreams(FedoraRepository repository, String pid, List datastreams) throws SWORDException {
for (Datastream attDatastream : datastreams) {
if (attDatastream instanceof VoidDatastream) {
- if (repository.hasDatastream(pid, attDatastream.getId())) {
+ if (repository.hasDatastream(pid, attDatastream.getId()) && DELETED.equals(attDatastream.getState())) {
repository.setDatastreamState(pid, attDatastream.getId(), DELETED, null);
}
} else {
@@ -298,12 +353,12 @@ private void update(FedoraRepository repository, String pid, Datastream datastre
}
}
- private void updateOrAdd(FedoraRepository repository, String pid, Datastream datastream) throws SWORDException {
- if (datastream != null) {
- if (repository.hasDatastream(pid, datastream.getId())) {
- repository.modifyDatastream(pid, datastream, null);
+ private void updateOrAdd(FedoraRepository repository, String pid, Datastream inputDatastream) throws SWORDException {
+ if (inputDatastream != null) {
+ if (repository.hasDatastream(pid, inputDatastream.getId())) {
+ repository.modifyDatastream(pid, inputDatastream, null);
} else {
- repository.addDatastream(pid, datastream, null);
+ repository.addDatastream(pid, inputDatastream, null);
}
}
}
@@ -314,15 +369,15 @@ private void ensureAugmentedSlubInfoDatastream(List datastreams) thr
Document info;
Datastream slubInfo = findDatastream(METSContainer.DS_ID_SLUBINFO, datastreams);
- if (slubInfo != null) {
- info = ((XMLInlineDatastream) slubInfo).toXML();
- } else {
+ if (slubInfo == null) {
info = new Document();
info.addContent(new Element("info", Namespaces.SLUB));
slubInfo = new XMLInlineDatastream(METSContainer.DS_ID_SLUBINFO, info);
slubInfo.setLabel(METSContainer.DS_ID_SLUBINFO_LABEL);
slubInfo.setVersionable(Boolean.parseBoolean(System.getProperty("datastream.versioning", "false")));
datastreams.add(slubInfo);
+ } else {
+ info = ((XMLInlineDatastream) slubInfo).toXML();
}
Element rights = info.getRootElement().getChild("rights", Namespaces.SLUB);
@@ -334,17 +389,7 @@ private void ensureAugmentedSlubInfoDatastream(List datastreams) thr
final List attachmentElements;
try {
attachmentElements = XPATH_ATTACHMENTS.selectNodes(rights);
- Map attachmentElementMap = new HashMap() {{
- CollectionUtils.forAllDo(
- attachmentElements,
- new Closure() {
- @Override
- public void execute(Object input) {
- Element item = (Element) input;
- put(item.getAttributeValue("ref"), item);
- }
- });
- }};
+ Map attachmentElementMap = getAttachmentElementMap(attachmentElements);
for (Datastream attachmentDatastream : attachmentDatastreams) {
if (attachmentDatastream instanceof AugmentedDatastream) {
AugmentedDatastream augmentedDatastream = (AugmentedDatastream) attachmentDatastream;
@@ -357,7 +402,7 @@ public void execute(Object input) {
attachment.setAttribute("ref", attachmentDatastreamId);
attachment.setAttribute("hasArchivalValue", yesno(augmentedDatastream.isHasArchivalValue()));
attachment.setAttribute("isDownloadable", yesno(augmentedDatastream.isDownloadable()));
- } else if (attachmentDatastream instanceof VoidDatastream) {
+ } else if ((attachmentDatastream instanceof VoidDatastream) && DELETED.equals(attachmentDatastream.getState())) {
rights.removeContent(
new XPathQuery("slub:attachment[@ref='" + attachmentDatastream.getId() + "']")
.selectNode(rights));
diff --git a/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_AbstractTest.java b/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_AbstractTest.java
index 3d6e3cf..ce74cbb 100644
--- a/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_AbstractTest.java
+++ b/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_AbstractTest.java
@@ -77,22 +77,25 @@ abstract class QucosaMETSFileHandler_AbstractTest {
));
}
- public static final String MEDIA_TYPE = "application/vnd.qucosa.mets+xml";
public static final String COLLECTION = "collection:test";
- public static final String USERNAME = "fedoraAdmin";
- public static final String SUBMITTER = "qucosa";
- public static final String METS_FILE_OK = "/mets_ok.xml";
- public static final String METS_FILE_URL = "/mets_url_file.xml";
- public static final String METS_FILE_BAD = "/mets_missing_mods.xml";
- public static final String METS_FILE_BAD2 = "/mets_invalid_file.xml";
+ public static final String CONTENT_MODEL = "info:fedora/qucosa:CModel";
+ public static final String MEDIA_TYPE = "application/vnd.qucosa.mets+xml";
public static final String METS_FILE_ADD_DS = "/mets_add_ds.xml";
public static final String METS_FILE_ALLREFS = "/mets_all_references.xml";
+ public static final String METS_FILE_BAD = "/mets_missing_mods.xml";
+ public static final String METS_FILE_BAD2 = "/mets_invalid_file.xml";
+ public static final String METS_FILE_CHECKSUM = "/mets_file_checksum.xml";
public static final String METS_FILE_DELETE_DS = "/mets_delete_ds.xml";
public static final String METS_FILE_FILEGROUPS = "/mets_download_filegroup.xml";
+ public static final String METS_FILE_OK = "/mets_ok.xml";
public static final String METS_FILE_UPDATE = "/mets_update.xml";
- public static final String METS_FILE_CHECKSUM = "/mets_file_checksum.xml";
public static final String METS_FILE_UPDATE_MD5 = "9a8d972d972eb799d989d0d2307c9822";
- public static final String CONTENT_MODEL = "info:fedora/qucosa:CModel";
+ public static final String METS_FILE_URL = "/mets_url_file.xml";
+ public static final String METS_JUST_SLUBINFO = "/mets_just_slubinfo.xml";
+ public static final String METS_JUST_SLUBINFO_WITHOUT_RIGHTS = "/mets_just_slubinfo_without_rights.xml";
+ public static final String METS_NO_FLOCAT = "/mets_no_flocat.xml";
+ public static final String SUBMITTER = "qucosa";
+ public static final String USERNAME = "fedoraAdmin";
protected FedoraRepository mockFedoraRepository;
protected Appender mockAppender;
diff --git a/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_UpdateTest.java b/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_UpdateTest.java
index 00bb7f3..209a258 100644
--- a/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_UpdateTest.java
+++ b/src/test/java/org/purl/sword/server/fedora/fileHandlers/QucosaMETSFileHandler_UpdateTest.java
@@ -19,6 +19,8 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.custommonkey.xmlunit.XMLAssert;
+import org.jdom.Document;
+import org.jdom.Element;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.purl.sword.atom.Link;
@@ -66,47 +68,44 @@ public void md5CheckNotPerformedIfNoMd5IsGiven() throws Exception {
@Test
public void dcGetsUpdated() throws Exception {
- verifyModifyDatastream("test:1", "DC");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "DC", METS_FILE_UPDATE);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void modsGetsUpdated() throws Exception {
- verifyModifyDatastream("test:1", "MODS");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "MODS", METS_FILE_UPDATE);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void slubinfoGetsUpdated() throws Exception {
- verifyModifyDatastream("test:1", "SLUB-INFO");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_FILE_UPDATE);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void slubinfoGetsAdded() throws Exception {
- verifyAddDatastream("test:1", "SLUB-INFO");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_FILE_UPDATE);
+ verify(mockFedoraRepository, atLeastOnce()).addDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void qucosaXmlGetsUpdated() throws Exception {
- verifyModifyDatastream("test:1", "QUCOSA-XML");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "QUCOSA-XML", METS_FILE_UPDATE);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void qucosaXmlGetsAdded() throws Exception {
- verifyAddDatastream("test:1", "QUCOSA-XML");
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "QUCOSA-XML", METS_FILE_UPDATE);
+ verify(mockFedoraRepository, atLeastOnce()).addDatastream(eq("test:1"), argument.capture(), anyString());
}
@Test
public void datastreamGetsUpdated() throws Exception {
- FileHandler fh = new QucosaMETSFileHandler();
- pretendObjectHasDatastream("test:1", "ATT-1");
- ArgumentCaptor argument = ArgumentCaptor.forClass(Datastream.class);
- DepositCollection depositCollection = buildDeposit(METS_FILE_UPDATE, "test:1");
-
- fh.updateDeposit(depositCollection, buildServiceDocument());
-
- verify(mockFedoraRepository).modifyDatastream(
- eq("test:1"),
- argument.capture(),
- anyString());
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "ATT-1", METS_FILE_UPDATE);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
assertEquals("Attachment", argument.getValue().getLabel());
}
@@ -160,12 +159,7 @@ public void swordResultHasCorrectEditLink() throws Exception {
@Test
public void removes_attachment_for_deleted_file_in_SlubInfo() throws Exception {
- FileHandler fh = new QucosaMETSFileHandler();
- pretendObjectHasDatastream("test:1", "SLUB-INFO");
- ArgumentCaptor argument = ArgumentCaptor.forClass(Datastream.class);
-
- fh.updateDeposit(buildDeposit(METS_FILE_DELETE_DS, "test:1"), buildServiceDocument());
-
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_FILE_DELETE_DS);
verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
Datastream ds = argument.getValue();
final String inXMLString = JDomHelper.makeString(((XMLInlineDatastream) ds).toXML());
@@ -173,22 +167,81 @@ public void removes_attachment_for_deleted_file_in_SlubInfo() throws Exception {
XMLAssert.assertXpathNotExists("//slub:rights/slub:attachment[@ref='ATT-1']", inXMLString);
}
- private void verifyAddDatastream(String pid, String dsid) throws Exception {
- FileHandler fh = new QucosaMETSFileHandler();
- pretendObjectHasDatastream(pid, dsid);
- ArgumentCaptor argument = ArgumentCaptor.forClass(Datastream.class);
- DepositCollection depositCollection = buildDeposit(METS_FILE_UPDATE, pid);
- fh.updateDeposit(depositCollection, buildServiceDocument());
- verify(mockFedoraRepository, atLeastOnce()).addDatastream(eq(pid), argument.capture(), anyString());
+ @Test
+ public void updates_existing_attachment_archival_value() throws Exception {
+ prepareMockWithSlubInfo();
+
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_NO_FLOCAT);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
+
+ final String inXMLString = JDomHelper.makeString(((XMLInlineDatastream) argument.getValue()).toXML());
+ XMLAssert.assertXpathExists("//slub:rights/slub:attachment[@ref='ATT-2'" +
+ " and @hasArchivalValue='yes'" +
+ " and @isDownloadable='no']", inXMLString);
+ }
+
+ @Test
+ public void dont_overwrite_existing_attachment_elements() throws Exception {
+ prepareMockWithSlubInfo();
+
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_JUST_SLUBINFO);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
+
+ final String inXMLString = JDomHelper.makeString(((XMLInlineDatastream) argument.getValue()).toXML());
+ XMLAssert.assertXpathExists("//slub:rights/slub:attachment[@ref='ATT-0' and @hasArchivalValue='yes']", inXMLString);
+ XMLAssert.assertXpathExists("//slub:rights/slub:attachment[@ref='ATT-2' and @hasArchivalValue='no']", inXMLString);
+ }
+
+ @Test
+ public void merge_attachment_elements_from_repo_if_there_is_none_in_deposit() throws Exception {
+ prepareMockWithSlubInfo();
+
+ ArgumentCaptor argument = runUpdateDeposit("test:1", "SLUB-INFO", METS_JUST_SLUBINFO_WITHOUT_RIGHTS);
+ verify(mockFedoraRepository).modifyDatastream(eq("test:1"), argument.capture(), anyString());
+
+ final String inXMLString = JDomHelper.makeString(((XMLInlineDatastream) argument.getValue()).toXML());
+ XMLAssert.assertXpathExists("//slub:rights/slub:attachment[@ref='ATT-0' and @hasArchivalValue='yes']", inXMLString);
+ XMLAssert.assertXpathExists("//slub:rights/slub:attachment[@ref='ATT-2' and @hasArchivalValue='no']", inXMLString);
+
+ }
+
+ private void prepareMockWithSlubInfo() {
+ when(mockFedoraRepository.getDatastream("test:1", "SLUB-INFO"))
+ .thenReturn(new XMLInlineDatastream("SLUB-INFO", buildSlubInfoWithAttachments(
+ "ATT-0", "yes",
+ "ATT-2", "no")));
+ }
+
+ private Document buildSlubInfoWithAttachments(String... params) {
+ if ((params.length == 0) || params.length % 2 != 0) {
+ throw new IllegalArgumentException("Expect even number of parameters");
+ }
+
+ Document slubInfoDocument;
+ slubInfoDocument = new Document();
+ final Element info = new Element("info", Namespaces.SLUB);
+ final Element rights = new Element("rights", Namespaces.SLUB);
+ slubInfoDocument.addContent(info.addContent(rights));
+
+ for (int i = 0; i < params.length; i = i + 2) {
+ final String dsid = params[i];
+ final String hasArchivalValue = params[i + 1];
+ final Element attachment = new Element("attachment", Namespaces.SLUB);
+ attachment.setAttribute("ref", dsid);
+ attachment.setAttribute("hasArchivalValue", hasArchivalValue);
+ rights.addContent(attachment);
+ }
+
+ return slubInfoDocument;
}
- private void verifyModifyDatastream(String pid, String dsid) throws Exception {
+ private ArgumentCaptor runUpdateDeposit(String pid, String dsid, String metsFileName) throws Exception {
FileHandler fh = new QucosaMETSFileHandler();
pretendObjectHasDatastream(pid, dsid);
ArgumentCaptor argument = ArgumentCaptor.forClass(Datastream.class);
- DepositCollection depositCollection = buildDeposit(METS_FILE_UPDATE, pid);
+ DepositCollection depositCollection = buildDeposit(metsFileName, pid);
fh.updateDeposit(depositCollection, buildServiceDocument());
- verify(mockFedoraRepository).modifyDatastream(eq(pid), argument.capture(), anyString());
+ return argument;
}
private String reverse(String s) {
diff --git a/src/test/resources/mets_just_slubinfo.xml b/src/test/resources/mets_just_slubinfo.xml
new file mode 100644
index 0000000..a8ce286
--- /dev/null
+++ b/src/test/resources/mets_just_slubinfo.xml
@@ -0,0 +1,64 @@
+
+
+
+
+
+
+
+
+
+ Test
+
+
+
+
+
+
+
+
+
+
+
+
+ Frau Administrator
+ mailto:qucosa.admin@slub-dresden.de
+
+ OpenAire Project Name
+ Mandant
+
+ Lizenzangabe
+ 2014-11-26
+ false
+ false
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/test/resources/mets_just_slubinfo_without_rights.xml b/src/test/resources/mets_just_slubinfo_without_rights.xml
new file mode 100644
index 0000000..66580dd
--- /dev/null
+++ b/src/test/resources/mets_just_slubinfo_without_rights.xml
@@ -0,0 +1,58 @@
+
+
+
+
+
+
+
+
+
+ Test
+
+
+
+
+
+
+
+
+
+
+
+
+ Frau Administrator
+ mailto:qucosa.admin@slub-dresden.de
+
+ OpenAire Project Name
+ Mandant
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/test/resources/mets_no_flocat.xml b/src/test/resources/mets_no_flocat.xml
new file mode 100644
index 0000000..9d72bc5
--- /dev/null
+++ b/src/test/resources/mets_no_flocat.xml
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+ METS File Checksum Test
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+