From 3df27f658631f358f503cd7d2cdde201a79d76d7 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 3 Jun 2024 16:30:37 +0200 Subject: [PATCH] Replace project references in ids when mapped (#5006) Co-authored-by: Simon Dumas --- .../epfl/bluebrain/nexus/ship/IriPatcher.scala | 16 +++++++++++----- .../ch/epfl/bluebrain/nexus/ship/RunShip.scala | 2 +- .../nexus/ship/EventProcessorSuite.scala | 2 +- .../bluebrain/nexus/ship/IriPatcherSuite.scala | 12 +++++++++++- .../ship/resources/SourcePatcherSuite.scala | 2 +- .../nexus/ship/views/ViewPatcherSuite.scala | 2 +- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/IriPatcher.scala b/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/IriPatcher.scala index d55db55ae3..ee5cac4386 100644 --- a/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/IriPatcher.scala +++ b/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/IriPatcher.scala @@ -1,6 +1,7 @@ package ch.epfl.bluebrain.nexus.ship import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri +import ch.epfl.bluebrain.nexus.ship.config.InputConfig.ProjectMapping import ch.epfl.bluebrain.nexus.ship.config.IriPatcherConfig /** @@ -21,13 +22,18 @@ object IriPatcher { override def apply(original: Iri): Iri = original } - def apply(originalPrefix: Iri, targetPrefix: Iri): IriPatcher = new IriPatcher { + def apply(originalPrefix: Iri, targetPrefix: Iri, projectMapping: ProjectMapping): IriPatcher = new IriPatcher { private val originalPrefixAsString = originalPrefix.toString override def apply(original: Iri): Iri = { val originalAsString = original.toString if (originalAsString.startsWith(originalPrefixAsString)) { - val suffix = original.stripPrefix(originalPrefixAsString) - targetPrefix / suffix + val suffix = original.stripPrefix(originalPrefixAsString) + val suffixWithMappedProject = projectMapping.foldLeft(suffix) { + case (accSuffix, (originalProject, targetProject)) => + accSuffix.replaceAll(originalProject.toString, targetProject.toString) + } + + targetPrefix / suffixWithMappedProject } else original } @@ -35,9 +41,9 @@ object IriPatcher { override def enabled: Boolean = true } - def apply(config: IriPatcherConfig): IriPatcher = + def apply(config: IriPatcherConfig, projectMapping: ProjectMapping): IriPatcher = if (config.enabled) - IriPatcher(config.originalPrefix, config.targetPrefix) + IriPatcher(config.originalPrefix, config.targetPrefix, projectMapping) else noop } diff --git a/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/RunShip.scala b/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/RunShip.scala index 97347efe3d..73a6a3135e 100644 --- a/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/RunShip.scala +++ b/ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/RunShip.scala @@ -45,7 +45,7 @@ object RunShip { val originalBaseUri = config.originalBaseUri val targetBaseUri = config.targetBaseUri val projectMapper = ProjectMapper(config.projectMapping) - implicit val iriPatcher: IriPatcher = IriPatcher(config.iriPatcher) + implicit val iriPatcher: IriPatcher = IriPatcher(config.iriPatcher, config.projectMapping) for { // Provision organizations _ <- orgProvider.create(config.organizations.values) diff --git a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/EventProcessorSuite.scala b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/EventProcessorSuite.scala index 91c0e4c64d..5e932cd173 100644 --- a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/EventProcessorSuite.scala +++ b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/EventProcessorSuite.scala @@ -29,7 +29,7 @@ class EventProcessorSuite extends NexusSuite { } test("Replace the original id in the value and pass it down") { - implicit val iriPatcher = IriPatcher(iri"https://bbp.epfl.ch/", iri"https://openbrainplatform.com/") + implicit val iriPatcher = IriPatcher(iri"https://bbp.epfl.ch/", iri"https://openbrainplatform.com/", Map.empty) val project = ProjectRef.unsafe("org", "proj") val originalId = iri"https://bbp.epfl.ch/pr1" val expectedId = iri"https://openbrainplatform.com/pr1" diff --git a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/IriPatcherSuite.scala b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/IriPatcherSuite.scala index 3426ab0630..404a090020 100644 --- a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/IriPatcherSuite.scala +++ b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/IriPatcherSuite.scala @@ -1,6 +1,7 @@ package ch.epfl.bluebrain.nexus.ship import ch.epfl.bluebrain.nexus.delta.rdf.syntax.iriStringContextSyntax +import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite class IriPatcherSuite extends NexusSuite { @@ -8,7 +9,10 @@ class IriPatcherSuite extends NexusSuite { private val originalPrefix = iri"https://bbp.epfl.ch/" private val targetPrefix = iri"https:/openbrainplatform.com/" - private val iriPatcher = IriPatcher(originalPrefix, targetPrefix) + private val originalProject = ProjectRef.unsafe("org", "proj") + private val targetProject = ProjectRef.unsafe("target-org", "target-proj") + + private val iriPatcher = IriPatcher(originalPrefix, targetPrefix, Map(originalProject -> targetProject)) test("Keep the original if it starts by another prefix") { val original = iri"https://www.epfl.ch/something" @@ -21,4 +25,10 @@ class IriPatcherSuite extends NexusSuite { assertEquals(iriPatcher(original), expected) } + test("Replace the target prefix and the project reference if the original prefix matches") { + val original = iri"https://bbp.epfl.ch/data/org/proj/id" + val expected = iri"https:/openbrainplatform.com/data/target-org/target-proj/id" + assertEquals(iriPatcher(original), expected) + } + } diff --git a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcherSuite.scala b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcherSuite.scala index d4424caea1..2e906eba5e 100644 --- a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcherSuite.scala +++ b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/resources/SourcePatcherSuite.scala @@ -25,7 +25,7 @@ class SourcePatcherSuite extends NexusSuite { test("Patch iris in original payload") { val originalPrefix = iri"https://bbp.epfl.ch/" val targetPrefix = iri"https://openbrainplatform.com/" - val iriPatcher = IriPatcher(originalPrefix, targetPrefix) + val iriPatcher = IriPatcher(originalPrefix, targetPrefix, Map.empty) val template = "payload/sample-neuromorphology-entity.json" for { originalPayload <- loader.jsonContentOf(template, "prefix" -> originalPrefix) diff --git a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/views/ViewPatcherSuite.scala b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/views/ViewPatcherSuite.scala index a17b844a4c..278d7225e4 100644 --- a/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/views/ViewPatcherSuite.scala +++ b/ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/views/ViewPatcherSuite.scala @@ -29,7 +29,7 @@ class ViewPatcherSuite extends NexusSuite { private val targetProject = ProjectRef.unsafe("new-org", "new-project2") - private val iriPatcher = IriPatcher(originalPrefix, targetPrefix) + private val iriPatcher = IriPatcher(originalPrefix, targetPrefix, Map.empty) private val projectMapper = ProjectMapper(Map(project2 -> targetProject)) private val viewPatcher = new ViewPatcher(projectMapper, iriPatcher)