Example #1
0
        CreateAuctionCommand GetCreateCommand()
        {
            var categories = new List <string>()
            {
                "Fake category", "Fake subcategory", "Fake subsubcategory 0"
            };
            var cmd = new CreateAuctionCommand(20.0m, product, DateTime.UtcNow.AddMinutes(20),
                                               DateTime.UtcNow.AddDays(12),
                                               categories, Tag.From(new[] { "tag1" }), "test name", false);

            cmd.SignedInUser         = user.UserIdentity;
            cmd.AuctionCreateSession = session;
            return(cmd);
        }
        public void METHOD()
        {
            var services = TestDepedencies.Instance.Value;
            var user     = new User();

            user.Register("testUserName");
            user.MarkPendingEventsAsHandled();
            var product       = new Product("test product name", "example description", Condition.New);
            var sem           = new SemaphoreSlim(0, 1);
            var sem2          = new SemaphoreSlim(0, 1);
            var correlationId = new CorrelationId("test_correlationId");
            var categories    = new List <string>()
            {
                "Fake category", "Fake subcategory", "Fake subsubcategory 0"
            };

            var userRepository = new Mock <IUserRepository>();

            userRepository.Setup(f => f.FindUser(It.IsAny <UserIdentity>()))
            .Returns(user);

            var command = new CreateAuctionCommand(20.0m, product, DateTime.UtcNow.AddMinutes(10),
                                                   DateTime.UtcNow.AddDays(12),
                                                   categories, Tag.From(new[] { "tag1" }), "test auction name", false);

            command.SignedInUser = user.UserIdentity;

            IAppEvent <AuctionCreated> publishedEvent = null;


            var eventHandler = new Mock <TestAuctionCreatedHandler>(
                services.AppEventBuilder,
                services.DbContext,
                Mock.Of <IRequestStatusService>()
                );

            eventHandler.CallBase = true;
            eventHandler
            .Setup(f => f.Consume(It.IsAny <IAppEvent <AuctionCreated> >())
                   )
            .Callback((IAppEvent <AuctionCreated> ev) =>
            {
                publishedEvent = ev;
                sem.Release();
            })
            .CallBase();
            services.SetupEventBus(eventHandler.Object);

            var implProv = new Mock <IImplProvider>();

            implProv
            .Setup(f => f.Get <IAuctionRepository>())
            .Returns(services.AuctionRepository);
            RollbackHandlerRegistry.ImplProvider = implProv.Object;
            var testRollbackHandler = new Mock <TestCreateAuctionRollbackHandler>(implProv.Object);

            testRollbackHandler.CallBase = true;
            testRollbackHandler
            .Setup(f => f.Rollback(It.IsAny <IAppEvent <Event> >()))
            .CallBase();
            testRollbackHandler.Object.AfterAction = () => { sem2.Release(); };


            var session = user.UserIdentity.GetAuctionCreateSession();

            command.AuctionCreateSession = session;

            var handlerDepedencies = new CreateAuctionCommandHandlerDepedencies()
            {
                auctionRepository       = services.AuctionRepository,
                auctionSchedulerService = services.SchedulerService,
                eventBusService         = services.EventBus,
                logger = Mock.Of <ILogger <CreateAuctionCommandHandler> >(),
                auctionCreateSessionService = services.GetAuctionCreateSessionService(session),
                auctionImageRepository      = services.AuctionImageRepository,
                categoryBuilder             = new CategoryBuilder(services.CategoryTreeService),
                userRepository = userRepository.Object
            };
            var commandHandler = new TestCreateAuctionCommandHandler(handlerDepedencies, testRollbackHandler.Object);

            commandHandler.Handle(command, CancellationToken.None);
            if (!sem.Wait(TimeSpan.FromSeconds(60)))
            {
                Assert.Fail();
            }
            ;


            var createdAuciton = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId);

            if (!sem2.Wait(TimeSpan.FromSeconds(60)))
            {
                Assert.Fail();
            }
            ;
            var auctionAfterRollback = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId);

            testRollbackHandler.Verify(f => f.Rollback(It.IsAny <IAppEvent <Event> >()), Times.Once());
            createdAuciton.Should()
            .NotBe(null);
            auctionAfterRollback.Should()
            .Be(null);
        }