Line data Source code
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Diagnostics;
4 : using System.Linq;
5 : using cdmdotnet.Logging;
6 : using cdmdotnet.Logging.Configuration;
7 : using cdmdotnet.StateManagement.Threaded;
8 : using Cqrs.Azure.ServiceBus.Tests.Unit;
9 : using Cqrs.Events;
10 : using Cqrs.MongoDB.Events;
11 : using Cqrs.MongoDB.Serialisers;
12 : using MongoDB.Driver;
13 : using NUnit.Framework;
14 : using TestClass = NUnit.Framework.TestFixtureAttribute;
15 : using TestMethod = NUnit.Framework.TestAttribute;
16 : using TestInitialize = NUnit.Framework.SetUpAttribute;
17 : using TestCleanup = NUnit.Framework.TearDownAttribute;
18 : using TestContext = System.Object;
19 :
20 : namespace Cqrs.MongoDB.Tests.Integration
21 : {
22 : /// <summary>
23 : /// A series of tests on the <see cref="MongoDbEventStore{TAuthenticationToken}"/> class
24 : /// </summary>
25 : [TestClass]
26 : public class MongoDbEventStoreTests
27 1 : {
28 : [TestMethod]
29 0 : public void Save_ValidEvent_EventCanBeRetreived()
30 : {
31 : // Arrange
32 : var correlationIdHelper = new CorrelationIdHelper(new ThreadedContextItemCollectionFactory());
33 : correlationIdHelper.SetCorrelationId(Guid.NewGuid());
34 : var logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper);
35 : try
36 : {
37 : // Arrange
38 : var connectionStringFactory = new TestMongoEventStoreConnectionStringFactory();
39 : TestMongoEventStoreConnectionStringFactory.DatabaseName = string.Format("Test-{0}", new Random().Next(0, 9999));
40 :
41 : var eventStore = new MongoDbEventStore<Guid>(new MongoDbEventBuilder<Guid>(), new MongoDbEventDeserialiser<Guid>(), logger, connectionStringFactory);
42 :
43 : var event1 = new TestEvent
44 : {
45 : Rsn = Guid.NewGuid(),
46 : Id = Guid.NewGuid(),
47 : CorrelationId = correlationIdHelper.GetCorrelationId(),
48 : Frameworks = new List<string> { "Test 1" },
49 : TimeStamp = DateTimeOffset.UtcNow
50 : };
51 : var event2 = new TestEvent
52 : {
53 : Rsn = Guid.NewGuid(),
54 : Id = Guid.NewGuid(),
55 : CorrelationId = correlationIdHelper.GetCorrelationId(),
56 : Frameworks = new List<string> { "Test 2" },
57 : TimeStamp = DateTimeOffset.UtcNow
58 : };
59 :
60 : // Act
61 : eventStore.Save<TestEvent>(event1);
62 : eventStore.Save<TestEvent>(event2);
63 :
64 : // Assert
65 : var timer = new Stopwatch();
66 : IList<IEvent<Guid>> events = eventStore.Get<TestEvent>(event1.Id).ToList();
67 : timer.Stop();
68 : Console.WriteLine("Load one operation took {0}", timer.Elapsed);
69 : Assert.AreEqual(1, events.Count);
70 : Assert.AreEqual(event1.Id, events.Single().Id);
71 : Assert.AreEqual(event1.Frameworks.Single(), events.Single().Frameworks.Single());
72 :
73 : timer.Restart();
74 : events = eventStore.Get<TestEvent>(event2.Id).ToList();
75 : timer.Stop();
76 : Console.WriteLine("Load one operation took {0}", timer.Elapsed);
77 : Assert.AreEqual(1, events.Count);
78 : Assert.AreEqual(event2.Id, events.Single().Id);
79 : Assert.AreEqual(event2.Frameworks.Single(), events.Single().Frameworks.Single());
80 :
81 : timer.Restart();
82 : IList<EventData> correlatedEvents = eventStore.Get(event1.CorrelationId).ToList();
83 : timer.Stop();
84 : Console.WriteLine("Load several correlated operation took {0}", timer.Elapsed);
85 : Assert.AreEqual(2, correlatedEvents.Count);
86 : }
87 : finally
88 : {
89 : // Clean-up
90 : TestMongoDataStoreConnectionStringFactory.DatabaseName = TestMongoEventStoreConnectionStringFactory.DatabaseName;
91 : var factory = new TestMongoDbDataStoreFactory(logger, new TestMongoDataStoreConnectionStringFactory());
92 : IMongoCollection<TestEvent> collection = factory.GetTestEventCollection();
93 : collection.Database.Client.DropDatabase(TestMongoDataStoreConnectionStringFactory.DatabaseName);
94 : }
95 : }
96 : }
97 : }
|