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