Line data Source code
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using cdmdotnet.Logging;
5 : using cdmdotnet.Logging.Configuration;
6 : using cdmdotnet.StateManagement.Threaded;
7 : using Cqrs.Commands;
8 : using Cqrs.Domain;
9 : using Cqrs.Domain.Factories;
10 : using Cqrs.Events;
11 : using Cqrs.Authentication;
12 : using Cqrs.Snapshots;
13 : using NUnit.Framework;
14 :
15 : namespace Cqrs.Tests.Extensions.TestHelpers
16 : {
17 : [TestFixture]
18 : public abstract class Specification<TAggregate, THandler, TCommand>
19 : where TAggregate : AggregateRoot<ISingleSignOnToken>
20 : where THandler : class, ICommandHandler<ISingleSignOnToken, TCommand>
21 : where TCommand : ICommand<ISingleSignOnToken>
22 0 : {
23 :
24 : protected TAggregate Aggregate { get; set; }
25 : protected IUnitOfWork<ISingleSignOnToken> UnitOfWork { get; set; }
26 0 : protected abstract IEnumerable<IEvent<ISingleSignOnToken>> Given();
27 0 : protected abstract TCommand When();
28 0 : protected abstract THandler BuildHandler();
29 :
30 : protected Snapshot Snapshot { get; set; }
31 : protected IList<IEvent<ISingleSignOnToken>> EventDescriptors { get; set; }
32 : protected IList<IEvent<ISingleSignOnToken>> PublishedEvents { get; set; }
33 :
34 : [SetUp]
35 0 : public void Run()
36 : {
37 : var eventstorage = new SpecEventStorage(Given().ToList());
38 : var snapshotstorage = new SpecSnapShotStorage(Snapshot);
39 : var eventpublisher = new SpecEventPublisher();
40 : var aggregateFactory = new AggregateFactory(null, new ConsoleLogger(new LoggerSettings(), new CorrelationIdHelper(new ThreadedContextItemCollectionFactory())));
41 :
42 : var snapshotStrategy = new DefaultSnapshotStrategy<ISingleSignOnToken>();
43 : var repository = new SnapshotRepository<ISingleSignOnToken>(snapshotstorage, snapshotStrategy, new Repository<ISingleSignOnToken>(aggregateFactory, eventstorage, eventpublisher, new NullCorrelationIdHelper()), eventstorage, aggregateFactory);
44 : UnitOfWork = new UnitOfWork<ISingleSignOnToken>(repository);
45 :
46 : Aggregate = UnitOfWork.Get<TAggregate>(Guid.Empty);
47 :
48 : var handler = BuildHandler();
49 : handler.Handle(When());
50 :
51 : Snapshot = snapshotstorage.Snapshot;
52 : PublishedEvents = eventpublisher.PublishedEvents;
53 : EventDescriptors = eventstorage.Events;
54 : }
55 : }
56 :
57 : internal class SpecSnapShotStorage : ISnapshotStore
58 : {
59 0 : public SpecSnapShotStorage(Snapshot snapshot)
60 : {
61 : Snapshot = snapshot;
62 : }
63 :
64 : public Snapshot Snapshot { get; set; }
65 :
66 0 : public Snapshot Get(Guid id)
67 : {
68 : return Snapshot;
69 : }
70 :
71 0 : public void Save(Snapshot snapshot)
72 : {
73 : Snapshot = snapshot;
74 : }
75 : }
76 :
77 : internal class SpecEventPublisher : IEventPublisher<ISingleSignOnToken>
78 : {
79 0 : public SpecEventPublisher()
80 : {
81 : PublishedEvents = new List<IEvent<ISingleSignOnToken>>();
82 : }
83 :
84 0 : public void Publish<T>(T @event)
85 : where T : IEvent<ISingleSignOnToken>
86 : {
87 : PublishedEvents.Add(@event);
88 : }
89 :
90 0 : public void Publish<TEvent>(IEnumerable<TEvent> events)
91 : where TEvent : IEvent<ISingleSignOnToken>
92 : {
93 : foreach (TEvent @event in events)
94 : PublishedEvents.Add(@event);
95 : }
96 :
97 : public IList<IEvent<ISingleSignOnToken>> PublishedEvents { get; set; }
98 : }
99 :
100 : internal class SpecEventStorage : IEventStore<ISingleSignOnToken>
101 : {
102 0 : public SpecEventStorage(IList<IEvent<ISingleSignOnToken>> events)
103 : {
104 : Events = events;
105 : }
106 :
107 : public IList<IEvent<ISingleSignOnToken>> Events { get; set; }
108 :
109 0 : public void Save<T>(IEvent<ISingleSignOnToken> @event)
110 : {
111 : Events.Add(@event);
112 : }
113 :
114 0 : public void Save(Type aggregateRootType, IEvent<ISingleSignOnToken> @event)
115 : {
116 : Events.Add(@event);
117 : }
118 :
119 0 : public IEnumerable<IEvent<ISingleSignOnToken>> Get<T>(Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
120 : {
121 : return Get(typeof(T), aggregateId, useLastEventOnly, fromVersion);
122 : }
123 :
124 0 : public IEnumerable<IEvent<ISingleSignOnToken>> Get(Type aggregateType, Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
125 : {
126 : return Events.Where(x => x.Version > fromVersion);
127 : }
128 :
129 0 : public IEnumerable<EventData> Get(Guid correlationId)
130 : {
131 : return Enumerable.Empty<EventData>();
132 : }
133 : }
134 : }
|