From 556825749901e8942c8704087c23f35313ea57bf Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 20 Sep 2020 23:40:36 +0200 Subject: [PATCH] work --- src/DDDDemo.sln | 8 ++++- .../ConsumerServicesTests.cs | 23 ++++++++++---- .../FarmerServicesTests.cs | 5 +-- src/VegetableShop.API/Program.cs | 19 ++++++------ .../LogItemOnFarmerPutOnSaleEventTest.cs | 31 +++++++++++++++++++ .../VegetableShop.Domain.UnitTests.csproj | 21 +++++++++++++ .../Errors/UnauthorizedSaleException.cs | 4 +-- .../Persistence/ConsumerRepositoryImpl.cs | 15 +++------ .../Persistence/Context.cs | 26 ++++++++++++++++ .../Persistence/FarmerRepositoryImpl.cs | 19 +++--------- .../Persistence/VegetableRepositoryImpl.cs | 12 ++++--- 11 files changed, 134 insertions(+), 49 deletions(-) create mode 100644 src/VegetableShop.Domain.UnitTests/LogItemOnFarmerPutOnSaleEventTest.cs create mode 100644 src/VegetableShop.Domain.UnitTests/VegetableShop.Domain.UnitTests.csproj create mode 100644 src/VegetableShop.Infrastructure/Persistence/Context.cs diff --git a/src/DDDDemo.sln b/src/DDDDemo.sln index d749436..8f06e90 100644 --- a/src/DDDDemo.sln +++ b/src/DDDDemo.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# +# Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VegetableShop.Domain", "VegetableShop.Domain\VegetableShop.Domain.csproj", "{9EE77ACA-9351-46CF-B55B-CE76EFBCDB04}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VegetableShop.API.IntegrationTests", "VegetableShop.API.IntegrationTests\VegetableShop.API.IntegrationTests.csproj", "{7D802ED7-C73C-437B-A91E-BA013939AF7C}" @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VegetableShop.API", "Vegeta EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VegetableShop.Domain.AcceptanceTests", "VegetableShop.Domain.AcceptanceTests\VegetableShop.Domain.AcceptanceTests.csproj", "{BADB4EB0-3817-4114-B256-8162B76C86A3}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_tests", "_tests", "{54A647A6-A4DA-450B-9C0F-4A4912D5B229}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -42,4 +44,8 @@ Global {BADB4EB0-3817-4114-B256-8162B76C86A3}.Release|Any CPU.ActiveCfg = Release|Any CPU {BADB4EB0-3817-4114-B256-8162B76C86A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {BADB4EB0-3817-4114-B256-8162B76C86A3} = {54A647A6-A4DA-450B-9C0F-4A4912D5B229} + {7D802ED7-C73C-437B-A91E-BA013939AF7C} = {54A647A6-A4DA-450B-9C0F-4A4912D5B229} + EndGlobalSection EndGlobal diff --git a/src/VegetableShop.API.IntegrationTests/ConsumerServicesTests.cs b/src/VegetableShop.API.IntegrationTests/ConsumerServicesTests.cs index 426da3e..84c12c0 100644 --- a/src/VegetableShop.API.IntegrationTests/ConsumerServicesTests.cs +++ b/src/VegetableShop.API.IntegrationTests/ConsumerServicesTests.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging.Abstractions; using VegetableShop.API.Services; +using VegetableShop.Domain.Model; using VegetableShop.Infrastructure.Persistence; using Xunit; @@ -8,18 +9,28 @@ namespace VegetableShop.API.IntegrationTests public class ConsumersServicesTests { private readonly ConsumerServices _consumerServices; + private readonly ConsumerRepositoryImpl _consumerRepository; + private readonly FarmerRepositoryImpl _farmerRepository; + private readonly VegetableRepositoryImpl _vegetableRepository; + private readonly Context _context; public ConsumersServicesTests() { var loggerFactory = new NullLoggerFactory(); - var consumerRepository = new ConsumerRepositoryImpl(loggerFactory); - var farmerRepository = new FarmerRepositoryImpl(loggerFactory); - var vegetableRepositoryImpl = new VegetableRepositoryImpl(); + _context = new Context(loggerFactory); + _consumerRepository = new ConsumerRepositoryImpl(_context); + _farmerRepository = new FarmerRepositoryImpl(_context); + _vegetableRepository = new VegetableRepositoryImpl(_context); _consumerServices = new ConsumerServices(loggerFactory, - consumerRepository, - farmerRepository, - vegetableRepositoryImpl); + _consumerRepository, + _farmerRepository, + _vegetableRepository); + + // Initialize test data + _context.Consumers.Add(1L, new Consumer(loggerFactory)); + _context.Farmers.Add(1L, new Farmer(loggerFactory)); + _context.Vegetables.Add(1L, new Vegetable("carrot")); } [Fact] diff --git a/src/VegetableShop.API.IntegrationTests/FarmerServicesTests.cs b/src/VegetableShop.API.IntegrationTests/FarmerServicesTests.cs index 4aec597..5df2c72 100644 --- a/src/VegetableShop.API.IntegrationTests/FarmerServicesTests.cs +++ b/src/VegetableShop.API.IntegrationTests/FarmerServicesTests.cs @@ -14,8 +14,9 @@ namespace VegetableShop.API.IntegrationTests public FarmerServicesTests() { var loggerFactory = new NullLoggerFactory(); - var vegetableRepository = new VegetableRepositoryImpl(); - _farmerRepository = new FarmerRepositoryImpl(loggerFactory); + var context = new Context(loggerFactory); + var vegetableRepository = new VegetableRepositoryImpl(context); + _farmerRepository = new FarmerRepositoryImpl(context); _farmerServices = new FarmerServices(loggerFactory, _farmerRepository, vegetableRepository); } diff --git a/src/VegetableShop.API/Program.cs b/src/VegetableShop.API/Program.cs index 528a8d0..fef7870 100644 --- a/src/VegetableShop.API/Program.cs +++ b/src/VegetableShop.API/Program.cs @@ -32,21 +32,22 @@ namespace VegetableShop.API private Program() { var loggerFactory = SetupLogging(); + var context = new Context(loggerFactory); - ConsumerRepository consumerRepository = new ConsumerRepositoryImpl(loggerFactory); - FarmerRepository farmerRepository = new FarmerRepositoryImpl(loggerFactory); - VegetableRepository vegetableRepository = new VegetableRepositoryImpl(); + ConsumerRepository consumerRepository = new ConsumerRepositoryImpl(context); + FarmerRepository farmerRepository = new FarmerRepositoryImpl(context); + VegetableRepository vegetableRepository = new VegetableRepositoryImpl(context); _consumerServices = new ConsumerServices(loggerFactory, consumerRepository, - farmerRepository, - vegetableRepository); - - _farmerServices = new FarmerServices(loggerFactory, farmerRepository, vegetableRepository); - DomainEvents.AddObserver(new LogItemOnFarmerPutOnSaleEvent(_loggerFactory)); + _farmerServices = new FarmerServices(loggerFactory, + farmerRepository, + vegetableRepository); + + DomainEvents.AddObserver(new LogItemOnFarmerPutOnSaleEvent(loggerFactory)); } private void Run() @@ -56,7 +57,7 @@ namespace VegetableShop.API _consumerServices.BuyVegetables(1, 1, 1, 10); } - public static void Main(string[] args) + public static void Main() { new Program().Run(); } diff --git a/src/VegetableShop.Domain.UnitTests/LogItemOnFarmerPutOnSaleEventTest.cs b/src/VegetableShop.Domain.UnitTests/LogItemOnFarmerPutOnSaleEventTest.cs new file mode 100644 index 0000000..6ddea0a --- /dev/null +++ b/src/VegetableShop.Domain.UnitTests/LogItemOnFarmerPutOnSaleEventTest.cs @@ -0,0 +1,31 @@ +using FakeItEasy; +using Microsoft.Extensions.Logging; +using VegetableShop.Domain.Events; +using VegetableShop.Domain.Model; +using Xunit; + +namespace VegetableShop.Domain.UnitTests +{ + public class LogItemOnFarmerPutOnSaleEventTest + { + private readonly LogItemOnFarmerPutOnSaleEvent _handler; + private readonly ILogger _logger; + + public LogItemOnFarmerPutOnSaleEventTest() + { + _logger = A.Fake>(); + _handler = new LogItemOnFarmerPutOnSaleEvent(_logger); + } + + [Fact] + public void Test1() + { + var evt = new FarmerPutOnSaleEvent(1L, A.Fake(), new Price()); + _handler.Handle(evt); + + A.CallTo(() => _logger.LogInformation(default)) + .WithAnyArguments() + .MustHaveHappenedOnceExactly(); + } + } +} \ No newline at end of file diff --git a/src/VegetableShop.Domain.UnitTests/VegetableShop.Domain.UnitTests.csproj b/src/VegetableShop.Domain.UnitTests/VegetableShop.Domain.UnitTests.csproj new file mode 100644 index 0000000..089fd60 --- /dev/null +++ b/src/VegetableShop.Domain.UnitTests/VegetableShop.Domain.UnitTests.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + + diff --git a/src/VegetableShop.Domain/Errors/UnauthorizedSaleException.cs b/src/VegetableShop.Domain/Errors/UnauthorizedSaleException.cs index d989a04..0c84b8f 100644 --- a/src/VegetableShop.Domain/Errors/UnauthorizedSaleException.cs +++ b/src/VegetableShop.Domain/Errors/UnauthorizedSaleException.cs @@ -5,9 +5,9 @@ namespace VegetableShop.Domain.Errors { public class UnauthorizedSaleException : Exception { - public UnauthorizedSaleException(Farmer farmer, Vegetable vegetable) + public UnauthorizedSaleException(Farmer farmer, Vegetable vegetable) : + base($"Unauthorized sale of {vegetable} for {farmer}") { - throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/src/VegetableShop.Infrastructure/Persistence/ConsumerRepositoryImpl.cs b/src/VegetableShop.Infrastructure/Persistence/ConsumerRepositoryImpl.cs index a352487..71fb243 100644 --- a/src/VegetableShop.Infrastructure/Persistence/ConsumerRepositoryImpl.cs +++ b/src/VegetableShop.Infrastructure/Persistence/ConsumerRepositoryImpl.cs @@ -1,22 +1,17 @@ -using System.Collections.Generic; -using Microsoft.Extensions.Logging; using VegetableShop.Domain.Model; using VegetableShop.Domain.Repositories; namespace VegetableShop.Infrastructure.Persistence { - public class ConsumerRepositoryImpl: ConsumerRepository + public class ConsumerRepositoryImpl : ConsumerRepository { - private readonly Dictionary _consumers; + private readonly Context _context; - public ConsumerRepositoryImpl(ILoggerFactory loggerFactory) + public ConsumerRepositoryImpl(Context context) { - _consumers = new Dictionary - { - { 1L, new Consumer(loggerFactory) } - }; + _context = context; } - public Consumer GetById(in long consumerId) => _consumers[consumerId]; + public Consumer GetById(in long consumerId) => _context.Consumers[consumerId]; } } \ No newline at end of file diff --git a/src/VegetableShop.Infrastructure/Persistence/Context.cs b/src/VegetableShop.Infrastructure/Persistence/Context.cs new file mode 100644 index 0000000..8f6cebb --- /dev/null +++ b/src/VegetableShop.Infrastructure/Persistence/Context.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using Microsoft.Extensions.Logging; +using VegetableShop.Domain.Model; + +namespace VegetableShop.Infrastructure.Persistence +{ + public class Context + { + public Dictionary Consumers { get; } + public Dictionary Farmers { get; } + public Dictionary Vegetables { get; } + + public Context(ILoggerFactory loggerFactory) + { + Consumers = new Dictionary { + { 1L, new Consumer(loggerFactory) } + }; + Farmers = new Dictionary { + { 1L, new Farmer(loggerFactory) } + }; + Vegetables = new Dictionary { + { 1L, new Vegetable("carrot") } + };; + } + } +} \ No newline at end of file diff --git a/src/VegetableShop.Infrastructure/Persistence/FarmerRepositoryImpl.cs b/src/VegetableShop.Infrastructure/Persistence/FarmerRepositoryImpl.cs index 11ba44c..9dfbc68 100644 --- a/src/VegetableShop.Infrastructure/Persistence/FarmerRepositoryImpl.cs +++ b/src/VegetableShop.Infrastructure/Persistence/FarmerRepositoryImpl.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; -using Microsoft.Extensions.Logging; using VegetableShop.Domain.Model; using VegetableShop.Domain.Repositories; @@ -7,19 +5,12 @@ namespace VegetableShop.Infrastructure.Persistence { public class FarmerRepositoryImpl: FarmerRepository { - private readonly ILoggerFactory _loggerFactory; - - public FarmerRepositoryImpl(ILoggerFactory loggerFactory) + private readonly Context _context; + + public FarmerRepositoryImpl(Context context) { - _loggerFactory = loggerFactory; - _farmers = new Dictionary - { - { 1L, new Farmer(_loggerFactory) } - }; + _context = context; } - - private readonly Dictionary _farmers; - - public Farmer GetById(in long farmerId) => _farmers[farmerId]; + public Farmer GetById(in long farmerId) => _context.Farmers[farmerId]; } } \ No newline at end of file diff --git a/src/VegetableShop.Infrastructure/Persistence/VegetableRepositoryImpl.cs b/src/VegetableShop.Infrastructure/Persistence/VegetableRepositoryImpl.cs index ef1db8d..4e7c298 100644 --- a/src/VegetableShop.Infrastructure/Persistence/VegetableRepositoryImpl.cs +++ b/src/VegetableShop.Infrastructure/Persistence/VegetableRepositoryImpl.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using VegetableShop.Domain.Model; using VegetableShop.Domain.Repositories; @@ -6,10 +5,13 @@ namespace VegetableShop.Infrastructure.Persistence { public class VegetableRepositoryImpl: VegetableRepository { - private readonly Dictionary _vegetables = new Dictionary + private readonly Context _context; + + public VegetableRepositoryImpl(Context context) { - { 1L, new Vegetable("carrot") } - }; - public Vegetable GetById(in long vegetableId) => _vegetables[vegetableId]; + _context = context; + } + + public Vegetable GetById(in long vegetableId) => _context.Vegetables[vegetableId]; } } \ No newline at end of file