-
Notifications
You must be signed in to change notification settings - Fork 8
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
7 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/PostalRegistry/PostalInformation/Commands/UpdatePostalNames.cs
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,47 @@ | ||
namespace PostalRegistry.PostalInformation.Commands | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Be.Vlaanderen.Basisregisters.Generators.Guid; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Provenance; | ||
using Be.Vlaanderen.Basisregisters.Utilities; | ||
|
||
public sealed class UpdatePostalNames : IHasCommandProvenance | ||
{ | ||
private static readonly Guid Namespace = new Guid("6af54580-27cc-4b16-a6c9-d14eee229ed8"); | ||
|
||
public PostalCode PostalCode { get; } | ||
|
||
public IReadOnlyCollection<PostalName> PostalNamesToAdd { get; } | ||
public IReadOnlyCollection<PostalName> PostalNamesToRemove { get; } | ||
public Provenance Provenance { get; } | ||
|
||
public UpdatePostalNames( | ||
PostalCode postalCode, | ||
IReadOnlyCollection<PostalName> postalNamesToAdd, | ||
IReadOnlyCollection<PostalName> postalNamesToRemove, | ||
Provenance provenance) | ||
{ | ||
PostalCode = postalCode; | ||
PostalNamesToAdd = postalNamesToAdd; | ||
PostalNamesToRemove = postalNamesToRemove; | ||
Provenance = provenance; | ||
} | ||
|
||
public Guid CreateCommandId() | ||
=> Deterministic.Create(Namespace, $"UpdatePostalNames-{ToString()}"); | ||
|
||
public override string? ToString() | ||
=> ToStringBuilder.ToString(IdentityFields()); | ||
|
||
private IEnumerable<object> IdentityFields() | ||
{ | ||
yield return PostalCode; | ||
|
||
foreach (var field in Provenance.GetIdentityFields()) | ||
{ | ||
yield return field; | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/PostalRegistry/PostalInformation/Exceptions/PostalNameAlreadyExistsException.cs
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,20 @@ | ||
namespace PostalRegistry.PostalInformation.Exceptions | ||
{ | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
[Serializable] | ||
public sealed class PostalNameAlreadyExistsException : PostalRegistryException | ||
{ | ||
public PostalName PostalName { get; } | ||
|
||
public PostalNameAlreadyExistsException(PostalName postalName) | ||
{ | ||
PostalName = postalName; | ||
} | ||
|
||
private PostalNameAlreadyExistsException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ } | ||
} | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
test/PostalRegistry.Tests/AggregateTests/WhenUpdatingPostalNames/GivenNoPostalInformation.cs
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,35 @@ | ||
namespace PostalRegistry.Tests.AggregateTests.WhenUpdatingPostalNames | ||
{ | ||
using AutoFixture; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource.Testing; | ||
using global::AutoFixture; | ||
using PostalInformation; | ||
using PostalInformation.Commands; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class GivenNoPostalInformation : PostalRegistryTest | ||
{ | ||
private readonly Fixture _fixture; | ||
|
||
public GivenNoPostalInformation(ITestOutputHelper testOutputHelper) : base(testOutputHelper) | ||
{ | ||
_fixture = new Fixture(); | ||
_fixture.Customize(new WithFixedPostalCode()); | ||
_fixture.Customize(new WithIntegerNisCode()); | ||
} | ||
|
||
[Fact] | ||
public void ThenAggregateNotFoundExceptionIsThrown() | ||
{ | ||
var command = _fixture.Create<UpdatePostalNames>(); | ||
|
||
Assert( | ||
new Scenario() | ||
.Given() | ||
.When(command) | ||
.Throws(new AggregateNotFoundException(command.PostalCode.ToString(), typeof(PostalInformation)))); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
test/PostalRegistry.Tests/AggregateTests/WhenUpdatingPostalNames/GivenPostalInformation.cs
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,98 @@ | ||
namespace PostalRegistry.Tests.AggregateTests.WhenUpdatingPostalNames | ||
{ | ||
using System.Collections.Generic; | ||
using AutoFixture; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource.Testing; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Provenance; | ||
using Builders; | ||
using FluentAssertions; | ||
using global::AutoFixture; | ||
using PostalInformation; | ||
using PostalInformation.Commands; | ||
using PostalInformation.Events; | ||
using PostalInformation.Exceptions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class GivenPostalInformation : PostalRegistryTest | ||
{ | ||
private readonly Fixture _fixture; | ||
|
||
public GivenPostalInformation(ITestOutputHelper testOutputHelper) : base(testOutputHelper) | ||
{ | ||
_fixture = new Fixture(); | ||
_fixture.Customize(new WithFixedPostalCode()); | ||
_fixture.Customize(new WithIntegerNisCode()); | ||
_fixture.Customize(new InfrastructureCustomization()); | ||
} | ||
|
||
[Fact] | ||
public void GivenNameAlreadyExists_ThenPostalNameAlreadyExistsExceptionIsThrown() | ||
{ | ||
var named = _fixture.Create<PostalInformationPostalNameWasAdded>(); | ||
var postalName = new PostalName(named.Name, named.Language); | ||
var command = new UpdatePostalNames(_fixture.Create<PostalCode>(), | ||
[postalName], | ||
new List<PostalName>(), | ||
_fixture.Create<Provenance>()); | ||
|
||
Assert( | ||
new Scenario() | ||
.Given( | ||
command.PostalCode, | ||
_fixture.Create<PostalInformationWasRegistered>(), | ||
named) | ||
.When(command) | ||
.Throws(new PostalNameAlreadyExistsException(postalName))); | ||
} | ||
|
||
[Fact] | ||
public void ThenPostalNamesAreUpdated() | ||
{ | ||
var named = _fixture.Create<PostalInformationPostalNameWasAdded>(); | ||
var nameToAdd = new PostalName(_fixture.Create<string>(), Language.Dutch); | ||
var command = new UpdatePostalNames(_fixture.Create<PostalCode>(), | ||
[nameToAdd], | ||
[new PostalName(named.Name, named.Language)], | ||
_fixture.Create<Provenance>()); | ||
|
||
Assert( | ||
new Scenario() | ||
.Given( | ||
command.PostalCode, | ||
_fixture.Create<PostalInformationWasRegistered>(), | ||
named) | ||
.When(command) | ||
.Then(new Fact(command.PostalCode, | ||
new PostalInformationPostalNameWasRemoved(command.PostalCode, new PostalName(named.Name, named.Language))), | ||
new Fact(command.PostalCode, | ||
new PostalInformationPostalNameWasAdded(command.PostalCode, nameToAdd)))); | ||
} | ||
|
||
[Fact] | ||
public void StateCheck() | ||
{ | ||
// Arrange | ||
var postalInformationPostalNameWasAdded = _fixture.Create<PostalInformationPostalNameWasAdded>(); | ||
var postalInformationPostalNameWasRemoved = new PostalInformationPostalNameWasRemovedBuilder(_fixture) | ||
.WithName(postalInformationPostalNameWasAdded.Name, postalInformationPostalNameWasAdded.Language) | ||
.Build(); | ||
var postalInformationPostalNameWasAdded2 = _fixture.Create<PostalInformationPostalNameWasAdded>(); | ||
|
||
// Act | ||
var sut = PostalInformation.Factory(); | ||
sut.Initialize([ | ||
_fixture.Create<PostalInformationWasRegistered>(), | ||
_fixture.Create<MunicipalityWasAttached>(), | ||
postalInformationPostalNameWasAdded, | ||
postalInformationPostalNameWasRemoved, | ||
postalInformationPostalNameWasAdded2 | ||
]); | ||
|
||
// Assert | ||
sut.PostalNames.Count.Should().Be(1); | ||
sut.PostalNames.Should().Contain(x => x.Name == postalInformationPostalNameWasAdded2.Name && x.Language == postalInformationPostalNameWasAdded2.Language); | ||
} | ||
} | ||
} |