Skip to content

Commit

Permalink
Patch cross-project resolvers when a project is renamed (#4874)
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 Apr 17, 2024
1 parent 831bc97 commit 2972cb2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors
import ch.epfl.bluebrain.nexus.delta.sourcing.config.EventLogConfig
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{EntityType, Identity}
import ch.epfl.bluebrain.nexus.ship.resolvers.ResolverProcessor.logger
import ch.epfl.bluebrain.nexus.ship.resolvers.ResolverProcessor.{logger, patchValue}
import ch.epfl.bluebrain.nexus.ship.{EventClock, EventProcessor, ImportStatus, ProjectMapper}
import io.circe.Decoder

Expand All @@ -40,10 +40,12 @@ class ResolverProcessor private (resolvers: Resolvers, projectMapper: ProjectMap
event match {
case ResolverCreated(_, _, value, _, _, _, _) =>
implicit val caller: Caller = Caller(s, identities(value))
resolvers.create(id, projectRef, value)
val patched = patchValue(value, projectMapper)
resolvers.create(id, projectRef, patched)
case ResolverUpdated(_, _, value, _, _, _, _) =>
implicit val caller: Caller = Caller(s, identities(value))
resolvers.update(id, projectRef, cRev, value)
val patched = patchValue(value, projectMapper)
resolvers.update(id, projectRef, cRev, patched)
case _: ResolverTagAdded =>
// Tags have been removed
IO.unit
Expand Down Expand Up @@ -74,6 +76,14 @@ object ResolverProcessor {

private val logger = Logger[ResolverProcessor]

def patchValue(value: ResolverValue, projectMapper: ProjectMapper): ResolverValue =
value match {
case ip: InProjectValue => ip
case cp: CrossProjectValue =>
val mappedProjects = cp.projects.map(projectMapper.map)
cp.copy(projects = mappedProjects)
}

def apply(
fetchContext: FetchContext,
projectMapper: ProjectMapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.epfl.bluebrain.nexus.ship.resolvers

import cats.data.NonEmptyList
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.nxv
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.model.ResolverValue.{CrossProjectValue, InProjectValue}
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.model.{IdentityResolution, Priority}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.ship.ProjectMapper
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite

class ResolverProcessorSuite extends NexusSuite {

private val originalProject = ProjectRef.unsafe("bbp", "sscx")
private val targetProject = ProjectRef.unsafe("obp", "somato")

private val projectMapper = ProjectMapper(Map(originalProject -> targetProject))

private val priority = Priority.unsafe(42)

test("Patching does not affect in project resolvers") {
val original = InProjectValue(priority)

val obtained = ResolverProcessor.patchValue(original, projectMapper)
assertEquals(obtained, original)
}

test("Patching a cross project resolver") {
val unpatchedProject = ProjectRef.unsafe("neurosciencegraph", "datamodels")
val originalProjects = NonEmptyList.of(unpatchedProject, originalProject)
val original = CrossProjectValue(
Some("My resolver"),
Some("My description"),
priority,
Set(nxv + "Schema"),
originalProjects,
IdentityResolution.UseCurrentCaller
)

val expected = original.copy(projects = NonEmptyList.of(unpatchedProject, targetProject))
val obtained = ResolverProcessor.patchValue(original, projectMapper)
assertEquals(obtained, expected)
}

}

0 comments on commit 2972cb2

Please sign in to comment.