|
| 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.deposit.normalize; |
| 17 | + |
| 18 | +import static edu.unc.lib.boxc.common.test.TestHelpers.setField; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | +import static org.mockito.Matchers.anyString; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.io.FileNotFoundException; |
| 26 | +import java.io.Writer; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import org.apache.jena.rdf.model.Model; |
| 33 | +import org.apache.jena.rdf.model.ModelFactory; |
| 34 | +import org.apache.jena.rdf.model.Resource; |
| 35 | +import org.apache.jena.vocabulary.DC; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +import edu.unc.lib.boxc.deposit.api.RedisWorkerConstants.DepositField; |
| 40 | +import edu.unc.lib.boxc.deposit.impl.model.DepositDirectoryManager; |
| 41 | +import edu.unc.lib.deposit.fcrepo4.AbstractDepositJobTest; |
| 42 | +import edu.unc.lib.deposit.work.JobFailedException; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author bbpennel |
| 46 | + */ |
| 47 | +public class PreconstructedDepositJobTest extends AbstractDepositJobTest { |
| 48 | + private PreconstructedDepositJob job; |
| 49 | + private Map<String, String> status; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setup() throws Exception { |
| 53 | + status = new HashMap<>(); |
| 54 | + when(depositStatusFactory.get(anyString())).thenReturn(status); |
| 55 | + |
| 56 | + job = new PreconstructedDepositJob(); |
| 57 | + job.setDepositUUID(depositUUID); |
| 58 | + job.setDepositDirectory(depositDir); |
| 59 | + setField(job, "pidMinter", pidMinter); |
| 60 | + setField(job, "depositModelManager", depositModelManager); |
| 61 | + setField(job, "depositsDirectory", depositsDirectory); |
| 62 | + setField(job, "depositStatusFactory", depositStatusFactory); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void noSourceNoModelFilePrepopulatedModel() throws Exception { |
| 67 | + Model writeModel = job.getWritableModel(); |
| 68 | + Resource preResc = writeModel.getResource(depositPid.getRepositoryPath()); |
| 69 | + preResc.addLiteral(DC.title, "Test value"); |
| 70 | + job.closeModel(); |
| 71 | + |
| 72 | + job.run(); |
| 73 | + |
| 74 | + Model model = job.getReadOnlyModel(); |
| 75 | + assertEquals(1, model.listStatements().toList().size()); |
| 76 | + Resource postResc = model.getResource(depositPid.getRepositoryPath()); |
| 77 | + assertTrue(postResc.hasLiteral(DC.title, "Test value")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void withExternalSourceNoModelFile() throws Exception { |
| 82 | + Path externalBasePath = tmpFolder.newFolder().toPath(); |
| 83 | + DepositDirectoryManager extDirManager = new DepositDirectoryManager(depositPid, externalBasePath, true); |
| 84 | + status.put(DepositField.sourceUri.name(), extDirManager.getDepositDir().toUri().toString()); |
| 85 | + Files.createFile(extDirManager.getPremisPath(depositPid)); |
| 86 | + |
| 87 | + job.run(); |
| 88 | + |
| 89 | + Model model = job.getReadOnlyModel(); |
| 90 | + assertEquals(0, model.listStatements().toList().size()); |
| 91 | + |
| 92 | + DepositDirectoryManager intDirManager = new DepositDirectoryManager( |
| 93 | + depositPid, depositsDirectory.toPath(), true, false); |
| 94 | + Path intPremisPath = intDirManager.getPremisPath(depositPid); |
| 95 | + assertTrue(Files.exists(intPremisPath)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void withInternalSourceNoModelFile() throws Exception { |
| 100 | + DepositDirectoryManager preDirManager = new DepositDirectoryManager( |
| 101 | + depositPid, depositsDirectory.toPath(), true); |
| 102 | + status.put(DepositField.sourceUri.name(), preDirManager.getDepositDir().toUri().toString()); |
| 103 | + Files.createFile(preDirManager.getPremisPath(depositPid)); |
| 104 | + |
| 105 | + job.run(); |
| 106 | + |
| 107 | + Model model = job.getReadOnlyModel(); |
| 108 | + assertEquals(0, model.listStatements().toList().size()); |
| 109 | + |
| 110 | + DepositDirectoryManager postDirManager = new DepositDirectoryManager( |
| 111 | + depositPid, depositsDirectory.toPath(), true, false); |
| 112 | + Path intPremisPath = postDirManager.getPremisPath(depositPid); |
| 113 | + assertTrue(Files.exists(intPremisPath)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void noSourceWithModelFile() throws Exception { |
| 118 | + DepositDirectoryManager preDirManager = new DepositDirectoryManager( |
| 119 | + depositPid, depositsDirectory.toPath(), true); |
| 120 | + Model importModel = ModelFactory.createDefaultModel(); |
| 121 | + Resource preResc = importModel.getResource(depositPid.getRepositoryPath()); |
| 122 | + preResc.addLiteral(DC.title, "Import value"); |
| 123 | + Writer writer = Files.newBufferedWriter(preDirManager.getModelPath()); |
| 124 | + importModel.write(writer, "N3"); |
| 125 | + |
| 126 | + job.run(); |
| 127 | + |
| 128 | + Model model = job.getReadOnlyModel(); |
| 129 | + assertEquals(1, model.listStatements().toList().size()); |
| 130 | + Resource postResc = model.getResource(depositPid.getRepositoryPath()); |
| 131 | + assertTrue(postResc.hasLiteral(DC.title, "Import value")); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void withSourceWithModelFile() throws Exception { |
| 136 | + Path externalBasePath = tmpFolder.newFolder().toPath(); |
| 137 | + DepositDirectoryManager extDirManager = new DepositDirectoryManager( |
| 138 | + depositPid, externalBasePath, true); |
| 139 | + Model importModel = ModelFactory.createDefaultModel(); |
| 140 | + Resource extResc = importModel.getResource(depositPid.getRepositoryPath()); |
| 141 | + extResc.addLiteral(DC.title, "Import me"); |
| 142 | + Writer writer = Files.newBufferedWriter(extDirManager.getModelPath()); |
| 143 | + importModel.write(writer, "N3"); |
| 144 | + |
| 145 | + status.put(DepositField.sourceUri.name(), extDirManager.getDepositDir().toUri().toString()); |
| 146 | + Files.createFile(extDirManager.getPremisPath(depositPid)); |
| 147 | + |
| 148 | + job.run(); |
| 149 | + |
| 150 | + Model model = job.getReadOnlyModel(); |
| 151 | + assertEquals(1, model.listStatements().toList().size()); |
| 152 | + Resource postResc = model.getResource(depositPid.getRepositoryPath()); |
| 153 | + assertTrue(postResc.hasLiteral(DC.title, "Import me")); |
| 154 | + |
| 155 | + DepositDirectoryManager intDirManager = new DepositDirectoryManager( |
| 156 | + depositPid, depositsDirectory.toPath(), true, false); |
| 157 | + Path intPremisPath = intDirManager.getPremisPath(depositPid); |
| 158 | + assertTrue(Files.exists(intPremisPath)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void noSourceWithModelFilePrepopulatedModel() throws Exception { |
| 163 | + Model writeModel = job.getWritableModel(); |
| 164 | + Resource preResc = writeModel.getResource(depositPid.getRepositoryPath()); |
| 165 | + preResc.addLiteral(DC.title, "Pre-existing Condition"); |
| 166 | + job.closeModel(); |
| 167 | + |
| 168 | + DepositDirectoryManager preDirManager = new DepositDirectoryManager( |
| 169 | + depositPid, depositsDirectory.toPath(), true); |
| 170 | + Model importModel = ModelFactory.createDefaultModel(); |
| 171 | + Resource importResc = importModel.getResource(depositPid.getRepositoryPath()); |
| 172 | + importResc.addLiteral(DC.title, "Imported Titles"); |
| 173 | + Writer writer = Files.newBufferedWriter(preDirManager.getModelPath()); |
| 174 | + importModel.write(writer, "N3"); |
| 175 | + |
| 176 | + job.run(); |
| 177 | + |
| 178 | + Model model = job.getReadOnlyModel(); |
| 179 | + assertEquals(1, model.listStatements().toList().size()); |
| 180 | + Resource postResc = model.getResource(depositPid.getRepositoryPath()); |
| 181 | + assertTrue(postResc.hasLiteral(DC.title, "Imported Titles")); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void sourceDirDoesNotExistTest() throws Exception { |
| 186 | + Path externalBasePath = tmpFolder.newFolder().toPath(); |
| 187 | + Files.delete(externalBasePath); |
| 188 | + DepositDirectoryManager extDirManager = new DepositDirectoryManager(depositPid, externalBasePath, true, false); |
| 189 | + status.put(DepositField.sourceUri.name(), extDirManager.getDepositDir().toUri().toString()); |
| 190 | + |
| 191 | + try { |
| 192 | + job.run(); |
| 193 | + fail(); |
| 194 | + } catch (JobFailedException e) { |
| 195 | + assertTrue(e.getCause() instanceof FileNotFoundException); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments