示例#1
0
        public async Task Success2()
        {
            // Arrange
            using var gamesHost = new GamesHostFactory();

            var gamesServiceClient = new GameService.GameServiceClient(gamesHost.CreateChannel());

            var findChallengeScoringRequest = new FindChallengeScoringRequest
            {
                Game = EnumGame.LeagueOfLegends
            };

            var findChallengeScoringResponse = await gamesServiceClient.FindChallengeScoringAsync(findChallengeScoringRequest);

            var createdAt = new DateTimeProvider(
                new DateTime(
                    2019,
                    12,
                    30,
                    21,
                    9,
                    22,
                    DateTimeKind.Utc));

            var startedAt = new DateTimeProvider(
                new DateTime(
                    2019,
                    12,
                    30,
                    22,
                    42,
                    50,
                    DateTimeKind.Utc));

            var challenge = new Challenge(
                new ChallengeId(),
                new ChallengeName("TEST CHALLENGE"),
                Game.LeagueOfLegends,
                BestOf.One,
                Entries.Two,
                new ChallengeTimeline(createdAt, ChallengeDuration.OneDay),
                new Scoring(
                    findChallengeScoringResponse.Scoring.Items.OrderBy(scoring => scoring.Order)
                    .ToDictionary(scoring => scoring.StatName, scoring => scoring.StatWeighting)));

            var participant = new Participant(
                new ParticipantId(),
                new UserId(),
                PlayerId.Parse("V1R8S4W19KGdqSTn-rRO-pUGv6lfu2BkdVCaz_8wd-m6zw"),
                new DateTimeProvider(createdAt.DateTime + TimeSpan.FromMinutes(5)));

            challenge.Register(participant);

            challenge.Register(
                new Participant(
                    new ParticipantId(),
                    new UserId(),
                    new PlayerId(),
                    new DateTimeProvider(createdAt.DateTime + TimeSpan.FromMinutes(10))));

            challenge.Start(startedAt);

            var mockServiceBusPublisher = new Mock <IServiceBusPublisher>();

            mockServiceBusPublisher.Setup(serviceBusPublisher => serviceBusPublisher.PublishAsync(It.IsAny <ChallengeSynchronizedIntegrationEvent>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            using var challengesHost = new ChallengesHostFactory().WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(mockServiceBusPublisher.Object).As <IServiceBusPublisher>().SingleInstance();
                });
            });

            challengesHost.Server.CleanupDbContext();

            await challengesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var repository = scope.GetRequiredService <IChallengeRepository>();

                repository.Create(challenge);

                await repository.CommitAsync(false);
            });

            var recurringJob = new ChallengeRecurringJob(
                new ChallengeService.ChallengeServiceClient(challengesHost.CreateChannel()),
                new GameService.GameServiceClient(gamesHost.CreateChannel()));

            // Act
            await recurringJob.SynchronizeChallengesAsync(EnumGame.LeagueOfLegends);

            // Assert
            await challengesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var repository = scope.GetRequiredService <IChallengeRepository>();

                var persistentChallenge = await repository.FindChallengeAsync(challenge.Id);

                persistentChallenge.FindParticipant(participant.PlayerId).Matches.Should().HaveCount(2);
            });

            mockServiceBusPublisher.Verify(
                serviceBusPublisher => serviceBusPublisher.PublishAsync(It.IsAny <ChallengeSynchronizedIntegrationEvent>()),
                Times.Exactly(1));
        }
        public async Task Success()
        {
            // Arrange
            var mockServiceBusPubliser = new Mock <IServiceBusPublisher>();

            mockServiceBusPubliser.Setup(serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>()))
            .Verifiable();

            var user = new User
            {
                Id             = Guid.NewGuid(),
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                UserName       = "******",
                Country        = Country.Canada
            };

            var account = new Account(user.Id.ConvertTo <UserId>());

            var moneyAccount = new MoneyAccountDecorator(account);

            moneyAccount.Deposit(Money.TwoHundred).MarkAsSucceeded();

            var doxatag = new Doxatag(
                account.Id,
                "TestUser",
                1000,
                new UtcNowDateTimeProvider());

            var gamePlayerId = new PlayerId();

            var challenge = new Challenge(
                new ChallengeId(),
                new ChallengeName("TestChallenge"),
                Game.LeagueOfLegends,
                BestOf.One,
                Entries.Two,
                new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.OneDay),
                Scoring);

            var payout = new ChallengePayoutFactory();

            var challengePayout = new Cashier.Domain.AggregateModels.ChallengeAggregate.Challenge(
                challenge.Id,
                payout.CreateInstance().GetChallengePayout(ChallengePayoutEntries.One, MoneyEntryFee.OneHundred));

            using var gamesHost = new GamesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id));

            gamesHost.Server.CleanupDbContext();

            await gamesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var gameCredentialRepository = scope.GetRequiredService <IGameCredentialRepository>();

                gameCredentialRepository.CreateCredential(
                    new Credential(
                        account.Id,
                        challenge.Game,
                        gamePlayerId,
                        new UtcNowDateTimeProvider()));

                await gameCredentialRepository.UnitOfWork.CommitAsync(false);
            });

            var gameServiceClient = new GameService.GameServiceClient(gamesHost.CreateChannel());

            using var challengesHost = new ChallengesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id))
                                       .WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(mockServiceBusPubliser.Object).As <IServiceBusPublisher>().SingleInstance();
                });
            });

            challengesHost.Server.CleanupDbContext();

            await challengesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengeRepository>();

                challengeRepository.Create(challenge);

                await challengeRepository.CommitAsync(false);
            });

            var challengeServiceClient = new ChallengeService.ChallengeServiceClient(challengesHost.CreateChannel());

            using var cashierHost = new CashierHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id));

            cashierHost.Server.CleanupDbContext();

            await cashierHost.Server.UsingScopeAsync(
                async scope =>
            {
                var accountRepository = scope.GetRequiredService <IAccountRepository>();

                accountRepository.Create(account);

                await accountRepository.CommitAsync();
            });

            await cashierHost.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengePayoutRepository>();

                challengeRepository.Create(challengePayout);

                await challengeRepository.CommitAsync();
            });

            var cashierServiceClient = new CashierService.CashierServiceClient(cashierHost.CreateChannel());

            using var identityHost = new IdentityHostFactory();

            identityHost.Server.CleanupDbContext();

            await identityHost.Server.UsingScopeAsync(
                async scope =>
            {
                var doxatagRepository = scope.GetRequiredService <IUserService>();

                await doxatagRepository.CreateAsync(user, "Pass@word1");
            });

            await identityHost.Server.UsingScopeAsync(
                async scope =>
            {
                var doxatagRepository = scope.GetRequiredService <IDoxatagRepository>();

                doxatagRepository.Create(doxatag);

                await doxatagRepository.UnitOfWork.CommitAsync(false);
            });

            var identityServiceClient = new IdentityService.IdentityServiceClient(identityHost.CreateChannel());

            using var challengesAggregatorHost = new ChallengesWebAggregatorHostFactory()
                                                 .WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id))
                                                 .WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(challengeServiceClient).SingleInstance();

                    container.RegisterInstance(cashierServiceClient).SingleInstance();

                    container.RegisterInstance(identityServiceClient).SingleInstance();

                    container.RegisterInstance(gameServiceClient).SingleInstance();
                });
            });

            var client = challengesAggregatorHost.CreateClient();

            // Act
            var response = await client.PostAsJsonAsync(
                $"api/challenges/{challenge.Id}/participants",
                new
            {
            });

            // Assert
            response.EnsureSuccessStatusCode();

            mockServiceBusPubliser.Verify(
                serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>()),
                Times.Once);
        }
示例#3
0
        public async Task Success()
        {
            // Arrange
            var challenges = CreateChallenges().ToList();

            var mockChallengeService = new Mock <IChallengeService>();

            mockChallengeService.Setup(
                challengeService => challengeService.GetMatchesAsync(
                    Game.LeagueOfLegends,
                    It.IsAny <PlayerId>(),
                    It.IsAny <DateTime?>(),
                    It.IsAny <DateTime?>(),
                    It.IsAny <IImmutableSet <string> >()))
            .ReturnsAsync(CreateChallengeMatches().ToList())
            .Verifiable();

            var mockServiceBusPublisher = new Mock <IServiceBusPublisher>();

            mockServiceBusPublisher.Setup(serviceBusPublisher => serviceBusPublisher.PublishAsync(It.IsAny <ChallengeSynchronizedIntegrationEvent>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            using var challengesHost = new ChallengesHostFactory().WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(mockServiceBusPublisher.Object).As <IServiceBusPublisher>().SingleInstance();
                });
            });

            challengesHost.Server.CleanupDbContext();

            await challengesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var repository = scope.GetRequiredService <IChallengeRepository>();

                repository.Create(challenges);

                await repository.CommitAsync(false);
            });

            using var gamesHost = new GamesHostFactory().WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(mockChallengeService.Object).As <IChallengeService>().SingleInstance();
                });
            });

            var recurringJob = new ChallengeRecurringJob(
                new ChallengeService.ChallengeServiceClient(challengesHost.CreateChannel()),
                new GameService.GameServiceClient(gamesHost.CreateChannel()));

            // Act
            await recurringJob.SynchronizeChallengesAsync(EnumGame.LeagueOfLegends);

            // Assert
            mockServiceBusPublisher.Verify(
                serviceBusPublisher => serviceBusPublisher.PublishAsync(It.IsAny <ChallengeSynchronizedIntegrationEvent>()),
                Times.Exactly(2));

            mockChallengeService.Verify(
                challengeService => challengeService.GetMatchesAsync(
                    Game.LeagueOfLegends,
                    It.IsAny <PlayerId>(),
                    It.IsAny <DateTime?>(),
                    It.IsAny <DateTime?>(),
                    It.IsAny <IImmutableSet <string> >()),
                Times.Exactly(4));
        }