Skip to content

Commit 5dfb166

Browse files
committed
Fix bugs with deposit record transformation, add helper method for building ingest source managers in tests
1 parent 2c5b5cd commit 5dfb166

File tree

5 files changed

+212
-115
lines changed

5 files changed

+212
-115
lines changed

migration-util/src/main/java/edu/unc/lib/dcr/migration/deposit/DepositRecordTransformer.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,28 @@ private void addManifests() {
232232

233233
// Seek the path to the staged file for this manifest
234234
Path manifestPath = paths.stream()
235-
.filter(p -> p.endsWith(dsName + ".0"))
235+
.filter(p -> p.toString().endsWith(dsName + ".0"))
236236
.findFirst()
237-
.orElseGet(null);
237+
.orElse(null);
238238

239239
if (manifestPath == null) {
240+
manifestNum++;
240241
log.error("Failed to find path for recorded manifest {} on object {}", dsName, bxc3Pid);
241242
continue;
242243
}
243244

245+
PID manifestPid = getDepositManifestPid(bxc5Pid, dsName);
244246
// Transfer the manifest to its permanent storage location
245-
URI manifestStoredUri = transferSession.transfer(bxc5Pid, manifestPath.toUri());
247+
URI manifestStoredUri = transferSession.transfer(manifestPid, manifestPath.toUri());
246248

247249
// Populate manifest timestamps
248250
Model manifestModel = ModelFactory.createDefaultModel();
249251
Resource selfResc = manifestModel.getResource("");
250-
selfResc.addLiteral(Fcrepo4Repository.lastModified, created);
251-
selfResc.addLiteral(Fcrepo4Repository.created, created);
252+
selfResc.addLiteral(DC.title, StringUtils.isBlank(label) ? dsName : label);
253+
selfResc.addProperty(Fcrepo4Repository.lastModified, created, XSDDatatype.XSDdateTime);
254+
selfResc.addProperty(Fcrepo4Repository.created, created, XSDDatatype.XSDdateTime);
252255

253256
// Create the manifest in fedora
254-
PID manifestPid = getDepositManifestPid(bxc5Pid, label);
255257
repoObjFactory.createOrUpdateBinary(manifestPid, manifestStoredUri, label,
256258
mimetype, null, md5, manifestModel);
257259

@@ -275,4 +277,8 @@ public void setRepositoryObjectFactory(RepositoryObjectFactory repoObjFactory) {
275277
public void setTransactionManager(TransactionManager txManager) {
276278
this.txManager = txManager;
277279
}
280+
281+
public PID getPid() {
282+
return bxc3Pid;
283+
}
278284
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Copyright 2008 The University of North Carolina at Chapel Hill
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package edu.unc.lib.dcr.migration.deposit;
17+
18+
import static edu.unc.lib.dcr.migration.MigrationConstants.toBxc3Uri;
19+
import static edu.unc.lib.dcr.migration.fcrepo3.ContentModelHelper.FedoraProperty.hasModel;
20+
import static edu.unc.lib.dcr.migration.fcrepo3.FoxmlDocumentHelpers.PREMIS_DS;
21+
import static edu.unc.lib.dcr.migration.premis.Premis2Constants.INITIATOR_ROLE;
22+
import static edu.unc.lib.dcr.migration.premis.Premis2Constants.VIRUS_CHECK_TYPE;
23+
import static edu.unc.lib.dcr.migration.premis.TestPremisEventHelpers.EVENT_DATE;
24+
import static edu.unc.lib.dcr.migration.premis.TestPremisEventHelpers.addAgent;
25+
import static edu.unc.lib.dcr.migration.premis.TestPremisEventHelpers.addEvent;
26+
import static edu.unc.lib.dcr.migration.premis.TestPremisEventHelpers.createPremisDoc;
27+
import static edu.unc.lib.dcr.migration.premis.TestPremisEventHelpers.listEventResources;
28+
import static java.nio.charset.StandardCharsets.UTF_8;
29+
import static java.nio.file.Files.newOutputStream;
30+
import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
31+
import static org.junit.Assert.assertEquals;
32+
33+
import java.io.IOException;
34+
import java.io.OutputStream;
35+
import java.nio.file.Path;
36+
import java.util.Date;
37+
import java.util.List;
38+
39+
import org.apache.commons.io.FileUtils;
40+
import org.apache.commons.io.IOUtils;
41+
import org.apache.jena.rdf.model.Model;
42+
import org.apache.jena.rdf.model.Resource;
43+
import org.apache.jena.vocabulary.RDF;
44+
import org.jdom2.Document;
45+
import org.jdom2.Element;
46+
import org.jdom2.output.XMLOutputter;
47+
import org.junit.Rule;
48+
import org.junit.rules.TemporaryFolder;
49+
50+
import edu.unc.lib.dcr.migration.fcrepo3.ContentModelHelper.ContentModel;
51+
import edu.unc.lib.dcr.migration.fcrepo3.FoxmlDocumentBuilder;
52+
import edu.unc.lib.dl.event.PremisLogger;
53+
import edu.unc.lib.dl.fcrepo4.BinaryObject;
54+
import edu.unc.lib.dl.fcrepo4.DepositRecord;
55+
import edu.unc.lib.dl.fedora.PID;
56+
import edu.unc.lib.dl.rdf.Premis;
57+
import edu.unc.lib.dl.util.DateTimeUtil;
58+
59+
/**
60+
* @author bbpennel
61+
*/
62+
public abstract class AbstractDepositRecordTransformationIT {
63+
static {
64+
System.setProperty("fcrepo.properties.management", "relaxed");
65+
}
66+
67+
protected final static Date DEFAULT_CREATED_DATE = DateTimeUtil.parseUTCToDate(
68+
FoxmlDocumentBuilder.DEFAULT_CREATED_DATE);
69+
protected final static Date DEFAULT_MODIFIED_DATE = DateTimeUtil.parseUTCToDate(
70+
FoxmlDocumentBuilder.DEFAULT_LAST_MODIFIED);
71+
72+
@Rule
73+
public final TemporaryFolder tmpFolder = new TemporaryFolder();
74+
75+
protected Path objectsPath;
76+
77+
protected Path datastreamsPath;
78+
79+
protected Path serializeFoxml(PID pid, Document doc) throws IOException {
80+
Path xmlPath = objectsPath.resolve("uuid_" + pid.getId());
81+
OutputStream outStream = newOutputStream(xmlPath);
82+
new XMLOutputter().output(doc, outStream);
83+
return xmlPath;
84+
}
85+
86+
protected Path writeManifestFile(PID pid, String dsName, String content) throws IOException {
87+
Path dsPath = datastreamsPath.resolve("uuid_" + pid.getId() + "+" + dsName + "+" + dsName + ".0");
88+
FileUtils.writeStringToFile(dsPath.toFile(), content, UTF_8);
89+
return dsPath;
90+
}
91+
92+
protected Model createModelWithTypes(PID pid, ContentModel... models) {
93+
Model model = createDefaultModel();
94+
Resource resc = model.getResource(toBxc3Uri(pid));
95+
for (ContentModel contentModel : models) {
96+
resc.addProperty(hasModel.getProperty(), contentModel.getResource());
97+
}
98+
return model;
99+
}
100+
101+
protected void addPremisLog(PID originalPid) throws IOException {
102+
Document premisDoc = createPremisDoc(originalPid);
103+
String detail = "virus scan";
104+
Element eventEl = addEvent(premisDoc, VIRUS_CHECK_TYPE, detail, EVENT_DATE);
105+
addAgent(eventEl, "Name", INITIATOR_ROLE, "virusscanner");
106+
107+
String premisDsName = "uuid_" + originalPid.getId() + "+" + PREMIS_DS + "+" + PREMIS_DS + ".0";
108+
Path xmlPath = datastreamsPath.resolve(premisDsName);
109+
OutputStream outStream = newOutputStream(xmlPath);
110+
new XMLOutputter().output(premisDoc, outStream);
111+
}
112+
113+
protected BinaryObject getManifestByName(List<BinaryObject> binList, String dsName) {
114+
return binList.stream()
115+
.filter(b -> b.getPid().getComponentPath().endsWith(dsName))
116+
.findFirst()
117+
.get();
118+
}
119+
120+
protected void assertManifestDetails(Date expectedTimestamp, String expectedMimetype,
121+
String expectedContent, BinaryObject manifestBin) throws IOException {
122+
assertEquals(expectedTimestamp, manifestBin.getLastModified());
123+
assertEquals(expectedTimestamp, manifestBin.getCreatedDate());
124+
assertEquals(expectedMimetype, manifestBin.getMimetype());
125+
assertEquals(expectedContent, IOUtils.toString(manifestBin.getBinaryStream(), UTF_8));
126+
}
127+
128+
protected void assertPremisTransformed(DepositRecord depRec) throws IOException {
129+
PremisLogger premisLog = depRec.getPremisLog();
130+
Model eventsModel = premisLog.getEventsModel();
131+
List<Resource> eventRescs = listEventResources(depRec.getPid(), eventsModel);
132+
assertEquals(1, eventRescs.size());
133+
134+
Resource eventResc = eventRescs.get(0);
135+
assertEquals("Event type did not match expected value",
136+
Premis.VirusCheck, eventResc.getPropertyResourceValue(RDF.type));
137+
}
138+
}

0 commit comments

Comments
 (0)