-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added service and handler to check for existing consolidations
- Loading branch information
1 parent
d3cbfb4
commit 62b59e3
Showing
4 changed files
with
178 additions
and
1 deletion.
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
51 changes: 51 additions & 0 deletions
51
...inet.DataHub.MarketParticipant.Domain/Services/Rules/ExistingActorConsolidationService.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,51 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Exception; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
using Energinet.DataHub.MarketParticipant.Domain.Repositories; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Domain.Services.Rules; | ||
|
||
public sealed class ExistingActorConsolidationService : IExistingActorConsolidationService | ||
{ | ||
private readonly IActorConsolidationRepository _actorConsolidationRepository; | ||
|
||
public ExistingActorConsolidationService(IActorConsolidationRepository organizationRepository) | ||
{ | ||
_actorConsolidationRepository = organizationRepository; | ||
} | ||
|
||
public async Task CheckExistingConsolidationAsync(ActorId fromActorId, ActorId toActorId) | ||
{ | ||
var existingConsolidations = (await _actorConsolidationRepository | ||
.GetAsync() | ||
.ConfigureAwait(false)).ToList(); | ||
|
||
if (existingConsolidations.Any(consolidation => consolidation.ActorFromId == fromActorId || consolidation.ActorToId == fromActorId)) | ||
{ | ||
throw new ValidationException("The specified From actor has already been consolidated before.") | ||
.WithErrorCode("actor.consolidation.fromexists"); | ||
} | ||
|
||
if (existingConsolidations.Any(consolidation => consolidation.ActorToId == fromActorId || consolidation.ActorFromId == toActorId)) | ||
{ | ||
throw new ValidationException("The specified From actor has an existing consolidation scheduled already.") | ||
.WithErrorCode("actor.consolidation.toexists"); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...net.DataHub.MarketParticipant.Domain/Services/Rules/IExistingActorConsolidationService.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,31 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Domain.Services.Rules; | ||
|
||
/// <summary> | ||
/// Ensures that there are no existing Consolidation for any of the actors chosen for a consolidation. | ||
/// </summary> | ||
public interface IExistingActorConsolidationService | ||
{ | ||
/// <summary> | ||
/// Checks whether either the from or to actor is already part of an existing consolidation, will throw an exception if they are. | ||
/// </summary> | ||
/// <param name="fromActorId">The <see cref="ActorId"/> of the discontinued actor.</param> | ||
/// <param name="toActorId">The <see cref="ActorId"/> of the surviving actor.</param> | ||
Task CheckExistingConsolidationAsync(ActorId fromActorId, ActorId toActorId); | ||
} |
86 changes: 86 additions & 0 deletions
86
...ginet.DataHub.MarketParticipant.Tests/Handlers/ScheduleActorConsolidationsHandlerTests.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,86 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Application.Commands.Actors; | ||
using Energinet.DataHub.MarketParticipant.Application.Handlers.Actors; | ||
using Energinet.DataHub.MarketParticipant.Application.Services; | ||
using Energinet.DataHub.MarketParticipant.Domain; | ||
using Energinet.DataHub.MarketParticipant.Domain.Repositories; | ||
using Energinet.DataHub.MarketParticipant.Domain.Services.Rules; | ||
using Energinet.DataHub.MarketParticipant.Tests.Common; | ||
using Moq; | ||
using Xunit; | ||
using Xunit.Categories; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Tests.Handlers; | ||
|
||
[UnitTest] | ||
public sealed class ScheduleActorConsolidationsHandlerTests | ||
{ | ||
[Fact] | ||
public async Task Handle_InvalidFromActorId_ThrowsException() | ||
{ | ||
// Arrange | ||
var actorRepositoryMock = new Mock<IActorRepository>(); | ||
var fromActorId = Guid.NewGuid(); | ||
var target = new ScheduleConsolidateActorsHandler( | ||
Mock.Of<IAuditIdentityProvider>(), | ||
Mock.Of<IActorConsolidationAuditLogRepository>(), | ||
Mock.Of<IActorConsolidationRepository>(), | ||
Mock.Of<IDomainEventRepository>(), | ||
Mock.Of<IUnitOfWorkProvider>(), | ||
actorRepositoryMock.Object, | ||
Mock.Of<IExistingActorConsolidationService>()); | ||
var command = new ScheduleConsolidateActorsCommand(fromActorId, new ConsolidationRequestDto(Guid.NewGuid(), DateTimeOffset.UtcNow.AddDays(65))); | ||
|
||
// Act + Assert | ||
var exception = await Record.ExceptionAsync(() => target.Handle(command, CancellationToken.None)); | ||
Assert.NotNull(exception); | ||
Assert.Equal(typeof(ValidationException), exception.GetType()); | ||
Assert.Contains(exception.Message, $"Entity '{fromActorId}' does not exist.", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_InvalidToActorId_ThrowsException() | ||
{ | ||
// Arrange | ||
var actorRepositoryMock = new Mock<IActorRepository>(); | ||
var validFromActor = TestPreparationModels.MockedActor(); | ||
var toActorId = Guid.NewGuid(); | ||
var target = new ScheduleConsolidateActorsHandler( | ||
Mock.Of<IAuditIdentityProvider>(), | ||
Mock.Of<IActorConsolidationAuditLogRepository>(), | ||
Mock.Of<IActorConsolidationRepository>(), | ||
Mock.Of<IDomainEventRepository>(), | ||
Mock.Of<IUnitOfWorkProvider>(), | ||
actorRepositoryMock.Object, | ||
Mock.Of<IExistingActorConsolidationService>()); | ||
actorRepositoryMock | ||
.Setup(actorRepository => actorRepository.GetAsync(validFromActor.Id)) | ||
.ReturnsAsync(validFromActor); | ||
|
||
var command = new ScheduleConsolidateActorsCommand(validFromActor.Id.Value, new ConsolidationRequestDto(toActorId, DateTimeOffset.UtcNow.AddDays(65))); | ||
|
||
// Act + Assert | ||
var exception = await Record.ExceptionAsync(() => target.Handle(command, CancellationToken.None)); | ||
Assert.NotNull(exception); | ||
Assert.Equal(typeof(ValidationException), exception.GetType()); | ||
Assert.Contains(exception.Message, $"Entity '{toActorId}' does not exist.", StringComparison.OrdinalIgnoreCase); | ||
} | ||
} |