From f9f765f3336517ce4a485633d64a602611d1f114 Mon Sep 17 00:00:00 2001 From: lindareijnhoudt Date: Mon, 13 Dec 2021 12:40:17 +0100 Subject: [PATCH] DD-710: Add agreements.xml to VAULT deposits (#66) --- .../cfg/agreements/TESTUSER-agreements.xml | 35 +++++++++++++++++++ .../Command.scala | 3 +- .../Configuration.scala | 1 + .../EasyConvertBagToDepositApp.scala | 19 ++++++++++ .../base-bag-not-found/metadata/amd.xml | 2 +- .../metadata/depositor-info/agreements.xml | 14 -------- .../agreements/TESTUSER-agreements.xml | 35 +++++++++++++++++++ .../AppSpec.scala | 25 +++++++++++++ .../Fixture/AppConfigSupport.scala | 1 + .../ddm/RewriteSpec.scala | 1 + 10 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 src/main/assembly/dist/cfg/agreements/TESTUSER-agreements.xml delete mode 100644 src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/depositor-info/agreements.xml create mode 100644 src/test/resources/debug-config/agreements/TESTUSER-agreements.xml diff --git a/src/main/assembly/dist/cfg/agreements/TESTUSER-agreements.xml b/src/main/assembly/dist/cfg/agreements/TESTUSER-agreements.xml new file mode 100644 index 00000000..d9358aea --- /dev/null +++ b/src/main/assembly/dist/cfg/agreements/TESTUSER-agreements.xml @@ -0,0 +1,35 @@ + + + + + Test User + 2019-01-02 + true + + + Test User + 2019-01-02 + true + + + diff --git a/src/main/scala/nl.knaw.dans.easy.bag2deposit/Command.scala b/src/main/scala/nl.knaw.dans.easy.bag2deposit/Command.scala index fbb07e43..f7927918 100644 --- a/src/main/scala/nl.knaw.dans.easy.bag2deposit/Command.scala +++ b/src/main/scala/nl.knaw.dans.easy.bag2deposit/Command.scala @@ -68,7 +68,8 @@ object Command extends App with DebugEnhancedLogging { fedoraProvider = fedoraProvider, maybePreStagedProvider = if (commandLine.preStaged()) Some(PreStagedProvider(new URI(properties.getString("migration-info.url")))) - else None + else None, + agreementsPath = cfgPath / "agreements" ) trace(configuration.maybePreStagedProvider) private val propertiesFactory = DepositPropertiesFactory( diff --git a/src/main/scala/nl.knaw.dans.easy.bag2deposit/Configuration.scala b/src/main/scala/nl.knaw.dans.easy.bag2deposit/Configuration.scala index d5d2e57a..88d2ceed 100644 --- a/src/main/scala/nl.knaw.dans.easy.bag2deposit/Configuration.scala +++ b/src/main/scala/nl.knaw.dans.easy.bag2deposit/Configuration.scala @@ -33,4 +33,5 @@ case class Configuration(version: String, userTransformer: UserTransformer, fedoraProvider: Option[FedoraProvider], maybePreStagedProvider: Option[PreStagedProvider], + agreementsPath: File, ) diff --git a/src/main/scala/nl.knaw.dans.easy.bag2deposit/EasyConvertBagToDepositApp.scala b/src/main/scala/nl.knaw.dans.easy.bag2deposit/EasyConvertBagToDepositApp.scala index 47f1a71e..e2bee12a 100644 --- a/src/main/scala/nl.knaw.dans.easy.bag2deposit/EasyConvertBagToDepositApp.scala +++ b/src/main/scala/nl.knaw.dans.easy.bag2deposit/EasyConvertBagToDepositApp.scala @@ -109,6 +109,8 @@ class EasyConvertBagToDepositApp(configuration: Configuration) extends DebugEnha amdIn <- getAmdXml(datasetId, amdFile) fromVault = depositProps.getString("deposit.origin") == "VAULT" amdOut <- configuration.userTransformer.transform(amdIn) + agreementsFile = metadata / "depositor-info" / "agreements.xml" + _ = checkAgreementsXml((amdOut \ "depositorId").text, agreementsFile) provenanceXml = provenance.collectChangesInXmls(Map( "http://easy.dans.knaw.nl/easy/dataset-administrative-metadata/" -> compare(amdIn, amdOut), "http://easy.dans.knaw.nl/schemas/md/ddm/" -> compare(oldDcmi, newDcmi), @@ -180,6 +182,23 @@ class EasyConvertBagToDepositApp(configuration: Configuration) extends DebugEnha } } + private def checkAgreementsXml(depositorId: String, agreementsFile: File) = { + if (agreementsFile.exists) { //the agreementsfile is created by easy-fedora-to-bag for FEDORA datasets + Success + } + else { + val templateFile: File = configuration.agreementsPath / (depositorId + "-agreements.xml") + if (!templateFile.exists) { + Failure(new FileNotFoundException(templateFile + " not found")) + } + else { + agreementsFile.parent.createIfNotExists(true) + templateFile.copyTo(agreementsFile) + } + } + } + + private def copyMigrationFiles(metadata: File, migration: File, fromVault: Boolean): Try[Unit] = Try { trace(metadata, migration) val filesXmlFile = (metadata / "files.xml").toString() diff --git a/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/amd.xml b/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/amd.xml index cac05721..58f0d4d3 100644 --- a/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/amd.xml +++ b/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/amd.xml @@ -3,7 +3,7 @@ PUBLISHED DRAFT 2020-02-02T20:02:00.000+01:00 - user001 + TESTUSER DRAFT diff --git a/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/depositor-info/agreements.xml b/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/depositor-info/agreements.xml deleted file mode 100644 index a60cbfea..00000000 --- a/src/test/resources/bags/01/87151a3a-12ed-426a-94f2-97313c7ae1f2/base-bag-not-found/metadata/depositor-info/agreements.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - Full Name - 2019-05-03T11:54:26.638+02:00 - true - - - user001 - 2019-05-03T11:54:26.638+02:00 - true - - diff --git a/src/test/resources/debug-config/agreements/TESTUSER-agreements.xml b/src/test/resources/debug-config/agreements/TESTUSER-agreements.xml new file mode 100644 index 00000000..d9358aea --- /dev/null +++ b/src/test/resources/debug-config/agreements/TESTUSER-agreements.xml @@ -0,0 +1,35 @@ + + + + + Test User + 2019-01-02 + true + + + Test User + 2019-01-02 + true + + + diff --git a/src/test/scala/nl.knaw.dans.easy.bag2deposit/AppSpec.scala b/src/test/scala/nl.knaw.dans.easy.bag2deposit/AppSpec.scala index c552a03f..e085d2a7 100644 --- a/src/test/scala/nl.knaw.dans.easy.bag2deposit/AppSpec.scala +++ b/src/test/scala/nl.knaw.dans.easy.bag2deposit/AppSpec.scala @@ -30,6 +30,7 @@ import scala.xml.XML class AppSpec extends AnyFlatSpec with XmlSupport with Matchers with AppConfigSupport with FileSystemSupport { private val resourceBags: File = File("src/test/resources/bags/01") private val validUUID = "04e638eb-3af1-44fb-985d-36af12fccb2d" + private val vaultUUID = "87151a3a-12ed-426a-94f2-97313c7ae1f2" "addPropsToBags" should "move valid exports" in { val delegate = mock[MockBagIndex] @@ -368,6 +369,30 @@ class AppSpec extends AnyFlatSpec with XmlSupport with Matchers with AppConfigSu normalized(XML.loadFile(filesXml.toJava)) shouldBe normalized(expectedFilesXml) } + it should "add agreements.xml file to VAULT datasets" in { + val delegate = mock[MockBagIndex] + (delegate.execute(_: String)) expects s"bag-sequence?contains=$vaultUUID" returning + new HttpResponse[String]("123", 200, Map.empty) + (delegate.execute(_: String)) expects s"bags/4722d09d-e431-4899-904c-0733cd773034" returning + new HttpResponse[String]("urn:nbn:nl:ui:13-z4-f8cm10.5072/dans-2xg-umq8", 200, Map.empty) + val appConfig = testConfig(delegatingBagIndex(delegate), null) + (appConfig.maybePreStagedProvider.get.get(_: String, _: Int)) expects(*, 1) returning Success(Seq.empty) + + + (resourceBags / vaultUUID).copyTo(testDir / "exports" / vaultUUID) + + new EasyConvertBagToDepositApp(appConfig).addPropsToBags( + (testDir / "exports").children, + maybeOutputDir = Some((testDir / "ingest-dir").createDirectories()), + DepositPropertiesFactory(appConfig, DOI, VAULT) + ) shouldBe Success("No fatal errors") + + val movedBag = testDir / "ingest-dir" / vaultUUID / "base-bag-not-found" + (resourceBags / vaultUUID / "base-bag-not-found" / "metadata").list.toSeq.map(_.name) shouldNot contain("agreements.xml") + (movedBag / "metadata" ).list.toSeq.map(_.name) should contain("depositor-info") + (movedBag / "metadata" / "depositor-info").list.toSeq.map(_.name) shouldBe Seq("agreements.xml") + } + it should "produce proper prefix in new files.xml elements AND apply preferred user ID" in { val delegate = mock[MockBagIndex] val appConfig = testConfig(delegatingBagIndex(delegate), null) diff --git a/src/test/scala/nl.knaw.dans.easy.bag2deposit/Fixture/AppConfigSupport.scala b/src/test/scala/nl.knaw.dans.easy.bag2deposit/Fixture/AppConfigSupport.scala index 2bea92dd..65b47aa2 100644 --- a/src/test/scala/nl.knaw.dans.easy.bag2deposit/Fixture/AppConfigSupport.scala +++ b/src/test/scala/nl.knaw.dans.easy.bag2deposit/Fixture/AppConfigSupport.scala @@ -32,6 +32,7 @@ trait AppConfigSupport extends BagIndexSupport with FedoraProviderSupport with P userTransformer = new UserTransformer(cfgFile), fedoraProvider = fedoraProvider, maybePreStagedProvider = Some(preStagedProvider), + agreementsPath = cfgFile / "agreements" ) } } diff --git a/src/test/scala/nl.knaw.dans.easy.bag2deposit/ddm/RewriteSpec.scala b/src/test/scala/nl.knaw.dans.easy.bag2deposit/ddm/RewriteSpec.scala index 0a10dd48..257532ee 100644 --- a/src/test/scala/nl.knaw.dans.easy.bag2deposit/ddm/RewriteSpec.scala +++ b/src/test/scala/nl.knaw.dans.easy.bag2deposit/ddm/RewriteSpec.scala @@ -122,6 +122,7 @@ class RewriteSpec extends AnyFlatSpec with XmlSupport with SchemaSupport with Ma userTransformer = new UserTransformer(cfgDir), fedoraProvider = None, maybePreStagedProvider = None, + agreementsPath = cfgDir / "agreements" )) // a few steps of EasyConvertBagToDepositApp.addPropsToBags