|
| 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