|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015 Rasmus Mikkelsen |
| 4 | +// https://github.com/rasmus/EventFlow |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +// this software and associated documentation files (the "Software"), to deal in |
| 8 | +// the Software without restriction, including without limitation the rights to |
| 9 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +// subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + |
| 23 | +using System; |
| 24 | +using EventFlow.Aggregates; |
| 25 | +using EventFlow.Extensions; |
| 26 | +using EventFlow.TestHelpers.Aggregates.Test; |
| 27 | +using EventFlow.TestHelpers.Aggregates.Test.Events; |
| 28 | +using FluentAssertions; |
| 29 | +using NUnit.Framework; |
| 30 | +using EventFlow.Configuration.Registrations; |
| 31 | +using EventFlow.TestHelpers; |
| 32 | +using System.Collections.Generic; |
| 33 | + |
| 34 | +namespace EventFlow.Tests.UnitTests.Extensions |
| 35 | +{ |
| 36 | + public class AggregatesExtensionsTests |
| 37 | + { |
| 38 | + [Test] |
| 39 | + public void AbstractAggregateRootImplementationIsNotSelected() |
| 40 | + { |
| 41 | + // arrange |
| 42 | + var registry = new AutofacServiceRegistration(); |
| 43 | + var sut = EventFlowOptions.New |
| 44 | + .UseServiceRegistration(registry); |
| 45 | + |
| 46 | + // act |
| 47 | + sut.AddAggregateRoots(EventFlowTests.Assembly); |
| 48 | + |
| 49 | + // assert |
| 50 | + registry.HasRegistrationFor<AbstractTestAggregate>().Should().Be(false); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void ClosedIAggregateRootImplementationIsSelected() |
| 55 | + { |
| 56 | + // arrange |
| 57 | + var registry = new AutofacServiceRegistration(); |
| 58 | + var sut = EventFlowOptions.New |
| 59 | + .UseServiceRegistration(registry); |
| 60 | + |
| 61 | + // act |
| 62 | + sut.AddAggregateRoots(EventFlowTestHelpers.Assembly); |
| 63 | + |
| 64 | + // assert |
| 65 | + registry.HasRegistrationFor<TestAggregate>().Should().Be(true); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void AbstractAggregateRootImplementationIsRejected() |
| 70 | + { |
| 71 | + // arrange |
| 72 | + var registry = new AutofacServiceRegistration(); |
| 73 | + var sut = EventFlowOptions.New |
| 74 | + .UseServiceRegistration(registry); |
| 75 | + |
| 76 | + // act |
| 77 | + Action act = () => sut.AddAggregateRoots(new List<Type> { typeof(AbstractTestAggregate) } ); |
| 78 | + |
| 79 | + // assert |
| 80 | + act.ShouldThrow<ArgumentException>(); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void NonIAggregateRootImplementationIsRejected() |
| 85 | + { |
| 86 | + // arrange |
| 87 | + var registry = new AutofacServiceRegistration(); |
| 88 | + var sut = EventFlowOptions.New |
| 89 | + .UseServiceRegistration(registry); |
| 90 | + |
| 91 | + // act |
| 92 | + Action act = () => sut.AddAggregateRoots(new List<Type> { typeof(TestId) }); |
| 93 | + |
| 94 | + // assert |
| 95 | + act.ShouldThrow<ArgumentException>(); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void ClosedIAggregateRootImplementationIsAccepted() |
| 100 | + { |
| 101 | + // arrange |
| 102 | + var registry = new AutofacServiceRegistration(); |
| 103 | + var sut = EventFlowOptions.New |
| 104 | + .UseServiceRegistration(registry); |
| 105 | + |
| 106 | + // act |
| 107 | + Action act = () => sut.AddAggregateRoots(new List<Type> { typeof(LocalTestAggregate) }); |
| 108 | + |
| 109 | + // assert |
| 110 | + act.ShouldNotThrow<ArgumentException>(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public abstract class AbstractTestAggregate : AggregateRoot<TestAggregate, TestId>, |
| 115 | + IEmit<DomainErrorAfterFirstEvent> |
| 116 | + { |
| 117 | + public AbstractTestAggregate(TestId id) : base(id) |
| 118 | + { |
| 119 | + } |
| 120 | + |
| 121 | + public void Apply(DomainErrorAfterFirstEvent aggregateEvent) |
| 122 | + { |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public class LocalTestAggregate : AbstractTestAggregate |
| 127 | + { |
| 128 | + public LocalTestAggregate(TestId id) : base(id) |
| 129 | + { |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments