Documentation Coverage Report
Current view: top level - Cqrs.MongoDB.Tests.Integration - MongoDbSnapshotStoreTests.cs Hit Total Coverage
Version: 2.2 Artefacts: 5 10 50.0 %
Date: 2018-08-07 15:04:50

          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 cdmdotnet.Logging;
      12             : using Cqrs.Authentication;
      13             : using Cqrs.Azure.ServiceBus.Tests.Unit;
      14             : using Cqrs.Configuration;
      15             : using Cqrs.Domain;
      16             : using Cqrs.Domain.Factories;
      17             : using Cqrs.Events;
      18             : using Cqrs.Messages;
      19             : using Cqrs.MongoDB.Events;
      20             : using Cqrs.MongoDB.Tests.Integration.Configuration;
      21             : using Cqrs.Ninject.Configuration;
      22             : using Cqrs.Snapshots;
      23             : using MongoDB.Driver;
      24             : using NUnit.Framework;
      25             : using TestClass = NUnit.Framework.TestFixtureAttribute;
      26             : using TestMethod = NUnit.Framework.TestAttribute;
      27             : using TestInitialize = NUnit.Framework.SetUpAttribute;
      28             : using TestCleanup = NUnit.Framework.TearDownAttribute;
      29             : using TestContext = System.Object;
      30             : 
      31             : namespace Cqrs.MongoDB.Tests.Integration
      32             : {
      33             :         /// <summary>
      34             :         /// A series of tests on the <see cref="MongoDbSnapshotStore"/> class
      35             :         /// </summary>
      36             :         [TestClass]
      37             :         public class MongoDbSnapshotStoreTests
      38           1 :         {
      39             :                 /// <summary>
      40             :                 /// Tests the <see cref="ISnapshotStore.Save"/> method
      41             :                 /// Passing a valid test <see cref="IEvent{TAuthenticationToken}"/>
      42             :                 /// Expecting the test <see cref="IEvent{TAuthenticationToken}"/> is able to be read.
      43             :                 /// </summary>
      44             :                 [TestMethod]
      45           1 :                 public void Should_load_events()
      46             :                 {
      47             :                         // Arrange
      48             :                         TestMongoDbSnapshotStoreConnectionStringFactory.DatabaseName = string.Format("Test-{0}", new Random().Next(0, 9999));
      49             :                         NinjectDependencyResolver.ModulesToLoad.Add(new CqrsModule<int, DefaultAuthenticationTokenHelper>());
      50             :                         NinjectDependencyResolver.ModulesToLoad.Add(new InProcessEventBusModule<int>());
      51             :                         NinjectDependencyResolver.ModulesToLoad.Add(new TestMongoDbModule<int>());
      52             :                         NinjectDependencyResolver.Start();
      53             :                         var unitOfWork = new UnitOfWork<int>(DependencyResolver.Current.Resolve<ISnapshotAggregateRepository<int>>(), DependencyResolver.Current.Resolve<IAggregateRepository<int>>());
      54             :                         var aggregate = DependencyResolver.Current.Resolve<IAggregateFactory>().Create<TestAggregate>(Guid.NewGuid());
      55             :                         unitOfWork.Add(aggregate);
      56             :                         try
      57             :                         {
      58             :                                 int count = 0;
      59             :                                 do
      60             :                                 {
      61             :                                         aggregate.GenerateRandomNumber();
      62             :                                         if (count % 10 == 0)
      63             :                                         {
      64             :                                                 unitOfWork.Commit();
      65             :                                                 unitOfWork.Add(aggregate);
      66             :                                         }
      67             :                                 } while (count++ <= 20);
      68             :                                 unitOfWork.Commit();
      69             : 
      70             :                                 // Act
      71             :                                 var aggregate2 = unitOfWork.Get<TestAggregate>(aggregate.Rsn);
      72             : 
      73             :                                 // Assert
      74             :                                 Assert.AreEqual(22, aggregate2.Version);
      75             :                                 Assert.AreEqual(aggregate.CurrentRandomNumber, aggregate2.CurrentRandomNumber);
      76             :                         }
      77             :                         finally
      78             :                         {
      79             :                                 // Clean-up
      80             :                                 TestMongoDataStoreConnectionStringFactory.DatabaseName = TestMongoDbSnapshotStoreConnectionStringFactory.DatabaseName;
      81             :                                 var factory = new TestMongoDbDataStoreFactory(DependencyResolver.Current.Resolve<ILogger>(), new TestMongoDataStoreConnectionStringFactory());
      82             :                                 IMongoCollection<TestEvent> collection = factory.GetTestEventCollection();
      83             :                                 collection.Database.Client.DropDatabase(TestMongoDataStoreConnectionStringFactory.DatabaseName);
      84             :                         }
      85             :                 }
      86             : 
      87             :                 /// <summary />
      88             :                 public class TestAggregate
      89             :                         : SnapshotAggregateRoot<int, TestAggregateSnapshot>
      90           0 :                 {
      91             :                         /// <summary>
      92             :                         /// Gets or sets the <see cref="IDependencyResolver"/> used.
      93             :                         /// </summary>
      94             :                         protected IDependencyResolver DependencyResolver { get; private set; }
      95             : 
      96             :                         /// <summary>
      97             :                         /// Gets or sets the <see cref="ILogger"/> used.
      98             :                         /// </summary>
      99             :                         protected ILogger Logger { get; private set; }
     100             : 
     101             :                         /// <summary>
     102             :                         /// Instantiates a new instance of <see cref="AggregateFactory"/>.
     103             :                         /// </summary>
     104           1 :                         public TestAggregate(IDependencyResolver dependencyResolver, ILogger logger, Guid rsn)
     105             :                         {
     106             :                                 DependencyResolver = dependencyResolver;
     107             :                                 Logger = logger;
     108             :                                 Rsn = rsn;
     109             :                         }
     110             : 
     111             :                         /// <summary />
     112             :                         public Guid Rsn
     113             :                         {
     114             :                                 get { return Id; }
     115             :                                 set { Id = value; }
     116             :                         }
     117             : 
     118             :                         /// <summary />
     119             :                         public int CurrentRandomNumber { get; set; }
     120             : 
     121             :                         /// <summary />
     122           0 :                         public void GenerateRandomNumber()
     123             :                         {
     124             :                                 ApplyChange(new RandomNumberEvent(Rsn));
     125             :                         }
     126             : 
     127             :                         private void Apply(RandomNumberEvent @event)
     128             :                         {
     129             :                                 CurrentRandomNumber = @event.RandomNumber;
     130             :                         }
     131             : 
     132             :                         #region Overrides of SnapshotAggregateRoot<int,TestAggregateSnapshot>
     133             : 
     134             :                         /// <summary>
     135             :                         /// Create a <see cref="TestAggregateSnapshot"/> of the current state of this instance.
     136             :                         /// </summary>
     137           1 :                         protected override TestAggregateSnapshot CreateSnapshot()
     138             :                         {
     139             :                                 return new TestAggregateSnapshot { CurrentRandomNumber = CurrentRandomNumber };
     140             :                         }
     141             : 
     142             :                         /// <summary>
     143             :                         /// Rehydrate this instance from the provided <paramref name="snapshot"/>.
     144             :                         /// </summary>
     145             :                         /// <param name="snapshot">The <see cref="TestAggregateSnapshot"/> to rehydrate this instance from.</param>
     146           1 :                         protected override void RestoreFromSnapshot(TestAggregateSnapshot snapshot)
     147             :                         {
     148             :                                 CurrentRandomNumber = snapshot.CurrentRandomNumber;
     149             :                         }
     150             : 
     151             :                         #endregion
     152             :                 }
     153             : 
     154             :                 /// <summary />
     155             :                 public class TestAggregateSnapshot
     156             :                         : Snapshot
     157           0 :                 {
     158             :                         /// <summary />
     159             :                         public int CurrentRandomNumber { get; set; }
     160             :                 }
     161             : 
     162             :                 /// <summary />
     163             :                 public class RandomNumberEvent
     164             :                         : IEventWithIdentity<int>
     165           0 :                 {
     166             :                         #region Implementation of IMessage
     167             : 
     168             :                         /// <summary>
     169             :                         /// An identifier used to group together several <see cref="IMessage"/>. Any <see cref="IMessage"/> with the same <see cref="CorrelationId"/> were triggered by the same initiating request.
     170             :                         /// </summary>
     171             :                         public Guid CorrelationId { get; set; }
     172             : 
     173             :                         /// <summary>
     174             :                         /// The originating framework this message was sent from.
     175             :                         /// </summary>
     176             :                         public string OriginatingFramework { get; set; }
     177             : 
     178             :                         /// <summary>
     179             :                         /// The frameworks this <see cref="IMessage"/> has been delivered to/sent via already.
     180             :                         /// </summary>
     181             :                         public IEnumerable<string> Frameworks { get; set; }
     182             : 
     183             :                         #endregion
     184             : 
     185             :                         #region Implementation of IMessageWithAuthenticationToken<int>
     186             : 
     187             :                         /// <summary>
     188             :                         /// The AuthenticationToken of the entity that triggered the event to be raised.
     189             :                         /// </summary>
     190             :                         public int AuthenticationToken { get; set; }
     191             : 
     192             :                         #endregion
     193             : 
     194             :                         #region Implementation of IEvent<int>
     195             : 
     196             :                         /// <summary>
     197             :                         /// The ID of the <see cref="IEvent{TAuthenticationToken}"/>
     198             :                         /// </summary>
     199             :                         public Guid Id { get; set; }
     200             : 
     201             :                         /// <summary>
     202             :                         /// The version of the <see cref="IEvent{TAuthenticationToken}"/>
     203             :                         /// </summary>
     204             :                         public int Version { get; set; }
     205             : 
     206             :                         /// <summary>
     207             :                         /// The date and time the event was raised or published.
     208             :                         /// </summary>
     209             :                         public DateTimeOffset TimeStamp { get; set; }
     210             : 
     211             :                         #endregion
     212             : 
     213             :                         #region Implementation of IEventWithIdentity<int>
     214             : 
     215             :                         /// <summary>
     216             :                         /// The identity of the <see cref="IAggregateRoot{TAuthenticationToken}">aggregate</see> being targeted.
     217             :                         /// </summary>
     218             :                         public Guid Rsn { get; set; }
     219             : 
     220             :                         #endregion
     221             : 
     222             :                         /// <summary />
     223             :                         public int RandomNumber { get; set; }
     224             : 
     225             :                         /// <summary />
     226           0 :                         public RandomNumberEvent(Guid rsn)
     227             :                         {
     228             :                                 Id = Guid.NewGuid();
     229             :                                 Rsn = rsn;
     230             :                                 RandomNumber = new Random().Next(0, 99999);
     231             :                         }
     232             :                 }
     233             :         }
     234             : }

Generated by: LCOV version 1.12