diff --git a/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.CreateTests.cs b/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.CreateTests.cs index c7fc4fcfa9..c6bce22765 100644 --- a/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.CreateTests.cs +++ b/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.CreateTests.cs @@ -98,52 +98,56 @@ public void There_can_only_be_one_active_relationship_between_two_identities() acting.Should().Throw().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists"); } - [Fact] - public void Creating_a_new_relationship_is_possible_if_existing_ones_are_rejected_or_revoked() + [Theory] + [InlineData(RelationshipStatus.Pending)] + [InlineData(RelationshipStatus.Active)] + [InlineData(RelationshipStatus.Terminated)] + [InlineData(RelationshipStatus.DeletionProposed)] + public void There_can_only_be_one_active_relationship_between_two_identities2(RelationshipStatus relationshipStatus) { // Arrange var existingRelationships = new List { - CreateRejectedRelationship(), - CreateRevokedRelationship() + CreateRelationshipInStatus(relationshipStatus) }; // Act var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships); // Assert - acting.Should().NotThrow(); + acting.Should().Throw().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists"); } [Fact] - public void Cannot_create_Relationship_if_terminated_Relationship_exists() + public void Creating_a_new_relationship_is_possible_if_existing_ones_are_rejected_or_revoked() { // Arrange var existingRelationships = new List { - CreateTerminatedRelationship() + CreateRejectedRelationship(), + CreateRevokedRelationship() }; // Act var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships); // Assert - acting.Should().Throw().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists"); + acting.Should().NotThrow(); } [Fact] - public void A_new_relationship_can_be_created_after_decomposing_the_old_one() + public void Cannot_create_Relationship_if_terminated_Relationship_exists() { // Arrange var existingRelationships = new List { - CreateDecomposedRelationship(IDENTITY_1, IDENTITY_2) + CreateTerminatedRelationship() }; // Act - var newRelationship = new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, [], existingRelationships); + var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships); // Assert - newRelationship.Status.Should().Be(RelationshipStatus.Pending); + acting.Should().Throw().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists"); } } diff --git a/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.cs b/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.cs index 85850c1204..b6f7c691c9 100644 --- a/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.cs +++ b/Modules/Relationships/src/Relationships.Domain/Aggregates/Relationships/Relationship.cs @@ -78,7 +78,7 @@ private static void EnsureTargetIsNotSelf(RelationshipTemplate relationshipTempl private static void EnsureNoOtherRelationshipToPeerExists(IdentityAddress target, IEnumerable existingRelationshipsToPeer) { - if (existingRelationshipsToPeer.Any(r => r.Status is RelationshipStatus.Active or RelationshipStatus.Pending or RelationshipStatus.Terminated)) + if (existingRelationshipsToPeer.Any(r => r.Status is RelationshipStatus.Active or RelationshipStatus.Pending or RelationshipStatus.Terminated or RelationshipStatus.DeletionProposed)) throw new DomainException(DomainErrors.RelationshipToTargetAlreadyExists(target)); } diff --git a/Modules/Relationships/src/Relationships.Domain/TestHelpers/TestData.cs b/Modules/Relationships/src/Relationships.Domain/TestHelpers/TestData.cs index aee1a991e8..cea44fe7bf 100644 --- a/Modules/Relationships/src/Relationships.Domain/TestHelpers/TestData.cs +++ b/Modules/Relationships/src/Relationships.Domain/TestHelpers/TestData.cs @@ -25,6 +25,18 @@ public static Relationship CreatePendingRelationship(IdentityAddress? from = nul return relationship; } + public static Relationship CreateRelationshipInStatus(RelationshipStatus status, IdentityAddress? from = null, IdentityAddress? to = null) + { + return status switch + { + RelationshipStatus.Pending => CreatePendingRelationship(from, to), + RelationshipStatus.Active => CreateActiveRelationship(from, to), + RelationshipStatus.Terminated => CreateTerminatedRelationship(from, to), + RelationshipStatus.DeletionProposed => CreateRelationshipWithProposedDeletion(from, to), + _ => throw new NotSupportedException($"This method currently does not support relationship status {status}.") + }; + } + public static Relationship CreateActiveRelationship(IdentityAddress? from = null, IdentityAddress? to = null) { to ??= IDENTITY_2; @@ -54,7 +66,15 @@ public static Relationship CreateRevokedRelationship() public static Relationship CreateTerminatedRelationship(IdentityAddress? from = null, IdentityAddress? to = null) { var relationship = CreateActiveRelationship(from, to); - relationship.Terminate(IDENTITY_1, DEVICE_1); + relationship.Terminate(relationship.From, DEVICE_1); + relationship.ClearDomainEvents(); + return relationship; + } + + public static Relationship CreateRelationshipWithProposedDeletion(IdentityAddress? from = null, IdentityAddress? to = null) + { + var relationship = CreateTerminatedRelationship(from, to); + relationship.Decompose(relationship.From, DEVICE_1); relationship.ClearDomainEvents(); return relationship; }