Skip to content

Commit

Permalink
Replace project references in ids when mapped (#5006)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Dumas <[email protected]>
  • Loading branch information
imsdu and Simon Dumas authored Jun 3, 2024
1 parent a86de00 commit 3df27f6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
16 changes: 11 additions & 5 deletions ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/IriPatcher.scala
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -21,23 +22,28 @@ 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
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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 {

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"
Expand All @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3df27f6

Please sign in to comment.