Line data Source code
1 : using System;
2 : using cdmdotnet.Logging;
3 : using Cqrs.Domain;
4 : using Cqrs.Domain.Exceptions;
5 : using Cqrs.Domain.Factories;
6 : using Cqrs.Authentication;
7 : using Cqrs.Commands;
8 : using Cqrs.Tests.Substitutes;
9 : using NUnit.Framework;
10 :
11 : namespace Cqrs.Tests.Domain.Saga
12 : {
13 : [TestFixture]
14 : public class When_getting_a_saga
15 0 : {
16 : private ISagaUnitOfWork<ISingleSignOnToken> _unitOfWork;
17 :
18 : private TestDependencyResolver _dependencyResolver;
19 :
20 : private TestCommandPublisher _commandPublisher;
21 :
22 : [SetUp]
23 0 : public void Setup()
24 : {
25 : var eventStore = new TestEventStore();
26 : _commandPublisher = new TestCommandPublisher();
27 : _dependencyResolver = new TestDependencyResolver(eventStore, _commandPublisher);
28 : var sagaFactory = new AggregateFactory(_dependencyResolver, _dependencyResolver.Resolve<ILogger>());
29 : var testEventPublisher = new TestEventPublisher();
30 : _unitOfWork = new SagaUnitOfWork<ISingleSignOnToken>(new SagaRepository<ISingleSignOnToken>(sagaFactory, eventStore, testEventPublisher, new NullCorrelationIdHelper()));
31 : }
32 :
33 : [Test]
34 0 : public void Should_get_saga_from_eventstore()
35 : {
36 : _dependencyResolver.UseTestEventStoreGuid = false;
37 : var saga = _unitOfWork.Get<TestSaga>(Guid.NewGuid());
38 : Assert.NotNull(saga);
39 : }
40 :
41 : [Test]
42 0 : public void Should_apply_events()
43 : {
44 : var saga = _unitOfWork.Get<TestSaga>(Guid.NewGuid());
45 : Assert.AreEqual(2, saga.DidSomethingCount);
46 : }
47 :
48 : [Test]
49 0 : public void Should_not_send_command()
50 : {
51 : var saga = _unitOfWork.Get<TestSaga>(Guid.NewGuid());
52 : Assert.AreEqual(0, _commandPublisher.Published);
53 : }
54 :
55 : [Test]
56 0 : public void Should_send_command_on_new_event()
57 : {
58 : var saga = _unitOfWork.Get<TestSaga>(Guid.NewGuid());
59 : saga.Handle(new TestAggregateDidSomethingElse2());
60 : Assert.AreEqual(1, _commandPublisher.Published);
61 : }
62 :
63 : [Test]
64 0 : public void Should_fail_if_saga_do_not_exist()
65 : {
66 : _dependencyResolver.UseTestEventStoreGuid = true;
67 : Assert.Throws<SagaNotFoundException<TestSaga, ISingleSignOnToken>>(() => _unitOfWork.Get<TestSaga>(Guid.Empty));
68 : }
69 :
70 : [Test]
71 0 : public void Should_track_changes()
72 : {
73 : var agg = new TestSaga(_dependencyResolver, Guid.NewGuid());
74 : _unitOfWork.Add(agg);
75 : var saga = _unitOfWork.Get<TestSaga>(agg.Id);
76 : Assert.AreEqual(agg, saga);
77 : }
78 :
79 : [Test]
80 0 : public void Should_get_from_session_if_tracked()
81 : {
82 : var id = Guid.NewGuid();
83 : _dependencyResolver.NewAggregateGuid = id;
84 : var saga = _unitOfWork.Get<TestSaga>(id);
85 : var saga2 = _unitOfWork.Get<TestSaga>(id);
86 :
87 : Assert.AreEqual(saga, saga2);
88 : }
89 :
90 : [Test]
91 0 : public void Should_throw_concurrency_exception_if_tracked()
92 : {
93 : var id = Guid.NewGuid();
94 : _unitOfWork.Get<TestSaga>(id);
95 :
96 : Assert.Throws<ConcurrencyException>(() => _unitOfWork.Get<TestSaga>(id, 100));
97 : }
98 :
99 : [Test]
100 0 : public void Should_get_correct_version()
101 : {
102 : var id = Guid.NewGuid();
103 : var saga = _unitOfWork.Get<TestSaga>(id);
104 :
105 : Assert.AreEqual(4, saga.Version);
106 : }
107 :
108 : [Test]
109 0 : public void Should_throw_concurrency_exception()
110 : {
111 : var id = Guid.NewGuid();
112 : Assert.Throws<ConcurrencyException>(() => _unitOfWork.Get<TestSaga>(id, 1));
113 : }
114 : }
115 : }
|