Skip to content

Commit

Permalink
Merge branch 'main' into feature/add-github-nuget-feed-as-part-of-sol…
Browse files Browse the repository at this point in the history
…ution
  • Loading branch information
anddrzejb authored Apr 14, 2024
2 parents c012a27 + 7fdf054 commit 3353fe7
Show file tree
Hide file tree
Showing 34 changed files with 399 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core;
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application;

using Core;

public interface IContractsRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.Sign;

using Common.Api.ErrorHandling;
using Core;
using IntegrationEvents;
using MassTransit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Infrastructure.Database.Repositories;

using Application;
using Core;
using Microsoft.EntityFrameworkCore;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Infrastructure.Database.Repositories;

using Core;
using Application;
using Microsoft.Extensions.DependencyInjection;

internal static class RepositoriesModule
Expand All @@ -11,4 +11,4 @@ internal static IServiceCollection AddRepositories(this IServiceCollection servi

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core;
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application;

using Core;

public interface IBindingContractsRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core;
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application;

using Core;

public interface IContractsRepository
{
Task<Contract?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<Contract?> GetPreviousForCustomerAsync(Guid customerId, CancellationToken cancellationToken = default);
Task AddAsync(Contract contract, CancellationToken cancellationToken = default);
Task CommitAsync(CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.SignContract;

using Common.Api.ErrorHandling;
using Core;
using IntegrationEvents;
using MassTransit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.TerminateBindingContract;

using Common.Api.ErrorHandling;
using Core;

[UsedImplicitly]
internal sealed class TerminateBindingContractCommandHandler(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.AddAnnex.BusinessRules;

using Common.Core.BusinessRules;
using Core.AddAnnex.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class AnnexCanOnlyBeAddedOnlyBeAddedToActiveBindingContractRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract;

using Common;
using SignContract;

internal sealed class ContractBuilder
{
internal static ContractBuilder Prepared() => new();

private DateTimeOffset? _preparedAt;

public ContractBuilder PreparedAt(DateTimeOffset preparedAt)
{
_preparedAt = preparedAt;
return this;
}

public SignedContractBuilder Signed() => new(Prepare());

private Contract Prepare()
{
var preparedAt = _preparedAt ?? FakeContractDates.PreparedAt;
var prepareContractParameters = PrepareContractParameters.GetValid();
var contract = Contract.Prepare(
Guid.NewGuid(),
prepareContractParameters.MaxAge,
prepareContractParameters.MaxHeight,
preparedAt);

return contract;
}

public static implicit operator Contract(ContractBuilder builder) => builder.Prepare();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract;

internal sealed class SignContractBuilder(Contract parentBuilder)
{
private DateTimeOffset _signDay;
private DateTimeOffset _fakeToday;

public SignContractBuilder SignedOn(DateTimeOffset signDay, DateTimeOffset fakeToday)
{
_signDay = signDay;
_fakeToday = fakeToday;

return this;
}

private BindingContract Build()
{
var bindingContract = parentBuilder.Sign(_signDay, _fakeToday);

return bindingContract;
}

public static implicit operator BindingContract(SignContractBuilder builder) => builder.Build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.Common;

using DomainDrivenDesign.BuildingBlocks;

internal static class EntityExtensions
{
public static TEvent? GetPublishedEvent<TEvent>(this Entity entity) where TEvent : class, IDomainEvent => entity.Events.OfType<TEvent>()
.MinBy(domainEvent => domainEvent.OccuredAt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.Common;

internal static class FakeContractDates
{
public static DateTimeOffset PreparedAt => new(2022, 2, 3, 1, 1, 1, TimeSpan.Zero);
public static DateTimeOffset SignDay => new(2022, 1, 3, 1, 1, 1, TimeSpan.Zero);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract.BusinessRules;

using Common.Core.BusinessRules;
using Core.PrepareContract.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class ContractCanBePreparedOnlyForAdultRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract.BusinessRules;

using Common.Core.BusinessRules;
using Core.PrepareContract.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class CustomerMustBeSmallerThanMaximumHeightLimitRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract.BusinessRules;

using Common.Core.BusinessRules;
using Core.PrepareContract.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class PreviousContractHasToBeSignedRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract;

using Common;
using Core.PrepareContract;

public sealed class PrepareContractTests
{
private readonly Guid _customerId = Guid.NewGuid();
private const int CustomerAge = 25;
private const int CustomerHeight = 170;
private readonly DateTimeOffset _preparedAt = new(2022, 2, 3, 1, 1, 1, TimeSpan.Zero);

[Fact]
internal void Given_prepare_contract_Then_should_raise_contract_prepared_event()
{
var contract = Contract.Prepare(_customerId, CustomerAge, CustomerHeight, _preparedAt);

var @event = contract.GetPublishedEvent<ContractPreparedEvent>();
@event?.CustomerId.Should().Be(_customerId);
@event?.PreparedAt.Should().Be(_preparedAt);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract.BusinessRules;

using Common.Core.BusinessRules;
using Core.SignContract.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class ContractCanOnlyBeSignedWithin30DaysFromPreparationRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract.BusinessRules;

using Core.SignContract.BusinessRules;
using EvolutionaryArchitecture.Fitnet.Common.Core.BusinessRules;
using Fitnet.Common.Core.BusinessRules;

public sealed class ContractMustNotBeAlreadySignedRuleTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract;

using Common;
using Core.SignContract;
using PrepareContract;

public class SignContractTests
public sealed class SignContractTests
{
[Theory]
[ClassData(typeof(SignContractTestData))]
Expand All @@ -13,42 +15,33 @@ internal void Given_sign_contract_Then_expiration_date_is_set_to_contract_durati
DateTimeOffset expectedExpirationDate)
{
// Arrange
var contract = PrepareContract(preparedAt);
Contract contract = ContractBuilder
.Prepared()
.PreparedAt(preparedAt);

// Act
var bindingContract = contract.Sign(signedAt, fakeNow);

// Assert
bindingContract.ExpiringAt.Should().Be(expectedExpirationDate);
var @event = bindingContract.GetPublishedEvent<BindingContractStartedEvent>();
@event?.ExpiringAt.Should().Be(expectedExpirationDate);
}

private static readonly DateTimeOffset PreparedAt = new(2023, 1, 1, 0, 0, 0, TimeSpan.Zero);
private static readonly DateTimeOffset FakeNow = PreparedAt.AddDays(1);
private static readonly DateTimeOffset SignedAt = PreparedAt.AddDays(1);
private static readonly DateTimeOffset FakeNow = FakeContractDates.PreparedAt.AddDays(1);
private static readonly DateTimeOffset SignedAt = FakeContractDates.PreparedAt.AddDays(1);

[Fact]
internal void Given_sign_contract_Then_contracts_becomes_binding_contract()
{
// Arrange
var contract = PrepareContract(PreparedAt);
Contract contract = ContractBuilder
.Prepared();

// Act
var bindingContract = contract.Sign(SignedAt, FakeNow);

// Assert
bindingContract.Should().NotBeNull();
bindingContract.Should().BeOfType<BindingContract>();
}

private static Contract PrepareContract(DateTimeOffset preparedAt)
{
var prepareContractParameters = PrepareContractParameters.GetValid();
var contract = Contract.Prepare(
Guid.NewGuid(),
prepareContractParameters.MaxAge,
prepareContractParameters.MaxHeight,
preparedAt);

return contract;
var @event = bindingContract.GetPublishedEvent<BindingContractStartedEvent>();
@event?.BindingFrom.Should().Be(SignedAt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract;

using Common;

internal sealed class SignedContractBuilder(Contract parentBuilder)
{
private DateTimeOffset? _signDay;
private DateTimeOffset? _fakeToday;

public SignedContractBuilder SignedOn(DateTimeOffset signDay, DateTimeOffset fakeToday)
{
_signDay = signDay;
_fakeToday = fakeToday;

return this;
}

private BindingContract Build()
{
var signDay = _signDay ?? FakeContractDates.SignDay;
var fakeToday = _fakeToday ?? FakeContractDates.SignDay;
var bindingContract = parentBuilder.Sign(signDay, fakeToday);

return bindingContract;
}

public static implicit operator BindingContract(SignedContractBuilder builder) => builder.Build();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.TerminateBindingContract.BusinessRules;

using Common.Core.BusinessRules;
using Core.TerminateBindingContract.BusinessRules;
using Fitnet.Common.Core.BusinessRules;
using TerminationIsPossibleOnlyAfterThreeMonthsHavePassed.TestData;

public sealed class TerminationIsPossibleOnlyAfterThreeMonthsHavePassedTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.TerminateBindingContract;

using Common;
using Core.TerminateBindingContract;
using PrepareContract;

public sealed class TerminateBindingContractTests
{
private readonly DateTimeOffset _terminatedAt = new(2023, 3, 3, 1, 1, 1, TimeSpan.Zero);

[Fact]
internal void Given_terminate_binding_contracts_Then_should_raise_binding_contracts()
{
BindingContract bindingContract = ContractBuilder
.Prepared()
.Signed();

bindingContract.Terminate(_terminatedAt);

var @event = bindingContract.GetPublishedEvent<BindingContractTerminatedEvent>();
@event?.TerminatedAt.Should().Be(_terminatedAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public sealed class Annex : Entity
public BindingContractId BindingContractId { get; init; }
public DateTimeOffset ValidFrom { get; init; }

// EF needs this constructor to create non-primitive types
private Annex() { }

private Annex(BindingContractId bindingContractId, DateTimeOffset validFrom)
{
Id = AnnexId.Create();
Expand All @@ -19,7 +22,7 @@ private Annex(BindingContractId bindingContractId, DateTimeOffset validFrom)
RecordEvent(@event);
}

internal static Annex Add(BindingContractId bindingContractId, DateTimeOffset validFrom) =>
internal static Annex Attach(BindingContractId bindingContractId, DateTimeOffset validFrom) =>
new(bindingContractId, validFrom);
}

Expand Down
Loading

0 comments on commit 3353fe7

Please sign in to comment.