Line data Source code
1 : using System;
2 : using System.Linq;
3 : using cdmdotnet.Logging;
4 : using Cqrs.Domain;
5 : using Cqrs.Domain.Exceptions;
6 : using Cqrs.Domain.Factories;
7 : using Cqrs.Authentication;
8 : using Cqrs.Tests.Substitutes;
9 : using NUnit.Framework;
10 :
11 : namespace Cqrs.Tests.Domain
12 : {
13 : [TestFixture]
14 : public class When_saving
15 0 : {
16 : private TestInMemoryEventStore _eventStore;
17 : private TestAggregateNoParameterLessConstructor _aggregate;
18 : private TestEventPublisher _eventPublisher;
19 : private IUnitOfWork<ISingleSignOnToken> _unitOfWork;
20 : private Repository<ISingleSignOnToken> _rep;
21 : private TestDependencyResolver _dependencyResolver;
22 :
23 : [SetUp]
24 0 : public void Setup()
25 : {
26 : _dependencyResolver = new TestDependencyResolver(null);
27 : var aggregateFactory = new AggregateFactory(_dependencyResolver, _dependencyResolver.Resolve<ILogger>());
28 : _eventStore = new TestInMemoryEventStore();
29 : _eventPublisher = new TestEventPublisher();
30 : _rep = new Repository<ISingleSignOnToken>(aggregateFactory, _eventStore, _eventPublisher, new NullCorrelationIdHelper());
31 : _unitOfWork = new UnitOfWork<ISingleSignOnToken>(_rep);
32 :
33 : _aggregate = new TestAggregateNoParameterLessConstructor(2);
34 :
35 : }
36 :
37 : [Test]
38 0 : public void Should_save_uncommited_changes()
39 : {
40 : _aggregate.DoSomething();
41 : _unitOfWork.Add(_aggregate);
42 : _unitOfWork.Commit();
43 : Assert.AreEqual(1, _eventStore.Events.Count);
44 : }
45 :
46 : [Test]
47 0 : public void Should_mark_commited_after_commit()
48 : {
49 : _aggregate.DoSomething();
50 : _unitOfWork.Add(_aggregate);
51 : _unitOfWork.Commit();
52 : Assert.AreEqual(0, _aggregate.GetUncommittedChanges().Count());
53 : }
54 :
55 : [Test]
56 0 : public void Should_publish_events()
57 : {
58 : _aggregate.DoSomething();
59 : _unitOfWork.Add(_aggregate);
60 : _unitOfWork.Commit();
61 : Assert.AreEqual(1, _eventPublisher.Published);
62 : }
63 :
64 : [Test]
65 0 : public void Should_add_new_aggregate()
66 : {
67 : var agg = new TestAggregateNoParameterLessConstructor(1);
68 : agg.DoSomething();
69 : _unitOfWork.Add(agg);
70 : _unitOfWork.Commit();
71 : Assert.AreEqual(1, _eventStore.Events.Count);
72 : }
73 :
74 : [Test]
75 0 : public void Should_set_date()
76 : {
77 : var agg = new TestAggregateNoParameterLessConstructor(1);
78 : agg.DoSomething();
79 : _unitOfWork.Add(agg);
80 : _unitOfWork.Commit();
81 : Assert.That(_eventStore.Events.First().TimeStamp, Is.InRange(DateTimeOffset.UtcNow.AddSeconds(-1), DateTimeOffset.UtcNow.AddSeconds(1)));
82 : }
83 :
84 : [Test]
85 0 : public void Should_set_version()
86 : {
87 : var agg = new TestAggregateNoParameterLessConstructor(1);
88 : agg.DoSomething();
89 : agg.DoSomething();
90 : _unitOfWork.Add(agg);
91 : _unitOfWork.Commit();
92 : Assert.That(_eventStore.Events.First().Version, Is.EqualTo(1));
93 : Assert.That(_eventStore.Events.Last().Version, Is.EqualTo(2));
94 :
95 : }
96 :
97 : [Test]
98 0 : public void Should_set_id()
99 : {
100 : var id = Guid.NewGuid();
101 : var agg = new TestAggregateNoParameterLessConstructor(1, id);
102 : agg.DoSomething();
103 : _unitOfWork.Add(agg);
104 : _unitOfWork.Commit();
105 : Assert.That(_eventStore.Events.First().Id, Is.EqualTo(id));
106 : }
107 :
108 : [Test]
109 0 : public void Should_clear_tracked_aggregates()
110 : {
111 : var agg = new TestAggregate(Guid.NewGuid());
112 : _dependencyResolver.NewAggregateGuid = agg.Id;
113 : _unitOfWork.Add(agg);
114 : agg.DoSomething();
115 : _unitOfWork.Commit();
116 : _eventStore.Events.Clear();
117 :
118 : Assert.Throws<AggregateNotFoundException<TestAggregate, ISingleSignOnToken>>(() => _unitOfWork.Get<TestAggregate>(agg.Id));
119 : }
120 : }
121 : }
|