|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2019 Rasmus Mikkelsen |
| 4 | +// Copyright (c) 2015-2019 eBay Software Foundation |
| 5 | +// https://github.com/eventflow/EventFlow |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | +// this software and associated documentation files (the "Software"), to deal in |
| 9 | +// the Software without restriction, including without limitation the rights to |
| 10 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 11 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 12 | +// subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 19 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 20 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 21 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +using System; |
| 25 | +using System.Collections.Generic; |
| 26 | +using System.Linq; |
| 27 | +using System.Threading; |
| 28 | +using System.Threading.Tasks; |
| 29 | +using EventFlow.Aggregates; |
| 30 | +using EventFlow.Configuration; |
| 31 | +using EventFlow.EventStores; |
| 32 | +using EventFlow.ReadStores; |
| 33 | +using EventFlow.TestHelpers; |
| 34 | +using EventFlow.TestHelpers.Aggregates.Events; |
| 35 | +using Moq; |
| 36 | +using NUnit.Framework; |
| 37 | + |
| 38 | +namespace EventFlow.Tests.UnitTests.ReadStores |
| 39 | +{ |
| 40 | + [Timeout(5000)] |
| 41 | + [Category(Categories.Unit)] |
| 42 | + public abstract class BaseReadModelTests<TReadModel> : TestsFor<ReadModelPopulator> |
| 43 | + where TReadModel : class, IReadModel |
| 44 | + { |
| 45 | + private const int ReadModelPageSize = 3; |
| 46 | + |
| 47 | + private Mock<IReadModelStore<TReadModel>> _readModelStoreMock; |
| 48 | + private Mock<IReadStoreManager<TReadModel>> _readStoreManagerMock; |
| 49 | + private Mock<IEventFlowConfiguration> _eventFlowConfigurationMock; |
| 50 | + private Mock<IEventStore> _eventStoreMock; |
| 51 | + private Mock<IResolver> _resolverMock; |
| 52 | + private List<IDomainEvent> _eventStoreData; |
| 53 | + |
| 54 | + [SetUp] |
| 55 | + public void SetUp() |
| 56 | + { |
| 57 | + _eventStoreMock = InjectMock<IEventStore>(); |
| 58 | + _eventStoreData = null; |
| 59 | + _resolverMock = InjectMock<IResolver>(); |
| 60 | + _readModelStoreMock = new Mock<IReadModelStore<TReadModel>>(); |
| 61 | + _readStoreManagerMock = new Mock<IReadStoreManager<TReadModel>>(); |
| 62 | + _eventFlowConfigurationMock = InjectMock<IEventFlowConfiguration>(); |
| 63 | + |
| 64 | + _resolverMock |
| 65 | + .Setup(r => r.Resolve<IEnumerable<IReadStoreManager>>()) |
| 66 | + .Returns(new[] { _readStoreManagerMock.Object }); |
| 67 | + _resolverMock |
| 68 | + .Setup(r => r.ResolveAll(typeof(IReadModelStore<TReadModel>))) |
| 69 | + .Returns(new[] { _readModelStoreMock.Object }); |
| 70 | + |
| 71 | + _eventFlowConfigurationMock |
| 72 | + .Setup(c => c.PopulateReadModelEventPageSize) |
| 73 | + .Returns(ReadModelPageSize); |
| 74 | + |
| 75 | + _eventStoreMock |
| 76 | + .Setup(s => s.LoadAllEventsAsync(It.IsAny<GlobalPosition>(), It.IsAny<int>(), It.IsAny<CancellationToken>())) |
| 77 | + .Returns<GlobalPosition, int, CancellationToken>((s, p, c) => Task.FromResult(GetEvents(s, p))); |
| 78 | + _readStoreManagerMock |
| 79 | + .Setup(m => m.ReadModelType) |
| 80 | + .Returns(typeof(TReadModel)); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public async Task PurgeIsCalled() |
| 85 | + { |
| 86 | + // Act |
| 87 | + await Sut.PurgeAsync<TReadModel>(CancellationToken.None).ConfigureAwait(false); |
| 88 | + |
| 89 | + // Assert |
| 90 | + _readModelStoreMock.Verify(s => s.DeleteAllAsync(It.IsAny<CancellationToken>()), Times.Once); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public async Task PopulateCallsApplyDomainEvents() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + ArrangeEventStore(Many<ThingyPingEvent>(6)); |
| 98 | + |
| 99 | + // Act |
| 100 | + await Sut.PopulateAsync<TReadModel>(CancellationToken.None).ConfigureAwait(false); |
| 101 | + |
| 102 | + // Assert |
| 103 | + _readStoreManagerMock.Verify( |
| 104 | + s => s.UpdateReadStoresAsync( |
| 105 | + It.Is<IReadOnlyCollection<IDomainEvent>>(l => l.Count == ReadModelPageSize), |
| 106 | + It.IsAny<CancellationToken>()), |
| 107 | + Times.Exactly(2)); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public async Task UnwantedEventsAreFiltered() |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + var events = new IAggregateEvent[] |
| 115 | + { |
| 116 | + A<ThingyPingEvent>(), |
| 117 | + A<ThingyDomainErrorAfterFirstEvent>(), |
| 118 | + A<ThingyPingEvent>(), |
| 119 | + }; |
| 120 | + ArrangeEventStore(events); |
| 121 | + |
| 122 | + // Act |
| 123 | + await Sut.PopulateAsync<TReadModel>(CancellationToken.None).ConfigureAwait(false); |
| 124 | + |
| 125 | + // Assert |
| 126 | + _readStoreManagerMock |
| 127 | + .Verify( |
| 128 | + s => s.UpdateReadStoresAsync( |
| 129 | + It.Is<IReadOnlyCollection<IDomainEvent>>(l => l.Count == 2 && l.All(e => e.EventType == typeof(ThingyPingEvent))), |
| 130 | + It.IsAny<CancellationToken>()), |
| 131 | + Times.Once); |
| 132 | + } |
| 133 | + |
| 134 | + private AllEventsPage GetEvents(GlobalPosition globalPosition, int pageSize) |
| 135 | + { |
| 136 | + var startPosition = globalPosition.IsStart |
| 137 | + ? 1 |
| 138 | + : int.Parse(globalPosition.Value); |
| 139 | + |
| 140 | + var events = _eventStoreData |
| 141 | + .Skip(Math.Max(startPosition - 1, 0)) |
| 142 | + .Take(pageSize) |
| 143 | + .ToList(); |
| 144 | + |
| 145 | + var nextPosition = Math.Min(Math.Max(startPosition, 1) + pageSize, _eventStoreData.Count + 1); |
| 146 | + |
| 147 | + return new AllEventsPage(new GlobalPosition(nextPosition.ToString()), events); |
| 148 | + } |
| 149 | + |
| 150 | + private void ArrangeEventStore(IEnumerable<IAggregateEvent> aggregateEvents) |
| 151 | + { |
| 152 | + ArrangeEventStore(aggregateEvents.Select(e => ToDomainEvent(e))); |
| 153 | + } |
| 154 | + |
| 155 | + private void ArrangeEventStore(IEnumerable<IDomainEvent> domainEvents) |
| 156 | + { |
| 157 | + _eventStoreData = domainEvents.ToList(); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments