-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
178 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
.../test/scala/ch/epfl/bluebrain/nexus/delta/sdk/organizations/OrganizationDeleterSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.projects | ||
|
||
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.kernel.utils.UUIDF | ||
import ch.epfl.bluebrain.nexus.delta.sdk.ConfigFixtures | ||
import ch.epfl.bluebrain.nexus.delta.sdk.generators.ProjectGen.defaultApiMappings | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.{OrganizationDeleter, Organizations} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.{Organization, OrganizationCommand} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.OrganizationRejection.{OrganizationNonEmpty, OrganizationNotFound} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.Projects.FetchOrganization | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ProjectRejection.WrappedOrganizationRejection | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.{ApiMappings, ProjectFields} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.{GlobalEventLog, MD5} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Identity, Label, ProjectRef} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.offset.Offset | ||
import ch.epfl.bluebrain.nexus.testkit.IOFixedClock | ||
import ch.epfl.bluebrain.nexus.testkit.bio.BioSuite | ||
import doobie.implicits._ | ||
import doobie.util.update.Update0 | ||
import monix.bio.{IO, Task, UIO} | ||
import munit.AnyFixture | ||
|
||
import java.util.UUID | ||
|
||
class OrganizationDeleterSpec extends BioSuite with IOFixedClock with ConfigFixtures { | ||
|
||
private val org = Label.unsafe("org") | ||
private val orgUuid = UUID.randomUUID() | ||
|
||
private def fetchOrg: FetchOrganization = { | ||
case `org` => UIO.pure(Organization(org, orgUuid, None)) | ||
case other => IO.raiseError(WrappedOrganizationRejection(OrganizationNotFound(other))) | ||
} | ||
|
||
private val config = ProjectsConfig(eventLogConfig, pagination, cacheConfig, deletionConfig) | ||
private val projectFixture = ProjectsFixture.init(fetchOrg, defaultApiMappings, config) | ||
override def munitFixtures: Seq[AnyFixture[_]] = List(projectFixture) | ||
private lazy val (xas, projects) = projectFixture() | ||
private lazy val orgDeleter = OrganizationDeleter(xas) | ||
private val proj1 = ProjectRef.unsafe("org", "myproj") | ||
private val fields = ProjectFields(None, ApiMappings.empty, None, None) | ||
private lazy val orgLog = GlobalEventLog(Organizations.definition, logConfig, xas) | ||
|
||
implicit val subject: Subject = Identity.User("Bob", Label.unsafe("realm")) | ||
implicit val uuidF: UUIDF = UUIDF.fixed(orgUuid) | ||
|
||
test("Fail when trying to delete a non-empty organization") { | ||
val arrange: IO[Any, Unit] = for { | ||
_ <- truncateTables | ||
_ <- orgLog.evaluate(org, OrganizationCommand.CreateOrganization(org, None, Identity.Anonymous)) | ||
_ <- projects.create(proj1, fields) | ||
} yield () | ||
|
||
val act: UIO[Either[OrganizationNonEmpty, Unit]] = | ||
orgDeleter.deleteIfEmpty(org).attempt | ||
|
||
def assert(result: Either[OrganizationNonEmpty, Unit]): IO[Any, Unit] = for { | ||
eventPartitions <- queryPartitions("scoped_events") | ||
statePartitions <- queryPartitions("scoped_states") | ||
fetchedProject <- projects.fetch(proj1) | ||
globalOrgEvent <- orgLog.currentEvents(Offset.Start).compile.to(List).map(_.map(_.value.uuid)) | ||
globalOrgState <- orgLog.currentStates(Offset.Start).compile.to(List).map(_.map(_.value.uuid)) | ||
} yield { | ||
assertEquals(result, Left(OrganizationNonEmpty(org))) | ||
val orgPartition = MD5.hash(org.value) | ||
assertEquals(eventPartitions.headOption, Some(s"scoped_events_$orgPartition")) | ||
assertEquals(statePartitions.headOption, Some(s"scoped_states_$orgPartition")) | ||
assertEquals(fetchedProject.value.ref, proj1) | ||
assertEquals(globalOrgState, List(orgUuid)) | ||
assertEquals(globalOrgEvent, List(orgUuid)) | ||
} | ||
|
||
arrange >> act >>= assert | ||
} | ||
|
||
test("Successfully delete an empty organization") { | ||
val arrange: IO[Any, Unit] = for { | ||
_ <- truncateTables | ||
_ <- orgLog.evaluate(org, OrganizationCommand.CreateOrganization(org, None, Identity.Anonymous)) | ||
} yield () | ||
|
||
val act: UIO[Either[OrganizationNonEmpty, Unit]] = | ||
orgDeleter.deleteIfEmpty(org).attempt | ||
|
||
def assert(result: Either[OrganizationNonEmpty, Unit]): IO[Any, Unit] = for { | ||
globalEventsDeleted <- orgLog.currentEvents(Offset.Start).compile.to(List).map(_.isEmpty) | ||
globalStateDeleted <- orgLog.currentStates(Offset.Start).compile.to(List).map(_.isEmpty) | ||
eventPartitionDeleted <- queryPartitions("scoped_events").map(_.isEmpty) | ||
statePartitionDeleted <- queryPartitions("scoped_states").map(_.isEmpty) | ||
} yield { | ||
assertEquals(result, Right(())) | ||
assertEquals(eventPartitionDeleted, true) | ||
assertEquals(statePartitionDeleted, true) | ||
assertEquals(globalStateDeleted, true) | ||
assertEquals(globalEventsDeleted, true) | ||
} | ||
|
||
arrange >> act >>= assert | ||
} | ||
|
||
def truncateTables: UIO[Unit] = | ||
List("global_events", "global_states", "scoped_events", "scoped_states").traverse(t => | ||
Update0(s"DELETE FROM $t", None).run.transact(xas.write) | ||
).void.hideErrors | ||
|
||
def queryPartitions(table: String): Task[List[String]] = | ||
sql""" | ||
SELECT inhrelid::regclass AS child | ||
FROM pg_catalog.pg_inherits | ||
WHERE inhparent = $table::regclass | ||
""".query[String].to[List].transact(xas.read) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters